Search in sources :

Example 1 with OnSendRequestsListener

use of net.osmand.plus.utils.AndroidNetworkUtils.OnSendRequestsListener in project Osmand by osmandapp.

the class InAppPurchaseHelper method sendTokens.

protected void sendTokens(@NonNull final List<PurchaseInfo> purchaseInfoList, final OnRequestResultListener listener) {
    final String userId = ctx.getSettings().BILLING_USER_ID.get();
    final String token = ctx.getSettings().BILLING_USER_TOKEN.get();
    final String email = ctx.getSettings().BILLING_USER_EMAIL.get();
    try {
        String url = "https://osmand.net/subscription/purchased";
        String userOperation = "Sending purchase info...";
        final List<Request> requests = new ArrayList<>();
        for (PurchaseInfo info : purchaseInfoList) {
            Map<String, String> parameters = new HashMap<>();
            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);
            addUserInfo(parameters);
            requests.add(new Request(url, parameters, userOperation, true, true));
        }
        AndroidNetworkUtils.sendRequestsAsync(ctx, requests, new OnSendRequestsListener() {

            @Override
            public void onRequestSending(@NonNull Request request) {
            }

            @Override
            public void onRequestSent(@NonNull RequestResponse response) {
            }

            @Override
            public void onRequestsSent(@NonNull List<RequestResponse> results) {
                for (RequestResponse rr : results) {
                    String sku = rr.getRequest().getParameters().get("sku");
                    PurchaseInfo info = getPurchaseInfo(sku);
                    if (info != null) {
                        updateSentTokens(info);
                        String result = rr.getResponse();
                        if (result != null) {
                            try {
                                JSONObject obj = new JSONObject(result);
                                if (obj.has("error")) {
                                    complain("SendToken Error: " + obj.getString("error") + " (response=" + result + " google=" + info.toString() + ")");
                                }
                            } catch (JSONException e) {
                                logError("SendToken", e);
                                complain("SendToken Error: " + (e.getMessage() != null ? e.getMessage() : "JSONException") + " (response=" + result + " google=" + info.toString() + ")");
                            }
                        }
                    }
                }
                if (listener != null) {
                    listener.onResult("OK", null, null);
                }
            }

            private void updateSentTokens(@NonNull PurchaseInfo info) {
                String tokensSentStr = ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.get();
                Set<String> tokensSent = new HashSet<>(Arrays.asList(tokensSentStr.split(";")));
                tokensSent.add(info.getSku());
                ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.set(TextUtils.join(";", tokensSent));
            }

            @Nullable
            private PurchaseInfo getPurchaseInfo(String sku) {
                for (PurchaseInfo info : purchaseInfoList) {
                    if (info.getSku().equals(sku)) {
                        return info;
                    }
                }
                return null;
            }
        });
    } catch (Exception e) {
        logError("SendToken Error", e);
        if (listener != null) {
            listener.onResult("Error", null, null);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) OnSendRequestsListener(net.osmand.plus.utils.AndroidNetworkUtils.OnSendRequestsListener) Request(net.osmand.plus.utils.AndroidNetworkUtils.Request) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) RequestResponse(net.osmand.plus.utils.AndroidNetworkUtils.RequestResponse) JSONException(org.json.JSONException) ParseException(java.text.ParseException) JSONObject(org.json.JSONObject) PurchaseInfo(net.osmand.plus.inapp.InAppPurchases.PurchaseInfo) Nullable(androidx.annotation.Nullable)

Aggregations

Nullable (androidx.annotation.Nullable)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 PurchaseInfo (net.osmand.plus.inapp.InAppPurchases.PurchaseInfo)1 OnSendRequestsListener (net.osmand.plus.utils.AndroidNetworkUtils.OnSendRequestsListener)1 Request (net.osmand.plus.utils.AndroidNetworkUtils.Request)1 RequestResponse (net.osmand.plus.utils.AndroidNetworkUtils.RequestResponse)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1