@Override public void purchaseError(GdxPayException exception) { if (observer != null) { observer.handlePurchaseError(exception); } }
@Override public void didFail (SKRequest request, NSError error) { String errorMessage = "Error requesting product info to later purchase: " + (error != null ? error.toString() : "unknown"); log(LOGTYPEERROR, errorMessage); observer.handlePurchaseError(new RuntimeException(errorMessage)); } }
@Override public void requestDidFailWithError(SKRequest request, NSError error) { String errorMessage = "Error requesting product info to later purchase: " + (error != null ? error.toString() : "unknown"); log(LOGTYPEERROR, errorMessage); observer.handlePurchaseError(new RuntimeException(errorMessage)); } }
@Override public void run() { String message = "Failed consuming product: " + transaction.getIdentifier(); observer.handlePurchaseError(new ConsumeException(message, transaction, e)); } });
@Override public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) { // check the edge case that the callback comes with a delay right after dispose() was called if (observer == null) return; if (responseCode == BillingClient.BillingResponse.OK && purchases != null) { handlePurchase(purchases, false); } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) { observer.handlePurchaseCanceled(); } else if (responseCode == BillingClient.BillingResponse.ITEM_ALREADY_OWNED) { observer.handlePurchaseError(new ItemAlreadyOwnedException()); } else { Gdx.app.error(TAG, "onPurchasesUpdated failed with responseCode " + responseCode); observer.handlePurchaseError(new GdxPayException("onPurchasesUpdated failed with responseCode " + responseCode)); } }
@Override public void run() { try { Log.d(LOG_TAG, "Purchase consumer starting"); final int result = consumePurchaseToken(transaction.getTransactionData()); if (result == 0) { observer.handlePurchase(transaction); } else { ResponseCode responseCode = ResponseCode.findByCode(result); String productId = transaction.getIdentifier(); String error = "Consuming " + productId + " failed, " + responseCode; observer.handlePurchaseError(new ConsumeException(error, transaction)); } } catch (final RemoteException e) { applicationProxy.postRunnable(new Runnable() { @Override public void run() { String message = "Failed consuming product: " + transaction.getIdentifier(); observer.handlePurchaseError(new ConsumeException(message, transaction, e)); } }); } } }
@Test public void purchaseErrorShouldDelegateResultErrorToObserver() throws Exception { PurchaseRequestCallback callback = connectBindAndPurchaseRequestForFullEditionEntitlement(); GdxPayException exception = new GdxPayException("Network error"); callback.purchaseError(exception); verify(purchaseObserver).handlePurchaseError(exception); }
@Override public void didReceiveResponse (SKProductsRequest request, SKProductsResponse response) { // Received the registered products from AppStore. products = response.getProducts(); if (products.size() == 1) { // Create a SKPayment from the product and start purchase flow SKProduct product = products.get(0); log(LOGTYPELOG, "Product info received/purchasing product " + product.getProductIdentifier() + " ..."); SKPayment payment = new SKPayment(product); SKPaymentQueue.getDefaultQueue().addPayment(payment); } else { // wrong product count returned String errorMessage = "Error purchasing product (wrong product info count returned: " + products.size() + ")!"; log(LOGTYPEERROR, errorMessage); observer.handlePurchaseError(new RuntimeException(errorMessage)); } }
@Override public void productsRequestDidReceiveResponse(SKProductsRequest request, SKProductsResponse response) { // Received the registered products from AppStore. products = response.products(); if (products.size() == 1) { // Create a SKPayment from the product and start purchase flow SKProduct product = products.get(0); log(LOGTYPELOG, "Product info received/purchasing product " + product .productIdentifier() + " ..."); SKPayment payment = SKPayment.paymentWithProduct(product); ((SKPaymentQueue) SKPaymentQueue.defaultQueue()).addPayment(payment); } else { // wrong product count returned String errorMessage = "Error purchasing product (wrong product info count " + "returned: " + products.size() + ")!"; log(LOGTYPEERROR, errorMessage); observer.handlePurchaseError(new RuntimeException(errorMessage)); } }
observer.handlePurchaseError(new ItemAlreadyOwnedException()); break; observer.handlePurchaseError(new GdxPayException("onPurchaseResponse: INVALID_SKU")); break; observer.handlePurchaseError(new GdxPayException("onPurchaseResponse: FAILED")); break; observer.handlePurchaseError(new GdxPayException("onPurchaseResponse: NOT_SUPPORTED")); break;
@Override public void purchase(String identifier) { // Find the SKProduct for this identifier. Offer offer = config.getOffer(identifier); if (offer == null) { log(LOGTYPEERROR, "Invalid product identifier, " + identifier); observer.handlePurchaseError(new RuntimeException("Invalid product identifier, " + identifier)); } else { String identifierForStore = offer.getIdentifierForStore (PurchaseManagerConfig.STORE_NAME_IOS_APPLE); SKProduct product = getProductByStoreIdentifier(identifierForStore); if (product == null) { // Product with this identifier not found: load product info first and try to purchase again log(LOGTYPELOG, "Requesting product info for " + identifierForStore); NSMutableSet<String> identifierForStoreSet = (NSMutableSet<String>) NSMutableSet .alloc().initWithCapacity(1); identifierForStoreSet.addObject(identifierForStore); productsRequest = SKProductsRequest.alloc().initWithProductIdentifiers (identifierForStoreSet); productsRequest.setDelegate(new AppleProductsDelegatePurchase()); productsRequest.start(); } else { // Create a SKPayment from the product and start purchase flow log(LOGTYPELOG, "Purchasing product " + identifier + " ..."); SKPayment payment = SKPayment.paymentWithProduct(product); ((SKPaymentQueue) SKPaymentQueue.defaultQueue()).addPayment(payment); } } }
if (error == null) { log(LOGTYPEERROR, "Transaction failed but error-object is null: " + transaction); observer.handlePurchaseError(new RuntimeException("Transaction failed: " + transaction)); } else { log(LOGTYPEERROR, "Transaction failed: " + error.toString()); observer.handlePurchaseError(new RuntimeException("Transaction failed: " + error.toString()));
log(LOGTYPEERROR, "Transaction failed but error-object is null: " + transaction); observer.handlePurchaseError(new RuntimeException("Transaction failed: " + transaction)); } else if (error.code() == SKErrorCode.PaymentCancelled) { } else { log(LOGTYPEERROR, "Transaction failed: " + error.toString()); observer.handlePurchaseError(new RuntimeException("Transaction failed: " + error.toString()));