Search in sources :

Example 1 with AmazonSubscription

use of net.osmand.live.subscriptions.AmazonIAPHelper.AmazonSubscription in project OsmAnd-tools by osmandapp.

the class UpdateSubscription method processAmazonSubscription.

private SubscriptionPurchase processAmazonSubscription(AmazonIAPHelper amazonIAPHelper, String purchaseToken, String sku, String orderId, Timestamp regTime, Timestamp startTime, Timestamp expireTime, long currentTime, boolean verbose) throws SQLException, SubscriptionUpdateException {
    AmazonSubscription subscription = null;
    String reason = "";
    String kind = null;
    try {
        if (orderId == null) {
            return null;
        }
        subscription = amazonIAPHelper.getAmazonSubscription(orderId, purchaseToken);
        if (verbose) {
            System.out.println("Result: " + subscription.toString());
        }
    } catch (IOException e) {
        int errorCode = 0;
        if (e instanceof AmazonIOException) {
            errorCode = ((AmazonIOException) e).responseCode;
        }
        if (expireTime != null && currentTime - expireTime.getTime() > MAX_WAITING_TIME_TO_EXPIRE) {
            reason = String.format(" subscription expired more than %.1f days ago (%s)", (currentTime - expireTime.getTime()) / (DAY * 1.0d), e.getMessage());
            kind = "expired";
        } else if (errorCode == AmazonIAPHelper.RESPONSE_CODE_USER_ID_ERROR || errorCode == AmazonIAPHelper.RESPONSE_CODE_TRANSACTION_ERROR) {
            kind = "gone";
            reason = " user doesn't exist (" + e.getMessage() + ") ";
        } else {
            reason = " unknown reason (should be checked and fixed)! " + e.getMessage();
        }
    }
    SubscriptionPurchase subscriptionPurchase = null;
    if (subscription != null) {
        String appStoreOrderId = simplifyOrderId(subscription.receiptId);
        if (!Algorithms.objectEquals(appStoreOrderId, orderId)) {
            throw new IllegalStateException(String.format("Order id '%s' != '%s' don't match", orderId, appStoreOrderId));
        }
        Long expiryTime = subscription.cancelDate != null ? subscription.cancelDate : subscription.renewalDate;
        subscriptionPurchase = new SubscriptionPurchase().setStartTimeMillis(subscription.purchaseDate).setExpiryTimeMillis(expiryTime).setAutoRenewing(subscription.autoRenewing).setOrderId(subscription.receiptId);
        if (!Algorithms.objectEquals(subscription.receiptId, orderId)) {
            throw new IllegalStateException(String.format("Order id '%s' != '%s' don't match", orderId, subscription.receiptId));
        }
        updateSubscriptionDb(orderId, sku, startTime, expireTime, currentTime, subscriptionPurchase);
    } else if (kind != null) {
        deleteSubscription(orderId, sku, currentTime, reason, kind);
    } else {
        System.err.println(String.format("ERROR updating sku '%s' orderId '%s': %s", sku, orderId, reason));
        int ind = 1;
        updCheckStat.setTimestamp(ind++, new Timestamp(currentTime));
        updCheckStat.setString(ind++, orderId);
        updCheckStat.setString(ind++, sku);
        updCheckStat.addBatch();
        checkChanges++;
        if (checkChanges > BATCH_SIZE) {
            updCheckStat.executeBatch();
            checkChanges = 0;
        }
    }
    return subscriptionPurchase;
}
Also used : SubscriptionPurchase(com.google.api.services.androidpublisher.model.SubscriptionPurchase) IOException(java.io.IOException) AmazonIOException(net.osmand.live.subscriptions.AmazonIAPHelper.AmazonIOException) AmazonIOException(net.osmand.live.subscriptions.AmazonIAPHelper.AmazonIOException) AmazonSubscription(net.osmand.live.subscriptions.AmazonIAPHelper.AmazonSubscription) Timestamp(java.sql.Timestamp)

Example 2 with AmazonSubscription

use of net.osmand.live.subscriptions.AmazonIAPHelper.AmazonSubscription in project OsmAnd-tools by osmandapp.

the class UserdataController method revalidateAmazonSubscription.

private SupporterDeviceSubscription revalidateAmazonSubscription(SupporterDeviceSubscription s) {
    if (amazonIAPHelper == null) {
        amazonIAPHelper = new AmazonIAPHelper();
    }
    try {
        if (s.orderId == null) {
            return s;
        }
        AmazonSubscription subscription = amazonIAPHelper.getAmazonSubscription(s.orderId, s.purchaseToken);
        if (subscription != null) {
            if (s.expiretime == null || s.expiretime.getTime() < subscription.renewalDate) {
                s.expiretime = new Date(subscription.renewalDate);
                // s.checktime = new Date(); // don't set checktime let jenkins do its job
                s.valid = System.currentTimeMillis() < subscription.renewalDate;
                subscriptionsRepo.save(s);
            }
        }
    } catch (IOException e) {
        LOG.error(String.format("Error retrieving amazon subscription %s - %s: %s", s.sku, s.orderId, e.getMessage()), e);
    }
    return s;
}
Also used : IOException(java.io.IOException) AmazonSubscription(net.osmand.live.subscriptions.AmazonIAPHelper.AmazonSubscription) AmazonIAPHelper(net.osmand.live.subscriptions.AmazonIAPHelper) Date(java.util.Date)

Aggregations

IOException (java.io.IOException)2 AmazonSubscription (net.osmand.live.subscriptions.AmazonIAPHelper.AmazonSubscription)2 SubscriptionPurchase (com.google.api.services.androidpublisher.model.SubscriptionPurchase)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 AmazonIAPHelper (net.osmand.live.subscriptions.AmazonIAPHelper)1 AmazonIOException (net.osmand.live.subscriptions.AmazonIAPHelper.AmazonIOException)1