Fix sendToken

This commit is contained in:
max-klaus 2019-12-14 18:06:47 +03:00
parent c061a16dbc
commit 6dc629fbc3
2 changed files with 106 additions and 56 deletions

View file

@ -3,8 +3,9 @@ package net.osmand;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import net.osmand.osm.io.Base64;
import net.osmand.osm.io.NetworkUtils; import net.osmand.osm.io.NetworkUtils;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -18,7 +19,6 @@ import java.io.BufferedOutputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -30,6 +30,7 @@ import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@ -43,29 +44,53 @@ public class AndroidNetworkUtils {
void onResult(String result); void onResult(String result);
} }
public static class RequestResponse {
private Request request;
private String response;
RequestResponse(@NonNull Request request, @Nullable String response) {
this.request = request;
this.response = response;
}
public Request getRequest() {
return request;
}
public String getResponse() {
return response;
}
}
public interface OnRequestsResultListener {
void onResult(@NonNull List<RequestResponse> results);
}
public static void sendRequestsAsync(final OsmandApplication ctx, public static void sendRequestsAsync(final OsmandApplication ctx,
final List<Request> requests, final List<Request> requests,
final OnRequestResultListener listener) { final OnRequestsResultListener listener) {
new AsyncTask<Void, Void, String>() { new AsyncTask<Void, Void, List<RequestResponse>>() {
@Override @Override
protected String doInBackground(Void... params) { protected List<RequestResponse> doInBackground(Void... params) {
List<RequestResponse> responses = new ArrayList<>();
for (Request request : requests) { for (Request request : requests) {
try { try {
return sendRequest(ctx, request.getUrl(), request.getParameters(), String response = sendRequest(ctx, request.getUrl(), request.getParameters(),
request.getUserOperation(), request.isToastAllowed(), request.isPost()); request.getUserOperation(), request.isToastAllowed(), request.isPost());
responses.add(new RequestResponse(request, response));
} catch (Exception e) { } catch (Exception e) {
// ignore responses.add(new RequestResponse(request, null));
} }
} }
return null; return responses;
} }
@Override @Override
protected void onPostExecute(String response) { protected void onPostExecute(@NonNull List<RequestResponse> results) {
if (listener != null) { if (listener != null) {
listener.onResult(response); listener.onResult(results);
} }
} }

View file

@ -17,6 +17,8 @@ import com.android.billingclient.api.SkuDetailsResponseListener;
import net.osmand.AndroidNetworkUtils; import net.osmand.AndroidNetworkUtils;
import net.osmand.AndroidNetworkUtils.OnRequestResultListener; import net.osmand.AndroidNetworkUtils.OnRequestResultListener;
import net.osmand.AndroidNetworkUtils.OnRequestsResultListener;
import net.osmand.AndroidNetworkUtils.RequestResponse;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
@ -950,26 +952,54 @@ public class InAppPurchaseHelper {
Map<String, String> parameters = new HashMap<>(); Map<String, String> parameters = new HashMap<>();
parameters.put("userid", userId); parameters.put("userid", userId);
parameters.put("sku", purchase.getSku()); parameters.put("sku", purchase.getSku());
parameters.put("orderId", purchase.getOrderId());
parameters.put("purchaseToken", purchase.getPurchaseToken()); parameters.put("purchaseToken", purchase.getPurchaseToken());
parameters.put("email", email); parameters.put("email", email);
parameters.put("token", token); parameters.put("token", token);
addUserInfo(parameters); addUserInfo(parameters);
requests.add(new AndroidNetworkUtils.Request(url, parameters, userOperation, true, true)); requests.add(new AndroidNetworkUtils.Request(url, parameters, userOperation, true, true));
} }
AndroidNetworkUtils.sendRequestsAsync(ctx, requests, new OnRequestResultListener() { AndroidNetworkUtils.sendRequestsAsync(ctx, requests, new OnRequestsResultListener() {
@Override @Override
public void onResult(String result) { public void onResult(@NonNull List<RequestResponse> results) {
for (RequestResponse rr : results) {
String sku = rr.getRequest().getParameters().get("sku");
Purchase purchase = getPurchase(sku);
if (purchase != null) {
updateSentTokens(purchase);
String result = rr.getResponse();
if (result != null) { if (result != null) {
try { try {
JSONObject obj = new JSONObject(result); JSONObject obj = new JSONObject(result);
if (!obj.has("error")) { if (!obj.has("error")) {
processPurchasedJson(obj);
} else {
complain("SendToken Error: "
+ obj.getString("error")
+ " (userId=" + userId + " token=" + token + " response=" + result + " google=" + purchase.toString() + ")");
}
} catch (JSONException e) {
logError("SendToken", e);
complain("SendToken Error: "
+ (e.getMessage() != null ? e.getMessage() : "JSONException")
+ " (userId=" + userId + " token=" + token + " response=" + result + " google=" + purchase.toString() + ")");
}
}
}
}
if (listener != null) {
listener.onResult("OK");
}
}
private void updateSentTokens(@NonNull Purchase purchase) {
String tokensSentStr = ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.get(); String tokensSentStr = ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.get();
Set<String> tokensSent = new HashSet<>(Arrays.asList(tokensSentStr.split(";"))); Set<String> tokensSent = new HashSet<>(Arrays.asList(tokensSentStr.split(";")));
for (Purchase purchase : purchases) {
tokensSent.add(purchase.getSku()); tokensSent.add(purchase.getSku());
}
ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.set(TextUtils.join(";", tokensSent)); ctx.getSettings().BILLING_PURCHASE_TOKENS_SENT.set(TextUtils.join(";", tokensSent));
}
private void processPurchasedJson(JSONObject obj) throws JSONException {
if (obj.has("visibleName") && !Algorithms.isEmpty(obj.getString("visibleName"))) { if (obj.has("visibleName") && !Algorithms.isEmpty(obj.getString("visibleName"))) {
ctx.getSettings().BILLING_USER_NAME.set(obj.getString("visibleName")); ctx.getSettings().BILLING_USER_NAME.set(obj.getString("visibleName"));
ctx.getSettings().BILLING_HIDE_USER_NAME.set(false); ctx.getSettings().BILLING_HIDE_USER_NAME.set(false);
@ -996,21 +1026,16 @@ public class InAppPurchaseHelper {
if (obj.has("email")) { if (obj.has("email")) {
ctx.getSettings().BILLING_USER_EMAIL.set(obj.getString("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); @Nullable
complain("SendToken Error: " private Purchase getPurchase(String sku) {
+ (e.getMessage() != null ? e.getMessage() : "JSONException") for (Purchase purchase : purchases) {
+ " (userId=" + userId + " token=" + token + " response=" + result + ")"); if (purchase.getSku().equals(sku)) {
return purchase;
} }
} }
if (listener != null) { return null;
listener.onResult("OK");
}
} }
}); });
} catch (Exception e) { } catch (Exception e) {