获取GOOGLE永久token
第一步:获取code
public function googleToken()
{
if (Input::has('code') && ($code = Input::get('code', 'str', ''))) {
var_dump($_REQUEST);
die;
} else {
$this->redirect("https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=您的域名接口/googleToken&client_id=". Config::get('google.clientid'));
}
}
第二步:获取永久token[字段名:refresh_token]
请求地址: https://accounts.google.com/o/oauth2/token
请求方式:POST
结果:
{
"access_token": "ya29.a0ARrdaM_KAdN0Eojm8LMIr2WyVEorQv3iOdHbgtSqPhEFN8m9JiEcuwf4UFIKKAKtT-ocRsY8Q9uGBe8t2G6ys9zt0x9KchV2VFmOY_zxJQ6g7xhhh3alBoZvW-JYV3acI6T_O5Xd-JZXWmk2xMocj1jXxymU",
"expires_in": 3599,
"refresh_token": "1//0Y_mLA", //這個參數只會返回一次,記得存起來,GOOGLE後臺可以設置有效期的
"scope": "https://www.googleapis.com/auth/androidpublisher",
"token_type": "Bearer"
}
PHP APP谷歌支付回调
/**
* 驗證google內購訂單
*
* @param $parsedJson 來自客戶端,一個訂單數據的JSON字符串
* @return array
*/
public function google()
{
$post = $_REQUEST;
$parsedJson = $post['data'];
if(empty($parsedJson)) $this->outPutJson(500,'數據域空');
$parsedJson = json_decode($parsedJson, true);
if(empty($parsedJson)) $this->outPutJson(500,'數據域空');
$transactionId = $parsedJson['orderId'];
$oid = $parsedJson['obfuscatedAccountId'];
if(empty($transactionId)) $this->outPutJson(500,'google訂單號空');
if(empty($oid)) $this->outPutJson(500,'訂單號空');
if (!empty($transactionId)) {
Order::I()->updateTransId($oid, $transactionId);
}
$pid = $parsedJson['productId'];
$orderToken = $parsedJson['purchaseToken'];
$packageName = $parsedJson['packageName'];
$accessToken = $this->getAccessToken();
if ($accessToken) {
$url = 'https://www.googleapis.com/androidpublisher/v3/applications/'.$packageName.'/purchases/products/'.$pid.'/tokens/'.$orderToken.'?access_token='.$accessToken;
$result = Http::get($url);
$contents = json_decode($result,true);
if (!empty($contents)) {
if (isset($contents['error'])) {
$this->outPutJson(500,'請求的身份驗證憑據無效');
}
if($contents['consumptionState'] === 0 && $contents['purchaseState'] === 0){
//驗證成功 購買成功並且沒有消耗 google支付中客戶端如果沒有進行消耗是不能再次購買該商品
$res = Order::I()->finishPayIos($oid,$transactionId);
if($res === true){
$this->outPutJson(200,'正式購買成功');
}else {
$this->outPutJson(500,$res);
}
}else{
//訂單驗證失敗
$this->outPutJson(500,'訂單狀態有誤');
}
}
} else {
$this->outPutJson(500,'TOKEN丟失');
}
}
/**
* google token
*
* @return array
*/
private function getAccessToken()
{
$cacheKey = "google.kanshu.access_token";
$accessToken = DI::Cache()->get($cacheKey);
if ($accessToken) {
return $accessToken;
}
$url = 'https://accounts.google.com/o/oauth2/token';
$data['refresh_token'] = Config::get('google.refresh_token');
$data['client_id'] = Config::get('google.clientid');
$data['client_secret'] = Config::get('google.key');
$data['grant_type'] = 'refresh_token';
$response = Http::post($url,$data);
$result = json_decode($response, true);
if ($result) {
if (isset($result['access_token'])) {
DI::Cache()->set($cacheKey, $result['access_token'], $result['expires_in']);
return $result['access_token'];
} else {
return false;
}
} else {
return false;
}
}