Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Get custom data of virtual purchase

Discussion in 'Cloud Code' started by muhammet12, Apr 3, 2024.

  1. muhammet12

    muhammet12

    Joined:
    Feb 29, 2020
    Posts:
    13
    I am using Cloud Code and need to get custom data of a virtual purchase, how can I do this? Thanks in advance

    -Edit: I am using JavaScript
     
    Last edited: Apr 3, 2024
  2. samg-unity

    samg-unity

    Unity Technologies

    Joined:
    Mar 23, 2021
    Posts:
    45
    Hi @muhammet12

    I provided a sample of how to do this with Inventory Items here: https://forum.unity.com/threads/how...m-data-using-cloud-code.1555757/#post-9707759 - in order to faciliate mapping to the virtual purchases, I've created the virtual purchase as below:

    upload_2024-4-3_11-29-44.png

    Then the previous sample can be modified to filter the player configuration for the virtual purchases like so:

    Code (JavaScript):
    1. const { ConfigurationApi } = require("@unity-services/economy-2.4");
    2.  
    3. module.exports = async ({ params, context, logger }) => {
    4.  
    5.   const {playerId, projectId} = context;
    6.   const {configAssignmentHash} = params;
    7.  
    8.   const configApi = new ConfigurationApi(context);
    9.  
    10.   const getPlayerConfigRequest = {
    11.     configAssignmentHash,
    12.     playerId,
    13.     projectId,
    14.   }
    15.  
    16.   try {
    17.    
    18.     var config = await configApi.getPlayerConfiguration(getPlayerConfigRequest);  
    19.  
    20.     const virtualPurchaseConfig = config.data.results.filter((configItem) => {
    21.         return configItem.type === "VIRTUAL_PURCHASE";
    22.     })
    23.    
    24.     return virtualPurchaseConfig;
    25.     // [
    26.     //   {
    27.     //     "costs": [
    28.     //       {
    29.     //         "amount": 100,
    30.     //         "resourceId": "GOLD"
    31.     //       }
    32.     //     ],
    33.     //     "created": {
    34.     //       "date": "2024-04-03T10:29:05Z"
    35.     //     },
    36.     //     "customData": {
    37.     //       "purchased": true
    38.     //     },
    39.     //     "id": "PURCHASE_SWORD",
    40.     //     "modified": {
    41.     //       "date": "2024-04-03T10:29:05Z"
    42.     //     },
    43.     //     "name": "PURCHASE SWORD",
    44.     //     "rewards": [
    45.     //       {
    46.     //         "amount": 1,
    47.     //         "resourceId": "SWORD"
    48.     //       }
    49.     //     ],
    50.     //     "type": "VIRTUAL_PURCHASE"
    51.     //   }
    52.     // ]
    53.    
    54.   } catch (err) {
    55.     logger.error("Error while retrieving virtual purchase items", {"error.message": err.message});
    56.     throw err;
    57.   }
    58. };
     
    GabKBelmonte likes this.