private void ResetPurchasableItem(InAppPurchasableItem purchasableInAppItem)
 {
     purchasableInAppItem.id             = null;
     purchasableInAppItem.name           = null;
     purchasableInAppItem.description    = null;
     purchasableInAppItem.localizedPrice = null;
     purchasableInAppItem.receipt        = null;
     purchasableInAppItem.transactionId  = null;
     purchasableInAppItem.currency       = null;
 }
        public void OnPurchaseFailed(Product item, PurchaseFailureReason reason)
        {
            Debug.Log("Billing::OnPurchaseFailed:" + item.metadata.localizedTitle);

            PurchaseIAPResultCode resultCode = PurchaseIAPResultCode.Failed;
            BillerErrors          error      = BillerErrors.NO_ERROR;
            InAppPurchasableItem  inApp      = CreatePurchasableInAppItem(item);

            switch (reason)
            {
            case PurchaseFailureReason.ExistingPurchasePending:
                error = BillerErrors.ATTEMPTING_TO_PURCHASE_PRODUCT_WITH_SAME_RECEIPT;
                break;

            case PurchaseFailureReason.PaymentDeclined:
                error = BillerErrors.PAYMENT_DECLINED;
                break;

            case PurchaseFailureReason.ProductUnavailable:
                error = BillerErrors.PRODUCT_UNAVAILABLE;
                break;

            case PurchaseFailureReason.PurchasingUnavailable:
                error = BillerErrors.PURCHASING_UNAVAILABLE;
                break;

            case PurchaseFailureReason.SignatureInvalid:
                error = BillerErrors.REMOTE_VALIDATION_FAILED;
                break;

            case PurchaseFailureReason.Unknown:
                error = BillerErrors.UNKNOWN;
                break;

            case PurchaseFailureReason.UserCancelled:
                resultCode = PurchaseIAPResultCode.Cancelled;
                break;
            }

            _isPurchaseInProgress = false;

            PurchaseIAPResult result = new PurchaseIAPResult(inApp, resultCode, error);

            PsdkEventSystem.Instance.NotifyOnBillingPurchased(result);
            reportPurchaseCampaignResult(false);
        }
        public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
        {
            Debug.Log("Billing::Processing Purchase: " + e.purchasedProduct.definition.id);
            Debug.Log("Billing::Receipt: " + e.purchasedProduct.receipt);
            Debug.Log("Billing::Transaction Id: " + e.purchasedProduct.transactionID);

            _iapInValidation     = CreatePurchasableInAppItem(e);
            _iapInValidationArgs = e;

            // If restore is in progress - Skip receipt validation ! We have to do this because the server will not validate a receipt that has already been validated in the past
            if (_isRestoreInProgress)
            {
                // Indicate we have handled this purchase, we will not be informed of it again.

                Debug.Log("Billing::ProcessPurchase : Skipping receipt validation since restore is in progress");

                if (IsNoAdsItem(_iapInValidation.id))
                {
                    Debug.Log("Billing::ProcessPurchase : IsNoAdsItem=true calling PurchaseAd");
                    PSDKMgr.Instance.PurchaseAd();
                }
                else
                {
                    Debug.Log("Billing::ProcessPurchase : IsNoAdsItem=false");
                }

                PurchaseIAPResult result = new PurchaseIAPResult(_iapInValidation, PurchaseIAPResultCode.Success, BillerErrors.NO_ERROR);

                SavePurchaseLocally(_iapInValidation.id);

                PsdkEventSystem.Instance.NotifyOnBillingPurchased(result);

                _isPurchaseInProgress = false;

                PSDKMgr.Instance.GetInAppPurchase().ItemPurchased(e.purchasedProduct.definition.storeSpecificId);

                return(PurchaseProcessingResult.Complete);
            }

            Dictionary <string, object> recieptJson = Json.Deserialize(_iapInValidation.receipt) as Dictionary <string, object>;
            string payload = recieptJson.Get <string>("Payload");

            string purchaseToken = null;

                        #if UNITY_ANDROID
            if (payload != null)
            {
                Dictionary <string, object> tmp = Json.Deserialize(payload) as Dictionary <string, object>;
                if (tmp != null)
                {
                    string tmpStr = tmp.Get <string> ("json");
                    if (tmpStr != null)
                    {
                        Dictionary <string, object> innerTmp = Json.Deserialize(tmpStr) as Dictionary <string, object>;
                        if (innerTmp != null)
                        {
                            purchaseToken = innerTmp.Get <string> ("purchaseToken");
                        }
                        else
                        {
                            Debug.Log("ProcessPurchase failed to Deserialize json");
                        }
                    }
                    else
                    {
                        Debug.Log("ProcessPurchase failed to find json attribut in payload");
                    }
                }
                else
                {
                    Debug.Log("ProcessPurchase failed to Deserialize payload");
                }
            }
            else
            {
                Debug.Log("ProcessPurchase failed to find payload attribute");
            }
                        #else
            purchaseToken = payload;
                        #endif
            if (purchaseToken != null)
            {
                Debug.Log("ProcessPurchase purchaseToken: " + purchaseToken);
                PSDKMgr.Instance.ValidateReceiptAndReport(purchaseToken, _iapInValidation.localizedPrice, _iapInValidation.currency, _iapInValidation.id);
            }
            else
            {
                Debug.Log("ProcessPurchase purchaseToken was not found.");
            }

            reportPurchaseCampaignResult(true);

            OnValidPurchaseResponseEvent(_iapInValidation.localizedPrice, _iapInValidation.currency, _iapInValidation.id, true);
            if (!IsConsumable(e.purchasedProduct.definition.storeSpecificId))
            {
                PSDKMgr.Instance.GetInAppPurchase().ItemPurchased(e.purchasedProduct.definition.storeSpecificId);
            }

            return(PurchaseProcessingResult.Complete);
        }