How to connect Unity IAP to Economy?

I am going to connect Unity IAP(Google In App Purchase) to Economy.
I defined all products (Google console and Economy configuration) and purchase.
When I call these below functions, I don’t know how to get Google receipt in Unity IAP and how to use them.

RedeemGooglePlayStorePurchaseArgs args = new RedeemGooglePlayStorePurchaseArgs(“PURCHASE_ID”, “PURCHASE_DATA”, “PURCHASE_DATA_SIGNATURE”, 0, “USD”);

RedeemGooglePlayPurchaseResult purchaseResult = await EconomyService.Instance.RedeemGooglePlayPurchaseAsync(args);

Please help me if you have strong experience in this field.

Unfortunately, the IAP receipt cannot be used directly with the Economy api.

Here’s what RedeemGooglePlayStorePurchaseArgs expects:

  • RealMoneyPurchaseId: the real money purchase item id
  • PurchaseData: The Google Receipt Json
  • PurchaseDataSignature: The google receipt signature
  • LocalCost: local price
  • LocalCurrency: local currency iso code

The receipt payload from IAP needs to be deserialized into a google receipt to pass the right information.
A solution could look something like this:

[Serializable]
   class GoogleReceipt
   {
       [JsonProperty("json")]
       public string Json;
       [JsonProperty("signature")]
       public string Signature;
   }
   // product here is the result of the transaction in IAP
   var unifiedReceipt = JsonConvert.DeserializeObject<UnifiedReceipt>(product.receipt);
   var googleReceipt = JsonConvert.DeserializeObject<GoogleReceipt>(unifiedReceipt.Payload);

   var economyProduct = Configuration.RealMoneyPurchases.FirstOrDefault(x => x.StoreIdentifiers.GooglePlayStore == product.definition.id);

   var args = new RedeemGooglePlayStorePurchaseArgs(
           realMoneyPurchaseId: economyProduct.Id,
           purchaseData: googleReceipt.Json,
           purchaseDataSignature: googleReceipt.Signature,
           localCost: (int)product.metadata.localizedPrice,
           localCurrency: product.metadata.isoCurrencyCode);

   // Call economy api

Let me know if this helps and works for you.

1 Like

Hi, erickb!

How is it the workaround for iOS? We’ve succesfully integrated IAP and Economy for Google Play, but we’re experiencing some validation issues on IAP purchases on Apple’s App Store.

Thanks in advance!

I am struggling with this too - there isn’t much documentation about it.

Is it bad practice to just call EconomyService.Instance.PlayerInventory.AddInventoryItemAsync instead of EconomyService.Instance.Purchases.RedeemGooglePlayPurchaseAsync and EconomyService.Instance.Purchases.RedeemAppleAppStorePurchaseAsync?

1 Like