Search in sources :

Example 1 with SupporterDeviceSubscriptionPrimaryKey

use of net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscriptionPrimaryKey in project OsmAnd-tools by osmandapp.

the class SubscriptionController method purchased.

// Android sends
// parameters.put("userid", userId);
// parameters.put("sku", info.getSku());
// parameters.put("orderId", info.getOrderId());
// parameters.put("purchaseToken", info.getPurchaseToken());
// parameters.put("email", email);
// parameters.put("token", token);
// parameters.put("version", Version.getFullVersion(ctx));
// parameters.put("lang", ctx.getLanguage() + "");
// parameters.put("nd", ctx.getAppInitializer().getFirstInstalledDays() + "");
// parameters.put("ns", ctx.getAppInitializer().getNumberOfStarts() + "");
// parameters.put("aid", ctx.getUserAndroidId());
// iOS sends
// [params setObject:@"ios" forKey:@"os"];
// [params setObject:userId forKey:@"userid"];
// [params setObject:token forKey:@"token"];
// [params setObject:sku forKey:@"sku"];
// [params setObject:transactionId forKey:@"purchaseToken"];
// [params setObject:receiptStr forKey:@"payload"];
// [params setObject:email forKey:@"email"];
@PostMapping(path = { "/purchased", "/purchased.php" })
public ResponseEntity<String> purchased(HttpServletRequest request) {
    SupporterDeviceSubscription subscr = new SupporterDeviceSubscription();
    // it was mixed in early ios versions, so orderid was passed as purchaseToken and payload as purchaseToken;
    // "ios".equals(request.getParameter("purchaseToken")) ||
    boolean ios = Algorithms.isEmpty(request.getParameter("orderId"));
    subscr.purchaseToken = ios ? request.getParameter("payload") : request.getParameter("purchaseToken");
    subscr.orderId = ios ? request.getParameter("purchaseToken") : request.getParameter("orderId");
    subscr.sku = request.getParameter("sku");
    subscr.timestamp = new Date();
    if (isEmpty(subscr.orderId)) {
        return error("Please validate the purchase (orderid is empty).");
    }
    if (isEmpty(subscr.purchaseToken)) {
        return error("Please validate the purchase (purchase token is empty).");
    }
    String userId = request.getParameter("userid");
    if (!isEmpty(userId)) {
        connectUserWithOrderId(userId, subscr.orderId, request);
    }
    Optional<SupporterDeviceSubscription> subscrOpt = subscriptionsRepository.findById(new SupporterDeviceSubscriptionPrimaryKey(subscr.sku, subscr.orderId));
    if (subscrOpt.isPresent() && !Algorithms.isEmpty(subscr.purchaseToken)) {
        SupporterDeviceSubscription dbSubscription = subscrOpt.get();
        if (!Algorithms.isEmpty(dbSubscription.purchaseToken) && !Algorithms.objectEquals(subscr.purchaseToken, dbSubscription.purchaseToken)) {
            if (dbSubscription.valid != null && dbSubscription.valid.booleanValue()) {
                dbSubscription.prevvalidpurchasetoken = dbSubscription.purchaseToken;
            }
            dbSubscription.valid = null;
            dbSubscription.kind = null;
            dbSubscription.purchaseToken = subscr.purchaseToken;
            subscriptionsRepository.save(dbSubscription);
        }
        return ResponseEntity.ok("{ \"res\" : \"OK\" }");
    }
    subscriptionsRepository.save(subscr);
    return ResponseEntity.ok("{ \"res\" : \"OK\" }");
}
Also used : SupporterDeviceSubscription(net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscription) SupporterDeviceSubscriptionPrimaryKey(net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscriptionPrimaryKey) Date(java.util.Date) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with SupporterDeviceSubscriptionPrimaryKey

use of net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscriptionPrimaryKey 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

SupporterDeviceSubscription (net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscription)2 SupporterDeviceSubscriptionPrimaryKey (net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscriptionPrimaryKey)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 InAppReceipt (net.osmand.live.subscriptions.ReceiptValidationHelper.InAppReceipt)1