Пример #1
0
        private void LaunchBilling(SkuDetails sku, Activity parent)
        {
            Log.Debug(TAG, "LaunchBilling()");
            BillingFlowParams billingFlowParams = BillingFlowParams.NewBuilder()
                                                  .SetSkuDetails(sku)
                                                  .Build();

            billingClient.LaunchBillingFlow(parent, billingFlowParams);
        }
Пример #2
0
        /// <summary>
        /// Completes the Purchase
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        private async Task <PurchaseResult> DoPurchaseAsync(SkuDetails product)
        {
            if (BillingClient == null || !BillingClient.IsReady)
            {
                await ConnectAsync();
            }
            _tcsPurchase = new TaskCompletionSource <PurchaseResult>();

            BillingFlowParams flowParams   = BillingFlowParams.NewBuilder().SetSkuDetails(product).Build();
            BillingResult     responseCode = BillingClient.LaunchBillingFlow(CrossCurrentActivity.Current.Activity, flowParams);

            return(await _tcsPurchase?.Task ?? default);
        }
Пример #3
0
        /// <summary>
        /// Initializes an async process to purchas the product. Only one purchase request can be happening at a time
        /// </summary>
        /// <param name="productId">Product to buy</param>
        /// <returns>Purchase object</returns>
        public async Task <InAppPurchaseResult> PurchaseAsync(string productId)
        {
            if (_billingClient == null || !_billingClient.IsReady)
            {
                throw new InAppPurchaseException(PurchaseError.DeveloperError, "Billing Client is not connected");
            }

            // Make sure no purchases are being currently made
            if (_transactionPurchased != null && !_transactionPurchased.Task.IsCanceled)
            {
                throw new InAppPurchaseException(PurchaseError.DeveloperError, "Another Purchase is in progress");
            }

            // First, get the SkuDetail
            SkuDetails sku;

            if (!_retrievedProducts.TryGetValue(productId, out sku))
            {
                throw new InAppPurchaseException(PurchaseError.DeveloperError,
                                                 $"Cannot find a retrieved Product with {productId} SKU. Products must be first queried from the Play Store");
            }

            // Build FlowParam for the Purchase
            BillingFlowParams flowParams = BillingFlowParams.NewBuilder()
                                           .SetSkuDetails(sku)
                                           .Build();

            // Set a new Task Source to wait for completion
            _transactionPurchased = new TaskCompletionSource <InAppPurchaseResult>();
            Task <InAppPurchaseResult> taskPurchaseComplete = _transactionPurchased.Task;

            //_billingClient.QueryPurchaseHistoryAsync(BillingClient.SkuType.Inapp, this);

            // Initiate the Billing Process.
            BillingResult response = _billingClient.LaunchBillingFlow(_activity, flowParams);

            if (response.ResponseCode != BillingResponseCode.Ok)
            {
                // Reset the in-app-purchase flow
                _transactionPurchased?.TrySetCanceled();
                _transactionPurchased = null;
                throw new InAppPurchaseException(response.ResponseCode.ToPurchaseError(), response.DebugMessage);
            }

            // Wait till the Task is complete (e.g. Succeeded or Failed - which will result in Exception)
            InAppPurchaseResult purchase = await taskPurchaseComplete;

            _transactionPurchased = null;

            return(purchase);
        }