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