use of net.osmand.live.subscriptions.AmazonIAPHelper.AmazonIOException 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;
}
Aggregations