google支付回调验证(备用)

google支付回调验证(备用)

大家好,又见面了,我是全栈君。

Google支付问题

         20150218,挂机的日本服务器出现google支付被刷单现象,虽然目前进行的修补,但是这个问题并没有完全从根源上解决。并且公司以前的GooglePlay支付也有不完善的地方,在SDK端给支付回调发送支付信息后,支付回调程序没有调用Google API进行订单验证。因此Google支付流程需要进行完善。

 

Google支付解决方案

         上面的支付问题,Google有自己的解决方案,就是根据订单号去向Google API发送验证申请,Google API会返回订单相关信息。可以根据这个信息和SDK返回的信息进行对比验证。

         对于申请Google账号之类的流程,相信运营已经很清楚了,但是使用Google API还需要使用Google Developer Console创建Web Application账户,而后获取到client_id、client_secret、refresh_token。具体流程见下面:

 

1.       登陆 Google Developer Console ,地址:https://code.google.com/apis/console/

2.       在APIs & auth 项中找到 Credentials ,点击创建一个auth2.0 的web 应用

google支付回调验证(备用)

其中4的地址一定是 可用域名 + /oauth2callback

创建完后,可以获得,client_id, client_secret, redirect_url

3.       获取Authorization code

google中心在登陆状态,打开新页面输入如下地址:

https://accounts.google.com/o/oauth2/auth?scope=https://www.proxy.ustclug.org/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri={REDIRECT_URIS}&client_id={CLIENT_ID}

将蓝色部分根据相应的值进行替换;

这时会提示你是否要授权,点击授权,url地址栏会自动跳转,之后会获得code例如:https://www.example.com/oauth2callback?code=4/CpVOd8CljO_gxTRE1M5jtwEFwf8gRD44vrmKNDi4GSS.kr-GHuseD-oZEnp6UADFXm0E0MD3FlAI

 

4.       利用code获取refresh_token, 这里需要post请求

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

请求参数:code, client_id, client_secret, redirect_uri, grant_type

其中 grant_type 值为 authorization_code

第一次发起请求得到的JSON字符串如下所示,以后再请求将不再出现refresh_token(长令牌,一般不会失效),需要保存好refresh_token,可以存放到配置文件(或者写到数据库),以备后用

expires_in是指access_token的时效,为3600秒

{

    “access_token”: “ya29.3gC2jw5vm77YPkylq0H5sPJeJJDHX93Kq8qZHRJaMlknwJ85595eMogL300XKDOEI7zIsdeFEPY6zg”,

    “token_type”: “Bearer”,

    “expires_in”: 3600,

    “refresh_token”: “1/FbQD448CdDPfDEDpCy4gj_m3WDr_M0U5WupquXL_o”

}

 

在获取到client_id、client_secret、refresh_token后,我们的支付回调程序就可以使用订单号去请求Google Api进行验证。

 

Google支付回调验证流程

         通过上一步获取到client_id、client_secret、refresh_token之后,支付回调程序就可以调用google api进行支付验证。具体流程如下:

1.       获取access_token。

请求地址:https://accounts.google.com/o/oauth2/token
请求方式:post
请求参数:client_id, client_secret, refresh_toke, grant_type
grant_type 值固定为 refresh_token
返回:json

 

Using the refresh token

Each access token is only valid for a short time. Once the current access token expires, the server will need to use the refresh token to get a new one. To do this, send a POST request to https://accounts.google.com/o/oauth2/tokenwith the following fields set:

grant_type=refresh_token
client_id=<the client ID token created in the APIs Console>
client_secret=<the client secret corresponding to the client ID>
refresh_token=<the refresh token from the previous step>

A successful response will contain another access token:

{
  "access_token" : "ya29.AHES3ZQ_MbZCwac9TBWIbjW5ilJkXvLTeSl530Na2",
  "token_type" : "Bearer",
  "expires_in" : 3600,
}

The refresh token thus allows a web server continual access to the API without requiring an active login to a Google account.

 

2.       通过获得access_token 就可以请求谷歌的 API 接口,获得订单状态

在这里我所需要获取的是我在应用内给GooglePlay支付的购买信息,此类信息包含以下几个属性:(可参考Google Play Developer API下的Purchases.products

        A ProductPurchase resource indicates the status of a user’s inapp product purchase.

请求接口:https://www.proxy.ustclug.org/androidpublisher/v2/applications/packageName/purchases/products/productId/tokens/purchaseToken?access_token=access_token

 

     
packageName The package name of the application the inapp product was sold in (for example, ‘com.some.thing’).  
productId

           

The inapp product SKU (for example, ‘com.some.thing.inapp1’).

purchaseToken The token provided to the user’s device when the inapp product was purchased. 就是订单中purchaseToken  
     
     
     

返回数据 

1

2

3

4

5

6

7

{

  "kind": "androidpublisher#productPurchase",

  "purchaseTimeMillis": long,

  "purchaseState": integer,

  "consumptionState": integer,

  "developerPayload": string

}

 

consumptionState integer The consumption state of the inapp product. Possible values are:

  1.   0:Yet to be consumed
  2.   1:Consumed
 
developerPayload string A developer-specified string that contains supplemental information about an order.  
kind string This kind represents an inappPurchase object in the androidpublisher service.  
purchaseState integer The purchase state of the order. Possible values are:

  1.   0:Purchased
  2.   1:Cancelled 我们就是依靠这个判断购买信息
 
purchaseTimeMillis long The time the product was purchased, in milliseconds since the epoch (Jan 1, 1970).  

 

 

 

到此支付验证完成!

 

参考文档:

http://blog.csdn.net/hjun01/article/details/42032841

 http://www.vimer.cn/2014/04/google%E6%94%AF%E4%BB%98%E6%8E%A5%E5%8F%A3%E8%A2%AB%E5%88%B7%E4%BB%A5%E5%8F%8A%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88.html

 

调用接口遇到的几个问题:

 

1. Access Not Configured.

{

 “error”: {

  “errors”: [

   {

    “domain”: “usageLimits”,

    “reason”: “accessNotConfigured”,

    “message”: “Access Not Configured. The API(Google Play Android Developer API)is not enabled for you project.Please use Google Developers Console to update your configuration.”

   }

  ],

  “code”: 403,

  “message”: “Access Not Configured. The API(Google Play Android Developer API)is not enabled for you project.Please use Google Developers Console to update your configuration.”

 }

}

google支付回调验证(备用)

在这个页面: https://console.developers.google.com  

Google Developer Console

1.  “Google Developer Console” > “APIs & Auth” subcategory “APIs” > (api list) “Google Play Android Developer API”. Set “STATUS” to “ON”.

2.  “APIs & auth” subcategory “Credentials” > “Create new Client ID”. Choose “Service account” and create the id.

3.  You should get a P12 key from the browser.

 google支付回调验证(备用)

 

 

 

 

问题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”.

 google支付回调验证(备用)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/111756.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SSM整合Activiti工作流[通俗易懂]

    SSM整合Activiti工作流[通俗易懂]学完Activiti所有的知识点了,现在可以用一个小项目实战一下。本来自己想写一个简单点的流程,但是发现项目太小有些知识点用不上,所有就写了一个流程比较长、复杂点。把我前面博客中写的知识点多用上了,也巩固一下前面所掌握的。通过首先介绍一下项目的流程:1.员工发起请假申请——>项目组长进行初步审批——>就进入并行网关未通过就直接结束流程2.并行网关:…

    2022年5月3日
    53
  • springboot全局字符编码设置(解决乱码问题)

    springboot全局字符编码设置(解决乱码问题)有时候我们会发现这种问题,明明已经设置了字符编码过滤器但是还会有乱码的情况出现,这个问题令我们很是头疼,我之前也遇到过这种情况。那怎么解决呢?springboot编码格式设置有三种方式,不管使用哪种方式,总有一款适合你。1、在application.properties中设置#编码格式spring.http.encoding.force=truespring.http.encod…

    2022年4月27日
    348
  • ios手游游戏辅助挂机工具_ios挂机RPG游戏大全_iPhone挂机RPG类手游排行榜_ios挂机RPG类手游精选推荐_ios挂机RP类手游下载…

    ios手游游戏辅助挂机工具_ios挂机RPG游戏大全_iPhone挂机RPG类手游排行榜_ios挂机RPG类手游精选推荐_ios挂机RP类手游下载…《康熙皇帝养成计划》是一款原创角色养成挂机手游,游戏以少年复仇为主线,融合勇闯神龙、秘境探险、帮派结盟、培养豪杰、金科演武…在《魔王村长和杂货店》中你将置身于一个日式RPG的幻想大陆之中,扮演一个看似寻常的村庄村长。宅男逆袭成大神剧情,又可与心仪校花共同谱写高校恋爱物语,还能体验玄幻修真与灵异兼备的乐趣,革新的游戏体能全面开启。《天天挂奇迹》是一款超畅快刺激的挂机RPG类游戏,游戏以经典《奇迹…

    2022年6月2日
    40
  • oracle删除数据索引还会在吗_索引数据频繁修改

    oracle删除数据索引还会在吗_索引数据频繁修改首先查到表中的现有索引:select*fromuser_indexeswheretable_namein(‘表名’,”);删除索引:dropindex索引名;

    2025年9月14日
    5
  • mybatis拦截器详解_Java拦截器

    mybatis拦截器详解_Java拦截器拦截器可在mybatis进行sql底层处理的时候执行额外的逻辑,最常见的就是分页逻辑、对结果集进行处理过滤敏感信息等。}}}}}else{}}}折叠从上面的代码可以看到mybatis支持的拦截类型只有四种(按拦截顺序)1.Executor执行器接口2.StatementHandlersql构建处理器3.ParameterHandler参数处理器4.ResultSetHandler结果集处理器。…

    2022年9月10日
    4
  • linux中如何用ftp命令下载文件,linux中ftp下载文件命令的用法

    linux中如何用ftp命令下载文件,linux中ftp下载文件命令的用法linxu下的ftp命令是用来下载文件或者上传文件的,下面由学习啦小编为大家整理了linux的ftp下载文件命令的用法的相关知识,希望对大家有帮助!一、linux中的ftp下载文件命令的用法从远程ftp服务器下载文件的命令格式:get远程ftp服务器上当前目录下要下载的文件名[下载到本地机器上当前目录时的文件名],如:getnmap_file[nmap]意思是把远程ftp服务器下的文件nm…

    2022年8月24日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号