satispay-web-button-test

node v8.17.0
version: 5.0.0
endpointsharetweet
This sample implements the Satispay web button with nodejs and express. Try it: https://satispay-web-button-test-eqqxm8tmpyxu.runkit.sh/
const express = require("express") const bodyParser = require('body-parser') const fetch = require('node-fetch') const https = require('https') const apiEndpoint = 'https://test.authservices.satispay.com' const satispayKey = process.env.SATISPAY_KEY_TEST const satispayBearer = process.env.SATISPAY_BEARER_TEST const app = express()
In this example we send simple html code with web button and callback function. TIP: Use "data-orderid" to receive the order id on the webhook. Documentation: https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html#web-button
app.get('/', (req, res) => { res.send(`<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width" initial-scale="1" shrink-to-fit="no"> <title>Satispay Web Button (Test)</title> </head> <body> <script id="satispay-web-button" src="https://test.online.satispay.com/button.min.js" data-key="${satispayKey}" data-amount="2500" data-description="#123456" data-usercallback="${process.env.RUNKIT_ENDPOINT_URL}user-callback" data-orderid="123456" > </script> <script> var satispayWebButton = document.querySelector('#satispay-web-button') satispayWebButton.on('success', function(charge) { console.log('success', charge) }) satispayWebButton.on('fail', function(charge) { console.log('fail', charge) }) satispayWebButton.on('load', function() { console.log('load') }) satispayWebButton.on('close', function() { console.log('close') }) satispayWebButton.on('completed', function(chargeId) { console.log('completed', chargeId) }) </script> </body> </html>`) })
In webook you need to get the order details from your database with "orderid" parameter. In this example we charge 1€ without reading from database. Documentation: https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html#create-a-charge
app.post('/user-callback', bodyParser.json(), async (req, res) => { // Create a charge with our api const apiResponse = await fetch(`${apiEndpoint}/online/v1/charges`, { method: 'post', body: JSON.stringify({ // Read the user id (uuid) from body user_id: req.body.uuid, currency: 'EUR', amount: 2500, description: '#123456', metadata: { // Set order_id in metadata to use in charge callback order_id: req.body.orderid }, callback_url: `${process.env.RUNKIT_ENDPOINT_URL}callback?charge_id={uuid}`, expire_in: 120 }), headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${satispayBearer}` } }) if (apiResponse.ok) { // Reply with status code 200 return res.sendStatus(200) } else { // Reply with status code 500 return res.sendStatus(500) } })
In callback you need to check if the charge status is success, read the charge id from query string and fetch the details from our api. If the charge is success you can set the order as paid on your database. Documentation: https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html#get-a-charge
app.get('/callback', async (req, res) => { console.log(req.query) // Get charge details from our api with charge_id from query const apiResponse = await fetch(`${apiEndpoint}/online/v1/charges/${req.query.charge_id}`, { headers: { 'Authorization': `Bearer ${satispayBearer}` } }) if (apiResponse.ok) { // Read json (the charge details) from response body const charge = await apiResponse.json() console.log(charge) // Check if status is success if (charge.status === 'SUCCESS') { // Read the orderid from charge metadata const orderId = charge.metadata.order_id // Now you can set the order as paid } } return res.sendStatus(200) })
Starting web server on port 3000.
app.listen(3000)
Loading…

no comments

    sign in to comment