question

Jon avatar image
Jon asked

Receipt Validation and Cloudscript / PlayerData

What's the recommended way to do IAP receipt validation and also execute some cloudscript player data in regards to that IAP purchase?

I have some IAP items that need to update some title player data along with granting catalog item / currency.

So I think it would look something like

[client gets receipt] -> validates receipt via backend -> triggers cloudscript(using receipt validation event to trigger)

Let's say the cloudscript function is called "ProcessIAP"

But how would I confirm that the receipt payload is valid? (because client can call that cloudscript ProcessIAP). It seems the client could spoof the payload and just call cloudscript function with a bad receipt.

Thanks

10 |1200

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

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

This documentation -- Getting started with PlayFab, Unity IAP, and Android and the blog -- Receipt Validation for iOS and Android provide the tutorials of IAP purchase. As the documentation said, you can use this client API ValidateGooglePlayPurchase to validate a Google Play purchase. Besides, these API methods ValidateAmazonIAPReceipt, ValidateIOSReceipt, ValidateWindowsStoreReceipt can be used for other platforms' receipt validation. Clients pass the receipt to PlayFab with the above API calls.

Then, as you said, PlayFab will validate the receipt on the backend. In this step, PlayFab will validate the receipt via the corresponding provider’s server (Google, Apple) and will behave according to the response from the provider’s server. Spoof usually won’t work.

After PlayFab did the receipt validation, the event player_receipt_validation would be generated, when the receipt is validated as valid, the player_receipt_validation's "Valid" property would return true, if the receipt is invalid, the "Valid" property would return false.

>> What's the recommended way to do IAP receipt validation and also execute some cloudscript player data in regards to that IAP purchase?

The workflow you mentioned is the recommended way.

  1. The client gets receipts.
  2. The client passes the Receipt Json to PlayFab using API methods, such as ValidateGooglePlayPurchase. PlayFab validates receipt on the backend. The result of validation would display in the event player_receipt_validation.
  3. The event player_receipt_validation trigger the CloudScript functionProcessIAP”. CloudScript function verify the context of player_receipt_validation.
  4. If the "Valid" property is true, execute the main logic to update the data.

>> But how would I confirm that the receipt payload is valid? (because client can call that cloudscript ProcessIAP).

In the CloudScript function, you can get the PlayStream event's context. You can confirm this function is triggered by player_receipt_validation through this context's name, and confirm the receipt is valid through this context's properties. For more information about using context in the CloudScript, please check this documentation -- Writing custom CloudScript, especially this section -- Intermediate Overview: Globals and advanced arguments. More details about the properties of player_receipt_validation, please check this documentation -- player_receipt_validation.

You can refer to the following CloudScript function to confirm the function is triggered by the player_receipt_validation event and the receipt is valid.

handlers.ProcessIAP = function (args, context) {
    
    var psEvent = context.playStreamEvent;
    log.info(psEvent);
    if (psEvent != null && psEvent.EventName == "player_receipt_validation") {
        if (psEvent.Valid == true) {
            //Update the read-only player data here.
            return { result: "Executed the function." };
        } else { return { result: "Error: The receipt is invalid." }; }
 
    } else { return { result: "Error: This function isn't triggered by player_receipt_validation." }; }
 
};

1 comment
10 |1200

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

Jon avatar image Jon commented ·

Thank you for this very detailed answer! Using event context to verify the event name is what I was missing.

0 Likes 0 ·

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.