Webhooks

Webhooks will keep your app up to date with the payment progress.

Make sure to listen out for our payment complete webhook before you deliver your product.

Set up Webhooks

To begin receiving webhooks, you must first add a valid URL to the 'Manage Webhook' section of the Gateway Dashboard. If this is successful, the URL will show appear in this section. Note that you can currently only have one URL, so all your endpoint should account for all the products you offer.

Validating a Request

To ensure a secure connection between our servers and your app, we encrypt the request body as a Json Web Token (JWT), signed with your API key. This is the same API key as is used for making requests and can be found in your Dashboard.

Example with NPM Package

// get the data from the request body
const encryptedData = req.body;

// decrypt the data
const data = await piecardInstance._decryptPayment(encryptedData);

// in the request will be the payment data
const {
    id,
    amount,
    client,
    memo,
    sandbox,
    metadata,
    success
} = data.payment;

// the payment will be complete if success = true
if (data.event_type === 'completed') {
    // find customer and product details from metadata
    // and deliver the product
}

Example with NodeJS

const jwt = require('jsonwebtoken');
const { isJWT } = require("validator");

// check the request body is a JWT
const isValidJwt = isJWT(req.body);
if (isValidJwt) {
    
    // decode the request
    const data = jwt.verify(req.body, 'YOUR_API_KEY');

    // in the request will be the payment data
    const {
        id,
        amount,
        client,
        memo,
        sandbox,
        metadata,
        success
    } = data.payment;
    
    // the payment will be complete if success = true
    if (data.event_type === 'completed') {
        // find customer and product details from metadata
        // and deliver the product
    }

}

Last updated