Swipelux
Server-side integration/Listen to webhook events

Example with PHP

For this example, we'll set up a basic PHP server to handle webhook events from Swipelux.

Make sure you have Composer installed as we'll use it to install the required dependencies.

Create a new project

Create a new directory for your project and initialize Composer:

mkdir webhook-handler
cd webhook-handler
composer init

Install dependencies

Install the required dependencies:

composer require firebase/php-jwt

Create a new index.php file

index.php
<?php
 
require 'vendor/autoload.php';
 
// Parse the incoming webhook payload
$rawData = file_get_contents('php://input');
$webhookPayload = json_decode($rawData, true);
 
// For now, just log the payload
error_log(print_r($webhookPayload, true));

Verify the webhook payload

index.php
<?php
 
require 'vendor/autoload.php';
 
use Firebase\JWT\JWT;
use Firebase\JWT\JWK;
 
function getPublicKeys() {
  $response = file_get_contents("https://api.swipelux.com/api/merchants/webhookKeys");
  $keys = json_decode($response, true)['keys'];
  return $keys;
}
 
function verifyWebhookPayload($webhookPayload) {
  $publicKeys = getPublicKeys();
  $publicKey = JWK::parseKeySet(['keys' => $publicKeys], 'ES256');
 
  $token = $webhookPayload['protected'] . '.' . $webhookPayload['payload'] . '.' . $webhookPayload['signature'];
  $decoded = JWT::decode($token, $publicKey);
  return $decoded;
}
 
// Parse the incoming webhook payload
$rawData = file_get_contents('php://input');
$webhookPayload = json_decode($rawData, true);
 
// For now, just log the payload
error_log(print_r($webhookPayload, true));

Handle verified webhook payload

index.php
<?php
 
require 'vendor/autoload.php';
 
use Firebase\JWT\JWT;
use Firebase\JWT\JWK;
 
function getPublicKeys() {
  $response = file_get_contents("https://api.swipelux.com/api/merchants/webhookKeys");
  $keys = json_decode($response, true)['keys'];
  return $keys;
}
 
function verifyWebhookPayload($webhookPayload) {
  $publicKeys = getPublicKeys();
  $publicKey = JWK::parseKeySet(['keys' => $publicKeys], 'ES256');
 
  $token = $webhookPayload['protected'] . '.' . $webhookPayload['payload'] . '.' . $webhookPayload['signature'];
  $decoded = JWT::decode($token, $publicKey);
  return $decoded;
}
 
// Parse the incoming webhook payload
$rawData = file_get_contents('php://input');
$webhookPayload = json_decode($rawData, true);
 
// For now, just log the payload
error_log(print_r($webhookPayload, true));
 
try {
    $verifiedPayload = verifyWebhookPayload($webhookPayload);
 
    // Handle different webhook events
    switch ($verifiedPayload->status) {
        case 'PROCESSING':
            error_log('Payment processing: ' . $verifiedPayload->orderId);
            break;
        case 'COMPLETED':
            error_log('Payment completed: ' . $verifiedPayload->orderId);
            break;
        case 'FAILED':
            error_log('Payment failed: ' . $verifiedPayload->orderId);
            break;
    }
 
    http_response_code(200);
    echo json_encode(['message' => 'Webhook processed successfully']);
} catch (Exception $e) {
    error_log('Webhook error: ' . $e->getMessage());
    http_response_code(400);
    echo json_encode(['error' => 'Invalid webhook payload']);
}

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