/** * docs in {@link PurchaseObserver#onPurchaseStateChange(com.soomla.billing.Consts.PurchaseState, * String, long, String)}. */ @Override public void onPurchaseStateChange( Consts.PurchaseState purchaseState, String productId, long purchaseTime, String developerPayload) { try { PurchasableVirtualItem purchasableVirtualItem = StoreInfo.getPurchasableItem(productId); BusProvider.getInstance() .post(new PlayPurchaseEvent(purchasableVirtualItem, developerPayload)); if (purchaseState == Consts.PurchaseState.PURCHASED) { purchasableVirtualItem.give(1); } if (purchaseState == Consts.PurchaseState.REFUNDED) { if (!StoreConfig.friendlyRefunds) { purchasableVirtualItem.take(1); } } BusProvider.getInstance().post(new ItemPurchasedEvent(purchasableVirtualItem)); } catch (VirtualItemNotFoundException e) { StoreUtils.LogError( TAG, "ERROR : Couldn't find the " + purchaseState.name() + " VirtualCurrencyPack OR GoogleMarketItem with productId: " + productId + ". It's unexpected so an unexpected error is being emitted."); BusProvider.getInstance().post(new UnexpectedStoreErrorEvent()); } }
/** * docs in {@link * PurchaseObserver#onRequestPurchaseResponse(com.soomla.billing.BillingService.RequestPurchase, * com.soomla.billing.Consts.ResponseCode)}. */ @Override public void onRequestPurchaseResponse( BillingService.RequestPurchase request, Consts.ResponseCode responseCode) { if (responseCode == Consts.ResponseCode.RESULT_OK) { // purchase was sent to server } else if (responseCode == Consts.ResponseCode.RESULT_USER_CANCELED) { try { BusProvider.getInstance() .post(new PlayPurchaseCancelledEvent(StoreInfo.getPurchasableItem(request.mProductId))); } catch (VirtualItemNotFoundException e) { StoreUtils.LogError( TAG, "ERROR : Couldn't find the CANCELLED VirtualCurrencyPack OR GoogleMarketItem with productId: " + request.mProductId + ". It's unexpected so an unexpected error is being emitted."); BusProvider.getInstance().post(new UnexpectedStoreErrorEvent()); } } else { // purchase failed ! BusProvider.getInstance().post(new UnexpectedStoreErrorEvent()); StoreUtils.LogError(TAG, "ERROR : Purchase failed for productId: " + request.mProductId); } }
/** * Start a purchase process with Google Play. * * @param googleMarketItem is the item to purchase. This item has to be defined EXACTLY the same * in Google Play. * @param payload a payload to get back when this purchase is finished. */ public boolean buyWithGooglePlay(GoogleMarketItem googleMarketItem, String payload) { if (!checkInit()) return false; SharedPreferences prefs = new ObscuredSharedPreferences( SoomlaApp.getAppContext(), SoomlaApp.getAppContext() .getSharedPreferences(StoreConfig.PREFS_NAME, Context.MODE_PRIVATE)); String publicKey = prefs.getString(StoreConfig.PUBLIC_KEY, ""); if (publicKey.isEmpty() || publicKey.equals("[YOUR PUBLIC KEY FROM GOOGLE PLAY]")) { StoreUtils.LogError(TAG, "You didn't provide a public key! You can't make purchases."); return false; } if (!mBillingService.requestPurchase( googleMarketItem.getProductId(), Consts.ITEM_TYPE_INAPP, payload)) { return false; } try { BusProvider.getInstance() .post( new PlayPurchaseStartedEvent( StoreInfo.getPurchasableItem(googleMarketItem.getProductId()))); } catch (VirtualItemNotFoundException e) { StoreUtils.LogError( TAG, "Couldn't find a purchasable item with productId: " + googleMarketItem.getProductId()); } return true; }