Task <SKPaymentTransaction> PurchaseAsync(string productId) { TaskCompletionSource <SKPaymentTransaction> tcsTransaction = new TaskCompletionSource <SKPaymentTransaction>(); Action <SKPaymentTransaction, bool> handler = null; handler = new Action <SKPaymentTransaction, bool>((tran, success) => { // Only handle results from this request if (productId != tran.Payment.ProductIdentifier) { return; } // Unsubscribe from future events paymentObserver.TransactionCompleted -= handler; if (!success) { tcsTransaction.TrySetException(new Exception(tran?.Error.LocalizedDescription)); } else { tcsTransaction.TrySetResult(tran); } }); paymentObserver.TransactionCompleted += handler; var payment = SKPayment.CreateFrom(productId); SKPaymentQueue.DefaultQueue.AddPayment(payment); return(tcsTransaction.Task); }
public void SKPayment_PaymentWithProduct() { SKProduct product = new SKProduct(); SKPayment payment = SKPayment.CreateFrom(product); Assert.IsNotNull(payment); }
public void PurchaseProduct() { _completionSource.TrySetCanceled(); _completionSource = new TaskCompletionSource <PurchaseResponse>(); SKPayment payment = SKPayment.CreateFrom(_appStoreProductId); SKPaymentQueue.DefaultQueue.AddPayment(payment); }
//protected async override Task<Receipt> BuyNative(Purchase purchase) async Task <Receipt> BuyNative(Purchase purchase) { var product = (SKProduct)purchase.NativeObject; var payment = SKPayment.CreateFrom(product); var source = new TaskCompletionSource <Receipt>(); _observerDelegate.Sources[purchase.Id] = source; SKPaymentQueue.DefaultQueue.AddPayment(payment); return(await source.Task); }
/// <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) { // First, set the Completion Task Source. The Payment Observer will handle only one request at a time TaskCompletionSource <InAppPurchaseResult> transactionPurchased = new TaskCompletionSource <InAppPurchaseResult>(); _paymentObserver.SetTransactionPurchasedTask(transactionPurchased); SKPayment payment = SKPayment.CreateFrom(productId); SKPaymentQueue.DefaultQueue.AddPayment(payment); // Wait till the Task is complete (e.g. Succeeded or Failed - which will result in Exception) return(await transactionPurchased.Task); }
Purchase(IProduct product) { if (await Resume()) { currentProduct = product; currentPurchaseTask = new TaskCompletionSource <Purchase>(); SKPayment payment = SKPayment.CreateFrom(product.ProductId); SKPaymentQueue.DefaultQueue.AddPayment(payment); return(await currentPurchaseTask.Task); } else { throw new PurchaseError("Service cannot be started or there is annother active purchase"); } }
public void PurchaseProduct(SKProduct product) { try { if (Reachability.InternetConnectionStatus() == NetworkStatus.NotReachable) { Logger.Log("ERROR: PurchaseProduct: No open internet connection is available."); using (var alert = new UIAlertView("ERROR", "No open internet connection is available.", null, "OK", null)) alert.Show(); return; } if (!CanMakePayments()) { Logger.Log("ERROR: PurchaseProduct: Cannot make payments"); using (var alert = new UIAlertView("ERROR", "Sorry but you cannot make purchases from the In App Billing store. Please try again later.", null, "OK", null)) alert.Show(); return; } if (product == null) { Logger.Log("ERROR: PurchaseProduct: Product is null"); return; } Logger.Log("PurchaseProduct: Product ID: " + product.ProductIdentifier); SKPayment payment = SKPayment.CreateFrom(product); SKPaymentQueue.DefaultQueue.AddPayment(payment); } catch (Exception ex) { Logger.Log("ERROR: PurchaseProduct: " + ex); } }
private Task <SKPaymentTransaction> Purchase(string productId) { var tcsTransaction = new TaskCompletionSource <SKPaymentTransaction>(); void TransactionCompletedHandler(SKPaymentTransaction trans, bool result) { if (trans?.Payment == null) { return; } if (productId != trans.Payment.ProductIdentifier) { return; } this.transactionObserver.TransactionCompleted -= TransactionCompletedHandler; if (result) { tcsTransaction.SetResult(trans); return; } var errorCode = trans.Error?.Code ?? -1; var description = trans.Error?.LocalizedDescription ?? string.Empty; tcsTransaction.SetException(new Exception($"交易失敗(errorCode: {errorCode})\r\n{description}")); } this.transactionObserver.TransactionCompleted += TransactionCompletedHandler; // 購買 SKPaymentQueue.DefaultQueue.AddPayment(SKPayment.CreateFrom(productId)); return(tcsTransaction.Task); }
Task <SKPaymentTransaction> PurchaseAsync(string productId) { var tcsTransaction = new TaskCompletionSource <SKPaymentTransaction>(); Action <SKPaymentTransaction, bool> handler = null; handler = new Action <SKPaymentTransaction, bool>((tran, success) => { if (tran?.Payment == null) { return; } // Only handle results from this request if (productId != tran.Payment.ProductIdentifier) { return; } // Unsubscribe from future events paymentObserver.TransactionCompleted -= handler; if (success) { tcsTransaction.TrySetResult(tran); return; } var errorCode = tran?.Error?.Code ?? -1; var description = tran?.Error?.LocalizedDescription ?? string.Empty; var error = PurchaseError.GeneralError; switch (errorCode) { case (int)SKError.PaymentCancelled: error = PurchaseError.UserCancelled; break; case (int)SKError.PaymentInvalid: error = PurchaseError.PaymentInvalid; break; case (int)SKError.PaymentNotAllowed: error = PurchaseError.PaymentNotAllowed; break; case (int)SKError.ProductNotAvailable: error = PurchaseError.ItemUnavailable; break; case (int)SKError.Unknown: error = PurchaseError.GeneralError; break; case (int)SKError.ClientInvalid: error = PurchaseError.BillingUnavailable; break; } tcsTransaction.TrySetException(new InAppBillingPurchaseException(error, description)); }); paymentObserver.TransactionCompleted += handler; var payment = SKPayment.CreateFrom(productId); SKPaymentQueue.DefaultQueue.AddPayment(payment); return(tcsTransaction.Task); }
Task <SKPaymentTransaction> PurchaseAsync(string productId) { Console.WriteLine($"PurchaseAsync() - productId: { productId }"); var tcsTransaction = new TaskCompletionSource <SKPaymentTransaction>(); Action <SKPaymentTransaction, bool> handler = null; handler = new Action <SKPaymentTransaction, bool>((tran, success) => { if (tran != null) { Console.WriteLine($"PurchaseAsync() - tran: { tran }"); if (tran.Payment != null) { Console.WriteLine($"PurchaseAsync() - tran - tran.Payment.ProductIdentifier: { tran.Payment.ProductIdentifier }"); } else { Console.WriteLine($"PurchaseAsync() - tran - tran.Payment.ProductIdentifier is null"); } if (tran.TransactionIdentifier != null) { Console.WriteLine($"PurchaseAsync() - tran - tran.TransactionIdentifier: { tran.TransactionIdentifier }"); } else { Console.WriteLine($"PurchaseAsync() - tran - tran.TransactionIdentifier is null"); } if (tran.Error != null) { Console.WriteLine($"PurchaseAsync() - tran - tran.Error.Code: { tran.Error.Code }"); Console.WriteLine($"PurchaseAsync() - tran - tran.Error.LocalizedDescription: { tran.Error.LocalizedDescription }"); Console.WriteLine($"PurchaseAsync() - tran - tran.Error.LocalizedFailureReason: { tran.Error.LocalizedFailureReason }"); } else { Console.WriteLine($"PurchaseAsync() - tran - tran.Error is null"); } } else { Console.WriteLine($"PurchaseAsync() - tran is null"); } Console.WriteLine($"PurchaseAsync() - success: { success }"); if (tran?.Payment == null) { return; } // Only handle results from this request if (productId != tran.Payment.ProductIdentifier) { Console.WriteLine($"PurchaseAsync() - Only handle results from this request"); return; } // Unsubscribe from future events paymentObserver.TransactionCompleted -= handler; if (success) { tcsTransaction.TrySetResult(tran); return; } var errorCode = tran?.Error?.Code ?? -1; Console.WriteLine($"PurchaseAsync() - errorCode: { errorCode }"); var description = tran?.Error?.LocalizedDescription ?? string.Empty; var error = PurchaseError.GeneralError; switch (errorCode) { case (int)SKError.PaymentCancelled: error = PurchaseError.UserCancelled; break; case (int)SKError.PaymentInvalid: error = PurchaseError.PaymentInvalid; break; case (int)SKError.PaymentNotAllowed: error = PurchaseError.PaymentNotAllowed; break; case (int)SKError.ProductNotAvailable: error = PurchaseError.ItemUnavailable; break; case (int)SKError.Unknown: error = PurchaseError.GeneralError; break; case (int)SKError.ClientInvalid: error = PurchaseError.BillingUnavailable; break; } Console.WriteLine($"PurchaseAsync() - Before - TrySetException"); if (tran != null && !string.IsNullOrWhiteSpace(tran.TransactionIdentifier)) { tcsTransaction.TrySetException(new InAppBillingPurchaseException(error, description, tran.TransactionIdentifier)); } else { tcsTransaction.TrySetException(new InAppBillingPurchaseException(error, description)); } Console.WriteLine($"PurchaseAsync() - After - TrySetException"); Console.WriteLine($"PurchaseAsync() - description: { description }"); Console.WriteLine($"PurchaseAsync() - error: { error }"); }); paymentObserver.TransactionCompleted += handler; Console.WriteLine($"PurchaseAsync() - Before - CreateFrom"); var payment = SKPayment.CreateFrom(productId); Console.WriteLine($"PurchaseAsync() - After - CreateFrom"); Console.WriteLine($"PurchaseAsync() - payment: { payment }"); SKPaymentQueue.DefaultQueue.AddPayment(payment); return(tcsTransaction.Task); }
async Task <SKPaymentTransaction> PurchaseAsync(string productId) { var tcsTransaction = new TaskCompletionSource <SKPaymentTransaction>(); Action <SKPaymentTransaction, bool> handler = null; handler = new Action <SKPaymentTransaction, bool>((tran, success) => { if (tran?.Payment == null) { return; } // Only handle results from this request if (productId != tran.Payment.ProductIdentifier) { return; } // Unsubscribe from future events paymentObserver.TransactionCompleted -= handler; if (success) { tcsTransaction.TrySetResult(tran); return; } var errorCode = tran?.Error?.Code ?? -1; var description = tran?.Error?.LocalizedDescription ?? string.Empty; var error = PurchaseError.GeneralError; switch (errorCode) { case (int)SKError.PaymentCancelled: error = PurchaseError.UserCancelled; break; case (int)SKError.PaymentInvalid: error = PurchaseError.PaymentInvalid; break; case (int)SKError.PaymentNotAllowed: error = PurchaseError.PaymentNotAllowed; break; case (int)SKError.ProductNotAvailable: error = PurchaseError.ItemUnavailable; break; case (int)SKError.Unknown: try { var underlyingError = tran?.Error?.UserInfo?["NSUnderlyingError"] as NSError; error = underlyingError?.Code == 3038 ? PurchaseError.AppleTermsConditionsChanged : PurchaseError.GeneralError; } catch { error = PurchaseError.GeneralError; } break; case (int)SKError.ClientInvalid: error = PurchaseError.BillingUnavailable; break; } tcsTransaction.TrySetException(new InAppBillingPurchaseException(error, description)); }); paymentObserver.TransactionCompleted += handler; #if __IOS__ || __TVOS__ var payment = SKPayment.CreateFrom(productId); #else var products = await GetProductAsync(new[] { productId }); var product = products?.FirstOrDefault(); if (product == null) { throw new InAppBillingPurchaseException(PurchaseError.InvalidProduct); } var payment = SKPayment.CreateFrom(product); //var payment = SKPayment.CreateFrom((SKProduct)SKProduct.FromObject(new NSString(productId))); #endif SKPaymentQueue.DefaultQueue.AddPayment(payment); return(await tcsTransaction.Task); }