@Override public void purchaseRestore() { try { List<Transaction> transactions = googleInAppBillingService.getPurchases(); Array<Transaction> entitlements = new Array<>(Transaction.class); for (int i = 0; i < transactions.size(); i++) { Transaction transaction = transactions.get(i); if (OfferType.CONSUMABLE == getOfferType(transaction.getIdentifier())) { googleInAppBillingService.consumePurchase(transaction, observer); } else { entitlements.add(transaction); } } if (observer != null) { observer.handleRestore(entitlements.toArray()); } } catch (GdxPayException e) { if (observer != null) { observer.handleRestoreError(e); } } }
@Override protected PurchaseManagerConfig getPurchaseManagerConfig() { PurchaseManagerConfig purchaseManagerConfig = new PurchaseManagerConfig(); for (LevelPack levelPack : LevelManager.getLevelPacks()) { if (!levelPack.isFree()) { Offer offer = new Offer(); offer .setType(OfferType.ENTITLEMENT) .setIdentifier(levelPack.getGoogleSku()); offer.putIdentifierForStore( PurchaseManagerConfig.STORE_NAME_ANDROID_GOOGLE, levelPack.getGoogleSku()); purchaseManagerConfig.addOffer(offer); } } String key = PhysicsComponent.s(com.draga.shape.Circle.s( GraphicComponent.s(Circle.s) + MenuScreen.s(IngameMenuScreen.s, -3) + GraphicComponent.s(HudScreen.s) + MenuScreen.s(DeadZoneInputModifier.s, 4) + GraphicComponent.s(Joystick.s))); purchaseManagerConfig.addStoreParam( PurchaseManagerConfig.STORE_NAME_ANDROID_GOOGLE, key); return purchaseManagerConfig; }
private void getPurchaseIdsByType(List<String> inAppPurchasesIds, List<String> subsPurchasesIds) { for (int i = 0; i < purchaseManagerConfig.getOfferCount(); i++) { Offer offer = purchaseManagerConfig.getOffer(i); if (offer.getType().equals(OfferType.SUBSCRIPTION)) subsPurchasesIds.add(offer.getIdentifier()); else inAppPurchasesIds.add(offer.getIdentifier()); } }
private OfferType getOfferType(String identifier) { Offer offer = purchaseManagerConfig.getOffer(identifier); if (offer == null || offer.getType() == null) { throw new IllegalStateException("No offer or offerType configured for identifier: " + identifier + ", offer: " + offer); } return offer.getType(); }
private List<String> getIdsForEntitlementsAndConsumables() { final List<String> result = new ArrayList<String>(); final int offerCount = config.getOfferCount(); for (int i = 0; i < offerCount; i++) { Offer offer = config.getOffer(i); if (offer.getType() == OfferType.ENTITLEMENT || offer.getType() == OfferType.CONSUMABLE) { result.add(offer.getIdentifier()); } } return result; }
private List<String> getIdsForSubscriptions() { final List<String> result = new ArrayList<String>(); final int offerCount = config.getOfferCount(); for (int i = 0; i < offerCount; i++) { Offer offer = config.getOffer(i); if (offer.getType() == OfferType.SUBSCRIPTION) { result.add(offer.getIdentifier()); } } return result; }
@Override public void purchase(final String identifier) { assertInstalled(); final OfferType offerType = getOfferType(identifier); googleInAppBillingService.startPurchaseRequest(identifier, OfferToInAppPurchaseConverter.convertOfferType(offerType), new PurchaseRequestCallback() { @Override public void purchaseSuccess(Transaction transaction) { if (observer != null) { switch (offerType) { case CONSUMABLE: // Warning: observer.handlePurchase is called in googleInAppBillingService.consumePurchase. // That is not clean, I would prefer to keep it on one place. // Should be refactored later. googleInAppBillingService.consumePurchase(transaction, observer); break; case ENTITLEMENT: case SUBSCRIPTION: observer.handlePurchase(transaction); break; default: String error = "Unsupported OfferType=" + getOfferType(identifier) + " for identifier=" + identifier; throw new GdxPayException(error); } } } @Override public void purchaseError(GdxPayException exception) { if (observer != null) { observer.handlePurchaseError(exception); } } @Override public void purchaseCanceled() { if (observer != null) { observer.handlePurchaseCanceled(); } } }); }
static public String convertOfferType(OfferType offerType ) { if (offerType.equals(OfferType.SUBSCRIPTION)) return PURCHASE_TYPE_SUBSCRIPTION; return PURCHASE_TYPE_IN_APP; }
public static Offer offerFullEditionEntitlement() { Offer offer = new Offer(); offer.setIdentifier(PRODUCT_IDENTIFIER_FULL_EDITION); offer.setType(OfferType.ENTITLEMENT); return offer; }
public static Offer offerSubscription() { Offer offer = new Offer(); offer.setIdentifier("com.appname.subscription"); offer.setType(OfferType.SUBSCRIPTION); return offer; }
public static Offer offerConsumable() { Offer offer = new Offer(); offer.setIdentifier("com.appname.consumable.100.coins"); offer.setType(OfferType.CONSUMABLE); return offer; }