public static async Task <PurchaseResults> RequestProductPurchaseAsync(string productId, bool isTestingMode)
 {
     return(isTestingMode ? await CurrentAppSimulator.RequestProductPurchaseAsync(productId) : await CurrentApp.RequestProductPurchaseAsync(productId));
 }
 RequestProductPurchaseAsync(string productId)
 {
     return(IsMockEnabled
         ? CurrentAppSimulator.RequestProductPurchaseAsync(productId)
         : CurrentApp.RequestProductPurchaseAsync(productId));
 }
        public static void PurchaseProduct(string productId, string developerPayload)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                // Kick off purchase; don't ask for a receipt when it returns
                IAsyncOperation <string> asyncOp;
                try
                {
                    asyncOp = CurrentApp.RequestProductPurchaseAsync(productId, false);
                }
                catch (Exception e)
                {
                    string errorMessage;
                    // When the user does not complete the purchase (e.g. cancels or navigates back from the Purchase Page), an exception with an HRESULT of E_FAIL is expected.
                    switch ((HResult)e.HResult)
                    {
                    case HResult.E_FAIL:
                        errorMessage = "Purchase cancelled";
                        break;

                    default:
                        errorMessage = e.Message;
                        break;
                    }
                    if (PurchaseFailed != null)
                    {
                        PurchaseFailed(errorMessage);
                    }
                    return;
                }
                asyncOp.Completed = (op, status) =>
                {
                    string errorMessage;
                    ProductLicense productLicense = null;
                    if (CurrentApp.LicenseInformation.ProductLicenses.TryGetValue(productId, out productLicense))
                    {
                        if (productLicense.IsActive)
                        {
                            if (PurchaseSucceeded != null)
                            {
                                PurchaseSucceeded(productId, developerPayload);
                            }
                            return;
                        }
                        else
                        {
                            errorMessage = op.ErrorCode.Message;
                        }
                    }
                    else
                    {
                        errorMessage = op.ErrorCode.Message;
                    }

                    if (PurchaseFailed != null)
                    {
                        PurchaseFailed(errorMessage);
                    }
                };
            });
        }
Beispiel #4
0
 protected async Task <PurchaseResults> RequestProductPurchaseAsyncInternal(string productId)
 {
     return(await CurrentApp.RequestProductPurchaseAsync(productId));
 }
Beispiel #5
0
        private async Task PurchaseInAppPurchase(string Product, string strRoaming)
        {
            try
            {
                if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 3))
                {
                    string[] filterList = new string[] { "Durable" };

                    if (UtilityData.addOnCollection == null || UtilityData.addOnCollection.ExtendedError != null)
                    {
                        UtilityData.addOnCollection = await storeContext.GetUserCollectionAsync(filterList);
                    }

                    var isPurchasedList = UtilityData.addOnCollection.Products.Values.Where(p => p.InAppOfferToken.Equals(Product)).ToList();
                    if (isPurchasedList.Count == 0)
                    {
                        if (UtilityData.addOnsAssociatedStoreProducts == null)
                        {
                            UtilityData.addOnsAssociatedStoreProducts = await storeContext.GetAssociatedStoreProductsAsync(filterList);
                        }

                        var AddOnList = UtilityData.addOnsAssociatedStoreProducts.Products.Values.Where(p => p.InAppOfferToken.Equals(Product)).ToList();

                        StorePurchaseResult result = await storeContext.RequestPurchaseAsync(AddOnList[0].StoreId);

                        if (result != null)
                        {
                            switch (result.Status)
                            {
                            case StorePurchaseStatus.AlreadyPurchased:
                                break;

                            case StorePurchaseStatus.Succeeded:
                                ApplicationData.Current.RoamingSettings.Values[strRoaming] = true;
                                UtilityClass.MessageDialog("Thank You very much for Purchasing. We really appreciate your kind support!", "Thank You very much! :)");
                                break;

                            case StorePurchaseStatus.NetworkError:
                            case StorePurchaseStatus.ServerError:
                                UtilityClass.MessageDialog("An Error Occured , Please Try Again!", "Error occured while purchasing.");
                                break;
                            }
                        }
                    }
                }
                else
                {
                    if (UtilityData.AppLicenseInformation == null)
                    {
                        UtilityData.AppLicenseInformation = CurrentApp.LicenseInformation;
                    }
                    if (!UtilityData.AppLicenseInformation.ProductLicenses[Product].IsActive)
                    {
                        try
                        {
                            PurchaseResults results = await CurrentApp.RequestProductPurchaseAsync(Product);

                            if (results.Status == ProductPurchaseStatus.Succeeded)
                            {
                                ApplicationData.Current.RoamingSettings.Values[strRoaming] = UtilityData.AppLicenseInformation.ProductLicenses[Product].IsActive;
                                BlankPage4.ncSettings.setSettings = "IAP";
                                UtilityClass.MessageDialog("Thank You very much for Purchasing. We really appreciate your kind support!", "Thank You very much! :)");
                            }
                        }
                        catch (Exception)
                        {
                            UtilityClass.MessageDialog("An Error Occured, Please Try Again", "Error");
                        }
                    }
                    else
                    {
                    }
                }
            }
            catch (Exception)
            {
                UtilityClass.MessageDialog("An Error Occured, Please Try Again", "Error");
            }
        }
        private async void buyInAppAsync(string inAppID, InAppPurchaseBuyCallbackMethod purchasedCallback)
                #endif
        {
                        #if WINDOWS_PHONE
            WinRTPlugin.Dispatcher.BeginInvoke(async delegate()
                        #else
            await WinRTPlugin.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async delegate()
                        #endif
            {
                                #if WINDOWS_PHONE
                if ((testing && (!wp8TestLicenseInformation.ProductLicenses[inAppID].IsActive || wp8TestLicenseInformation.ProductLicenses[inAppID].IsConsumable)) ||
                    (!testing && (!licenseInformation.ProductLicenses[inAppID].IsActive || licenseInformation.ProductLicenses[inAppID].IsConsumable)))
                                #else
                if (!licenseInformation.ProductLicenses[inAppID].IsActive || isConsumbable(inAppID))
                                #endif
                {
                    try
                    {
                        string receipt = null;
                                                #if WINDOWS_PHONE
                        string productID = null;
                        if (testing)
                        {
                            productID = wp8TestListingInformation.ProductListings[inAppID].ProductId;
                        }
                        else
                        {
                            productID = licenseInformation.ProductLicenses[inAppID].ProductId;
                        }
                                                #elif UNITY_METRO_8_0
                        string productID = licenseInformation.ProductLicenses[inAppID].ProductId;
                                                #else
                        PurchaseResults results;
                        string productID = licenseInformation.ProductLicenses[inAppID].ProductId;
                                                #endif

                        if (testing)
                        {
                                                        #if WINDOWS_PHONE
                            receipt = await CurrentAppSimulator.RequestProductPurchaseAsync(productID, true);
                            if (wp8TestLicenseInformation.ProductLicenses[inAppID].IsActive)
                            {
                                PlayerPrefsEx.SetIntAsync("ReignIAP_PurchasedAwarded_" + inAppID, 0, true);
                            }
                                                        #elif UNITY_METRO_8_0
                            receipt = await CurrentAppSimulator.RequestProductPurchaseAsync(productID, true);
                                                        #else
                            results = await CurrentAppSimulator.RequestProductPurchaseAsync(productID);
                            receipt = results.ReceiptXml;
                                                        #endif
                        }
                        else
                        {
                                                        #if WINDOWS_PHONE
                            receipt = await CurrentApp.RequestProductPurchaseAsync(productID, true);
                            if (licenseInformation.ProductLicenses[inAppID].IsActive)
                            {
                                PlayerPrefsEx.SetIntAsync("ReignIAP_PurchasedAwarded_" + inAppID, 0, true);
                            }
                                                        #elif UNITY_METRO_8_0
                            receipt = await CurrentApp.RequestProductPurchaseAsync(productID, true);
                                                        #else
                            results = await CurrentApp.RequestProductPurchaseAsync(productID);
                            receipt = results.ReceiptXml;
                                                        #endif
                        }

                                                #if UNITY_METRO_8_0
                        if (!string.IsNullOrEmpty(receipt) || licenseInformation.ProductLicenses[inAppID].IsActive)
                        {
                            PlayerPrefsEx.SetIntAsync("ReignIAP_PurchasedAwarded_" + inAppID, 0, true);
                        }
                                                #elif UNITY_METRO
                        if (results.Status == ProductPurchaseStatus.Succeeded || results.Status == ProductPurchaseStatus.AlreadyPurchased || licenseInformation.ProductLicenses[inAppID].IsActive)
                        {
                            PlayerPrefsEx.SetIntAsync("ReignIAP_PurchasedAwarded_" + inAppID, 0, true);
                        }
                                                #endif

                        if (purchasedCallback != null)
                        {
                                                        #if WINDOWS_PHONE
                            if (testing)
                            {
                                purchasedCallback(inAppID, receipt, wp8TestLicenseInformation.ProductLicenses[inAppID].IsActive);
                                if (wp8TestLicenseInformation.ProductLicenses[inAppID].IsConsumable)
                                {
                                    CurrentAppSimulator.ReportProductFulfillment(productID);
                                }
                            }
                            else
                            {
                                purchasedCallback(inAppID, receipt, licenseInformation.ProductLicenses[inAppID].IsActive);
                                if (licenseInformation.ProductLicenses[inAppID].IsConsumable)
                                {
                                    CurrentApp.ReportProductFulfillment(productID);
                                }
                            }
                                                        #elif UNITY_METRO_8_0
                            purchasedCallback(inAppID, receipt, !string.IsNullOrEmpty(receipt) || licenseInformation.ProductLicenses[inAppID].IsActive);
                            if (isConsumbable(inAppID))
                            {
                                Debug.LogError("NOTE: Consumable IAP not supported in 8.0");
                            }
                                                        #else
                            UnityEngine.WSA.Application.InvokeOnAppThread(() =>
                            {
                                purchasedCallback(inAppID, receipt, results.Status == ProductPurchaseStatus.Succeeded || results.Status == ProductPurchaseStatus.AlreadyPurchased || licenseInformation.ProductLicenses[inAppID].IsActive);
                            }, false);
                            if (isConsumbable(inAppID))
                            {
                                if (testing)
                                {
                                    await CurrentAppSimulator.ReportConsumableFulfillmentAsync(productID, results.TransactionId);
                                }
                                else
                                {
                                    await CurrentApp.ReportConsumableFulfillmentAsync(productID, results.TransactionId);
                                }
                            }
                                                        #endif
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogError(e.Message);
                        UnityEngine.WSA.Application.InvokeOnAppThread(() =>
                        {
                            if (purchasedCallback != null)
                            {
                                purchasedCallback(inAppID, null, false);
                            }
                        }, false);
                    }
                }
                else
                {
                    UnityEngine.WSA.Application.InvokeOnAppThread(() =>
                    {
                        if (purchasedCallback != null)
                        {
                            purchasedCallback(inAppID, null, true);
                        }
                    }, false);
                }
            });
        }