Google Pay接入笔记

一、背景

最近公司的某个APP需要接入Google Pay,以此记录一下相关信息

二、客户端

Google Pay全部的支付操作都是由客户端完成,再将支付结果发送给服务端

三、服务端防刷

因为服务端只接收支付结果,所以需要进行校验,以防被刷;防刷的方式是通过Google API发送申请,获取该订单的相关信息,从而判断该订单是否重复

1、创建OAuth 2.0凭据

https://console.developers.google.com/apis/credentials

Google Pay接入笔记_第1张图片

添加的域名必须已被授权

Google Pay接入笔记_第2张图片

创建完成客户获得client_id,client_secrert,redirect_url

Google Pay接入笔记_第3张图片

2、生成Authorization Code

Google中心登陆状态,另访问https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri={REDIRECT_URIS}&client_id={CLIENT_ID}

同意授权之后会跳转到redirect_url,redirect_url之后的code参数即为Authorization Code

3、获取refresh_token

请求地址:https://accounts.google.com/o/oauth2/token

请求参数:code, client_id, client_secret, redirect_uri, grant_type;必须为POST请求

其中grant_type值为 authorization_code

Google Pay接入笔记_第4张图片

返回的结果为JSON结构,其中refresh_token需要永久保存

4、获取access_token

请求地址:https://accounts.google.com/o/oauth2/token

请求参数:client_id, client_secret, refresh_toke, grant_type;必须为POST请求

grant_type 值固定为 refresh_token

Google Pay接入笔记_第5张图片

返回的JSON,access_token有效期为3600
{

  "access_token": "ya29.ImCpB-UBTisiQeeipoEAT8-ufAHtKeRCC9Wlft297PnOGGPh8qRCIfg6QjMPLVq7tYkMByxn--QOKbVimiJTuL1_syMZMMFQfSEYvj2zM39awG8EvOz7axdIpAWJSYLu-SM",

  "expires_in": 3600,

  "scope": "https://www.googleapis.com/auth/androidpublisher",

  "token_type": "Bearer"

}

5、使用access_token调用Google API校验订单

https://www.googleapis.com/androidpublisher/v3/applications/packageName/purchases/products/productId/tokens/purchaseToken?access_token=access_token

v1、v2接口在20191231会被停用,所以这里建议直接使用v3

packageName:包名,类似com.some.thing

productId:商品id,类似com.some.thing.inapp1

purchaseToken:订单支付成功生成

返回的JSON,其中purchaseState即为订单是否已支付,orderId为用户所见订单号,建议保存用以对账

{

"kind": "androidpublisher#productPurchase",

"purchaseTimeMillis": "1571447997364",

"purchaseState": 0,

"consumptionState": 1,

"developerPayload": "",

"orderId": "GPA.3323-0287-6492-61582",

"purchaseType": 0,

"acknowledgementState": 1

}

四、服务端逻辑

1、客户端发送packageName,productId,purchaseToken

2、服务端首先通过purchaseToken判断该请求是否已发货,已处理直接返回

3、获取access_token

4、Google API校验,验证通过进行发货

5、保存该订单已发货,后续收到请求不在处理

五、其他

Google API使用时可能会遇到两个问题

1、Access Not Configured:需要给APP启用Google Play Android Developer API

Google Pay接入笔记_第6张图片

2、projectNotLinked:需要在Google Play Developer Console关联APP

参考:

https://www.cnblogs.com/android-blogs/p/6380725.html?utm_source=itdadao&utm_medium=referral

你可能感兴趣的:(Google Pay接入笔记)