Search Unity

Amazon Subscription - Not Working

Discussion in 'Unity IAP' started by shadermanger, Jun 15, 2021.

  1. shadermanger

    shadermanger

    Joined:
    Sep 21, 2020
    Posts:
    21
    I'm Currently Implementing a Subscription in my app for Apple, PlayStore, and Amazon. My Script is Working Fine With Apple and Google PlayStore but the problem here is with Amazon.


    what will replace this cross-platform validator because this only works with google and apple?

    Code (CSharp):
    1. var validator = new CrossPlatformValidator(GooglePlayTangle.Data(),AppleTangle.Data(), Application.identifier);
    On the other hand Product Receipt Always Return Null Even Purchase is Successful at first.

    My Code Is Here Please Help Me.

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Purchasing;
    5. using UnityEngine.Purchasing.Security;
    6.  
    7. public class Purchaser : MonoBehaviour, IStoreListener
    8. {
    9.     private IStoreController controller;
    10.     private IExtensionProvider extensions;
    11.     public static Purchaser instance;
    12.  
    13.     public static bool hideLock = true;
    14.     //Subscription Ids
    15.     public static string kProductIDWeek = "WeekSubscription";
    16.     public static string kProductIDMonth = "MonthSubscription";
    17.     public static string kProductIDThreeMonth = "ThreeMonthSubscription";
    18.     public static string kProductIDYear = "YearSubscription";
    19.  
    20.     // Google Play Store-specific product identifier subscription product.
    21.     private static string kProductNameAmazonWeek = "com.nexttechgamesstudio.alphabets.color.by.number.weekly.premium";
    22.     private static string kProductNameAmazonMonth = "com.nexttechgamesstudio.alphabets.color.by.number.monthly.premium";
    23.     private static string kProductNameAmazonThreeMonth = "com.nexttechgamesstudio.mandala.glitter.color.by.number.everythreemonth.premium";
    24.     private static string kProductNameAmazonYear = "com.nexttechgamesstudio.mandala.glitter.color.by.number.yearly.premium";
    25.     // Does the math to see if your apple subscription is past its experation date
    26.  
    27.  
    28.     void Start()
    29.     {
    30.         if (instance == null)
    31.             DontDestroyOnLoad(gameObject);
    32.  
    33.         instance = this;
    34.  
    35.         if (controller == null)
    36.         {
    37.             InitializePurchasing();
    38.         }
    39.     }
    40.  
    41.  
    42.     public void InitializePurchasing()
    43.     {
    44.         if (IsInitialized())
    45.             return;
    46.  
    47.  
    48.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    49.         builder.AddProduct(kProductIDWeek, ProductType.Subscription, new IDs() { { kProductNameAmazonWeek, AmazonApps.Name }, });
    50.         builder.AddProduct(kProductIDMonth, ProductType.Subscription, new IDs() { { kProductNameAmazonMonth, AppleAppStore.Name }, });
    51.         builder.AddProduct(kProductIDThreeMonth, ProductType.Subscription, new IDs() { { kProductNameAmazonThreeMonth, AppleAppStore.Name }, });
    52.         builder.AddProduct(kProductIDYear, ProductType.Subscription, new IDs() { { kProductNameAmazonYear, AppleAppStore.Name }, });
    53.  
    54.         Debug.Log("add products done");
    55.         UnityPurchasing.Initialize(this, builder);
    56.     }
    57.     private bool IsInitialized()
    58.     {
    59.         return controller != null && extensions != null;
    60.     }
    61.  
    62.     public void BuyWeek()
    63.     {
    64.         BuyProductID(kProductIDWeek);
    65.     }
    66.     public void BuyMonth()
    67.     {
    68.         BuyProductID(kProductIDMonth);
    69.     }
    70.     public void BuyThreeMonth()
    71.     {
    72.         BuyProductID(kProductIDThreeMonth);
    73.     }
    74.     public void BuyYear()
    75.     {
    76.         BuyProductID(kProductIDYear);
    77.     }
    78.  
    79.  
    80.  
    81.     void BuyProductID(string productId)
    82.     {
    83.         if (IsInitialized())
    84.         {
    85.             Product product = controller.products.WithID(productId);
    86.  
    87.             if (product != null && product.availableToPurchase)
    88.             {
    89.                 Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
    90.                 controller.InitiatePurchase(product);
    91.             }
    92.             else
    93.                 Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    94.         }
    95.         else
    96.             Debug.Log("BuyProductID FAIL. Not initialized.");
    97.     }
    98.  
    99.  
    100.  
    101.     public void RestorePurchases()
    102.     {
    103.         if (!IsInitialized())
    104.         {
    105.             Debug.Log("RestorePurchases FAIL. Not initialized.");
    106.             return;
    107.         }
    108.     }
    109.  
    110.  
    111.     public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    112.     {
    113.  
    114.         // Purchasing has succeeded initializing. Collect our Purchasing references.
    115.         Debug.Log("OnInitialized: PASS");
    116.  
    117.         // Overall Purchasing system, configured with products for this application.
    118.  
    119.         this.controller = controller;
    120.         this.extensions = extensions;
    121.  
    122.         foreach (Product p in controller.products.all)
    123.         {
    124.             if (IsSubscribed(p))
    125.             {
    126.                 Debug.LogError(" :: Receipt is Null ? :: "+(p.receipt == null).ToString() );
    127.                 Debug.LogError(p.availableToPurchase + " :: availableToPurchase ::");
    128.                 Debug.LogError(p.definition.id + " :: definition ::");
    129.                 Debug.LogError(p.hasReceipt + " :: hasReceipt ::");
    130.                 Debug.LogError(p.metadata.localizedTitle + " :: localizedTitle ::");
    131.                 Debug.LogError(p.transactionID + " :: transactionID ::");
    132.  
    133.  
    134.                 if (p.receipt != null)
    135.                 {
    136.                     Debug.Log("Subscription is Active");
    137.                     PlayerPrefs.SetInt("IsSubscriptionValid", 1);
    138.                     hideLocks();
    139.                 }
    140.                 else
    141.                 {
    142.                     Debug.Log("Receipt No Valid");
    143.                     PlayerPrefs.SetInt("IsSubscriptionValid", 0);
    144.                 }
    145.             }
    146.             else
    147.             {
    148.                 Debug.Log("Subscription is NOT Active");
    149.             }
    150.         }
    151.     }
    152.  
    153.     private bool IsSubscribed(Product product)
    154.     {
    155.         if (String.Equals(product.definition.id, kProductIDWeek, StringComparison.Ordinal))
    156.             return true;
    157.         else if (String.Equals(product.definition.id, kProductIDMonth, StringComparison.Ordinal))
    158.             return true;
    159.         else if (String.Equals(product.definition.id, kProductIDThreeMonth, StringComparison.Ordinal))
    160.             return true;
    161.         else if (String.Equals(product.definition.id, kProductIDYear, StringComparison.Ordinal))
    162.             return true;
    163.         else
    164.             return false;
    165.     }
    166.  
    167.  
    168.  
    169.     public void OnInitializeFailed(InitializationFailureReason error)
    170.     {
    171.         Debug.Log("OnInitializeFailed InitializationFailureReason: " + error);
    172.     }
    173.  
    174.     public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    175.     {
    176.         if (IsSubscribed(args.purchasedProduct))
    177.         {
    178.             Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    179.             PlayerPrefs.SetString("Receipt", args.purchasedProduct.receipt);
    180.             PlayerPrefs.Save();
    181.             PlayerPrefs.SetInt("IsSubscriptionValid", 1);
    182.             hideLocks();
    183.         }
    184.         else
    185.         {
    186.             Debug.Log("Subscription is NOT Active");
    187.             PlayerPrefs.SetInt("IsSubscriptionValid", 0);
    188.         }
    189.  
    190.         return PurchaseProcessingResult.Complete;
    191.     }
    192.  
    193.     public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    194.     {
    195.  
    196.         Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
    197.     }
    198.  
    199.  
    200.     void hideLocks()
    201.     {
    202.         LoadingDecision loading = FindObjectOfType<LoadingDecision>();
    203.         if (loading != null)
    204.         {
    205.             loading.hideLocks();
    206.         }
    207.     }
    208.  
    209. }
    210.  

    Regards: Nadeem Shahzad
    Contact Details: nadeem.shahzad.10236@gmail.com - +92340-7631749
     
  2. shadermanger

    shadermanger

    Joined:
    Sep 21, 2020
    Posts:
    21
    @nicholasr Are You There please let me know !
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Do other products beside subscriptions work for you? I just published to Amazon a couple of days ago without issue (but I wasn't using subscriptions). I believe we still need to add support for Amazon subscriptions, I will check with the team.
     
  4. shadermanger

    shadermanger

    Joined:
    Sep 21, 2020
    Posts:
    21
    Yes, All other products were working fine!
     
    JeffDUnity3D likes this.
  5. shadermanger

    shadermanger

    Joined:
    Sep 21, 2020
    Posts:
    21
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Not yet, we'll try to get this into an upcoming release. I would guess 4-6 weeks
     
    shadermanger likes this.
  7. shadermanger

    shadermanger

    Joined:
    Sep 21, 2020
    Posts:
    21
    Waiting For You Update this whenever it's done! Will Remind you after some time
     
  8. ltomov

    ltomov

    Joined:
    Aug 3, 2017
    Posts:
    96
    Has there been any progress on this?

    I can confirm this is still a problem in 4.0.3 version of the IAP plugin and Unity 2020.3.14f1.

    Subscriptions work fine in Apple Appstore and Google Play, non-consumable IAPs work everywhere, including Amazon.

    I found this post:
    https://answers.unity.com/questions/1367978/unity-iap-amazon-subscriptions.html

    It says that we need to add the parent SKU of the subscription with builder.AddProduct and then make the purchase using it.
    But I can't do that - adding the parent SKU causes all other products to fail adding, with errors like "Unavailable product [unity-id]-[store-id]"