springboot微信小程序支付
今天有项目开发用到多个微信商户进行支付的场景,使用微信的SDK不太方便,以下大概使用了微信支付的示例仅供参考:
微信支付在请求接口时都要生成请求头,直接上代码:
/** * 构造请求签名串 * @param method * @param url * @param timestamp * @param nonceStr * @param body * @return */ static String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body) { String canonicalUrl = url.encodedPath(); if (url.encodedQuery() != null) { canonicalUrl += "?" + url.encodedQuery(); } return method + "\n" + canonicalUrl + "\n" + timestamp + "\n" + nonceStr + "\n" + body + "\n"; }
/** * 生成签名 * @param message 签名串 * @param privateKey 商户私钥 * @return * @throws RemoteException * @throws RuntimeException */ static String sign(byte[] message, String privateKey) throws RemoteException,RuntimeException{ Signature sign = null; String stringSign = null; try { sign = Signature.getInstance("SHA256withRSA"); try { sign.initSign(PemUtil.loadPrivateKeyFromString(privateKey)); try { sign.update(message); stringSign = Base64.getEncoder().encodeToString(sign.sign()); } catch (SignatureException e) { throw new RemoteException(e.getMessage()); } } catch (InvalidKeyException e) { throw new RemoteException(e.getMessage()); } } catch (NoSuchAlgorithmException e) { throw new RemoteException(e.getMessage()); } return stringSign; }
支付示例:
/** * 支付 * @param privateKeyPath * @param privateKey * @return * @throws RemoteException */ public static JSONObject pay() throws RemoteException{ //应用appid String appid = "wxd*****3f"; //支付商户id String merchantId = "163***20"; //支付密钥 String apiV3Key = "5dc*******413"; //商户系列号 String merchantSerialNumber = "62C345476F*****3E71981"; //商户商户私钥路径 String privateKeyPath = this.getClass().getResource("/static/wechat/apiclient_key.pem").getPath(); //使用hutool获取证书文件内容 privateKey FileReader fileReader = new FileReader(privateKeyPath); String privateKey = fileReader.readString(); //微信支付下单API String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi"; HttpUrl httpurl = HttpUrl.parse(url); JSONObject data = new JSONObject(); JSONObject amount = new JSONObject(); JSONObject payer = new JSONObject(); //随机数 这里示例我先用hutool随机生成 String nonce_str = String.valueOf(RandomUtil.randomInt(1043243, 100432432)); //订单号 这里示例我先用hutool随机生成 String out_trade_no = String.valueOf(RandomUtil.randomInt(1043243, 100432432)); //当前时间戳 long timestamp = System.currentTimeMillis()/1000; data.put("mchid",merchantId); data.put("out_trade_no",out_trade_no); data.put("appid",appid); data.put("description","支付测试"); data.put("notify_url","https://notify_url"); amount.put("total",1); amount.put("currency","CNY"); payer.put("openid","oYZ*******HAhG8"); data.put("amount",amount); data.put("payer",payer); String message = buildMessage("POST", httpurl, timestamp, nonce_str, data.toJSONString()); String signature = null; try { //获取请求签名 signature = sign(message.getBytes("utf-8"),privateKey); } catch (UnsupportedEncodingException e) { throw new RemoteException(e.getMessage()); } //生成请求头 String header = buildHeader(merchantId,nonce_str,signature,timestamp,merchantSerialNumber); //发起请求 String result = HttpRequest.post(url) .body(data.toJSONString()) .header("Content-Type", "application/json") .header("Accept", "application/json") .header("Authorization",header) .execute().body(); JSONObject resultJson = JSONObject.parseObject(result); JSONObject requestData = new JSONObject(); if(resultJson.get("code") != null){ requestData.put("code",100); requestData.put("msg",resultJson.getString("message")); }else{ String prepay_id = resultJson.getString("prepay_id"); JSONObject payData = payData(appid,timestamp,nonce_str,prepay_id,privateKey); requestData.put("code",200); requestData.put("res",payData); } return requestData; } /** * JSAPI调起支付 * @param appid * @param timestamp * @param nonceStr * @param prepayId * @param privateKey * @return * @throws RemoteException */ public static JSONObject payData(String appid, long timestamp, String nonceStr, String prepayId,String privateKey) throws RemoteException{ String payMessage = payMessage(appid,timestamp,nonceStr,prepayId); JSONObject data = new JSONObject(); try { String paySign = sign(payMessage.getBytes("utf-8"),privateKey); data.put("appId",appid); data.put("timeStamp",timestamp); data.put("package","prepay_id="+prepayId); data.put("signType","RSA"); data.put("paySign",paySign); data.put("nonceStr",nonceStr); } catch (UnsupportedEncodingException e) { throw new RemoteException(e.getMessage()); } return data; }