Receive Xsolla webhooks

When processing a purchase, Xsolla sends webhooks to notify of various events (e.g. payment cancellation, getting a cart’s content). You can use these webhooks to:

  • add purchases to the player inventory whether you use your own solution or a third-party solution
  • implement your own logic for verifying and processing an order to log information, grant rewards, give discounts for an order, etc.

To receive webhooks on the BaaS side, add ready-to-use functions to your project by following the instructions for Firebase and PlayFab.

Add Cloud Function to Firebase project

  1. Initialize your Firebase project.
  2. Import the webhooks receive function, within which <WebhookSecretKey> is the Secret key found in Publisher Account in the Project settings > Webhooks section.

Function code for receiving webhooks:

Copy
Full screen
Small screen
const webhookSecretKey = "<WebhookSecretKey>";

exports.webhook = functions.https.onRequest((request, response) => {
  const requestRawBody = request.rawBody.toString("utf-8");
  const crypto = require("crypto");
  const sig = "Signature " + crypto
      .createHash("sha1")
      .update(requestRawBody + webhookSecretKey)
      .digest("hex");
  if (request.headers.authorization !== sig) {
    response.status(401).send();
    return;
  }

  // TODO Replace this block with your game's logic for purchases handling
  switch (request.body.notification_type) {
    case "order_paid": {
      const userId = request.body.user.external_id;
      const skus = request.body.items.map(function(it) {
        return it.sku;
      }).join(", ");
      const price =
          `${request.body.order.amount} ${request.body.order.currency}`;
      functions.logger.log(
          "Order Paid\n",
          `A user ${userId} has just paid ${price} for ${skus}\n`,
          "Full Data\n",
          request.body
      );
      break;
    }
    case "order_canceled": {
      const userId = request.body.user.external_id;
      const orderId = request.body.order.id;
      functions.logger.log(
          "Order Canceled\n",
          `A user ${userId} has just canceled order ${orderId}\n`,
          "Full Data\n",
          request.body
      );
      break;
    }
  }

  response.status(204).send();
});
  1. Implement the logic for handling webhooks.
Note
The function is made for receiving webhooks related to getting a cart’s content when making a purchase and canceling a payment. You can implement the processing of webhooks related to other events on the payment page yourself.
  1. Deploy the function to a production environment as per this example.
  2. In the Firebase console, go to Build > Functions and copy the URL of the webhooks receive function.
  3. Open your project in Publisher Account.
  4. Go to Project Settings > Webhooks.
  5. In the Webhook URL field, specify the URL of the webhooks receive function.

Add Cloud Script to PlayFab project

Note
PlayFab Cloud Scripts do not directly support functions with HTTP triggers, so Azure functions are used to implement the receiving of webhooks.

  1. Prepare development environment to work with Azure Functions.
  2. Following the example, add a function for receiving webhooks, within which <WebhookSecretKey> is the Secret key found in Publisher Account in the Project settings > Webhooks section.

Function code for receiving webhooks:

Copy
Full screen
Small screen
const webhookSecretKey = "<WebhookSecretKey>";

module.exports = async function (context, request) {
    const requestRawBody = request.rawBody.toString("utf-8");
    const crypto = require("crypto");
    const sig = "Signature " + crypto
        .createHash("sha1")
        .update(requestRawBody + webhookSecretKey)
        .digest("hex");
    if (request.headers.authorization !== sig) {
        context.res = {
            status: 401
        };
        return;
    }

    // TODO Replace this block with your game's logic for purchases handling
    switch (request.body.notification_type) {
        case "order_paid": {
            const userId = request.body.user.external_id;
            const skus = request.body.items.map(function (it) {
                return it.sku;
            }).join(", ");
            const price =
                `${request.body.order.amount} ${request.body.order.currency}`;
            context.log(
                "Order Paid\n" +
                `A user ${userId} has just paid ${price} for ${skus}\n` +
                "Full Data\n" +
                JSON.stringify(request.body)
            );
            break;
        }
        case "order_canceled": {
            const userId = request.body.user.external_id;
            const orderId = request.body.order.id;
            context.log(
                "Order Canceled\n" +
                `A user ${userId} has just canceled order ${orderId}\n` +
                "Full Data\n" +
                JSON.stringify(request.body)
            );
            break;
        }
    }

    context.res = {
        status: 204
    };
}
  1. Implement the logic for handling webhooks.
Note
The function is made for receiving webhooks related to getting a cart’s content when making a purchase and canceling a payment. You can implement the processing of webhooks related to other events on the payment page yourself.
  1. Deploy the function to a production environment.
  2. Copy function URL.
  3. Go to your PlayFab project.
  4. Register Cloud Script function.
  5. Open your project in Publisher Account.
  6. Go to Project Settings > Webhooks.
  7. In the Webhook URL field, specify the URL of the webhooks receive function.
Was this article helpful?
Thank you!
Is there anything we can improve? Message
We’re sorry to hear that
Please explain why this article wasn’t helpful to you. Message
Thank you for your feedback!
We’ll review your message and use it to help us improve your experience.
Rate this page
Rate this page
Is there anything we can improve?

Don’t want to answer

Thank you for your feedback!
Last updated: January 22, 2024

Found a typo or other text error? Select the text and press Ctrl+Enter.

Report a problem
We always review our content. Your feedback helps us improve it.
Provide an email so we can follow up
Thank you for your feedback!