public void BuyProduct(ProductInfo productInfo)
    {
        if (iapAvailable != true)
        {
            OnObtainProductInfoFailure?.Invoke(IAP_NOT_AVAILABLE);
            return;
        }

        PurchaseIntentReq purchaseIntentReq = new PurchaseIntentReq
        {
            PriceType = productInfo.PriceType,
            ProductId = productInfo.ProductId,
            // ToDo : developer payload???
            DeveloperPayload = "test"
        };

        ITask <PurchaseIntentResult> task = iapClient.CreatePurchaseIntent(purchaseIntentReq);

        task.AddOnSuccessListener((result) =>
        {
            if (result != null)
            {
                Debug.Log("[HMSPlugin]:" + result.ErrMsg + result.ReturnCode.ToString());
                Debug.Log("[HMSPlugin]: Bought " + purchaseIntentReq.ProductId);
                Status status = result.Status;
                status.StartResolutionForResult((androidIntent) =>
                {
                    PurchaseResultInfo purchaseResultInfo = iapClient.ParsePurchaseResultInfoFromIntent(androidIntent);

                    Debug.Log("HMSPluginResult: " + purchaseResultInfo.ReturnCode);
                    Debug.Log("HMErrorMssg: " + purchaseResultInfo.ErrMsg);
                    Debug.Log("HMS: HMSInAppPurchaseData" + purchaseResultInfo.InAppPurchaseData);
                    Debug.Log("HMS: HMSInAppDataSignature" + purchaseResultInfo.InAppDataSignature);

                    switch (purchaseResultInfo.ReturnCode)
                    {
                    case OrderStatusCode.ORDER_STATE_SUCCESS:
                        OnBuyProductSuccess.Invoke(purchaseResultInfo);
                        break;

                    default:
                        OnBuyProductFailure.Invoke(purchaseResultInfo.ReturnCode);
                        break;
                    }
                }, (exception) =>
                {
                    Debug.Log("[HMSPlugin]:startIntent ERROR");
                });
            }
        }).AddOnFailureListener((exception) =>
        {
            Debug.Log("[HMSPlugin]: ERROR BuyProduct!!" + exception.Message);
        });
    }
Exemplo n.º 2
0
        public void InternalBuyProduct(ProductInfo productInfo, bool consumeAfter = true, string payload = "")
        {
            if (iapAvailable != true)
            {
                OnObtainProductInfoFailure?.Invoke(IAP_NOT_AVAILABLE);
                return;
            }

            PurchaseIntentReq purchaseIntentReq = new PurchaseIntentReq
            {
                PriceType        = productInfo.PriceType,
                ProductId        = productInfo.ProductId,
                DeveloperPayload = payload
            };

            ITask <PurchaseIntentResult> task = iapClient.CreatePurchaseIntent(purchaseIntentReq);

            task.AddOnSuccessListener((result) =>
            {
                if (result != null)
                {
                    Debug.Log("[HMSIAPManager]:" + result.ErrMsg + result.ReturnCode.ToString());
                    Debug.Log("[HMSIAPManager]: Buying " + purchaseIntentReq.ProductId);
                    Status status = result.Status;
                    status.StartResolutionForResult((androidIntent) =>
                    {
                        PurchaseResultInfo purchaseResultInfo = iapClient.ParsePurchaseResultInfoFromIntent(androidIntent);

                        if (purchaseResultInfo.ReturnCode == OrderStatusCode.ORDER_STATE_SUCCESS)
                        {
                            Debug.Log("[HMSIAPManager] HMSInAppPurchaseData" + purchaseResultInfo.InAppPurchaseData);
                            Debug.Log("[HMSIAPManager] HMSInAppDataSignature" + purchaseResultInfo.InAppDataSignature);
                            OnBuyProductSuccess.Invoke(purchaseResultInfo);
                            if (consumeAfter)
                            {
                                ConsumePurchase(purchaseResultInfo);
                            }
                        }
                        else
                        {
                            switch (purchaseResultInfo.ReturnCode)
                            {
                            case OrderStatusCode.ORDER_STATE_CANCEL:
                                Debug.LogError("[HMSIAPManager] User cancel payment");
                                break;

                            case OrderStatusCode.ORDER_STATE_FAILED:
                                Debug.LogError("[HMSIAPManager] order payment failed");
                                break;

                            case OrderStatusCode.ORDER_PRODUCT_OWNED:
                                Debug.LogError("[HMSIAPManager] Product owned");
                                break;

                            default:
                                Debug.LogError("[HMSIAPManager] BuyProduct failed. ReturnCode: " + purchaseResultInfo.ReturnCode + ", ErrorMsg: " + purchaseResultInfo.ErrMsg);
                                break;
                            }
                            OnBuyProductFailure?.Invoke(purchaseResultInfo.ReturnCode);
                        }
                    }, (exception) =>
                    {
                        Debug.LogError("[HMSIAPManager] startIntent ERROR");
                    });
                }
            }).AddOnFailureListener((exception) =>
            {
                Debug.LogError("[HMSIAPManager]: BuyProduct failed. CauseMessage: " + exception.WrappedCauseMessage + ", ExceptionMessage: " + exception.WrappedExceptionMessage);
            });
        }