前言:最近公司接了个商城项目,被分到了做微信支付,支付宝支付和stripe国际支付,在此记录一下。
正文:
微信支付接入方式分很多种如下:
比如h5,大部分业务几乎都在后台,而app支付一半在前端一半在后端,我这里讲的是app支付
官方说明文档地址:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1
想要调试支付,必须得注册成商户,需要有营业执照什么的,不像微信公众号会有测试公众号,这也是我们比较难测试的地方
时序图:
从上面看出来,后端的工作只有三个:
1.生成加密的支付数据交给前端(统一下单,链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1)
2.编写后台回调(支付结果通知,链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3)
3.查询支付结果(查询订单,链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_2&index=4)
所以在app支付中我们后端只需要给前端准备数据,和接受回调就行了,还有一个查询订单支付结果,一共只3个接口需要编写,相对于h5少了很多东西。
在这里我展示统一下单,和回调方法:
第一步:
准备商户数据
package com.otcbi.pay.service.impl; import com.alibaba.fastjson.JSONObject; import com.otcbi.coin.common.exception.ApiException; import com.otcbi.coin.common.exception.ApiResponMessage; import com.otcbi.common.dto.OperateResult; import com.otcbi.pay.dto.ShopOrder; import com.otcbi.pay.entity.WechatOrder; import com.otcbi.pay.service.IWechatOrderService; import com.otcbi.pay.service.WeiXinPayService; import com.otcbi.pay.utils.MD5Util; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype. |