- Home /
Question by
shahin2019 · Apr 10, 2020 at 07:42 AM ·
c#unity 2dgoogle playiappurchase
how can I make a IAP Subscription code in unity 2019.1.9 for google play ?
how can I make a IAP Subscription code in unity 2019.1.9 for google play , means when he buy 5000 score subscription so add 5000 score to UIManager but it expired so back scores ? I do not using subscription so far help me please period subscription is : weekly is this code correct
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
using CodeStage.AntiCheat.ObscuredTypes;
public class ShopManager2 : MonoBehaviour,IStoreListener
{ public static ShopManager2 Instance{ set; get;}
private static IStoreController m_StoreController;
private static IExtensionProvider m_StoreExtensionProvider;
public static string kProductIDConsumable = "consumable";
public static string kProductIDNonConsumable = "nonconsumable";
public static string kProductIDSubscription = "subscription";
private static string kProductNameAppleSubscription="com.unity3d.subscription.new";
private static string kProductNameGooglePlaySubscription ="com.company.s.subscription.zshahin";
void Start()
{
if (m_StoreController == null)
{
InitializePurchasing();
}
}
public void InitializePurchasing()
{
if (IsInitialized())
{
return;
}
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(kProductIDConsumable, ProductType.Consumable);
builder.AddProduct(kProductIDNonConsumable, ProductType.NonConsumable);
builder.AddProduct(kProductIDSubscription, ProductType.Subscription, new IDs(){
{ kProductNameAppleSubscription, AppleAppStore.Name },
{ kProductNameGooglePlaySubscription, GooglePlay.Name },
});
UnityPurchasing.Initialize(this, builder);
}
private bool IsInitialized()
{
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void BuyConsumable()
{
BuyProductID(kProductIDConsumable);
}
public void BuyNonConsumable()
{
BuyProductID(kProductIDNonConsumable);
}
public void BuySubscription()
{
BuyProductID(kProductIDSubscription);
}
void BuyProductID(string productId)
{
if (IsInitialized())
{
Product product = m_StoreController.products.WithID(productId);
if (product != null && product.availableToPurchase)
{
Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
m_StoreController.InitiatePurchase(product);
}
else
{
Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
}
}
else
{
Debug.Log("BuyProductID FAIL. Not initialized.");
}
}
public void RestorePurchases()
{
if (!IsInitialized())
{
Debug.Log("RestorePurchases FAIL. Not initialized.");
return;
}
if (Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer)
{
Debug.Log("RestorePurchases started ...");
var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
apple.RestoreTransactions((result) => {
Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
});
}
else
{
Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
}
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
Debug.Log("OnInitialized: PASS");
m_StoreController = controller;
m_StoreExtensionProvider = extensions;
}
public void OnInitializeFailed(InitializationFailureReason error)
{
Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{ //This can be called anytime after initialization
//And it should probably be limited to Google Play and not just Android
#if UNITY_ANDROID
foreach (Product p in m_StoreController.products.all) {
GooglePurchaseData data = new GooglePurchaseData(p.receipt);
if (p.hasReceipt) {
Debug.Log(data.json.autoRenewing);
Debug.Log(data.json.orderId);
Debug.Log(data.json.packageName);
Debug.Log(data.json.productId);
Debug.Log(data.json.purchaseTime);
Debug.Log(data.json.purchaseState);
Debug.Log(data.json.purchaseToken);
}
}
#endif
if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
}
else if (String.Equals(args.purchasedProduct.definition.id, kProductIDNonConsumable, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
}
else if (String.Equals(args.purchasedProduct.definition.id, kProductIDSubscription, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
}
else
{
Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}
return PurchaseProcessingResult.Complete;
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason);
{
Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
}
}
Comment
Your answer
Follow this Question
Related Questions
IAP Restore purchase on Android "Already Recorded Transaction" 0 Answers
Unity IAP displays the Play Store Description and not my in-game description 0 Answers
IAP button fails to update GUI after scene change 0 Answers
How to use Google play billing library 3? 0 Answers
Unity IAP Codeless not Working on Android Device after Application.Quit() 0 Answers