1. 程式人生 > >google支付服務端訂單驗證PHP程式碼

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]
count.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/日的次數限制