use of com.soomla.store.events.RestoreTransactionsFinishedEvent in project android-store by soomla.
the class SoomlaStore method restoreTransactions.
/**
* Restoring old purchases for the current user (device).
*/
public void restoreTransactions() {
if (mInAppBillingService == null) {
SoomlaUtils.LogError(TAG, "Billing service is not loaded. Can't invoke restoreTransactions.");
return;
}
mInAppBillingService.initializeBillingService(new IabCallbacks.IabInitListener() {
@Override
public void success(boolean alreadyInBg) {
if (!alreadyInBg) {
notifyIabServiceStarted();
}
SoomlaUtils.LogDebug(TAG, "Setup successful, restoring purchases");
IabCallbacks.OnRestorePurchasesListener restorePurchasesListener = new IabCallbacks.OnRestorePurchasesListener() {
@Override
public void success(List<IabPurchase> purchases) {
SoomlaUtils.LogDebug(TAG, "Transactions restored");
if (purchases.size() > 0) {
if (SoomlaConfig.logDebug) {
String ownedSkus = "";
for (IabPurchase purchase : purchases) {
ownedSkus += purchase.getSku() + " / ";
}
SoomlaUtils.LogDebug(TAG, "Got owned items: " + ownedSkus);
}
handleSuccessfulPurchases(purchases, true, new HandleSuccessfulPurchasesFinishedHandler() {
@Override
public void onFinished() {
// Restore transactions always finished successfully even if
// something wrong happened when handling a specific item.
BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(true));
}
});
} else {
BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(true));
}
}
@Override
public void fail(String message) {
BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(false));
handleErrorResult(UnexpectedStoreErrorEvent.ErrorCode.GENERAL, message);
}
@Override
public void verificationStarted(List<IabPurchase> purchases) {
handleVerificationStarted(purchases);
}
};
IabCallbacks.OnRestorePurchasesListener restoreSubscriptionsListener = new IabCallbacks.OnRestorePurchasesListener() {
@Override
public void success(List<IabPurchase> purchases) {
// collect subscription ids list
List<String> subscriptionIds = new ArrayList<String>();
for (IabPurchase purchase : purchases) {
subscriptionIds.add(purchase.getSku());
}
// collect subscriptionVG list
List<VirtualGood> subscriptions = new ArrayList<VirtualGood>();
for (VirtualGood good : StoreInfo.getGoods()) {
if ((good.getPurchaseType() instanceof PurchaseWithMarket) && ((PurchaseWithMarket) good.getPurchaseType()).isSubscription()) {
subscriptions.add(good);
}
}
// give unset subscriptions and take expired
for (VirtualGood subscription : subscriptions) {
String productId = ((PurchaseWithMarket) subscription.getPurchaseType()).getMarketItem().getProductId();
if (subscriptionIds.contains(productId)) {
// TODO: is here should be 1 to give? Maybe current item has not only just 0/1 state
subscription.give(1, false);
} else {
try {
subscription.take(StoreInventory.getVirtualItemBalance(subscription.getItemId()), false);
} catch (VirtualItemNotFoundException ex) {
// possibly unreachable block
}
}
}
// TODO: Should we notify user about repaired or expired subscriptions?
}
@Override
public void fail(String message) {
SoomlaUtils.LogDebug(TAG, "Subscriptions restoring failed: " + message);
}
@Override
public void verificationStarted(List<IabPurchase> purchases) {
// should we do it in subscription restoring? possibly it should be empty
}
};
try {
mInAppBillingService.restorePurchasesAsync(restorePurchasesListener);
} catch (IllegalStateException ex) {
SoomlaUtils.LogError(TAG, "Can't proceed with restorePurchases. error: " + ex.getMessage());
restorePurchasesListener.fail("Can't proceed with restorePurchases. error: " + ex.getMessage());
}
}
@Override
public void fail(String message) {
reportIabInitFailure(message);
}
});
}
Aggregations