npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xl-webhook-sdk

v0.0.20

Published

Xsolla Webhook SDK for Node.js

Downloads

41

Readme

Xsolla Webhook SDK for Node.js

Please note that the SDK is still being debugged.

The Webhook SDK, available in Node.js, is an implementation of the methods in the Webhooks Overview. It is designed to streamline and standardize the handling process of Webhook response for merchants.

See to find more: https://developers.xsolla.com/webhooks/overview

This page contains:

Installation

Installation using npm or yarn, with a requirement of Node > 18.

npm i xl-webhook-sdk
or
yarn add xl-webhook-sdk

Quick Guide

For a practical SDK usage sample with express, refer to sample/*. Excute the command to run the local server at http://localhost:3006/webhook.

cd sample
yarn
yarn run:sample

The guide will display how to quickly set up a functional service for handling webhook response using the Webhook SDK:

Step 1: Instance Creation

Initialize the Webhook SDK by creating an instance with the secret key of Publisher Account - Project settings - Webhooks - Secret key. It is recommended that set secretKey to .env and by process.env.WEBHOOK_SECRET_KEY to get.

const XsollaWebhookSDK = require("@xl-webhook-sdk");

app.post("/webhook", (req, res) => {
  let webhookSDK;

  try {
    webhookSDK = new XsollaWebhookSDK({
      secretKey: "mysecret",
      headers: req.headers,
      body: req.body,
    });

    webhookSDK.onUserValidation((data) => {
      console.log("Processed user_validation handler", data);
    });
  } catch (error) {
    // handle error
  }
});

Step 2: Event Registration

Handlers can be registered for pre-defined webhook events. Each registered function requires a callback as an argument. When the corresponding event occurs and the function is invoked, the SDK will pass the request.body from the webhook to the provided callback.

webhookSDK.onUserValidation((data) => {
  console.log("Processed user_validation event:", data);
});

webhookSDK.onPayment((data) => {
  console.log("Processed payment event:", data);
});

webhookSDK.onOrderPaid((data) => {
  console.log("Processed order_paid event:", data);
});

webhookSDK.onOrderCanceled((data) => {
  console.log("Processed order_canceled event:", data);
});

The onEvent method can be used to register handlers for other custom events:

webhookSDK.onEvent("user_search", (data) => {
  console.log("Processed user_search event:", data);
});

Step 3: Signature Verification (Optional)

Authenticity of data is verified before processing Webhook events. Signature Verification is designed to be optional and is turned on by default, set by needVerification: true/false in the constructor:

  • If the signature is valid, the registered event processing begins, requiring no additional handling. The SDK fully handles.
  • If the signature is invalid, statusCode and error will returns at result.
const result = webhookSDK.processEvent();

It will return an Invalid signature error in the following format:

{
  statusCode: 400,
  content: {
    error: {
      code: "INVALID_SIGNATURE",
      message: "Invalid signature"
    }
  }
}

When the SDK's handling of signature verification is not desired, needVerification can be set to false during instance creation. Please note that when this is set to false, the SDK will not verify the required and valid authorization headers & signature. These checks will need to be handled independently.

Also, a signature verification method verify() is also provided, allowing merchants to manage the signature verification process:

const XsollaWebhookSDK = require("@xl-webhook-sdk");

app.post("/webhook", (req, res) => {
  const webhookSDK = new XsollaWebhookSDK({
    secretKey: "mysecret",
    headers: req.headers,
    body: req.body,
    needVerification: false,
  });

  // Handle the signature verification
  const authHeader = req.headers && req.headers["authorization"];
  const signature = authHeader ? authHeader.replace("Signature ", "") : "";
  const payload = JSON.stringify(req.body);

  const isValid = webhookSDK.verify(payload, signature);
  if (!isValid) {
    // handle signature invalid
  }
});

Step 4: Event Reception and Processing

The event is received and processed, returns an object that contains statusCode and content. For the format of the returned result, refer to processEvent().

const result = webhookSDK.processEvent();

res.status(result.statusCode).send(result.content);

Step 5: Unmounting Event Handlers (Optional)

To unregister all events and handlers, the unmount method can be used:

webhookSDK.unmount();

API

Constructor

new XsollaWebhookSDK({
  secretKey: "mysecret",
  headers: request.headers,
  body: request.body,
  needVerification: true,
});

Returns webhookSDK instance.

webhookSDK.sign()

const webhookSDK = new XsollaWebhookSDK({ secretKey: "mysecret" });
const payload = JSON.stringify(req.body);
webhookSDK.sign(payload);

Returns a signature string value.

webhookSDK.verify()

const webhookSDK = new XsollaWebhookSDK({ secretKey: "mysecret" });
const payload = JSON.stringify(req.body);
const authHeader = request.headers.authorization;
const signature = authHeader ? authHeader.replace("Signature ", "") : "";
webhookSDK.verify(payload, signature);

Returns a boolean value:

  • true signature is valid, successful verification
  • false signature is invalid, verification failed

webhookSDK.onUserValidation()

Registers a callback for the a specific notification_type is user_validation event.

webhookSDK.onUserValidation(callback);

No return value.

webhookSDK.onPayment()

Registers a callback for the 'payment' event.

webhookSDK.onPayment(callback);

No return value.

webhookSDK.onOrderPaid()

Registers a callback for the 'order_paid' event.

webhookSDK.onOrderPaid(callback);

No return value.

webhookSDK.onOrderCanceled()

Registers a callback for the 'order_canceled' event.

webhookSDK.onOrderCanceled(callback);

No return value.

webhookSDK.onEvent()

Registers any event name and event handler function.

webhookSDK.onEvent(eventName, eventHandler);

No return value.

webhookSDK.processEvent()

Process event and resolve event's handler, and returns an object containing statusCode and content as result.

const result = webhookSDK.processEvent();
res.status(result.statusCode).send(result.content);

Returns an object with statusCode and content, the format of the returned result is as follows:

Please note, for order_paid or order_canceled notigication type, statusCode is 200. In all other cases, the statusCode returned is 204.

{
 statusCode: 200,
 content: 'ok'
}

{
 statusCode: 204,
 content: ''
}

{
  statusCode: 400,
  content: {
    error: {
      code: "INVALID_SIGNATURE",
      message: "Invalid signature"
    }
  }
}

{
  statusCode: 500,
  content: {
    error: {
      code: "SERVER_ERROR",
      message: "Internal Server Error"
    }
  }
}

webhookSDK.unmount()

Unregister all events.

webhookSDK.unmount();

No returns value.