程序开发调用微信支付以及微信回调地址配置

网友投稿 357 2023-11-08

目录支付接口签名工具类,以及时间戳方法回调接口

首先观看微信提供的文档

小程序开发调用微信支付以及微信回调地址配置

https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_3&index=1

清楚调用微信支付必须传递的参数

因为微信提供了小程序唤起微信支付的方法,后端只需要传递对应的参数给前端即可

首先在程序中配置申请的固定参数

?
1
2
3
4
wx.open.app_id=用户的appid
wx.open.app_secret=这是做登陆用的
weixin.pay.partner=商户号
wexxin.pay.partenerkey=商户号秘钥

编写工具类实现对固定值的读取

当用户点击购买会生成订单,这里代码省略

点击登陆时调用后端传给前端需要的值

对应微信文档https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1

可以看到,除了一些固定值,需要我们自己处理的有

签名:根据文档可以发现签名是有一定要求的

 简单来说就将其他传入固定值字段进行排序拼接,在根据商家号的key进行加密处理。

支付接口

?
1
2
3
4
5
6
7
8
9
10
11
@Autowired
private WXService wxService;
@GetMapping("pay")
public R creatNative(Integer orderid){
try {
Map map = wxService.payment(orderid);
return R.ok().data(map);
} catch (UnsupportedEncodingException e) {
return R.error().message("支付失败");
}
}

编写service逻辑,根据文档进行传值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Service
public class WXServiceImpl implements WXService {
@Autowired
private OrderService orderService;
@Override
public Map payment(Integer orderid) throws UnsupportedEncodingException {
//封装传递微信地址参数
Map paramMap = new HashMap();
paramMap.put("appid", ConstantPropertiesUtil.WX_OPEN_APP_ID); //公众号id
paramMap.put("mch_id", ConstantPropertiesUtil.PARTNER); //商户号
paramMap.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符串,调用工具类
paramMap.put("out_trade_no", orderid); //订单流水号
Order order = orderService.getById(orderid);
paramMap.put("total_fee", order.getPayment()); //金额
paramMap.put("spbill_create_ip", "127.0.0.1");  //终端ip
paramMap.put("notify_url", "http://XXXXX/weixin/callBack");//回调地址
paramMap.put("body",order.getProductname());  //商品名称
paramMap.put("timeStamp", WXUtil.getCurrentTimestamp()+"");//获取当前时间戳,单位秒
String sign = WXUtil.genSignature(ConstantPropertiesUtil.PARTNERKET,paramMap);    //sing
paramMap.put("sign", sign);  //签名
return paramMap;
}
}

签名工具类,以及时间戳方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class WXUtil {
public static String genSignature(String secretKey, Map<String, String> params) throws UnsupportedEncodingException {
if (secretKey == null || params == null || params.size() == 0) {
return "";
}
// 1. 参数名按照ASCII码表升序排序
String[] keys = params.keySet().toArray(new String[0]);
Arrays.sort(keys);
// 2. 按照排序拼接参数名与参数值
StringBuffer paramBuffer = new StringBuffer();
for (String key : keys) {
paramBuffer.append("&"+key).append(params.get(key) == null ? "" : "="+params.get(key));
}
// 3. 将secretKey拼接到最后
paramBuffer=paramBuffer.append("&key="+secretKey);
String pa =paramBuffer.substring(1);
// 4. MD5是128位长度的摘要算法,用16进制表示,一个十六进制的字符能表示4个位,所以签名后的字符串长度固定为32个十六进制字符。
return DigestUtils.md5Hex(pa.getBytes("UTF-8")).toUpperCase();
}
/**
* 获取当前时间戳,单位秒
* @return
*/
public static long getCurrentTimestamp() {
return System.currentTimeMillis()/1000;
}
/**
* 获取当前时间戳,单位毫秒
* @return
*/
public static long getCurrentTimestampMs() {
return System.currentTimeMillis();
}
}

此时即可完成支付,微信支付后,微信会给我们回调地址进行发送信息,由此我们可以判断支付状态以及获取微信支付返回的参数

回调接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//回调接口
@RequestMapping("callBack")
public String callBack(HttpServletRequest request, HttpServletResponse response) throws Exception{
System.out.println("接口已被调用");
ServletInputStream inputStream = request.getInputStream();
String notifyXml = StreamUtils.inputStream2String(inputStream, "utf-8");
System.out.println(notifyXml);
// 解析返回结果
Map<String, String> notifyMap = WXPayUtil.xmlToMap(notifyXml);
// 判断支付是否成功
if ("SUCCESS".equals(notifyMap.get("result_code"))) {
//编写自己的实现逻辑
// 支付成功:给微信发送我已接收通知的响应
// 创建响应对象
Map<String, String> returnMap = new HashMap<>();
returnMap.put("return_code", "SUCCESS");
returnMap.put("return_msg", "OK");
String returnXml = WXPayUtil.mapToXml(returnMap);
response.setContentType("text/xml");
System.out.println("支付成功");
return returnXml;
}
}
// 创建响应对象:微信接收到校验失败的结果后,会反复的调用当前回调函数
Map<String, String> returnMap = new HashMap<>();
returnMap.put("return_code", "FAIL");
returnMap.put("return_msg", "");
String returnXml = WXPayUtil.mapToXml(returnMap);
response.setContentType("text/xml");
System.out.println("校验失败");
return returnXml;
}

接收输入流转换工具类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class StreamUtils {
private static int _buffer_size = 1024;
/**
* InputStream流转换成String字符串
* @param inStream InputStream流
* @param encoding 编码格式
* @return String字符串
*/
public static String inputStream2String(InputStream inStream, String encoding){
String result = null;
ByteArrayOutputStream outStream = null;
try {
if(inStream != null){
outStream = new ByteArrayOutputStream();
byte[] tempBytes = new byte[_buffer_size];
int count = -1;
while((count = inStream.read(tempBytes, 0, _buffer_size)) != -1){
outStream.write(tempBytes, 0, count);
}
tempBytes = null;
outStream.flush();
result = new String(outStream.toByteArray(), encoding);
outStream.close();
}
} catch (Exception e) {
result = null;
} finally {
try {
if(inStream != null) {
inStream.close();
inStream = null;
}
if(outStream != null) {
outStream.close();
outStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}

到此这篇关于小程序开发调用微信支付以及微

您可能感兴趣的文章:微信小程序云开发实现微信支付功能业务逻辑可靠小程序通过小程序云实现微信支付功能实例小程序调用微信支付的方法小程序微信支付功能配置方法示例详解【基于thinkPHP】微信小程序调用微信支付接口的实现方法微信小程序微信支付接入开发实例详解微信小程序接入微信支付实现过程详解

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序运营是什么
下一篇:uni-app和原生小程序混合开发的具体实现过程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~