服务器到服务器检查 - 客户端

原生安卓(Android) IAB

此流程的入口点是通过函数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);
}

SKU和令牌可以直接从Purchase对象获得。 有了它们,你就可以用所需的信息向服务器发出请求:

  • packageName
  • productId
  • purchaseToken

示例代码:

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

📘

您可以查看https://github.com/Catappult/appcoins-iab-sample获得完整的流程示例。