Server side Receipt Validation: Need raw Apple and Google Receipts?

Hi Jeff, Team,

Firstly thank you for all your hard work with the unity IAP. Much appreciated.

Reference:

Having read this Unity - Manual: Receipt validation

I am modifying the Samples\In App Purchasing\4.0.3\In App Purchasing Sample\script\IAPDemo.cs

Specifically:

public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
{
var result = validator.Validate(e.purchasedProduct.receipt);

foreach (IPurchaseReceipt productReceipt in result) {
GooglePlayReceipt google = productReceipt as GooglePlayReceipt;
if (null != google) {

SendReceiptToServerForValidation(googleReceipt);

How do I get the Google Play actual receipt googleReceipt?
The “google” variable doesn’t seem to have this specifically where can I get this information:
Google Play: A Google Play’s receipt consists of two components.

  • Signed Data: A JSON data of what the end user purchased.
  • Signature: A base64 encoded string.

}

AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt;
if (null != apple) {

Is this the right way to get the raw apple receipt?

var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
// Get a reference to IAppleConfiguration during IAP initialization.
var appleConfig = builder.Configure();
var receiptData = System.Convert.FromBase64String(appleConfig.appReceipt);
AppleReceipt appleReceipt = new AppleValidator(AppleTangle.Data()).Validate(receiptData);
>> so I can call this:
SendReceiptToServerForValidation(appleReceipt);
Note: appleReceipt: An Apple’s receipt is a base64 encoded string.

}

}

}

I hope this is clear enough, essentially, I want to validate on the server. I need to pass the receipt to the server to validate. I had a look at this: GitHub - voltrue2/in-app-purchase: A Node.js module for in-App-Purchase for iOS, Android, Amazon and Windows. which seems to be the recommended way of doing it using NodeJS. I just dont see where I can get the raw Receipts needed to pass to this script.

The raw classes seem to be

\com.unity.purchasing@4.0.3\Runtime\SecurityStub\GooglePlayReceipt.cs
\com.unity.purchasing@4.0.3\Runtime\SecurityCore\AppleReceipt.cs

Please can you help?

Thanks!

You mentioned "The “google” variable doesn’t seem to have this specifically", so what does it contain? What if you just use that as the parameter? Also, what is in e.purchasedProduct.receipt ? How are you debugging?

Hi Jeff,

Yes, you are a star, thanks that has moved me a bit…

I think what I should do is pass

e.purchasedProduct.receipt

To the server.

How are you debugging?
Right now I am printing out the receipt on screen inside the actual devices (iphone x and android pixel xl) to see what we get. I have both a server-side .NET environment and a NodeJS environment. I’ll use the NodeJS example here: GitHub - voltrue2/in-app-purchase: A Node.js module for in-App-Purchase for iOS, Android, Amazon and Windows. and pass it

e.purchasedProduct.receipt

Im guessing that is correct having not tested it… I missed this and was looking for Google / Apple specific versions.

Another look at the code inside public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e):

string logStr = “result”;

foreach (IPurchaseReceipt productReceipt in result)
{
logStr += ("productReceipt.productID: " + productReceipt.productID);
logStr += ("productReceipt.purchaseDate: " + productReceipt.purchaseDate);
logStr += ("productReceipt.transactionID: " + productReceipt.transactionID);
GooglePlayReceipt google = productReceipt as GooglePlayReceipt;
if (null != google) {

You mentioned "The “google” variable doesn’t seem to have this specifically", so what does it contain?
GooglePlayReceipt google = productReceipt as GooglePlayReceipt;
GooglePlayReceipt is the Unity IAP class \com.unity.purchasing@4.0.3\Runtime\SecurityStub\GooglePlayReceipt.cs but I dont see these properties

  • Signed Data: A JSON data of what the end user purchased.
  • Signature: A base64 encoded string.
    GooglePlayReceipt has the following properties

logStr += ("google.purchaseState: " + google.purchaseState);
logStr += ("google.purchaseToken: " + google.purchaseToken);
logStr += ("google.orderID: " + google.orderID);
logStr += ("google.packageName: " + google.packageName);
}

AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt;
if (null != apple) {

Likewise to Google according to the documentation from Apple
I need to send the Receipt as a base64 encoded string.
AppleInAppPurchaseReceipt is the Unity IAP class
>\com.unity.purchasing@4.0.3\Runtime\SecurityCore\AppleReceipt.cs
It contains the following

logStr += ("apple.originalTransactionIdentifier: " + apple.originalTransactionIdentifier);
logStr += ("apple.subscriptionExpirationDate: " + apple.subscriptionExpirationDate);
logStr += ("apple.cancellationDate: " + apple.cancellationDate);
logStr += ("apple.quantity: " + apple.quantity);
logStr += ("apple.productType: " + apple.productType);
logStr += ("apple.isFreeTrial: " + apple.isFreeTrial);
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());

I guess my question here is this the right way to get AppleReceipt?
And do I need to send that to the server or should I just try e.purchasedProduct.receipt and see what happens?

// Get a reference to IAppleConfiguration during IAP initialization.
var appleConfig = builder.Configure();
var receiptData = System.Convert.FromBase64String(appleConfig.appReceipt);
AppleReceipt receipt = new AppleValidator(AppleTangle.Data()).Validate(receiptData);
}

// this prints everything on the device
SetErrorMessage(logStr);

}

  • Thanks again.

@ironunity2020 You are welcome! I am also debugging a receipt issue at the moment, I’ll confirm your questions when I can. It’s my understanding is that the iOS receipt object is already base64 encoded