use of net.osmand.live.subscriptions.ReceiptValidationHelper.InAppReceipt 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;
}
use of net.osmand.live.subscriptions.ReceiptValidationHelper.InAppReceipt in project OsmAnd-tools by osmandapp.
the class SubscriptionController method isEligibleForIntroductoryPrice.
private boolean isEligibleForIntroductoryPrice(@NonNull Collection<InAppReceipt> inAppReceipts) {
for (InAppReceipt receipt : inAppReceipts) {
String isTrialPeriod = receipt.fields.get("is_trial_period");
String isInIntroOfferPeriod = receipt.fields.get("is_in_intro_offer_period");
if ("true".equals(isTrialPeriod) || "true".equals(isInIntroOfferPeriod)) {
return false;
}
}
return true;
}
use of net.osmand.live.subscriptions.ReceiptValidationHelper.InAppReceipt in project OsmAnd-tools by osmandapp.
the class SubscriptionController method validateReceiptIos.
@PostMapping(path = { "/ios-receipt-validate" })
public ResponseEntity<String> validateReceiptIos(HttpServletRequest request) throws Exception {
String receipt = request.getParameter("receipt");
JsonObject receiptObj = validationService.loadReceiptJsonObject(receipt, false);
if (receiptObj != null) {
Map<String, Object> result = new HashMap<>();
List<InAppReceipt> inAppReceipts = validationService.loadInAppReceipts(receiptObj);
if (inAppReceipts != null) {
if (inAppReceipts.size() == 0) {
result.put("eligible_for_introductory_price", "true");
result.put("eligible_for_subscription_offer", "false");
result.put("result", false);
result.put("status", NO_SUBSCRIPTIONS_FOUND_STATUS);
return ResponseEntity.ok(jsonMapper.writeValueAsString(result));
} else {
result.put("eligible_for_introductory_price", isEligibleForIntroductoryPrice(inAppReceipts) ? "true" : "false");
// update existing subscription purchaseToken
for (InAppReceipt r : inAppReceipts) {
if (r.isSubscription()) {
Optional<SupporterDeviceSubscription> subscription = subscriptionsRepository.findById(new SupporterDeviceSubscriptionPrimaryKey(r.getProductId(), r.getOrderId()));
if (subscription.isPresent()) {
SupporterDeviceSubscription s = subscription.get();
if (!Algorithms.objectEquals(s.purchaseToken, receipt)) {
s.purchaseToken = receipt;
subscriptionsRepository.saveAndFlush(s);
}
}
}
}
List<Map<String, String>> activeSubscriptions = new ArrayList<>();
Map<String, Object> validationResult = validationService.validateReceipt(receiptObj, activeSubscriptions);
result.putAll(validationResult);
result.put("eligible_for_subscription_offer", isEligibleForSubscriptionOffer(inAppReceipts, activeSubscriptions) ? "true" : "false");
return ResponseEntity.ok(jsonMapper.writeValueAsString(result));
}
}
}
return error("Cannot load receipt.");
}
Aggregations