google支付服务端订单验证PHP代码

之前有转发一则关于google支付服务端验证的文章,今天把之前研究出得服务端订单支付验证代码(PHP版本)贴出来大家分享

在进行服务端交易验证之前,需要到google api consle后台https://console.developers.google.com开通google play developer api并获取请求api证书priket.p12文件;

交易状态API官方文档:https://developers.google.com/android-publisher/api-ref/purchases/products/get

下载google的php版本的官方sdk程序包,然后代码如下:


每次付费成功后,客户端调用计费回调方法可以获取交易的状态:其中会有purchase token和productId(就是在google应用app后台配置的支付商品自定义标示)

请求订单交易状态代码如下:

<?php
require_once 'Google/Client.php';
require_once 'Google/Service/AndroidPublisher.php';
//include 'Google/Auth/AssertionCredentials.php';

try{
$client = new Google_Client();
$key_file_location = 'prikey.p12';
$key = file_get_contents($key_file_location);
$service_account_name = '[email protected]ount.com';
$cred = new Google_Auth_AssertionCredentials(
  // Replace this with the email address from the client.
  $service_account_name,
  // Replace this with the scopes you are requesting.
  array('https://www.googleapis.com/auth/androidpublisher'),
  $key
);

$client->setAssertionCredentials($cred);


$service = new Google_Service_AndroidPublisher( $client );
$packageName='com.xxxx.xxxx';
$productId='9419';
$token = 'omoaokhfkmiipfffmemjaclh.AO-J1OyFcurjPmcY4J5MFMYvW5z6jq-X9SrpZCo93etscPe0SzDItMnAY50RLWLwOEYl71HQEBXG8fREgJp-EVZTOwkG8uHuKf3UPlx0u8rwK7Qw4bRE9SI';
$optps=array();
$resp = $service->purchases_products->get( $packageName, $productId, $token, $optps );
$resp = (array)$resp;
var_dump($resp);
}catch(Exception $e){
	echo $e->getCode(),'|',$e->getMessage();
}

?>


如果正常的话,api会相应关于该次交易的状态,相应的字段说明见官方文档:https://developers.google.com/android-publisher/api-ref/purchases/products#resource

consumptionState:商品消费状态,0表还未消费,1表消费了(因为google的同一个商品未消费的话是不允许重复购买的)

developerPayload:这个比较重要,是我们开发者自定义透传的,一般为自身系统订单号之类的交易唯一标示,可以用来比对

purchaseState:购买状态,1代表购买了,0代表取消


备注:貌似这个api调用google平台有20w/日的次数限制

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。