接入背景:
海外产品上线google,一些不良用户在进行google内购完成发货后,前往google进行退款,从而达到刷单的目的。我们需要通过一些google api接口来找到这些已退款的用户,进行标记,在必要的时候可将这些用户拉入黑名单。
google几个后台链接
Google APIs : https://console.developers.google.com
Google Play Console : https://play.google.com/apps/publish
IAM和管理 : https://console.developers.google.com/iam-admin/serviceaccounts
1、
Google Play Console -> 设置 -> 开发者账号 -> API权限 -> 创建服务账号。
赋予权限 所有者
或 Service Account Token Creator
, 勾选 启用 G Suite 全网域委派功能
,生成 json密钥。
创建完毕后必须在 服务账号 的列表里有对应的信息,否则将无法使用。
2、
Google Play Console -> 设置 -> 开发者账号 -> 用户和权限 -> 邀请新用户。
将刚才添加的服务账号邀请进来,授予指定项目 和 管理订单、查看财务数据 的权限。
3、
编写代码测试。
引入composer google api库 : https://github.com/googleapis/google-api-php-client
参考google退款订单接口文档 :
https://developers.google.com/android-publisher/voided-purchases
https://developers.google.com/android-publisher/api-ref/purchases/voidedpurchases/list
require_once dirname(__FILE__) . '/vendor/autoload.php';
// 生成json密钥的文件
$account_json = 'xxx.json';
putenv("GOOGLE_APPLICATION_CREDENTIALS={$account_json}");
$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(\Google_Service_AndroidPublisher::ANDROIDPUBLISHER);
// 设置创建服务账号时的邮箱地址
$client->setSubject('xxx@xxx.com');
// 创建服务
$publisher = new \Google_Service_AndroidPublisher($client);
// 包名
$package_name = '';
// 参数
$opt_params = [];
// 请求结果
$ret = $publisher->purchases_voidedpurchases->listPurchasesVoidedpurchases($package_name, $opt_params);
请求回来的每笔退款订单都会有一个唯一标识 purchaseToken
,此值就是在安卓前端支付成功后google返回订单详情数据 purchase_data 里的 purchaseToken 值。所以我们可以通过此值来对比找到我们对应的订单号。
如果执行出现以下失败,请留意你所关联的项目和你创建的服务账号是否对应,必须在你关联的项目中创建服务账号才行。
{
"error": {
"code": 403,
"message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console.",
"errors": [
{
"domain": "androidpublisher",
"message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console.",
"reason": "projectNotLinked"
}
]
}
}