Google支付 PHP端验证订单号的有效性

1.创建API应用
APIs Console https://code.google.com/apis/console
需要用谷歌开发者账号登陆,该账号要和发布谷歌应用申请谷歌应用的那个账号相同,才能把这个创建的project和游戏应用绑定上(绑定需要手动操作)
绑定项目
https://play.google.com/apps/publish/
Settings–> API access—> LINKED PROJECT 和 OAUTH CLIENTS
client_id 就是在这里创建出来的。
另外,你要在googleplay console—> 对应应用的project下—> APIs & auth—-> APIs ,把GooglePlay Android Developer API设置为ON。
如果是你是个人应用开发者的话,可以直接配置,否则一定要账户管理员给你个人gmail账号设置个权限,也就是只有帐户所有者才能配置API权限,请联系相应的帐户所有者更新API设置

这里写图片描述

2.获得refresh_token
2.1访问下面的地址
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=…&client_id=.
redirect_uri是上面步骤一中你填写的uri
client_id 是创建成功后,谷歌给你的
2.2完操作后,浏览器会自动跳转到你填写的uri页面,并跟上一个code,类似于这样的(4/eWdxD7b-YSQ5CNNb-c2iI83KQx19.wp6198ti5Zc7dJ3UXOl0T3aRLxQmbwI)
3.3获得refresh_token

//获取refresh_token  
$url = "https://accounts.google.com/o/oauth2/token";
$data = array(
    'grant_type'=>'authorization_code',
    'code'=>'4/eWdxD7b-YSQ5CNNb-c2iI83KQx19.wp6198ti5Zc7dJ3UXOl0T3aRLxQmbwI',
    'client_id'=>'',
    'client_secret'=>'',
    'redirect_uri'=>'http://www.tapenjoy.com/',
    );
 $contents = $this->curl($url,$data);
 echo $contents;

如果成功,会获得类似于这样的返回
{
“access_token” : “ya29.ZStBkRnGyZ2mUYOLgls7QVBxOg82XhBCFo8UIT5gM”,
“token_type” : “Bearer”,
“expires_in” : 3600,
“refresh_token” : “1/zaaHNytlC3SEBX7F2cfrHcqJEa3KoAHYeXES6nmho”
}
refresh_token后面不会出现,要保存好,要通过这个,获取access_token

//curl方法
private function curl($url,$data=null,$method = null){
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($method == 'post') {
        curl_setopt($ch, CURLOPT_POST,1);
    }
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);//使用了SOCKS5代理
    curl_setopt($ch,CURLOPT_PROXY,'192.168.100.2');//代理服务器
    curl_setopt($ch,CURLOPT_PROXYPORT,'1080');//代理端口
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书 https访问的时候
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书 https访问的时候
    if($data){
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//传递参数
    }
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
} 

3.订单验证

// 1.获取access_token
    $url = "https://accounts.google.com/o/oauth2/token";
    $data_tmp = array(
        'grant_type'=>'refresh_token',
        'refresh_token'=>'上面获取到的refresh_token',
        'client_id'=>'',
        'client_secret'=>'',
        );
    $contents = $this->curl($url,$data_tmp,'post');
    $contents = json_decode($contents,true);
    $access_token = $contents['access_token'];
// 2.通过获得access_token 就可以请求谷歌的 API接口,获得订单状态
    $url = "https://www.googleapis.com/androidpublisher/v2/applications/{$packageName}/purchases/products/{$productId}/tokens/{$purchaseToken}?access_token={$access_token}";
    $contents = $this->curl($url);
    $contents = json_decode($contents,true);
    if($contents['consumptionState'] == 0 && $contents['purchaseState'] == 0){
        //验证成功 没有消耗 购买成功
        //处理游戏逻辑
    }

参考文档
https://developers.google.com/android-publisher/authorization#generating_a_refresh_token
https://developers.google.com/android-publisher/api-ref/purchases/products/get
http://blog.csdn.net/lemonzone2010/article/details/44983659
http://blog.csdn.net/w13770269691/article/details/46428281
http://blog.csdn.net/hjun01/article/details/42032841

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
谷歌支付内购回调需要使用Google Play Developer API验证订单的合法性。以下是验证订单的步骤: 1. 获取谷歌开发者控制台的服务帐户JSON文件,该文件包含了您的API密钥。 2. 使用PHP编写代码,通过cURL发送POST请求,将订单ID作为参数发送至Google Play Developer API。 3. Google Play Developer API将返回订单的详细信息,包括订单状态、购买时间、购买者信息等。 4. 您可以通过验证订单状态、购买时间和购买者信息来确保订单的合法性。 以下是一个简单的PHP代码示例,用于验证Google Play支付内购回调: ```php <?php //获取POST请求中的订单ID $orderID = $_POST['orderId']; //设置API密钥 $apiKey = 'YOUR_API_KEY'; //设置请求URL $url = 'https://www.googleapis.com/androidpublisher/v3/applications/your_package_name/purchases/subscriptions/'. $orderID . '/tokens/token_string'; //发送POST请求 $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $apiKey)); $response = curl_exec($ch); curl_close($ch); //解析API返回的JSON数据 $json = json_decode($response); $purchaseState = $json->purchaseState; $purchaseTimeMillis = $json->purchaseTimeMillis; $purchaserEmail = $json->purchaserEmail; //验证订单状态和购买者信息 if ($purchaseState == 0 && $purchaserEmail == '[email protected]') { //订单合法 } else { //订单非法 } ?> ``` 上述代码中,`YOUR_API_KEY`需要替换为您的API密钥,`your_package_name`需要替换为您的应用程序包名称,`token_string`需要替换为您的订阅令牌。 请注意,您需要先在Google Play Console中启用Google Play Developer API,并生成API密钥。此外,您需要将API密钥存储在安全的位置,并确保只有授权的用户可以访问它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值