irpas技术客

小程序开发调用微信支付以及微信回调地址配置_远走与梦游_微信支付回调url设置

irpas 3411

首先观看微信提供的文档

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

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

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

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

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

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

@Component //@PropertySource("classpath:application.properties") public class ConstantPropertiesUtil implements InitializingBean { //读取配置文件并赋值 @Value("${wx.open.app_id}") private String appId; @Value("${wx.open.app_secret}") private String appSecret; @Value("{weixin.pay.partner}") private String partner; @Value("{wexxin.pay.partenerkey}") private String partenerkey; public static String WX_OPEN_APP_ID; public static String WX_OPEN_APP_SECRET; public static String PARTNER; public static String PARTNERKET; @Override public void afterPropertiesSet() throws Exception { WX_OPEN_APP_ID = appId; WX_OPEN_APP_SECRET = appSecret; PARTNER = partner; PARTNERKET = partenerkey; } }

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

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

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

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

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

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

支付接口 @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逻辑,根据文档进行传值

@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; } } 签名工具类,以及时间戳方法 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(); } }

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

回调接口 //回调接口 @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; }

接收输入流转换工具类

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; } }


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #微信支付回调url设置