Skip to main content

Receipt Verification and Item Handout

This is a tutorial on how to hand out items via receipt verification in BACKND Function using the receipt token generated after IAP in Unity.
In order to proceed with the tutorial, the BACKND Function development tool and BACKND Function project template must be installed.


  1. Use the BackendFunction template to create a new project.
    Refer to the Create Project documentation to create a new project.

  1. Write a Function method as follows.
    For the receipt verification method, a receipt token is required, so when debugging, you can write separate logic to check only whether the item has been handed out properly.
public Stream Function(Stream stream, ILambdaContext context)
{
try
{
// Initialize BACKND Function API
Backend.Initialize(ref stream);
}
catch(Exception e)
{
// When BACKND Function API initialization fails
return ReturnErrorObject("initialize " + e.ToString());
}

// Check whether the token key exists in `content` in debugConfig.json or in `Param` passed as a parameter value when calling InvokeFunction
// in the Backend SDK in debugConfig.json.
if(Backend.HasKey("token") == false)
{
return ReturnErrorObject("token key is not exist");
}

BackendReturnObject bro = null;
bool isApple = false;

// Check whether the productId key exists in Content.
if(Backend.HasKey("productId") == false)
{
// Apple receipt verification does not require a productId.
isApple = true;
}

// For Android receipt verification
if(isApple == false)
{
var pid = Backend.Content["productId"].ToString();
var token = Backend.Content["token"].ToString();

bro = Backend.Receipt.IsValidateGooglePurchase(pid, token, "Google receipt verification in BACKND Function");
}
// For Apple receipt verification
else
{
var token = Backend.Content["token"].ToString();

bro = Backend.Receipt.IsValidateApplePurchase(token, "Apple receipt verification in BACKND Function");
}
// Excluded if receipt verification result is null
if(bro == null)
{
return ReturnErrorObject("bro is null");
}
// If the receipt verification result fails, the reason for failure is returned
if(bro.IsSuccess() == false)
{
return ReturnErrorObject("fail to vaildate receipt: " + bro.GetMessage());
}

// Create information of the item to be handed out
Param param = new Param();
param.Add("name", "sword_0");
param.Add("atk", 999);
// Hand out the item
bro = Backend.GameInfo.Insert("item", param);
if(bro.IsSuccess() == false)
{
return ReturnErrorObject("fail to insert item: " + bro.GetMessage());
}

return Backend.StringToStream("success");
}

  1. Configure BACKND CLI config.json.
    Enter backend config in cmd, and save after entering the following in the open notepad(excluding authKey).
{
"account": {
"authKey": "BACKND Function authentication key issued from BACKND Console"
},
"projectInfo": {
// Enter the absolute path of the project created by the developer.
// The path must be separated by \\ so that the CLI can recognize it normally.
// ex) C:\\work\\bf
"projectPath": "Project path",
// Enter the name of the project created by the developer.
"csprojName": "BackendFunction.csproj",
"binName": "publish.zip"
},
"functionName": "giveSword",
"description": "Receipt verification method"
}

  1. Build the project using BACKND CLI.
    Enter the backend build command in cmd.
    Please refer to the Build documentation.

  1. Deploy the build output to the server using BACKND CLI.
    Enter the backend deploy giveSword command in cmd.
    Please refer to the Deploy documentation.

  1. Check if the deployment has been carried out correctly in BACKND Console.

  1. Refer to the Receipt Verification documentation in BACKND Function to check whether the deployed method returns correctly after the IAP process.

In the BACKND SDK, try calling the method as shown below, and check if it returns correctly.

public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
string id = string.Empty;
string token = string.Empty;
Param param = new Param();

#if UNITY_IOS
// For iOS, you can transmit the receipt token included in PurchaseEventArgs to BACKND Function without any changes.
param.Add("token", args.purchasedProduct.receipt);
#elif UNITY_ANDROID
// For Android, parse the receipt token included in PurchaseEventArgs
// using the BackEnd.Game.Payment.GoogleReceiptData.FromJson method to extract the id and token value,
// and then send it to BACKND Function.
BackEnd.Game.Payment.GoogleReceiptData.FromJson(args.purchasedProduct.receipt, out id, out token);
param.Add("productID", id);
param.Add("token", token);
#endif

// Call BACKND Function
Backend.BFunc.InvokeFunction("receiptVaildate", param, callback => {
if(callback.IsSuccess() == false)
{
Debug.LogError("Failed to execute BACKND Function: "+callback);
return;
}
var result = callback.GetReturnValuetoJSON()["result"].ToString();
Debug.Log("BACKND Function execution result: " + result);
});

return PurchaseProcessingResult.Complete;
}

If successful, the callback is returned as below.
statusCode : 200
message : Success returnValue : {"result":"success"}