Search in sources :

Example 1 with OnRequestResultListener

use of net.osmand.AndroidNetworkUtils.OnRequestResultListener in project Osmand by osmandapp.

the class InAppHelper method sendToken.

private void sendToken(String purchaseToken, 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 {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("userid", userId);
        parameters.put("sku", SKU_LIVE_UPDATES);
        parameters.put("purchaseToken", purchaseToken);
        parameters.put("email", email);
        parameters.put("token", token);
        AndroidNetworkUtils.sendRequestAsync(ctx, "http://download.osmand.net/subscription/purchased.php", parameters, "Sending purchase info...", true, true, new OnRequestResultListener() {

            @Override
            public void onResult(String result) {
                if (result != null) {
                    try {
                        JSONObject obj = new JSONObject(result);
                        if (!obj.has("error")) {
                            ctx.getSettings().BILLING_PURCHASE_TOKEN_SENT.set(true);
                            if (obj.has("visibleName") && !Algorithms.isEmpty(obj.getString("visibleName"))) {
                                ctx.getSettings().BILLING_USER_NAME.set(obj.getString("visibleName"));
                                ctx.getSettings().BILLING_HIDE_USER_NAME.set(false);
                            } else {
                                ctx.getSettings().BILLING_HIDE_USER_NAME.set(true);
                            }
                            if (obj.has("preferredCountry")) {
                                String prefferedCountry = obj.getString("preferredCountry");
                                if (!ctx.getSettings().BILLING_USER_COUNTRY_DOWNLOAD_NAME.get().equals(prefferedCountry)) {
                                    ctx.getSettings().BILLING_USER_COUNTRY_DOWNLOAD_NAME.set(prefferedCountry);
                                    CountrySelectionFragment countrySelectionFragment = new CountrySelectionFragment();
                                    countrySelectionFragment.initCountries(ctx);
                                    CountryItem countryItem = null;
                                    if (Algorithms.isEmpty(prefferedCountry)) {
                                        countryItem = countrySelectionFragment.getCountryItems().get(0);
                                    } else if (!prefferedCountry.equals(OsmandSettings.BILLING_USER_DONATION_NONE_PARAMETER)) {
                                        countryItem = countrySelectionFragment.getCountryItem(prefferedCountry);
                                    }
                                    if (countryItem != null) {
                                        ctx.getSettings().BILLING_USER_COUNTRY.set(countryItem.getLocalName());
                                    }
                                }
                            }
                            if (obj.has("email")) {
                                ctx.getSettings().BILLING_USER_EMAIL.set(obj.getString("email"));
                            }
                        } else {
                            complain("SendToken Error: " + obj.getString("error") + " (userId=" + userId + " token=" + token + " response=" + result + ")");
                        }
                    } catch (JSONException e) {
                        logError("SendToken", e);
                        complain("SendToken Error: " + (e.getMessage() != null ? e.getMessage() : "JSONException") + " (userId=" + userId + " token=" + token + " response=" + result + ")");
                    }
                }
                if (listener != null) {
                    listener.onResult("OK");
                }
            }
        });
    } catch (Exception e) {
        logError("SendToken Error", e);
        if (listener != null) {
            listener.onResult("Error");
        }
    }
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) OnRequestResultListener(net.osmand.AndroidNetworkUtils.OnRequestResultListener) CountrySelectionFragment(net.osmand.plus.liveupdates.CountrySelectionFragment) JSONException(org.json.JSONException) CountryItem(net.osmand.plus.liveupdates.CountrySelectionFragment.CountryItem) JSONException(org.json.JSONException)

Aggregations

HashMap (java.util.HashMap)1 OnRequestResultListener (net.osmand.AndroidNetworkUtils.OnRequestResultListener)1 CountrySelectionFragment (net.osmand.plus.liveupdates.CountrySelectionFragment)1 CountryItem (net.osmand.plus.liveupdates.CountrySelectionFragment.CountryItem)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1