Java处理谷歌支付

整个开发背景是前端在调用完google play支付流程后,需要后台验证支付结果以及在自己的服务生成订单相关信息。由此着手对google后台验证的调研。

创建API控制台项目

  1. 转到API控制台并使用您的Google Play控制台帐户登录。
  2. 选择创建项目
  3. 转到服务在左侧导航面板。
  4. 打开Google Play Android Developer API
  5. 接受服务条款。
  6. 转到左侧导航面板中的API Access
  7. 选择“ 创建OAuth 2.0客户端ID”
    • 在第一页上,您需要填写产品名称,但不需要徽标。请注意,您的最终用户将看不到产品名称。
    • 在第二页上,选择Web应用程序并设置重定向URI和Javascript源。以后可以更改这两个设置。
  8. 选择创建客户端ID

第一步:获取client_id和client_secret

登录开发账号,在API权限里面创建相应的Oauth客户端获取

第二步:获取Authorization code

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=http://localhost&client_id={client_id}

直接复制到浏览器地址栏回车,然后在地址栏会看到code,保存留待下一步用。

这个redirect_uri可以随便填的,目前验证用不到。

请求access token

通过向Google发送post 请求,来获得access token

https://accounts.google.com/o/oauth2/token

需要的参数:

  • code
  • client_id
  • cilent_secret
  • redirect_uri
  • grant_type

请求可以用postMan模拟一下。


获得返回的refresh_token,妥善保存此token。以后的请求中都不会再出现,对于当前创建的账号的唯一且永久有效的。

简单来说前面的都是权限校验,准备工作,我们主要是为了获取access_token。然后来调用谷歌的商品校验接口

GET https://www.googleapis.com/androidpublisher/v3/applications/packageName/purchases/products/productId/tokens/token?access_token={access_token}

把红色的值给替换掉

参数名称 描述
路径参数
packageName string 出售inapp产品的应用程序的包名称(例如,'com.some.thing')。
productId string inapp产品SKU(例如,'com.some.thing.inapp1')。
token string 购买inapp产品时,令牌提供给用户的设备。

如果成功会得到返回值

参数名称 描述  
consumptionState integer inapp产品的消费状态。可能的值是:
  1. Yet to be consumed
  2. 消费
 
developerPayload string 开发人员指定的字符串,包含有关订单的补充信息。  
kind string 这种类型代表androidpublisher服务中的inappPurchase对象。  
orderId string 与购买inapp产品相关联的订单ID。  
purchaseState integer 订单的购买状态。可能的值是:
  1. 购买
  2. 取消
 
purchaseTimeMillis long 购买产品的时间,自纪元(1970年1月1日)以来的毫秒数。  
purchaseType integer 购买inapp产品的类型。仅当未使用标准应用内结算流程进行此购买时,才会设置此字段。可能的值是:
  1. 测试(即从许可证测试帐户购买)
  2. 促销(即使用促销代码购买)

遇到的错误:

projectNotLinked

{
    "error": {
        "errors": [
            {
                "domain": "androidpublisher",
                "reason": "projectNotLinked",
                "message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console."
            }
        ],
        "code": 403,
        "message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console."
    }
}
这个错误搞了半天,由于谷歌官方文档的不及时更新,原来的方案可能有点变动。

在这个页设置关联:https://play.google.com/apps/publish/

Google Play Developer Console

1.  "Google Play Developer Console" > "Settings" > subcategory "API access".

2.  Make a link to your "Linked Project".

3.  "Service Account" place maybe already showing ur "Service account" CLIENT ID which made "google developer console"
 

新版的话需要进入后台页面,然后选择API权限,最后点击一下关联。

在“链接项目”下找到您的项目,然后单击“链接”按钮。

如果再次出现相同的错误,可能是因为您在链接项目之前已在控制台中配置并购买了产品。

要解决此问题,只需在您的应用中添加新产品即可。

还有一个问题要注意的是我们前面生成的access_token,只有几千秒的生效时间,以后要再得到access_token的话,我们就不需要那么复杂了,只需要调用刷新token 的接口就好。

https://www.googleapis.com/androidpublisher/v1/...?access_token=...

前面的参数都差不多,这里要注意refresh_token的话传入上一次获取到的token就OK了。

猜你喜欢

转载自blog.csdn.net/liaodehong/article/details/83274207