Google Pay Java 后端验证方式一

做服务端验证前,需要做一下准备工作

  1. 创建api项目这个和登录用的项目不是同一个
  2. 开启Google Play Android Developer API
  3. 设置oauth同意屏幕(就是拉起开发者授权账号登录时的登录页面)
  4. 创建web应用的oauth客户端ID
  5. google play开发者后台,API权限菜单中关联刚刚创建的项目,一个google play账号只需要也只能关联一个api项目就行了,这个项目可以查询关联账号中的所有应用的订单
  6. 拉起授权页面,使用google开发者账号给项目授权,得到code
  7. 通过code,拿到refreshToken,这个token只有第一次才会返回需要永久储存(这个refreshtoken很重要,需要保存下来),如果弄丢,只有重新创建一个oauth客户端ID,然后重复步骤6,7,拿到新的refreshtoken
  8. 刷新refreshToken, 得到accessToken,通过accesstoken就可以去查询订单状态了,这里的accessToken一般只有5分钟左右,5分钟后需要重新用refreshToken换取新的accessToken

setp1
创建api项目google api console

setp2
开启Google Play Android Developer API 

搜索“Google Play Android Developer API”

开启“Google Play Android Developer API”

setp3
开启同意屏幕

填上必填项


这里填上必填项就行了,这个授权同意屏幕,请求code时拉起来给咋们开发人员开的,填啥都无所谓

setp4
创建oauth2客户端id

创建页面和创建成功后的修改页面可以获取到clientId和clientSecret

到这里api项目就已经创建好了

setp5
google play后台关联api项目

setp6
获取code
地址:https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri={填写的重定向地址}&client_id={创建的clientId}
将上面的{XX}替换成创建api项目时填写的重定向地址,和clientId,然后将连接放到浏览器中打开,就会吊起授权界面,使用你的开发者账号授权登录
请求方式:浏览器中打开

这里可以看到,重定向地址上有两个参数code和scope,我们只需要code就行了,这里的code是urlencode后的,使用时需要decode

setp7
使用code换取refreshToken
地址:https://accounts.google.com/o/oauth2/token
请求方式:post
参数:grant_type=authorization_code
code=获取到的code(需要看看code中是否有%号,如果有需要urldecode)
client_id=创建api项目是的clientId(客户端ID)
client_secret=创建api项目时的clientSecret(客户端密钥)
redirect_uri=创建api项目时的重定向地址


这里就获取到refreshToken了,重点重点重点,refreshToken保存下来,它只会在第一次请求中返回,后续用在发一样的请求不会返回refreshtoken,如果不慎弄丢了,需要去重新创建一个WebClientId

setp8
使用refreshToken获取accessToken
地址:https://accounts.google.com/o/oauth2/token
请求方式:post
参数:grant_type=refresh_token
refresh_token=刚刚获取到的refreshToken
client_id=创建api项目是的clientId(客户端ID)
client_secret=创建api项目时的clientSecret(客户端密钥)

setp9
查询订单状态
https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/products/{productId}/tokens/{token}?access_token={access_token}
packageName:app包名,必须是创建登录api项目时,创建android客户端Id使用包名
productId:对应购买商品的商品ID
token:购买成功后Purchase对象的getPurchaseToken()
access_token:上面咋们获取到的accessToken
请求方式:get
返回值:

 
{

"purchaseTimeMillis": "1623980699933",//购买产品的时间,自纪元(1970 年 1 月 1 日)以来的毫秒数。

"purchaseState": 0,//订单的购买状态。可能的值为:0. 已购买 1. 已取消 2. 待定

"consumptionState": 0,//产品的消费状态。可能的值为: 0. 尚未消耗 1. 已消耗

"developerPayload": "",

"orderId": "GPA.xxxx-xxxx-xxxx-xxxxx",//google订单号

"purchaseType": 0,

"acknowledgementState": 0,

"kind": "androidpublisher#productPurchase",

"obfuscatedExternalAccountId": "透传字段",//上面客户支付时的透传字段,google指导是用来存放用户信息的,不能过长,否则客户端不能支付

"obfuscatedExternalProfileId": "",

"regionCode": "HK"

}

到这里整个支付验证流程就已经走完了,这里总结哈笔者这次试用过程中走过的一些坑:

  • google应用必须要在封闭测试状态下,并审核通过的应用才能支付,文档说的是内部测试就可以了,笔者每次都弄到封闭测试状态下才可以支付。
  • 在firebase中创建了项目,会自动同步到google api后台,不用再去单独创建登录使用的项目
  • 登录使用的api项目和查询支付使用的api项目是两个不同的项目相互不干扰,查询支付的api项目一个google play账号对应一个项目,这个google play账号中所有的应用,都可以通过这个查询支付的api项目去查询
  • 获取code授权api项目时,要使用google play后台的开发者账号授权
以下是使用Java实现微信支付后端代码的示例: 1. 创建统一下单请求对象 ```java public class UnifiedOrderRequest { private String appid; // 公众账号ID private String mch_id; // 商户号 private String nonce_str; // 随机字符串 private String body; // 商品描述 private String out_trade_no; // 商户订单号 private int total_fee; // 标价金额,单位为分 private String spbill_create_ip; // 终端IP private String notify_url; // 通知地址 private String trade_type; // 交易类型 private String openid; // 用户标识 // 省略 getter 和 setter 方法 } ``` 2. 创建统一下单响应对象 ```java public class UnifiedOrderResponse { private String return_code; // 返回状态码 private String return_msg; // 返回信息 private String appid; // 公众账号ID private String mch_id; // 商户号 private String nonce_str; // 随机字符串 private String sign; // 签名 private String result_code; // 业务结果 private String prepay_id; // 预支付交易会话标识 private String trade_type; // 交易类型 private String code_url; // 二维码链接 // 省略 getter 和 setter 方法 } ``` 3. 发送统一下单请求 ```java public class WechatPayService { private static final String UNIFIED_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder"; public UnifiedOrderResponse unifiedOrder(UnifiedOrderRequest request) throws Exception { String xml = buildUnifiedOrderXml(request); String responseXml = postXml(UNIFIED_ORDER_URL, xml); return parseUnifiedOrderXml(responseXml); } private String buildUnifiedOrderXml(UnifiedOrderRequest request) throws Exception { Map<String, Object> data = new HashMap<>(); data.put("appid", request.getAppid()); data.put("mch_id", request.getMch_id()); data.put("nonce_str", request.getNonce_str()); data.put("body", request.getBody()); data.put("out_trade_no", request.getOut_trade_no()); data.put("total_fee", request.getTotal_fee()); data.put("spbill_create_ip", request.getSpbill_create_ip()); data.put("notify_url", request.getNotify_url()); data.put("trade_type", request.getTrade_type()); data.put("openid", request.getOpenid()); data.put("sign", WechatPayUtil.sign(data, "商户密钥")); return WechatPayUtil.mapToXml(data); } private UnifiedOrderResponse parseUnifiedOrderXml(String xml) throws Exception { Map<String, String> data = WechatPayUtil.xmlToMap(xml); UnifiedOrderResponse response = new UnifiedOrderResponse(); response.setReturn_code(data.get("return_code")); response.setReturn_msg(data.get("return_msg")); response.setAppid(data.get("appid")); response.setMch_id(data.get("mch_id")); response.setNonce_str(data.get("nonce_str")); response.setSign(data.get("sign")); response.setResult_code(data.get("result_code")); response.setPrepay_id(data.get("prepay_id")); response.setTrade_type(data.get("trade_type")); response.setCode_url(data.get("code_url")); return response; } private String postXml(String url, String xml) throws Exception { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(xml, ContentType.APPLICATION_XML)); HttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String responseXml = EntityUtils.toString(entity); EntityUtils.consume(entity); return responseXml; } } ``` 4. 处理支付结果通知 ```java public class WechatPayNotifyService { public void handleNotify(HttpServletRequest request, HttpServletResponse response, String merchantKey) throws Exception { String xml = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8); Map<String, String> data = WechatPayUtil.xmlToMap(xml); if (WechatPayUtil.verifySign(data, merchantKey)) { if ("SUCCESS".equals(data.get("return_code")) && "SUCCESS".equals(data.get("result_code"))) { // 支付成功,处理业务逻辑 } String responseXml = buildNotifyResponseXml("SUCCESS", "OK"); response.getWriter().write(responseXml); } else { String responseXml = buildNotifyResponseXml("FAIL", "签名验证失败"); response.getWriter().write(responseXml); } } private String buildNotifyResponseXml(String returnCode, String returnMsg) { Map<String, String> data = new HashMap<>(); data.put("return_code", returnCode); data.put("return_msg", returnMsg); return WechatPayUtil.mapToXml(data); } } ``` 以上代码仅为示例,实际使用时需要根据具体业务需求进行适当调整。同时,为了确保交易的安全性,建议使用 HTTPS 协议进行通信,并对敏感信息进行加密处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值