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
- Initialize your Firebase project.
- 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
- javascript
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();
});
- 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.
- Deploy the function to a production environment as per this example.
- In the Firebase console, go to
Build > Functions and copy the URL of the webhooks receive function. - Open your project in Publisher Account.
- Go to Project Settings > Webhooks.
- In the Webhook server 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.
- Prepare development environment to work with Azure Functions.
- 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
- javascript
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
};
}
- 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.
- Deploy the function to a production environment.
- Copy function URL.
- Go to your PlayFab project.
- Register Cloud Script function.
- Open your project in Publisher Account.
- Go to Project Settings > Webhooks.
- In the Webhook server field, specify the URL of the webhooks receive function.
Was this article helpful?
Thank you for your feedback!
We’ll review your message and use it to help us improve your experience.Found a typo or other text error? Select the text and press Ctrl+Enter.