/// <summary> /// Sets the Apple store promotion visibility for the specified product on the current device. /// Call this inside the handler of the <see cref="InitializeSucceeded"/> event to set /// the visibility for a promotional product on Apple App Store. /// On non-Apple platforms this method is a no-op. /// </summary> /// <param name="productName">Product name.</param> /// <param name="visible">If set to <c>true</c> the product is shown, otherwise it is hidden.</param> public static void SetAppleStorePromotionVisibility(string productName, bool visible) { #if EM_UIAP if (!IsInitialized()) { Debug.Log("Couldn't set promotion visibility: In-App Purchasing is not initialized."); return; } IAPProduct iapProduct = GetIAPProductByName(productName); if (iapProduct == null) { Debug.Log("Couldn't set promotion visibility: not found product with name: " + productName); return; } Product prod = sStoreController.products.WithID(iapProduct.Id); if (sAppleExtensions != null) { sAppleExtensions.SetStorePromotionVisibility(prod, visible ? AppleStorePromotionVisibility.Show : AppleStorePromotionVisibility.Hide); } #else Debug.Log("Couldn't set Apple store promotion visibility: In-App Purchasing module is not enabled."); #endif }
/// <summary> /// Gets the subscription info of the product using the SubscriptionManager class, /// which currently supports the Apple store and Google Play store. /// </summary> /// <returns>The subscription info.</returns> /// <param name="productName">Product name.</param> public static SubscriptionInfo GetSubscriptionInfo(string productName) { if (Application.platform != RuntimePlatform.Android && Application.platform != RuntimePlatform.IPhonePlayer) { Debug.Log("Getting subscription info is only available on Android and iOS."); return(null); } if (!IsInitialized()) { Debug.Log("Couldn't get subscripton info: In-App Purchasing is not initialized."); return(null); } IAPProduct iapProduct = GetIAPProductByName(productName); if (iapProduct == null) { Debug.Log("Couldn't get subscription info: not found product with name: " + productName); return(null); } Product prod = sStoreController.products.WithID(iapProduct.Id); if (prod.definition.type != ProductType.Subscription) { Debug.Log("Couldn't get subscription info: this product is not a subscription product."); return(null); } if (string.IsNullOrEmpty(prod.receipt)) { Debug.Log("Couldn't get subscription info: this product doesn't have a valid receipt."); return(null); } if (!IsProductAvailableForSubscriptionManager(prod.receipt)) { Debug.Log("Couldn't get subscription info: this product is not available for SubscriptionManager class, " + "only products that are purchase by 1.19+ SDK can use this class."); return(null); } // Now actually get the subscription info using SubscriptionManager class. Dictionary <string, string> introPriceDict = null; if (sAppleExtensions != null) { introPriceDict = sAppleExtensions.GetIntroductoryPriceDictionary(); } string introJson = (introPriceDict == null || !introPriceDict.ContainsKey(prod.definition.storeSpecificId)) ? null : introPriceDict[prod.definition.storeSpecificId]; SubscriptionManager p = new SubscriptionManager(prod, introJson); return(p.getSubscriptionInfo()); }
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) { if (Debug.isDebugBuild) { Debug.Log("Processing purchase of product: " + args.purchasedProduct.transactionID); } bool validPurchase = true; // presume validity if not validate receipt bool canValidateReceipt = false; // disable receipt validation by default if (Application.platform == RuntimePlatform.Android) { // On Android, receipt validation is only available for Google Play store canValidateReceipt = EM_Settings.InAppPurchasing.IsValidateGooglePlayReceipt; canValidateReceipt &= (GetAndroidStore(EM_Settings.InAppPurchasing.TargetAndroidStore) == AndroidStore.GooglePlay); } else if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.tvOS) { // Receipt validation is also available for Apple app stores canValidateReceipt = EM_Settings.InAppPurchasing.IsValidateAppleReceipt; } if (canValidateReceipt) { validPurchase = ValidateReceipt(args.purchasedProduct.receipt, true); } IAPProduct pd = GetIAPProductById(args.purchasedProduct.definition.id); if (validPurchase) { if (Debug.isDebugBuild) { Debug.Log("Product purchase completed."); } // Fire purchase success event PurchaseCompleted(pd); } else { // Fire purchase failure event if (Debug.isDebugBuild) { Debug.Log("Purchase FAILED: Invalid receipt."); } PurchaseFailed(pd); } return(PurchaseProcessingResult.Complete); }
/// <summary> /// Gets the parsed purchase receipt for the product. /// This method only works if receipt validation is enabled. /// </summary> /// <returns>The purchase receipt.</returns> /// <param name="productName">Product name.</param> public static IPurchaseReceipt GetPurchaseReceipt(string productName) { if (Application.platform != RuntimePlatform.Android && Application.platform != RuntimePlatform.IPhonePlayer) { Debug.Log("Getting purchase receipt is only available on Android and iOS."); return(null); } if (!IsInitialized()) { Debug.Log("Couldn't get purchase receipt: In-App Purchasing is not initialized."); return(null); } IAPProduct iapProduct = GetIAPProductByName(productName); if (iapProduct == null) { Debug.Log("Couldn't get purchase receipt: not found product with name: " + productName); return(null); } Product pd = sStoreController.products.WithID(iapProduct.Id); if (!pd.hasReceipt) { Debug.Log("Couldn't get purchase receipt: this product doesn't have a receipt."); return(null); } if (!IsReceiptValidationEnabled()) { Debug.Log("Couldn't get purchase receipt: please enable receipt validation."); return(null); } IPurchaseReceipt[] purchaseReceipts; if (!ValidateReceipt(pd.receipt, out purchaseReceipts)) { Debug.Log("Couldn't get purchase receipt: the receipt of this product is invalid."); return(null); } foreach (var r in purchaseReceipts) { if (r.productID.Equals(pd.definition.storeSpecificId)) { return(r); } } // If we reach here, there's no receipt with the matching productID return(null); }
/// <summary> /// Purchases the specified product. /// </summary> /// <param name="product">Product.</param> public static void Purchase(IAPProduct product) { if (product != null && product.Id != null) { PurchaseWithId(product.Id); } else { Debug.Log("IAP purchasing failed: product or product ID is null."); } }
/// <summary> /// Purchases the specified product. /// </summary> /// <param name="product">Product.</param> public static void Purchase(IAPProduct product) { if (product != null && product.Id != null) { PurchaseWithId(product.Id); } else { Debug.Log("Purchase FAILED: Either the product or its id is invalid."); } }
/// <summary> /// Purchases the product with specified name. /// </summary> /// <param name="productName">Product name.</param> public static void Purchase(string productName) { IAPProduct pd = GetIAPProductByName(productName); if (pd != null && pd.Id != null) { PurchaseWithId(pd.Id); } else { Debug.Log("IAP purchasing failed: Not found product with name: " + productName + " or its ID is invalid."); } }
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason) { // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing // this reason with the user to guide their troubleshooting actions. if (Debug.isDebugBuild) { Debug.Log(string.Format("Purchase product FAILED: Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason)); } // Fire purchase failure event IAPProduct pd = GetIAPProductById(product.definition.id); PurchaseFailed(pd); }
/// <summary> /// Purchases the product with specified name. /// </summary> /// <param name="productName">Product name.</param> public static void Purchase(string productName) { IAPProduct pd = GetIAPProductByName(productName); if (pd != null && pd.Id != null) { PurchaseWithId(pd.Id); } else { if (Debug.isDebugBuild) { Debug.Log("PurchaseWithName FAILED: Not found product with name: " + productName + " or its id is invalid."); } } }
/// <summary> /// Gets the product localized data provided by the stores. /// </summary> /// <returns>The product localized data.</returns> /// <param name="productId">Product name.</param> public static ProductMetadata GetProductLocalizedData(string productName) { if (!IsInitialized()) { Debug.Log("Couldn't get product localized data: In-App Purchasing is not initialized."); return(null); } IAPProduct iapProduct = GetIAPProductByName(productName); if (iapProduct == null) { Debug.Log("Couldn't get product localized data: not found product with name: " + productName); return(null); } return(sStoreController.products.WithID(iapProduct.Id).metadata); }
/// <summary> /// Gets the product registered with UnityIAP stores by its name. This method returns /// a Product object, which contains more information than an IAPProduct /// object, whose main purpose is for displaying. /// </summary> /// <returns>The product.</returns> /// <param name="productName">Product name.</param> public static Product GetProduct(string productName) { if (!IsInitialized()) { Debug.Log("GetProduct FAILED: In-App Purchasing is not initialized."); return(null); } IAPProduct iapProduct = GetIAPProductByName(productName); if (iapProduct == null) { Debug.Log("GetProduct FAILED: Not found product with name: " + productName); return(null); } return(_storeController.products.WithID(iapProduct.Id)); }
/// <summary> /// Determines whether the product with the specified name is owned. /// A product is consider owned if it has a receipt. If receipt validation /// is enabled, it is also required that this receipt passes the validation check. /// Note that this method is mostly useful with non-consumable products. /// Consumable products' receipts are not persisted between app restarts, /// therefore their ownership only pertains in the session they're purchased. /// In the case of subscription products, this method only checks if a product has been purchased before, /// it doesn't check if the subscription has been expired or canceled. /// </summary> /// <returns><c>true</c> if the product has a receipt and that receipt is valid (if receipt validation is enabled); otherwise, <c>false</c>.</returns> /// <param name="productId">Product name.</param> public static bool IsProductOwned(string productName) { #if EM_UIAP if (!IsInitialized()) { Debug.Log("IsProductOwned FAILED: In-App Purchasing is not initialized.."); return(false); } IAPProduct iapProduct = GetIAPProductByName(productName); if (iapProduct == null) { Debug.Log("IsProductOwned FAILED: Not found product with name: " + productName); return(false); } Product pd = _storeController.products.WithID(iapProduct.Id); if (pd.hasReceipt) { bool isValid = true; // presume validity if not validate receipt. if (IsReceiptValidationEnabled()) { IPurchaseReceipt[] purchaseReceipts; isValid = ValidateReceipt(pd.receipt, out purchaseReceipts); } return(isValid); } else { return(false); } #else Debug.Log("IsProductOwned FAILED: IAP module is not enabled."); return(false); #endif }
/// <summary> /// Determines whether the product with the specified name is owned. /// A product is consider owned if it has a receipt. If receipt validation /// is enabled, it is also required that this receipt passes the validation check. /// Note that this method is mostly useful with non-consumable products. /// Consumable products' receipts are not persisted between app restarts, /// therefore their ownership only pertains in the session they're purchased. /// In the case of subscription products, this method only checks if a product has been purchased before, /// it doesn't check if the subscription has been expired or canceled. /// </summary> /// <returns><c>true</c> if the product has a receipt and that receipt is valid (if receipt validation is enabled); otherwise, <c>false</c>.</returns> /// <param name="productId">Product name.</param> public static bool IsProductOwned(string productName) { #if EM_UIAP if (!IsInitialized()) { return(false); } IAPProduct iapProduct = GetIAPProductByName(productName); if (iapProduct == null) { return(false); } Product pd = sStoreController.products.WithID(iapProduct.Id); if (pd.hasReceipt) { bool isValid = true; // presume validity if not validate receipt. if (IsReceiptValidationEnabled()) { IPurchaseReceipt[] purchaseReceipts; isValid = ValidateReceipt(pd.receipt, out purchaseReceipts); } return(isValid); } else { return(false); } #else return(false); #endif }
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) { Debug.Log("Processing purchase of product: " + args.purchasedProduct.transactionID); bool validPurchase = true; // presume validity if not validate receipt if (IsReceiptValidationEnabled()) { IPurchaseReceipt[] purchaseReceipts; validPurchase = ValidateReceipt(args.purchasedProduct.receipt, out purchaseReceipts); } IAPProduct pd = GetIAPProductById(args.purchasedProduct.definition.id); if (validPurchase) { Debug.Log("Product purchase completed."); // Fire purchase success event if (PurchaseCompleted != null) { PurchaseCompleted(pd); } } else { Debug.Log("Couldn't purchase product: Invalid receipt."); // Fire purchase failure event if (PurchaseFailed != null) { PurchaseFailed(pd); } } return(PurchaseProcessingResult.Complete); }