小程序怎么开发调用微信支付及微信回调地址
发表于:2024-10-23 作者:千家信息网编辑
千家信息网最后更新 2024年10月23日,本篇内容主要讲解"小程序怎么开发调用微信支付及微信回调地址",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"小程序怎么开发调用微信支付及微信回调地址"吧!首先
千家信息网最后更新 2024年10月23日小程序怎么开发调用微信支付及微信回调地址
本篇内容主要讲解"小程序怎么开发调用微信支付及微信回调地址",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"小程序怎么开发调用微信支付及微信回调地址"吧!
首先观看微信提供的文档
https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_3&index=1
清楚调用微信支付必须传递的参数
因为微信提供了小程序唤起微信支付的方法,后端只需要传递对应的参数给前端即可
首先在程序中配置申请的固定参数
wx.open.app_id=用户的appidwx.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逻辑,根据文档进行传值
@Servicepublic 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, Mapparams) 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); // 解析返回结果 MapnotifyMap = WXPayUtil.xmlToMap(notifyXml); // 判断支付是否成功 if ("SUCCESS".equals(notifyMap.get("result_code"))) { //编写自己的实现逻辑 // 支付成功:给微信发送我已接收通知的响应 // 创建响应对象 Map 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 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; }}
到此,相信大家对"小程序怎么开发调用微信支付及微信回调地址"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
支付
参数
地址
字符
程序
字符串
工具
接口
文档
时间
开发
成功
单位
商户
方法
排序
内容
前端
十六进制
对象
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
怀旧服务器盗贼输出最高的天赋
sql数据库分离备份
济源app软件开发哪家公司好
领导重视 网络安全法
服务器管理需要用到哪些
数据库弱密码修改
开源数据库可视化工具
软件开发费的税率
甘洛国家网络安全宣传
网络安全今天是第几届
河南驻马店云服务器云空间
win7设置ftp服务器
pg数据库优化常用策略
聊城数字化城管软件开发电话
逆行网络技术有限公司
网络安全等级评测公司资质
科技互联网创业计划书ppt
手机app软件开发报价
tp5 跨数据库事务
绵阳电脑城软件开发
数据库系统课程的填空题
程序员c语言软件开发
办公网络安全培训试题
浪潮服务器共享管理口配置
数据库中有几个模式
常熟创新软件开发技术指导
有哪些有名的网络安全培训班
我的时间如何开服务器
服务器安全 西安
数据库字段文本类型排序