use of com.soomla.store.events.MarketPurchaseStartedEvent in project android-store by soomla.
the class SoomlaStore method buyWithMarket.
/**
* Starts a purchase process in the market.
*
* @param marketItem The item to purchase - this item has to be defined EXACTLY the same in
* the market
* @param isSubscription determines if subscription is purchasing
* @param payload A payload to get back when this purchase is finished.
* @throws IllegalStateException
*/
public void buyWithMarket(final MarketItem marketItem, final boolean isSubscription, final String payload) throws IllegalStateException {
if (mInAppBillingService == null) {
SoomlaUtils.LogError(TAG, "Billing service is not loaded. Can't invoke buyWithMarket.");
return;
}
final PurchasableVirtualItem pvi;
try {
pvi = StoreInfo.getPurchasableItem(marketItem.getProductId());
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogError(TAG, "Couldn't find a purchasable item associated with: " + marketItem.getProductId());
BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL));
return;
}
mInAppBillingService.initializeBillingService(new IabCallbacks.IabInitListener() {
@Override
public void success(boolean alreadyInBg) {
if (!alreadyInBg) {
notifyIabServiceStarted();
}
IabCallbacks.OnPurchaseListener purchaseListener = new IabCallbacks.OnPurchaseListener() {
@Override
public void success(IabPurchase purchase) {
handleSuccessfulPurchase(purchase, false);
}
@Override
public void cancelled(IabPurchase purchase) {
handleCancelledPurchase(purchase);
}
@Override
public void alreadyOwned(IabPurchase purchase) {
String sku = purchase.getSku();
SoomlaUtils.LogDebug(TAG, "Tried to buy an item that was not" + " consumed (maybe it's an already owned " + "non-consumable). productId: " + sku);
try {
PurchasableVirtualItem pvi = StoreInfo.getPurchasableItem(sku);
consumeIfConsumable(purchase, pvi);
if (StoreInfo.isItemNonConsumable(pvi)) {
SoomlaUtils.LogDebug(TAG, "(alreadyOwned) the user tried to " + "buy a non-consumable that was already " + "owned. itemId: " + pvi.getItemId() + " productId: " + sku);
BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL));
}
} catch (VirtualItemNotFoundException e) {
SoomlaUtils.LogError(TAG, "(alreadyOwned) ERROR : Couldn't find the " + "VirtualCurrencyPack with productId: " + sku + ". It's unexpected so an unexpected error is being emitted.");
BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL));
}
}
@Override
public void fail(String message) {
handleErrorResult(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL, message);
}
@Override
public void verificationStarted(List<IabPurchase> purchases) {
handleVerificationStarted(purchases);
}
};
BusProvider.getInstance().post(new MarketPurchaseStartedEvent(pvi, getInAppBillingService().shouldVerifyPurchases()));
try {
if (isSubscription) {
mInAppBillingService.launchPurchaseFlow(IabHelper.ITEM_TYPE_SUBS, marketItem.getProductId(), purchaseListener, payload);
} else {
mInAppBillingService.launchPurchaseFlow(IabHelper.ITEM_TYPE_INAPP, marketItem.getProductId(), purchaseListener, payload);
}
} catch (IllegalStateException ex) {
SoomlaUtils.LogError(TAG, "Can't proceed with launchPurchaseFlow. error: " + ex.getMessage());
purchaseListener.fail("Can't proceed with launchPurchaseFlow. error: " + ex.getMessage());
}
}
@Override
public void fail(String message) {
reportIabInitFailure(message);
}
});
}
Aggregations