Add items to inventory based on Xsolla solution

Xsolla provides a solution for the implementation of the player's inventory based on the In-Game Store product (Player Inventory). This solution allows you to sync all purchases and user premium rewards across platforms.

You can add items purchased using a third-party payment UI (e.g., Google Play In-App Purchase) to Player Inventory.

Items are added to the inventory when the Grant items to users API server query is called. If you are using Firebase for the back end of the application, use the ready-made cloud function:

  1. Add a Cloud Function to your Firebase project.
  2. Implement the logic for calling the function.

Add Cloud Function to Firebase project

  1. Initialize your Firebase project.
  2. Import and configure the function for adding items to the inventory where:
    • <MerchantID>Merchant ID found in Publisher Account in the Project Settings > Webhooks section.
    • <ProjectID>Project ID found in Publisher Account in the Project Settings > Project ID section.
    • <ApiKey>API key found in Publisher Account in Company settings > API key section.

The function code for adding items to the inventory:

Copy
Full screen
Small screen
const merchantId = "<MerchantID>";
const projectId = "<ProjectID>";
const apiKey = "<ApiKey>";

exports.redeem = functions.https.onRequest((request, response) => {
  const sku = request.body.sku;
  const quantity = request.body.quantity;
  const userId = request.body.user_id;

  const authHeader = Buffer.from(`${merchantId}:${apiKey}`).toString("base64");

  const postData = JSON.stringify(
      [{
        "comment": "Purchased by Google Pay",
        "platform": "xsolla",
        "user": {
          "id": userId,
        },
        "items": [{
          "sku": sku,
          "quantity": quantity,
        }],
      }]
  );

  const options = {
    hostname: "store.xsolla.com",
    port: 443,
    path: `/api/v2/project/${projectId}/inventory/reward`,
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Content-Length": postData.length,
      "Authorization": `Basic ${authHeader}`,
    },
  };

  const req = https.request(options, (res) => {
    if (res.statusCode == 200) {
      response.status(200).send();
      return;
    }
    if (res.statusCode == 404) {
      response.status(404).send();
      return;
    }
    if (res.statusCode == 401) {
      response.status(401).send();
      return;
    }
    response.status(400).send();

    res.on("data", (d) => {
      process.stdout.write(d);
    });
  });
  req.on("error", (e) => {
    response.status(500).send(e.message);
  });

  req.write(postData);
  req.end();
});

  1. Deploy the function to a production environment as per this example.

Call function from application

To add items to inventory, add logic to call the function from the application, specify redeem as the function name and pass the sku, quantity, user_id parameters to it.

Some billing interfaces (e.g., Google Play In-App Purchase) require initiating the logic for adding an item to inventory on the client side. In this case, implement a client-side method to call the redeem function.

As an example of implementation for Android, you can use the xsolla-googleplay-sdk module which allows you to add purchases from Google Play In-App Purchases to your inventory.

To apply the example in your project:

  1. Start Android Studio.
  2. Add the xsolla-googleplay-sdk module to your project.
  3. Initialize the library. To do this, add the following line to your Android project source code where <your-deployment-host> is the host of the cloud function URL (e.g., https://us-central1-xsolla-sdk-demo.cloudfunctions.net):

Copy
Full screen
Small screen
InventoryAdmin.init("<your-deployment-host>")

The cloud function URL is displayed on the Firebase command line after running the function in production. Also, the function URL is available in the Firebase console in the Functions > Dashboard section.

  1. Implement the call of the InventoryAdmin.grantItemToUser (sku, userId, quantity, callback) method according to the application logic. The callback returns data on the success of the item being added to the inventory.

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: October 18, 2021

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!