我的Android应用中具有应用内购买功能。用户购买了易耗品之后,我想在Google Play上使用Google API(PHP)验证该购买
只需将购买令牌和sku产品ID发送到Google服务器。运作良好。 有时验证会失败。。但是,当我在Google Play控制台上检查购买的内容时,就可以了。
以下脚本立即在购买后(4-5秒后)向Google发送验证请求。
为什么Google有时无法验证实际购买?那是延误问题吗?
require_once './vendor/autoload.php';
putenv("GOOGLE_APPLICATION_CREDENTIALS=/path-to-my-credential-file/my-secret-json-file.json");
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes('https://www.googleapis.com/auth/androidpublisher');
$service = new \Google_Service_AndroidPublisher($client);
$packageName = "com.hello.world";
$productId = $_GET["sku"];
$token = $_GET["token"];
try{
$purchase = $service->purchases_products->get($packageName, $productId, $token);
$returnArray = array("status" => "success");
}catch(Exception $e){
$returnArray = array("status" => "fail");
}
//sometimes when there is a successful purchase it falls to catch.
echo json_encode($returnArray);