Swipelux
Server-side integration/Listen to webhook events

Example with Node.js

For this example, we'll use the express framework to create a simple webhook server.

Make sure you have Node.js installed, we recommend using the latest LTS version (as of this writing, it's v22.15.0).

Create a new project

Create a new directory for your project and initialize it with npm:

mkdir webhook-handler
cd webhook-handler
npm init -y

Install dependencies

npm install express jose

Create a basic express server

index.js
const express = require('express');
const app = express();
const port = 3000;
 
app.use(express.json());
 
app.post('/webhook', (req, res) => {
  console.log(req.body);
});
 
app.listen(port, () => {
  console.log(`Webhook server running on port ${port}`);
});

Verify the webhook payload

index.js
const express = require('express');
const jose = require('jose');
const app = express();
const port = 3000;
 
app.use(express.json());
 
async function getPublicKeys() {
  const response = await fetch(
    "https://api.swipelux.com/api/merchants/webhookKeys"
  );
  const { keys } = await response.json();
  return keys;
}
 
async function verifyWebhookPayload(webhookPayload) {
  const publicKeys = await getPublicKeys();
  const publicKey = await jose.importJWK(publicKeys[0], 'ES256');
 
  const jwtToken = `${webhookPayload.protected}.${webhookPayload.payload}.${webhookPayload.signature}`;
  const decoded = await jose.jwtVerify(jwtToken, publicKey);
  return decoded.payload;
}
 
app.post('/webhook', async (req, res) => {
  try {
    const verifiedPayload = await verifyWebhookPayload(req.body);
  } catch (error) {
    console.error('Webhook error:', error);
    res.status(400).json({ error: 'Invalid webhook payload' });
  }
});
 
app.listen(port, () => {
  console.log(`Webhook server running on port ${port}`);
});

Handle verified webhook payload

index.js
const express = require('express');
const jose = require('jose');
const app = express();
const port = 3000;
 
app.use(express.json());
 
async function getPublicKeys() {
  const response = await fetch(
    "https://api.swipelux.com/api/merchants/webhookKeys"
  );
  const { keys } = await response.json();
  return keys;
}
 
async function verifyWebhookPayload(webhookPayload) {
  const publicKeys = await getPublicKeys();
  const publicKey = await jose.importJWK(publicKeys[0], 'ES256');
 
  const jwtToken = `${webhookPayload.protected}.${webhookPayload.payload}.${webhookPayload.signature}`;
  const decoded = await jose.jwtVerify(jwtToken, publicKey);
  return decoded.payload;
}
 
app.post('/webhook', async (req, res) => {
  try {
    const verifiedPayload = await verifyWebhookPayload(req.body);
 
    switch (verifiedPayload.status) {
      case 'PROCESSING':
        console.log('Payment processing:', verifiedPayload.orderId);
        break;
      case 'COMPLETED':
        console.log('Payment completed:', verifiedPayload.orderId);
        break;
      case 'FAILED':
        console.log('Payment failed:', verifiedPayload.orderId);
        break;
    }
 
    res.status(200).json({ message: 'Webhook processed successfully' });
  } catch (error) {
    console.error('Webhook error:', error);
    res.status(400).json({ error: 'Invalid webhook payload' });
  }
});
 
app.listen(port, () => {
  console.log(`Webhook server running on port ${port}`);
});

Start receiving webhook events

After you've implemented the webhook handler, make sure to deploy it somewhere and set up a webhook endpoint URL in the Merchant Panel to start receiving events from us.

Ensure the endpoint is publicly accessible and properly configured.

On this page