PHP谷歌支付[APP]
2021/9/6 14:07:14
本文主要是介绍PHP谷歌支付[APP],对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
获取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; } }
这篇关于PHP谷歌支付[APP]的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-19php8的协程和hyperf的协程有什么区别?-icode9专业技术文章分享
- 2024-12-19php8 的fiber是什么?-icode9专业技术文章分享
- 2024-12-05怎么在php8,1 里面开启 debug?-icode9专业技术文章分享
- 2024-12-05怎么在php8,1 里面开启 debug?-icode9专业技术文章分享
- 2024-11-29使用PHP 将ETH账户的资产汇集到一个账户
- 2024-11-23怎么实现安卓+php 热更新方案?-icode9专业技术文章分享
- 2024-11-22PHP 中怎么实现判断多个值是否为空、null 或者为 false?-icode9专业技术文章分享
- 2024-11-11开源 PHP 商城项目 CRMEB 二次开发和部署教程
- 2024-11-09怎么使用php在kaufland平台刊登商品?-icode9专业技术文章分享
- 2024-11-05PHP的抽象类和接口是什么,有什么区别-icode9专业技术文章分享