Guest User

Untitled

a guest
May 31st, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const cors = require('cors')
  3. const bodyParser = require('body-parser')
  4. const CashPay = require('@developers.cash/cash-pay-server-js')
  5. const catalog = require('./public/catalog.json')
  6.  
  7. async function main() {
  8.   const app = express()
  9.  
  10.   app.use(express.static('public'))
  11.   app.use(bodyParser.json())
  12.   app.use(cors())
  13.  
  14.   // This will pull the Public Key from the pay.infra.cash server
  15.   let webhook = new CashPay.Webhook()
  16.   await webhook.addTrusted('https://pay.infra.cash')
  17.  
  18.   // Let's store a list of Orders as InvoiceID:Status pairs.
  19.   let orders = {}
  20.  
  21.   app.post('/request-invoice', async (req, res) => {
  22.     // Don't trust the browser to send correct prices and item details. Use the item id and read from the catalog directly.
  23.     let items = req.body.cart.map(cartItem =>
  24.       catalog.find((catalogItem) => cartItem.id === catalogItem.id)
  25.     )
  26.    
  27.     // Calculate the total amount due
  28.     let total = items.reduce((total, item) => total += item.price, 0)
  29.    
  30.     // Set the parameters of the invoice
  31.     let invoice = new CashPay.Invoice()
  32.     invoice.addAddress('bitcoincash:qplf0j8krjrsv95v0t3zj9dc4rcutw5khyy8dc80fu', `${total}USD`)
  33.       .setWebhook(['broadcasting', 'broadcasted', 'confirmed'], 'https://store.example.developers.cash/webhook')
  34.       .setPrivateData({
  35.         customer: req.body.customer, // Customer details
  36.         items: items
  37.       })
  38.    
  39.     // Actually create the invoice
  40.     await invoice.create()
  41.    
  42.     // Create an entry for this InvoiceID in our list of orders
  43.     orders[invoice.invoice.id] = 'pending'
  44.    
  45.     // Return the invoice (this method will omit sensitive data (unless 'true' is passed as a param)
  46.     return res.send(invoice.getPayload())
  47.   })
  48.  
  49.   app.post('/webhook', async (req, res) => {
  50.     try {
  51.       // An exception will be thrown if signature does not match
  52.       await webhook.verifySignature(req.body, req.headers)
  53.      
  54.       // The type of event (e.g. broadcasting, broadcasted or confirmed)
  55.       let eventType = req.body.event
  56.       let invoice = req.body.invoice
  57.      
  58.       // Make sure this invoice was created by us
  59.       if (typeof orders[invoice.id] === 'undefined') {
  60.         throw new Error(`${eventType}: Invoice ${invoice.id} does not exist in our orders.`)
  61.       }
  62.      
  63.       // Mark the invoice as broadcasting/broadcasted/confirmed
  64.       orders[invoice.id] = eventType
  65.      
  66.       // Get the data associated with the invoice
  67.       let data = JSON.parse(invoice.options.privateData)
  68.      
  69.       // Output event type and list of items to console
  70.       console.log(`${data.customer.email} ${eventType}:`)
  71.       console.log(`  ${data.customer.comments}`)
  72.       data.items.forEach(item => { console.log(`  ${item.id}: ${item.title}`) })
  73.      
  74.       // You must send a 200 response, otherwise CashPay will throw an error
  75.       res.status(200).send('Success')
  76.     } catch(err) {
  77.       console.log(err)
  78.       res.status(500).send(err.message)
  79.     }
  80.   })
  81.  
  82.   let server = app.listen(8080, function () {
  83.     console.log('Server is now running on Port 8080')
  84.   })
  85. }
  86.  
  87. main();
Add Comment
Please, Sign In to add comment