发布时间:2025-12-09 21:33:17 浏览次数:4
首先观看微信提供的文档
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;@Overridepublic 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进行加密处理。
编写service逻辑,根据文档进行传值
@Servicepublic class WXServiceImpl implements WXService {@Autowiredprivate OrderService orderService;@Overridepublic Map payment(Integer orderid) throws UnsupportedEncodingException {//封装传递微信地址参数Map paramMap = new HashMap();paramMap.put("appid", ConstantPropertiesUtil.WX_OPEN_APP_ID); //公众号idparamMap.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"); //终端ipparamMap.put("notify_url", "http://XXXXX/weixin/callBack");//回调地址paramMap.put("body",order.getProductname()); //商品名称paramMap.put("timeStamp", WXUtil.getCurrentTimestamp()+"");//获取当前时间戳,单位秒String sign = WXUtil.genSignature(ConstantPropertiesUtil.PARTNERKET,paramMap); //singparamMap.put("sign", sign); //签名return paramMap;}}此时即可完成支付,微信支付后,微信会给我们回调地址进行发送信息,由此我们可以判断支付状态以及获取微信支付返回的参数
接收输入流转换工具类
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;}}