Search in sources :

Example 1 with IntroductoryPriceInfo

use of com.google.api.services.androidpublisher.model.IntroductoryPriceInfo in project OsmAnd-tools by osmandapp.

the class UpdateSubscription method updateSubscriptionDb.

private boolean updateSubscriptionDb(String orderId, String sku, Timestamp startTime, Timestamp expireTime, long tm, SubscriptionPurchase subscription) throws SQLException, SubscriptionUpdateException {
    boolean updated = false;
    int ind = 1;
    updStat.setTimestamp(ind++, new Timestamp(tm));
    if (subscription.getStartTimeMillis() != null) {
        int maxDays = 40;
        if (startTime != null && Math.abs(startTime.getTime() - subscription.getStartTimeMillis()) > maxDays * DAY && startTime.getTime() > 100000 * 1000L) {
            throw new SubscriptionUpdateException(orderId, String.format("ERROR: Start timestamp changed more than %d days '%s' (db) != '%s' (appstore) '%s' %s", maxDays, new Date(startTime.getTime()), new Date(subscription.getStartTimeMillis()), orderId, sku));
        // System.err.println(String.format("ERROR: Start timestamp changed more than 14 days '%s' (db) != '%s' (appstore) '%s' %s",
        // new Date(startTime.getTime()),
        // new Date(subscription.getStartTimeMillis()), orderId, sku));
        }
        updStat.setTimestamp(ind++, new Timestamp(subscription.getStartTimeMillis()));
        updated = true;
    } else {
        updStat.setTimestamp(ind++, startTime);
    }
    if (subscription.getExpiryTimeMillis() != null) {
        if (expireTime == null || Math.abs(expireTime.getTime() - subscription.getExpiryTimeMillis()) > 10 * 1000) {
            System.out.println(String.format("Expire timestamp changed %s != %s for '%s' %s", expireTime == null ? "" : new Date(expireTime.getTime()), new Date(subscription.getExpiryTimeMillis()), orderId, sku));
        }
        updStat.setTimestamp(ind++, new Timestamp(subscription.getExpiryTimeMillis()));
        updated = true;
    } else {
        updStat.setTimestamp(ind++, expireTime);
    }
    if (subscription.getAutoRenewing() == null) {
        updStat.setNull(ind++, Types.BOOLEAN);
    } else {
        updStat.setBoolean(ind++, subscription.getAutoRenewing());
    }
    if (subType == SubscriptionType.IOS) {
        IntroductoryPriceInfo info = subscription.getIntroductoryPriceInfo();
        if (info != null) {
            updStat.setInt(ind++, (int) info.getIntroductoryPriceCycles());
        } else {
            updStat.setNull(ind++, Types.INTEGER);
        }
    } else if (subType == SubscriptionType.HUAWEI) {
        updStat.setString(ind++, subscription.getDeveloperPayload());
        updStat.setInt(ind++, (int) (subscription.getPriceAmountMicros() / 1000l));
        updStat.setString(ind++, subscription.getPriceCurrencyCode());
    } else if (subType == SubscriptionType.AMAZON) {
    // none
    } else {
        if (subscription.getPaymentState() == null) {
            updStat.setNull(ind++, Types.INTEGER);
        } else {
            updStat.setInt(ind++, subscription.getPaymentState());
        }
        updStat.setString(ind++, subscription.getDeveloperPayload());
        updStat.setInt(ind++, (int) (subscription.getPriceAmountMicros() / 1000l));
        updStat.setString(ind++, subscription.getPriceCurrencyCode());
        IntroductoryPriceInfo info = subscription.getIntroductoryPriceInfo();
        if (info != null) {
            updStat.setInt(ind++, (int) (info.getIntroductoryPriceAmountMicros() / 1000l));
            updStat.setString(ind++, info.getIntroductoryPriceCurrencyCode());
            updStat.setInt(ind++, (int) info.getIntroductoryPriceCycles());
            updStat.setString(ind++, info.getIntroductoryPricePeriod());
        } else {
            updStat.setNull(ind++, Types.INTEGER);
            updStat.setNull(ind++, Types.VARCHAR);
            updStat.setNull(ind++, Types.INTEGER);
            updStat.setNull(ind++, Types.VARCHAR);
        }
    }
    if (subscription.getExpiryTimeMillis() != null) {
        boolean expired = tm - subscription.getExpiryTimeMillis() > MAX_WAITING_TIME_TO_EXPIRE;
        updStat.setBoolean(ind++, !expired);
        if (expired) {
            updated = true;
            updStat.setString(ind++, "expired");
        } else {
            updStat.setNull(ind++, Types.VARCHAR);
        }
    } else {
        updStat.setBoolean(ind++, true);
        updStat.setNull(ind++, Types.VARCHAR);
    }
    updStat.setString(ind++, orderId);
    updStat.setString(ind, sku);
    System.out.println(String.format("%s %s start %s expire %s", updated ? "Updates " : "No changes ", sku, startTime == null ? "" : new Date(startTime.getTime()), expireTime == null ? "" : new Date(expireTime.getTime())));
    if (updated) {
        updStat.addBatch();
        changes++;
        if (changes > BATCH_SIZE) {
            updStat.executeBatch();
            changes = 0;
        }
    }
    return updated;
}
Also used : IntroductoryPriceInfo(com.google.api.services.androidpublisher.model.IntroductoryPriceInfo) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 2 with IntroductoryPriceInfo

use of com.google.api.services.androidpublisher.model.IntroductoryPriceInfo in project OsmAnd-tools by osmandapp.

the class UpdateSubscription method parseIosSubscription.

private SubscriptionPurchase parseIosSubscription(String sku, String orderId, int prevIntroCycles, JsonObject receiptObj) {
    SubscriptionPurchase subscription = null;
    List<InAppReceipt> inAppReceipts = ReceiptValidationHelper.parseInAppReceipts(receiptObj);
    if (!inAppReceipts.isEmpty()) {
        boolean autoRenewing = false;
        int introCycles = 0;
        long startDate = 0;
        long expiresDate = 0;
        String appstoreOrderId = null;
        for (InAppReceipt receipt : inAppReceipts) {
            // i.e. 2020-04-01 -> 2021-04-01 + 2021-04-05 -> 2021-04-05
            if (sku.equals(receipt.getProductId()) && orderId.equals(receipt.getOrderId())) {
                appstoreOrderId = receipt.getOrderId();
                Map<String, String> fields = receipt.fields;
                // purchase_date_ms is purchase date of prolongation
                boolean introPeriod = "true".equals(fields.get("is_in_intro_offer_period"));
                long inAppStartDateMs = Long.parseLong(fields.get("purchase_date_ms"));
                long inAppExpiresDateMs = Long.parseLong(fields.get("expires_date_ms"));
                if (startDate == 0 || inAppStartDateMs < startDate) {
                    startDate = inAppStartDateMs;
                }
                if (inAppExpiresDateMs > expiresDate) {
                    autoRenewing = receipt.autoRenew;
                    expiresDate = inAppExpiresDateMs;
                }
                if (introPeriod) {
                    introCycles++;
                }
            }
        }
        if (expiresDate > 0) {
            IntroductoryPriceInfo ipo = null;
            introCycles = Math.max(prevIntroCycles, introCycles);
            if (introCycles > 0) {
                ipo = new IntroductoryPriceInfo();
                ipo.setIntroductoryPriceCycles(introCycles);
            }
            subscription = new SubscriptionPurchase().setIntroductoryPriceInfo(ipo).setStartTimeMillis(startDate).setExpiryTimeMillis(expiresDate).setAutoRenewing(autoRenewing).setOrderId(appstoreOrderId);
            if (!Algorithms.objectEquals(subscription.getOrderId(), orderId)) {
                throw new IllegalStateException(String.format("Order id '%s' != '%s' don't match", orderId, subscription.getOrderId()));
            }
        }
    }
    return subscription;
}
Also used : IntroductoryPriceInfo(com.google.api.services.androidpublisher.model.IntroductoryPriceInfo) SubscriptionPurchase(com.google.api.services.androidpublisher.model.SubscriptionPurchase) InAppReceipt(net.osmand.live.subscriptions.ReceiptValidationHelper.InAppReceipt)

Aggregations

IntroductoryPriceInfo (com.google.api.services.androidpublisher.model.IntroductoryPriceInfo)2 SubscriptionPurchase (com.google.api.services.androidpublisher.model.SubscriptionPurchase)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 InAppReceipt (net.osmand.live.subscriptions.ReceiptValidationHelper.InAppReceipt)1