Server to Server Check - Client
Native Android IAB
The entry point for this process is after you get a valid purchase notification through
onPurchasesUpdated
@Override public void onPurchasesUpdated(int responseCode,
List<Purchase> purchases) {
Purchase purhcase = purchases.get(0);
String sku = purchase.getSku();
String token = purchase.getToken();
Log.d(TAG, "purchase sku:" + sku + ", token:" + token);
}
The sku and the token can be directly obtained from the Purchase object. Having them, you can fire a request to your server with the required information:
- Package_name
- Sku
- Purchase_token
Sample code:
HttpURLConnection conn = null;
try {
URL url = new URL(baseHost + "/purchase/" + applicationPackageName + "/check");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
JSONObject jsonParam = new JSONObject();
jsonParam.put("token", token);
jsonParam.put("product", sku);
Log.i("JSON", jsonParam.toString());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.writeBytes(jsonParam.toString());
os.flush();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
PurchaseVerificationResponse purchaseVerificationResponse =
gson.fromJson(new InputStreamReader(conn.getInputStream()),
PurchaseVerificationResponse.class);
Log.i(TAG, purchaseVerificationResponse.toString());
listener.onPurchaseValidationResult(sku, token, purchaseVerificationResponse.getStatus()
== PurchaseVerificationResponse.Status.SUCCESS);
} else {
listener.onPurchaseValidationError(sku, token, new Exception(
"Response code: " + responseCode + "\n" + "Message: " + conn.getResponseMessage()));
}
} catch (Exception e) {
listener.onPurchaseValidationError(sku, token, e);
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
You can check https://github.com/Catappult/appcoins-iab-sample for a fully working sample.
Using Unity IAB
The entry point for this process is after you get a valid purchase notification through OnPurchaseSuccessful
public void OnPurchaseSuccessful(Product p) {
Debug.Log("purchase successful with receipt: " + p.receipt);
}
receipt is a json string that you can parse and get the purchase token from.
Having the token you can fire a request to your server with the required information:
- Package_name
- Sku
- Purchase_token
Sample code:
Debug.Log("payload token: " + purchaseToken + "\n payload store specific id: " + sku);
if (purchaseToken != "" && sku != "")
{
WWWForm form = new WWWForm();
form.AddField("token", purchaseToken);
form.AddField("product", sku);
//Sample url, your server url should go here!
string url = "https://validators.aptoide.com/purchase/" + packageName + "/check";
Debug.Log("Going to post:\n" + form + "\n to " + url);
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
//deal with error
}
else
{
//Need to check response
string response = System.Text.Encoding.UTF8.GetString(www.downloadHandler.data);
//Parse response code from json response
m = Regex.Match(response, "status\": \"(\\w+)");
string status = "";
if (m.Groups.Count > 0)
{
status = m.Groups[1].Captures[0].Value;
}
if (status == "SUCCESS") {
Debug.Log("Purchase verified!");
GameManager.Instance.OnFullTrajectoryPurchased();
_purchaseBtn.SetActive(false);
} else
{
Debug.LogError("Failed to validate purchase");
}
}
}
}
You can check https://github.com/Aptoide/appcoins-mwc-game/ for a fully working sample.
Updated over 1 year ago