question

choi dong geun avatar image
choi dong geun asked

How do I purchase multiple items?

I want to purchase the number of several in the same ItemID.

There is no variable to use the PurchaseItem function.

Is there another way?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image
Seth Du answered

Currently, there are 2 work around solutions, and you are able use either of them in Cloud Script:

  • 1. Generate a list that contains a series of items the player wants to purchase, then grant them via serve API GrantItemsToUser. After that, SubtractUserVirtualCurrency is called for the payment.
  • 2.You may still use GrantItemsToUser API, but only grant one instance for each of the items, then ModifyItemUses is calledfor each instances. Lastly, call SubtractUserVirtualCurrency for the payment. Be aware that non-stackable items won’t work by this method.

Sending a feature request or voting for an existing one on the forum will always be appreciated and more votes from other users can help with the priority.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Daniel Keele avatar image
Daniel Keele answered

Here's my Cloudscript implementation of SethDu's first bullet. (Although it does not meet the requirements of the OP's request to purchase multiple of the same product.)

handlers.PurchaseItems = function (args)
{
    const itemListArray = args.itemList;
    const itemListSet = new Set(itemListArray);
    const clientPrice = args.price;
    
    // 1. get all catalog items
    let catalog;
    try {
        catalog = server.GetCatalogItems({}).Catalog;
    } catch(e) {
        return 500;
    }
    
    // 2. for each passed item, total the prices from the catalog
    let serverPrice = 0;
    
    for (let catalogItemIndex = 0; catalogItemIndex < catalog.length; catalogItemIndex++)
    {
        if (itemListSet.has(catalog[catalogItemIndex].ItemId))
        {
            serverPrice += catalog[catalogItemIndex].VirtualCurrencyPrices.yourVirtualCurrency;
        }
    }
    
    // 3. compare the passed price to the actual price
    if (serverPrice != clientPrice)
    {
        return 1053;
    }
    
    // 4. make sure the player has enough virtualCurrency
    const getPlayerCombinedInfoRequest = {
        "PlayFabId": currentPlayerId,
        "InfoRequestParameters": {
            "GetUserVirtualCurrency": true
        }
    };
    
    let virtualCurrencyBalance;
    
    try {
        virtualCurrencyBalance = server.GetPlayerCombinedInfo(getPlayerCombinedInfoRequest).InfoResultPayload.UserVirtualCurrency.yourVirtualCurrency;
    } catch(e) {
        return 500;
    }
    
    if (virtualCurrencyBalance < serverPrice)
    {
        return 1059;
    }


    // 5. grant items
    const grantItemsToUserRequest = {
        "ItemIds": itemListArray,
        "PlayFabId": currentPlayerId	
    }
    
    try {
        server.GrantItemsToUser(grantItemsToUserRequest);
    } catch(e) {
        return 500;
    }
    
    // 6. subtract virtual currency
    const subtractUserVirtualCurrencyRequest = {
        Amount: serverPrice,
        PlayFabId: currentPlayerId,
        VirtualCurrency: "yourVirtualCurrency"
    }
    
    try {
        server.SubtractUserVirtualCurrency(subtractUserVirtualCurrencyRequest);
    } catch(e) {
        return 500;
    }
    
    return 200;
};
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.