如何在 php 中验证 google play 应用内购买?

2022-01-20 00:00:00 php android google-play

我希望使用 php 页面通过 google play 验证应用内购买的收据.我该怎么做?

I wish to verify a receipt of in-app purchase in an app through google play using a php page. How should i do it?

推荐答案

我不是 PHP 专家,所以我不会发布任何代码,但整个过程非常直截了当移植到 PHP 应该很容易.您需要三件事来验证购买:

I'm no PHP expert so I'm not going to post any code, but the overall process is very straight forward and it should be dead easy to port to PHP. You need three things to verify a purchase:

  1. 您应用的公钥(来自 Google Play Developer 中的服务和 API控制台)
  2. 购买的原始 JSON
  3. 购买签名

如果您在 Android 上使用 IabHelper 类 你会得到一个 购买 对象在您成功购买或查询库存时.Purchase 对象包含您需要的两个方法:Purchase.getOriginalJson() 和 Purchase.getSignature().

If you implement in-app billing on Android using the IabHelper classes you'll get a Purchase object when you make a successful purchase or when you query the inventory. The Purchase object contains two methods that you need: Purchase.getOriginalJson() and Purchase.getSignature().

将您应用的公钥安全地存储在您的服务器上,并将签名和原始 JSON(在发送之前对其进行 base64 编码)发布到您的服务器.从 $_POST 中检索签名和 json 并参考 Google 的 如何验证购买的Java实现.好像可以使用openssl_verify函数内置的PHP.

Securely store your app's public key on your server and POST the signature and the original JSON (base64 encode it before you send it) to you server. Retrieve the signature and json from $_POST and refer to Google's Java implementation of how to verify a purchase. It seems as though you can use PHPs built in openssl_verify function.

相关文章