Search in sources :

Example 1 with InAppReceipt

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;
}
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)

Example 2 with InAppReceipt

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;
}
Also used : InAppReceipt(net.osmand.live.subscriptions.ReceiptValidationHelper.InAppReceipt)

Example 3 with InAppReceipt

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.");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) SupporterDeviceSubscriptionPrimaryKey(net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscriptionPrimaryKey) SupporterDeviceSubscription(net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscription) JsonObject(com.google.gson.JsonObject) InAppReceipt(net.osmand.live.subscriptions.ReceiptValidationHelper.InAppReceipt) Map(java.util.Map) HashMap(java.util.HashMap) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

InAppReceipt (net.osmand.live.subscriptions.ReceiptValidationHelper.InAppReceipt)3 IntroductoryPriceInfo (com.google.api.services.androidpublisher.model.IntroductoryPriceInfo)1 SubscriptionPurchase (com.google.api.services.androidpublisher.model.SubscriptionPurchase)1 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SupporterDeviceSubscription (net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscription)1 SupporterDeviceSubscriptionPrimaryKey (net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscriptionPrimaryKey)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1