Search Unity

Resolved EconomyAppleAppStorePurchaseFailedException: Purchase receipt invalid

Discussion in 'Economy' started by Hiago_AD, Feb 13, 2023.

  1. Hiago_AD

    Hiago_AD

    Joined:
    Oct 5, 2021
    Posts:
    20
    My code goes along those lines

    Code (CSharp):
    1. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    2. {
    3.     [...]
    4.         var realMoneyPurchaseId = (await EconomyService.Instance.Configuration.GetRealMoneyPurchasesAsync()).Find(def => def.StoreIdentifiers.AppleAppStore == purchaseId).Id;
    5.         var receipt = args.purchasedProduct.receipt;
    6.         var price = Mathf.RoundToInt((float)args.purchasedProduct.metadata.localizedPrice * 100);
    7.         var currencyCode = args.purchasedProduct.metadata.isoCurrencyCode;
    8.         var result = await EconomyService.Instance.Purchases.RedeemAppleAppStorePurchaseAsync(new RedeemAppleAppStorePurchaseArgs(realMoneyPurchaseId, receipt, price, currencyCode));
    9.     [...]
    10.         return PurchaseProcessingResult.Complete;
    11. }
    And I'm receiving an this error. The documentation clearly said that the receipt should be exactly what comes from the store, and it is. Am I missing something, or is it really a bug?

    I'm using version 2.0.4 as recommended, so I could use GetRealMoneyPurchasesAsync.
     
  2. Hiago_AD

    Hiago_AD

    Joined:
    Oct 5, 2021
    Posts:
    20
    Some prints from the editor. The JSON printed before the error is what receipt have. The behavior on the native app is the same, showing the editor for convenience.
     

    Attached Files:

  3. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Hi @Hiago_AD

    You will need to send the payload as opposed to the entire receipt to the Redeem method. You should also take a look at the method you are using to calculate the price, not all currencies have 2 minor currency units, so it isn't safe to assume you can multiply by 100.

    These code snippets should give you some pointers.

    Code (CSharp):
    1.     public class AppleReceipt
    2.     {
    3.         public string payload;
    4.     }

    Code (CSharp):
    1.   AppleReceipt ar = JsonConvert.DeserializeObject<AppleReceipt>(product.receipt);
    2.                
    3.   // Convert decimal currency value to int, to correctly handle currencies with <> 2 minor currecy units
    4.   int localCurrencyInt = (int)AnalyticsService.Instance.ConvertCurrencyToMinorUnits(
    5.       product.metadata.isoCurrencyCode,
    6.       (double)product.metadata.localizedPrice);              
    7.  
    8.   // Redeem IAP
    9.   RedeemAppleAppStorePurchaseArgs args = new RedeemAppleAppStorePurchaseArgs(
    10.       ecomomyRealMoneyPurchase.Id,
    11.       ar.payload,                  
    12.       localCurrencyInt,
    13.       product.metadata.isoCurrencyCode);
    14.  
    15.    RedeemAppleAppStorePurchaseResult result = await EconomyService.Instance.Purchases.RedeemAppleAppStorePurchaseAsync(args);
    16.  
    17.     // Player's currency balance updated on server if receipt valid
    18.     Debug.Log($"IAP Purchase Success :: {result.Verification.Status}");
    I hope that helps.
     
  4. Hiago_AD

    Hiago_AD

    Joined:
    Oct 5, 2021
    Posts:
    20
    This code helps a lot, but when I'm testing on native, the payload is a empty string. Is this because I'm on sandbox mode, or is it for some other reason?

    If it is so, what's the best way to test it? On the editor says that the item was already redeemed, and on the native app, the payload comes empty
     
  5. Hiago_AD

    Hiago_AD

    Joined:
    Oct 5, 2021
    Posts:
    20
    OK, I've found the answer in the IAP forum. Thanks!
     
  6. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Hi @Hiago_AD

    I'm glad you found the answer to your problem, can you perhaps share a link to it here?
     
  7. Hiago_AD

    Hiago_AD

    Joined:
    Oct 5, 2021
    Posts:
    20
    It was this thread, basically I need to have the IAP published with an app update in iOS to have a valid receipt