Server to Server Check - Client
如果您希望以中文查看此页面,请单击此处。
Native Android IAB
The entry point for this process is after you get a valid purchase notification through the function onPurchasesUpdated
override fun onPurchasesUpdated(responseCode: Int, purchases: List<Purchase>) {
val purchase = purchases[0]
val sku: String = purchase.sku
val token: String = purchase.token
Log.d(TAG, "purchase sku:$sku, token:$token")
}
@Override public void onPurchasesUpdated(int responseCode,
List<Purchase> purchases) {
Purchase purchase = 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:
packageName
productId
purchaseToken
Sample code:
var conn: HttpURLConnection? = null
try {
val url = URL("$baseHost/purchase/$applicationPackageName/check")
conn = url.openConnection() as HttpURLConnection
conn.requestMethod = "POST"
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
conn.setRequestProperty("Accept", "application/json")
conn.doOutput = true
conn.doInput = true
var jsonParam = JSONObject()
jsonParam.put("token", token)
jsonParam.put("product", sku)
Log.i("JSON", jsonParam.toString())
val os = DataOutputStream(conn.outputStream)
os.writeBytes(jsonParam.toString())
os.flush()
os.close()
val responseCode = conn.responseCode
if (responseCode == 200) {
val purchaseVerificationResponse = gson.fromJson(InputStreamReader(
conn.inputStream,
PurchaseVerificationResponse.class
))
Log.i(TAG, purchaseVerificationResponse.toString())
listener.onPurchaseValidationResult(
sku, token, purchaseVerificationResponse.status
== PurchaseVerificationResponse.Status.SUCCESS
)
}
else {
listener.onPurchaseValidationError(sku, token, Exception(
"Response code: $responseCode\nMessage: ${conn.responseMessage}"))
}
} catch (e: Exception) {
listener.onPurchaseValidationError(sku, token, e);
e.printStackTrace();
} finally {
conn?.disconnect()
}
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 Diceroll SDK for a fully working sample.
Updated 9 days ago