com.android.billingclient.api.Purchase Java Examples
The following examples show how to use
com.android.billingclient.api.Purchase.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BillingManager.java From UpdogFarmer with GNU General Public License v3.0 | 6 votes |
@Override public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) { if (responseCode == BillingClient.BillingResponse.OK && purchases != null) { for (Purchase purchase : purchases) { handlePurchase(purchase); } Log.i(TAG, "Purchases updated."); listener.onPurchasesUpdated(purchases); } else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) { // Handle an error caused by a user canceling the purchase flow. Log.i(TAG, "Purchase canceled."); listener.onPurchaseCanceled(); } else { // Handle any other error codes. Log.i(TAG, "Unknown error. Response code: " + responseCode); } }
Example #2
Source File: PreferencesBillingHelper.java From CommonUtils with Apache License 2.0 | 6 votes |
@Override public void onPurchasesUpdated(BillingResult br, @Nullable List<Purchase> purchases) { if (br.getResponseCode() == BillingResponseCode.OK) { listener.showToast(Toaster.build().message(R.string.thankYou).extra(purchases == null ? null : purchases.toString())); if (purchases == null || purchases.isEmpty()) return; for (Purchase p : purchases) { if (p.isAcknowledged()) continue; AcknowledgePurchaseParams params = AcknowledgePurchaseParams.newBuilder() .setPurchaseToken(p.getPurchaseToken()) .build(); billingClient.acknowledgePurchase(params, br1 -> { if (br1.getResponseCode() != BillingResponseCode.OK) handleBillingErrors(br1.getResponseCode()); }); } } else { handleBillingErrors(br.getResponseCode()); } }
Example #3
Source File: BillingPlugin.java From flutter_billing with Apache License 2.0 | 6 votes |
boolean handlePurchases(final List<Purchase> purchases) { Purchase purchase = findPurchase(purchases, identifier); if (purchase == null) return false; if (consume) { billingClient.consumeAsync(purchase.getPurchaseToken(), new ConsumeResponseListener() { @Override public void onConsumeResponse(int responseCode, String purchaseToken) { if (responseCode != BillingResponse.OK) { Log.w(TAG, "Failed to consume product with code " + responseCode); } result.success(getIdentifiers(purchases)); } }); } else { result.success(getIdentifiers(purchases)); } return true; }
Example #4
Source File: BillingManager.java From SchoolQuest with GNU General Public License v3.0 | 6 votes |
private void onQueryPurchasesFinished(Purchase.PurchasesResult result) { if (mBillingClient == null || result.getResponseCode() != BillingClient.BillingResponse.OK) { Log.w(TAG, "Billing client was null or result code (" + result.getResponseCode() + ") was bad – quitting"); return; } for (Purchase purchase: result.getPurchasesList()) { mBillingClient.consumeAsync(purchase.getPurchaseToken(), new ConsumeResponseListener() { @Override public void onConsumeResponse(int responseCode, String purchaseToken) { if (responseCode != BillingClient.BillingResponse.OK) { startServiceConnection(null); } } }); } }
Example #5
Source File: AccelerateDevelop.java From InviZible with GNU General Public License v3.0 | 6 votes |
public void initBilling() { mBillingClient = BillingClient.newBuilder(activity) .enablePendingPurchases() .setListener(new PurchasesUpdatedListener() { @Override public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchasesList) { if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchasesList != null) { Log.i(LOG_TAG, "Purchases are updated"); handlePurchases(purchasesList); } } }).build(); CachedExecutor.INSTANCE.getExecutorService().submit(() -> { mBillingClient.startConnection(AccelerateDevelop.this); }); }
Example #6
Source File: PictureInPictureUpgradeActivity.java From dtube-mobile-unofficial with Apache License 2.0 | 6 votes |
void handlePurchase(Purchase purchase) { if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) { // Grant entitlement to the user. //already purchased setContentView(R.layout.pipm_upgrade_activity_purchased); PreferenceManager.getDefaultSharedPreferences(PictureInPictureUpgradeActivity.this) .edit().putBoolean("upgraded",true).apply(); Preferences.hasUpgrade = true; setResult(RESULT_OK); // Acknowledge the purchase if it hasn't already been acknowledged. if (!purchase.isAcknowledged()) { AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder() .setPurchaseToken(purchase.getPurchaseToken()) .build(); billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener); } } }
Example #7
Source File: MainActivity.java From scroball with MIT License | 6 votes |
@Override protected void onResume() { super.onResume(); billingClient = new BillingClient.Builder(this).setListener(this).build(); billingClient.startConnection( new BillingClientStateListener() { @Override public void onBillingSetupFinished(@BillingResponse int billingResponseCode) { if (billingResponseCode == BillingResponse.OK) { Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP); onPurchasesUpdated( purchasesResult.getResponseCode(), purchasesResult.getPurchasesList()); } } @Override public void onBillingServiceDisconnected() {} }); }
Example #8
Source File: BillingManager.java From PhoneProfilesPlus with Apache License 2.0 | 6 votes |
@Override public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) { int responseCode = billingResult.getResponseCode(); //PPApplication.logE(TAG, "onPurchasesUpdated() response: " + responseCode); if (responseCode == BillingClient.BillingResponseCode.OK) { getFragment().purchaseSuccessful(purchases); if (purchases != null) { for (Purchase purchase : purchases) { consumePurchase(purchase); } } } else { getFragment().purchaseUnsuccessful(purchases); getFragment().displayAnErrorIfNeeded(responseCode); } }
Example #9
Source File: DonationFragment.java From PhoneProfilesPlus with Apache License 2.0 | 6 votes |
@SuppressWarnings("EmptyMethod") public void purchaseUnsuccessful(@SuppressWarnings("unused") List<Purchase> purchases) { /* if (purchases != null) { for (Purchase purchase : purchases) { String sku = purchase.getSku(); for (SkuDetails skuDetail : SKU_DETAILS) { if (skuDetail.getSku().equals(sku)) { if (PPApplication.logEnabled()) { PPApplication.logE(TAG, "purchaseUnsuccessful - sku=" + sku); PPApplication.logE(TAG, "purchaseUnsuccessful - currency=" + skuDetail.getPriceCurrencyCode()); PPApplication.logE(TAG, "purchaseUnsuccessful - priceS=" + skuDetail.getPrice()); PPApplication.logE(TAG, "purchaseUnsuccessful - priceMicros=" + skuDetail.getPriceAmountMicros()); PPApplication.logE(TAG, "purchaseUnsuccessful - price=" + skuDetail.getPriceAmountMicros() / 1000000.0); } // Answers.getInstance().logPurchase(new PurchaseEvent() // .putItemPrice(BigDecimal.valueOf(skuDetail.getPriceAmountMicros() / 1000000.0)) // .putCurrency(Currency.getInstance(skuDetail.getPriceCurrencyCode())) // .putItemName("Donation") // //.putItemType("Apparel") // .putItemId(sku) // .putSuccess(false)); } } } }*/ }
Example #10
Source File: MainActivity.java From scroball with MIT License | 6 votes |
@Override public void onPurchasesUpdated(int responseCode, List<Purchase> purchases) { if (responseCode != BillingResponse.OK) { purchaseFailed(); } else if (purchases != null) { for (Purchase purchase : purchases) { if (purchase.getSku().equals(REMOVE_ADS_SKU)) { RelativeLayout parent = (RelativeLayout) adView.getParent(); if (parent != null) { parent.removeView(adView); } this.invalidateOptionsMenu(); this.adsRemoved = true; SharedPreferences.Editor editor = application.getSharedPreferences().edit(); editor.putBoolean(REMOVE_ADS_SKU, true); editor.apply(); } } } }
Example #11
Source File: TestHelper.java From Cashier with Apache License 2.0 | 6 votes |
static void mockPurchases(AbstractGooglePlayBillingApi api, final List<Product> products) { List<Purchase> purchases = new ArrayList<>(); List<Purchase> inapp = new ArrayList<>(); List<Purchase> subs = new ArrayList<>(); for (Product product : products) { try { Purchase purchase = new TestPurchase(product); purchases.add(purchase); if (product.isSubscription()) { subs.add(purchase); } else { inapp.add(purchase); } } catch (JSONException e) {} } when(api.getPurchases()).thenReturn(purchases); when(api.getPurchases(BillingClient.SkuType.INAPP)).thenReturn(inapp); when(api.getPurchases(BillingClient.SkuType.SUBS)).thenReturn(subs); }
Example #12
Source File: InventoryQueryTest.java From Cashier with Apache License 2.0 | 6 votes |
@Test public void returns_error_when_sub_purchases_call_fails() { when(api.getPurchases(BillingClient.SkuType.SUBS)).thenReturn(null); when(api.getPurchases(BillingClient.SkuType.INAPP)).thenReturn(new ArrayList<Purchase>()); InventoryListener listener = mock(InventoryListener.class); InventoryQuery.execute( TestHelper.mockThreading(), api, listener, TestData.allInAppSkus, TestData.allSubSkus ); verify(listener).failure(any(Vendor.Error.class)); }
Example #13
Source File: InventoryQueryTest.java From Cashier with Apache License 2.0 | 6 votes |
@Test public void returns_error_when_inapp_purchases_call_fails() { when(api.getPurchases(BillingClient.SkuType.INAPP)).thenReturn(null); when(api.getPurchases(BillingClient.SkuType.SUBS)).thenReturn(new ArrayList<Purchase>()); InventoryListener listener = mock(InventoryListener.class); InventoryQuery.execute( TestHelper.mockThreading(), api, listener, TestData.allInAppSkus, TestData.allSubSkus ); verify(listener).failure(any(Vendor.Error.class)); }
Example #14
Source File: BillingManager.java From PhoneProfilesPlus with Apache License 2.0 | 5 votes |
private void consumePurchase(final Purchase purchase) { // Specify a runnable to start when connection to Billing client is established Runnable executeOnConnectedService = new Runnable() { @Override public void run() { ConsumeParams consumeParams = ConsumeParams.newBuilder() .setPurchaseToken(purchase.getPurchaseToken()) // https://developer.android.com/google/play/billing/developer-payload // Developer payload has historically been used for various purposes, including fraud // prevention and attributing purchases to the correct user. // With version 2.2 of the Google Play Billing Library, intended use cases that previously // relied on developer payload, are now fully supported in other parts of the library. //.setDeveloperPayload(purchase.getDeveloperPayload()) .build(); mBillingClient.consumeAsync(consumeParams, new ConsumeResponseListener() { @Override public void onConsumeResponse(@NonNull BillingResult billingResult, @NonNull String purchaseToken) { //PPApplication.logE(TAG, "onConsumeResponse() response: " + billingResult.getResponseCode()); /*if (responseCode == BillingClient.BillingResponse.OK) { // Handle the success of the consume operation. // For example, increase the number of player's coins, // that provide temporary benefits }*/ } } ); } }; // If Billing client was disconnected, we retry 1 time and if success, execute the query startServiceConnectionIfNeeded(executeOnConnectedService); }
Example #15
Source File: GoogleBillingHelper.java From Augendiagnose with GNU General Public License v2.0 | 5 votes |
/** * Acknowledge a purchase. * * @param purchase The purchase. */ private void doAcknowledgePurchaseIfRequired(final Purchase purchase) { if (!purchase.isAcknowledged()) { Log.d(TAG, "Acknowledging purchase " + purchase.getSku()); mBillingClient.acknowledgePurchase( AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build(), new AcknowledgePurchaseResponseListener() { @Override public void onAcknowledgePurchaseResponse(final BillingResult billingResult) { Log.d(TAG, "Acknowledgement result: " + billingResult.getResponseCode()); } }); } }
Example #16
Source File: GoogleIap.java From remixed-dungeon with GNU General Public License v3.0 | 5 votes |
private void handlePurchase(Purchase purchase) { if (!verifySignature(purchase.getOriginalJson(), purchase.getSignature())) { EventCollector.logException("bad signature"); return; } //GLog.w("purchase: %s",purchase.toString()); //mBillingClient.consumeAsync(purchase.getPurchaseToken(),this); if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) { // Acknowledge the purchase if it hasn't already been acknowledged. if (!purchase.isAcknowledged()) { AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder() .setPurchaseToken(purchase.getPurchaseToken()) .build(); mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, billingResult -> { EventCollector.logEvent("billing_result", billingResult.getResponseCode() + "->" + billingResult.getDebugMessage()); }); } mPurchases.put(purchase.getSku().toLowerCase(Locale.ROOT), purchase); String orderId = purchase.getOrderId(); String purchaseData = purchase.getOrderId() +"," + purchase.getPackageName() + "," + purchase.getSku() + "," + purchase.getPurchaseToken(); if( !Preferences.INSTANCE.getBoolean(orderId,false) ) { EventCollector.logEvent("iap_data", purchaseData); Preferences.INSTANCE.put(orderId,true); } } }
Example #17
Source File: FakeGooglePlayBillingApi.java From Cashier with Apache License 2.0 | 5 votes |
/** * Notifies pending purchase listeners of successful transaction * @param sku Sku of purchased product * @param purchase Purchase object representing successful transaction */ static void notifyPurchaseSuccess(String sku, Purchase purchase) { FakePurchaseListener listener = pendingPurchases.get(sku); if (listener != null) { listener.onFakePurchaseSuccess(purchase); } }
Example #18
Source File: IAPHelper.java From rootvalidator with GNU General Public License v3.0 | 5 votes |
@Override public void onBillingSetupFinished(int responseCode) { Timber.d("onBillingSetupFinished(responseCode=%d)", responseCode); if (BillingClient.BillingResponse.OK == responseCode) { final Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.INAPP); Timber.d("queryPurchases(): code=%d, purchases=%s", purchasesResult.getResponseCode(), purchasesResult.getPurchasesList()); onPurchasesUpdated(purchasesResult.getResponseCode(), purchasesResult.getPurchasesList()); } }
Example #19
Source File: IAPHelper.java From rootvalidator with GNU General Public License v3.0 | 5 votes |
public void check() { Single.create((SingleOnSubscribe<Purchase.PurchasesResult>) e -> e.onSuccess(billingClient.queryPurchases(BillingClient.SkuType.INAPP))) .subscribeOn(Schedulers.io()) .filter(r -> r.getResponseCode() == 0 && r.getPurchasesList() != null) .map(Purchase.PurchasesResult::getPurchasesList) .subscribe(this::notifyOfPurchases, Timber::e); }
Example #20
Source File: DonationFragment.java From PhoneProfilesPlus with Apache License 2.0 | 5 votes |
public void purchaseSuccessful(@SuppressWarnings("unused") List<Purchase> purchases) { /* if (purchases != null) { for (Purchase purchase : purchases) { String sku = purchase.getSku(); for (SkuDetails skuDetail : SKU_DETAILS) { if (skuDetail.getSku().equals(sku)) { if (PPApplication.logEnabled()) { PPApplication.logE(TAG, "purchaseSuccessful - sku=" + sku); PPApplication.logE(TAG, "purchaseSuccessful - currency=" + skuDetail.getPriceCurrencyCode()); PPApplication.logE(TAG, "purchaseSuccessful - priceS=" + skuDetail.getPrice()); PPApplication.logE(TAG, "purchaseSuccessful - priceMicros=" + skuDetail.getPriceAmountMicros()); PPApplication.logE(TAG, "purchaseSuccessful - price=" + skuDetail.getPriceAmountMicros() / 1000000.0); } // Answers.getInstance().logPurchase(new PurchaseEvent() // .putItemPrice(BigDecimal.valueOf(skuDetail.getPriceAmountMicros() / 1000000.0)) // .putCurrency(Currency.getInstance(skuDetail.getPriceCurrencyCode())) // .putItemName("Donation") // //.putItemType("Apparel") // .putItemId(sku) // .putSuccess(true)); } } } } */ if (getActivity() != null) { PPApplication.setDonationDonated(getActivity().getApplicationContext()); PPApplication.showToast(getActivity().getApplicationContext(), getString(R.string.donation_thanks_dialog), Toast.LENGTH_LONG); } }
Example #21
Source File: GoogleIap.java From remixed-dungeon with GNU General Public License v3.0 | 5 votes |
public void queryPurchases() { Runnable queryToExecute = () -> { Purchase.PurchasesResult result = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP); onPurchasesUpdated(result.getBillingResult(), result.getPurchasesList()); //mBillingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP, GoogleIap.this); }; executeServiceRequest(queryToExecute); }
Example #22
Source File: GoogleIap.java From remixed-dungeon with GNU General Public License v3.0 | 5 votes |
@Override public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> list) { if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) { for (Purchase purchase : list) { handlePurchase(purchase); } mPurchasesUpdatedListener.onPurchasesUpdated(); } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) { // Handle an error caused by a user cancelling the purchase flow. } else { // Handle any other error codes. } }
Example #23
Source File: FakeGooglePlayBillingApi.java From Cashier with Apache License 2.0 | 5 votes |
@Override public void consumePurchase(final @NonNull String purchaseToken, final @NonNull ConsumeResponseListener listener) { // Use new thread to simulate network operation new Thread() { public void run() { // Wait 1 second to simulate network operation try { sleep(1000L); } catch (InterruptedException e) {} for (Iterator<Purchase> it = testInappPurchases.iterator(); it.hasNext();) { if (it.next().getPurchaseToken().equals(purchaseToken)) { it.remove(); } } for (Iterator<Purchase> it = testSubPurchases.iterator(); it.hasNext();) { if (it.next().getPurchaseToken().equals(purchaseToken)) { it.remove(); } } // Return result on main thread mainHandler.post(new Runnable() { @Override public void run() { listener.onConsumeResponse(BillingClient.BillingResponse.OK, purchaseToken); } }); } }.start(); }
Example #24
Source File: FakeGooglePlayBillingApi.java From Cashier with Apache License 2.0 | 5 votes |
@Nullable @Override public List<Purchase> getPurchases(String itemType) { if (itemType.equals(BillingClient.SkuType.SUBS)) { return new ArrayList<>(testSubPurchases); } else { return new ArrayList<>(testInappPurchases); } }
Example #25
Source File: FakeGooglePlayBillingApi.java From Cashier with Apache License 2.0 | 5 votes |
@Nullable @Override public List<Purchase> getPurchases() { ArrayList<Purchase> purchases = new ArrayList<>(); purchases.addAll(testInappPurchases); purchases.addAll(testSubPurchases); return purchases; }
Example #26
Source File: FakeGooglePlayBillingApi.java From Cashier with Apache License 2.0 | 5 votes |
@Override public void launchBillingFlow(@NonNull Activity activity, @NonNull final String sku, final String itemType) { for (Product product : testProducts) { if (product.sku().equals(sku)) { activity.startActivity(FakeGooglePlayCheckoutActivity.intent(activity, product, TEST_PRIVATE_KEY)); // Put listener to pendingPurchases map and wait until either // notifyPurchaseSuccess or notifyPurchaseError is called from FakeGooglePlayCheckoutActivity pendingPurchases.put(sku, new FakePurchaseListener() { @Override public void onFakePurchaseSuccess(Purchase purchase) { pendingPurchases.remove(sku); if (itemType.equals(BillingClient.SkuType.SUBS)) { testSubPurchases.add(purchase); } else { testInappPurchases.add(purchase); } vendor.onPurchasesUpdated(BillingClient.BillingResponse.OK, Collections.singletonList(purchase)); } @Override public void onFakePurchaseError(int responseCode) { pendingPurchases.remove(sku); vendor.onPurchasesUpdated(responseCode, null); } }); return; } } }
Example #27
Source File: GooglePlayBillingPurchaseTest.java From Cashier with Apache License 2.0 | 5 votes |
@Test public void create_purchased() throws JSONException { Purchase billingPurchase = new TestPurchase(TestData.productInappA, JSON_PURCHASED, SIGNATURE); GooglePlayBillingPurchase purchase = GooglePlayBillingPurchase.create(TestData.productInappA, billingPurchase); assertEquals(billingPurchase.getOrderId(), purchase.orderId()); assertEquals(billingPurchase.getPurchaseToken(), purchase.token()); assertEquals(billingPurchase.getSku(), purchase.product().sku()); assertEquals(JSON_PURCHASED, purchase.receipt()); assertTrue(purchase.purchased()); }
Example #28
Source File: PictureInPictureUpgradeActivity.java From dtube-mobile-unofficial with Apache License 2.0 | 5 votes |
@Override public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) { if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchases != null) { for (Purchase purchase : purchases) { handlePurchase(purchase); } } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) { // Handle an error caused by a user cancelling the purchase flow. } else { // Handle any other error codes. } }
Example #29
Source File: PictureInPictureUpgradeActivity.java From dtube-mobile-unofficial with Apache License 2.0 | 5 votes |
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pipm_upgrade_activity); billingClient = BillingClient.newBuilder(this).setListener(this).enablePendingPurchases().build(); billingClient.startConnection(new BillingClientStateListener() { @Override public void onBillingSetupFinished(BillingResult billingResult) { if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) { Log.d("bill", "onBillingSetupFinished"); loadSKUs(); for (Purchase p: billingClient.queryPurchases("inapp" ).getPurchasesList()) { Log.d("billp", "purchases: " + p.getPurchaseToken()); //already purchased PreferenceManager.getDefaultSharedPreferences(PictureInPictureUpgradeActivity.this) .edit().putBoolean("upgraded",true).apply(); Preferences.hasUpgrade = true; } } } @Override public void onBillingServiceDisconnected() { // Try to restart the connection on the next request to // Google Play by calling the startConnection() method. Log.d("bill", "onBillingServiceDisconnected"); } }); }
Example #30
Source File: BillingManager.java From UpdogFarmer with GNU General Public License v3.0 | 5 votes |
private void queryPurchases() { final Purchase.PurchasesResult result = billingClient.queryPurchases(BillingClient.SkuType.INAPP); final List<Purchase> purchases = result.getPurchasesList(); if (purchases != null) { for (Purchase purchase : purchases) { handlePurchase(purchase); } } }