Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to connect Unity IAP to Economy?

Discussion in 'Economy' started by sava-game, Aug 18, 2023.

  1. sava-game

    sava-game

    Joined:
    Feb 14, 2023
    Posts:
    21
    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.
     
  2. erickb_unity

    erickb_unity

    Unity Technologies

    Joined:
    Sep 1, 2021
    Posts:
    88
    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:

    Code (CSharp):
    1. [Serializable]
    2.    class GoogleReceipt
    3.    {
    4.        [JsonProperty("json")]
    5.        public string Json;
    6.        [JsonProperty("signature")]
    7.        public string Signature;
    8.    }
    Code (CSharp):
    1.  
    2.    // product here is the result of the transaction in IAP
    3.    var unifiedReceipt = JsonConvert.DeserializeObject<UnifiedReceipt>(product.receipt);
    4.    var googleReceipt = JsonConvert.DeserializeObject<GoogleReceipt>(unifiedReceipt.Payload);
    5.  
    6.    var economyProduct = Configuration.RealMoneyPurchases.FirstOrDefault(x => x.StoreIdentifiers.GooglePlayStore == product.definition.id);
    7.  
    8.    var args = new RedeemGooglePlayStorePurchaseArgs(
    9.            realMoneyPurchaseId: economyProduct.Id,
    10.            purchaseData: googleReceipt.Json,
    11.            purchaseDataSignature: googleReceipt.Signature,
    12.            localCost: (int)product.metadata.localizedPrice,
    13.            localCurrency: product.metadata.isoCurrencyCode);
    14.  
    15.    // Call economy api
    16.  
    Let me know if this helps and works for you.
     
    Arnaud_Gorain likes this.
  3. Rafa-Garrido

    Rafa-Garrido

    Joined:
    Feb 17, 2015
    Posts:
    9
    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!