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