Search in sources :

Example 1 with Supporter

use of net.osmand.server.api.repo.SupportersRepository.Supporter in project OsmAnd-tools by osmandapp.

the class SubscriptionController method register.

@PostMapping(path = { "/register", "/register.php" }, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> register(HttpServletRequest request) {
    String email = request.getParameter("email");
    String visibleName = request.getParameter("visibleName");
    String preferredCountry = request.getParameter("preferredCountry");
    // no email validation cause client doesn't provide it 99%
    // avoid all nulls, empty, none
    boolean emailValid = email != null && email.contains("@");
    if (emailValid) {
        Optional<Supporter> optionalSupporter = supportersRepository.findByUserEmail(email);
        if (optionalSupporter.isPresent()) {
            Supporter supporter = optionalSupporter.get();
            return ResponseEntity.ok(userInfoAsJson(supporter));
        }
    }
    ThreadLocalRandom tlr = ThreadLocalRandom.current();
    int token = tlr.nextInt(100000, 1000000);
    Supporter supporter = new Supporter();
    supporter.userId = null;
    supporter.os = request.getParameter("os");
    supporter.token = String.valueOf(token);
    supporter.visibleName = visibleName;
    supporter.userEmail = email;
    supporter.preferredRegion = preferredCountry;
    supporter = supportersRepository.saveAndFlush(supporter);
    return ResponseEntity.ok(userInfoAsJson(supporter));
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Supporter(net.osmand.server.api.repo.SupportersRepository.Supporter) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with Supporter

use of net.osmand.server.api.repo.SupportersRepository.Supporter in project OsmAnd-tools by osmandapp.

the class SubscriptionController method update.

@PostMapping(path = { "/update", "/update.php" }, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> update(HttpServletRequest request) {
    String userId = request.getParameter("userid");
    if (isEmpty(userId)) {
        return error("Please validate user id.");
    }
    String token = request.getParameter("token");
    if (isEmpty(token)) {
        return error("Token is not present: fix will be in OsmAnd 3.2");
    }
    Optional<Supporter> sup = supportersRepository.findById(Long.parseLong(userId));
    if (!sup.isPresent()) {
        return error("Couldn't find your user id: " + userId);
    }
    Supporter supporter = sup.get();
    if (!token.equals(supporter.token)) {
        return error("Couldn't validate the token: " + token);
    }
    supporter.visibleName = request.getParameter("visibleName");
    supporter.userEmail = request.getParameter("email");
    supporter.preferredRegion = request.getParameter("preferredCountry");
    supporter = supportersRepository.save(supporter);
    return ResponseEntity.ok(userInfoAsJson(supporter));
}
Also used : Supporter(net.osmand.server.api.repo.SupportersRepository.Supporter) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with Supporter

use of net.osmand.server.api.repo.SupportersRepository.Supporter in project OsmAnd-tools by osmandapp.

the class SubscriptionController method connectUserWithOrderId.

private boolean connectUserWithOrderId(String userId, String orderId, HttpServletRequest request) {
    String token = request.getParameter("token");
    if (isEmpty(token)) {
        LOG.warn("USER: Token was not provided: " + toString(request.getParameterMap()));
        return false;
    }
    Optional<Supporter> sup = supportersRepository.findById(Long.parseLong(userId));
    if (!sup.isPresent()) {
        LOG.warn("USER: Couldn't find your user id: " + toString(request.getParameterMap()));
        return false;
    }
    Supporter supporter = sup.get();
    if (token != null && !token.equals(supporter.token)) {
        LOG.warn("USER: Token failed validation: " + toString(request.getParameterMap()));
        return false;
    }
    if (!Algorithms.objectEquals(supporter.orderId, orderId)) {
        supporter.orderId = orderId;
        supportersRepository.saveAndFlush(supporter);
    }
    return true;
}
Also used : Supporter(net.osmand.server.api.repo.SupportersRepository.Supporter)

Example 4 with Supporter

use of net.osmand.server.api.repo.SupportersRepository.Supporter in project OsmAnd-tools by osmandapp.

the class ApiController method getSubscriptions.

@GetMapping(path = { "/subscriptions/get" })
@ResponseBody
public String getSubscriptions(@RequestParam(required = false) String userId, @RequestParam(required = false) String userToken, @RequestParam(required = false) String orderId, @RequestHeader HttpHeaders headers, HttpServletRequest request) throws IOException, ParseException {
    MessageParams params = new MessageParams();
    params.hostAddress = request.getRemoteAddr();
    if (headers.getFirst("X-Forwarded-For") != null) {
        params.hostAddress = headers.getFirst("X-Forwarded-For");
    }
    if (!Algorithms.isEmpty(userId) && !Algorithms.isEmpty(userToken) && Algorithms.isEmpty(orderId)) {
        Optional<Supporter> sup = supportersRepository.findByUserId(Long.parseLong(userId));
        if (sup.isPresent()) {
            Supporter s = sup.get();
            if (userToken.equals(s.token)) {
                orderId = sup.get().orderId;
            }
        }
    }
    if (!Algorithms.isEmpty(orderId)) {
        List<SupporterDeviceSubscription> subscriptions = subscriptionsRepository.findByOrderId(orderId);
        List<Object> res = new ArrayList<>();
        for (SupporterDeviceSubscription sub : subscriptions) {
            Map<String, String> subMap = new HashMap<>();
            subMap.put("sku", sub.sku);
            if (sub.valid != null) {
                subMap.put("valid", sub.valid.toString());
            }
            String state = "undefined";
            if (sub.starttime != null) {
                subMap.put("start_time", String.valueOf(sub.starttime.getTime()));
            }
            if (sub.expiretime != null) {
                long expireTimeMs = sub.expiretime.getTime();
                int paymentState = sub.paymentstate == null ? 1 : sub.paymentstate.intValue();
                boolean autoRenewing = sub.autorenewing == null ? false : sub.autorenewing.booleanValue();
                if (expireTimeMs > System.currentTimeMillis()) {
                    if (paymentState == 1 && autoRenewing) {
                        state = "active";
                    } else if (paymentState == 1 && !autoRenewing) {
                        state = "cancelled";
                    } else if (paymentState == 0 && autoRenewing) {
                        state = "in_grace_period";
                    }
                } else {
                    if (paymentState == 0 && autoRenewing) {
                        state = "on_hold";
                    } else if (paymentState == 1 && autoRenewing) {
                        state = "paused";
                    } else if (paymentState == 1 && !autoRenewing) {
                        state = "expired";
                    }
                }
                subMap.put("expire_time", String.valueOf(expireTimeMs));
            }
            subMap.put("state", state);
            res.add(subMap);
        }
        return jsonMapper.writeValueAsString(res);
    }
    return "{}";
}
Also used : HashMap(java.util.HashMap) MessageParams(net.osmand.server.api.services.MotdService.MessageParams) ArrayList(java.util.ArrayList) SupporterDeviceSubscription(net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscription) Supporter(net.osmand.server.api.repo.SupportersRepository.Supporter) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Supporter (net.osmand.server.api.repo.SupportersRepository.Supporter)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 SupporterDeviceSubscription (net.osmand.server.api.repo.DeviceSubscriptionsRepository.SupporterDeviceSubscription)1 MessageParams (net.osmand.server.api.services.MotdService.MessageParams)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1