use of com.soomla.store.purchaseTypes.PurchaseWithMarket 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);
}
});
}
use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.
the class StoreInfo method replaceVirtualItem.
/**
* Replaces an old virtual item with a new one by doing the following:
* 1. Determines the type of the given virtual item.
* 2. Looks for the given virtual item in the relevant list, according to its type.
* 3. If found, removes it.
* 4. Adds the given virtual item.
*
* @param virtualItem the virtual item that replaces the old one if exists.
*/
public static void replaceVirtualItem(VirtualItem virtualItem) {
mVirtualItems.put(virtualItem.getItemId(), virtualItem);
if (virtualItem instanceof VirtualCurrency) {
for (int i = 0; i < mCurrencies.size(); i++) {
if (mCurrencies.get(i).getItemId().equals(virtualItem.getItemId())) {
mCurrencies.remove(i);
break;
}
}
mCurrencies.add((VirtualCurrency) virtualItem);
}
if (virtualItem instanceof VirtualCurrencyPack) {
VirtualCurrencyPack vcp = (VirtualCurrencyPack) virtualItem;
PurchaseType purchaseType = vcp.getPurchaseType();
if (purchaseType instanceof PurchaseWithMarket) {
mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vcp);
}
for (int i = 0; i < mCurrencyPacks.size(); i++) {
if (mCurrencyPacks.get(i).getItemId().equals(vcp.getItemId())) {
mCurrencyPacks.remove(i);
break;
}
}
mCurrencyPacks.add(vcp);
}
if (virtualItem instanceof VirtualGood) {
VirtualGood vg = (VirtualGood) virtualItem;
if (vg instanceof UpgradeVG) {
List<UpgradeVG> upgrades = mGoodsUpgrades.get(((UpgradeVG) vg).getGoodItemId());
if (upgrades == null) {
upgrades = new ArrayList<UpgradeVG>();
mGoodsUpgrades.put(((UpgradeVG) vg).getGoodItemId(), upgrades);
}
upgrades.add((UpgradeVG) vg);
}
PurchaseType purchaseType = vg.getPurchaseType();
if (purchaseType instanceof PurchaseWithMarket) {
mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vg);
}
for (int i = 0; i < mGoods.size(); i++) {
if (mGoods.get(i).getItemId().equals(vg.getItemId())) {
mGoods.remove(i);
break;
}
}
mGoods.add(vg);
}
}
use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.
the class StoreInfo method hasMarketIdDuplicates.
/** Private functions **/
/**
* Checks if given storeAssets is correct IStoreAssets instance
*
* @param assetsArray array of successors of PurchasableVirtualItem class.
* @return true if <code>marketId</code> duplicates exists and false otherwise.
*/
private static boolean hasMarketIdDuplicates(PurchasableVirtualItem[] assetsArray) {
HashSet<String> marketItemIds = new HashSet<String>();
for (PurchasableVirtualItem pvi : assetsArray) {
if (pvi.getPurchaseType() instanceof PurchaseWithMarket) {
String currentMarketId = ((PurchaseWithMarket) pvi.getPurchaseType()).getMarketItem().getProductId();
if (marketItemIds.contains(currentMarketId)) {
return true;
}
marketItemIds.add(currentMarketId);
}
}
return false;
}
use of com.soomla.store.purchaseTypes.PurchaseWithMarket in project android-store by soomla.
the class StoreInfo method fromJSONObject.
/**
* Transforms given jsonObject to StoreInfo
*
* @param jsonObject
* @throws JSONException
*/
private static void fromJSONObject(JSONObject jsonObject) throws JSONException {
mVirtualItems = new HashMap<String, VirtualItem>();
mPurchasableItems = new HashMap<String, PurchasableVirtualItem>();
mGoodsCategories = new HashMap<String, VirtualCategory>();
mGoodsUpgrades = new HashMap<String, List<UpgradeVG>>();
mCurrencyPacks = new LinkedList<VirtualCurrencyPack>();
mGoods = new LinkedList<VirtualGood>();
mCategories = new LinkedList<VirtualCategory>();
mCurrencies = new LinkedList<VirtualCurrency>();
if (jsonObject.has(StoreJSONConsts.STORE_CURRENCIES)) {
JSONArray virtualCurrencies = jsonObject.getJSONArray(StoreJSONConsts.STORE_CURRENCIES);
for (int i = 0; i < virtualCurrencies.length(); i++) {
JSONObject o = virtualCurrencies.getJSONObject(i);
VirtualCurrency c = new VirtualCurrency(o);
mCurrencies.add(c);
mVirtualItems.put(c.getItemId(), c);
}
}
if (jsonObject.has(StoreJSONConsts.STORE_CURRENCYPACKS)) {
JSONArray currencyPacks = jsonObject.getJSONArray(StoreJSONConsts.STORE_CURRENCYPACKS);
for (int i = 0; i < currencyPacks.length(); i++) {
JSONObject o = currencyPacks.getJSONObject(i);
VirtualCurrencyPack pack = new VirtualCurrencyPack(o);
mCurrencyPacks.add(pack);
mVirtualItems.put(pack.getItemId(), pack);
PurchaseType purchaseType = pack.getPurchaseType();
if (purchaseType instanceof PurchaseWithMarket) {
mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), pack);
}
}
}
// For example: VGU and VGP depend on other VGs
if (jsonObject.has(StoreJSONConsts.STORE_GOODS)) {
JSONObject virtualGoods = jsonObject.getJSONObject(StoreJSONConsts.STORE_GOODS);
if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_SU)) {
JSONArray suGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_SU);
for (int i = 0; i < suGoods.length(); i++) {
JSONObject o = suGoods.getJSONObject(i);
SingleUseVG g = new SingleUseVG(o);
addVG(g);
}
}
if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_LT)) {
JSONArray ltGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_LT);
for (int i = 0; i < ltGoods.length(); i++) {
JSONObject o = ltGoods.getJSONObject(i);
LifetimeVG g = new LifetimeVG(o);
addVG(g);
}
}
if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_EQ)) {
JSONArray eqGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_EQ);
for (int i = 0; i < eqGoods.length(); i++) {
JSONObject o = eqGoods.getJSONObject(i);
EquippableVG g = new EquippableVG(o);
addVG(g);
}
}
if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_PA)) {
JSONArray paGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_PA);
for (int i = 0; i < paGoods.length(); i++) {
JSONObject o = paGoods.getJSONObject(i);
SingleUsePackVG g = new SingleUsePackVG(o);
addVG(g);
}
}
if (virtualGoods.has(StoreJSONConsts.STORE_GOODS_UP)) {
JSONArray upGoods = virtualGoods.getJSONArray(StoreJSONConsts.STORE_GOODS_UP);
for (int i = 0; i < upGoods.length(); i++) {
JSONObject o = upGoods.getJSONObject(i);
UpgradeVG g = new UpgradeVG(o);
addVG(g);
List<UpgradeVG> upgrades = mGoodsUpgrades.get(g.getGoodItemId());
if (upgrades == null) {
upgrades = new ArrayList<UpgradeVG>();
mGoodsUpgrades.put(g.getGoodItemId(), upgrades);
}
upgrades.add(g);
}
}
}
// Categories depend on virtual goods. That's why the have to be initialized after!
if (jsonObject.has(StoreJSONConsts.STORE_CATEGORIES)) {
JSONArray virtualCategories = jsonObject.getJSONArray(StoreJSONConsts.STORE_CATEGORIES);
for (int i = 0; i < virtualCategories.length(); i++) {
JSONObject o = virtualCategories.getJSONObject(i);
VirtualCategory category = new VirtualCategory(o);
mCategories.add(category);
for (String goodItemId : category.getGoodsItemIds()) {
mGoodsCategories.put(goodItemId, category);
}
}
}
// Remove this code when no longer needed.
if (mNonConsumableMigrationNeeded) {
SoomlaUtils.LogDebug(TAG, "NonConsumables balance migration is required. Doing it now.");
nonConsBalancesToLTVGs();
}
}
Aggregations