您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關使用Java怎么實現一個微信支付和微信退款功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
/** * 微信統一下單接口 * @return */ @RequestMapping(value = "/doUnifiedOrder", method = RequestMethod.POST) public ResponseBean doUnifiedOrder(@RequestBody Map<String,Object> req) { Map resultMap=new HashMap(); String openid = (String) req.get("openId"); WXPay wxpay =null; try { //初始化證書 myConfig.initCert(); wxpay= new WXPay(myConfig); } catch (Exception e) { e.printStackTrace(); } //生成的隨機字符串 String nonce_str = WXPayUtil.generateNonceStr(); //獲取客戶端的ip地址 //獲取本機的ip地址 InetAddress addr = null; try { addr = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } //支付金額,需要轉成字符串類型,否則后面的簽名會失敗 String payOutMoney = WxMoney.changeY2F(String.valueOf(req.get("money"))); String tradeName = (String) req.get("tradeName"); if(tradeName!=null&&tradeName.length()>0){ }else{ tradeName = "課程支付"; } String body = tradeName; //商戶訂單號 String out_trade_no= WXPayUtil.generateNonceStr(); //統一下單接口參數 HashMap<String, String> data = new HashMap<String, String>(); data.put("appid", 你的appid); data.put("mch_id", 商戶id); data.put("nonce_str", nonce_str); data.put("body", body); data.put("out_trade_no",out_trade_no); data.put("total_fee", payOutMoney); data.put("spbill_create_ip", "127.0.0.1"); data.put("notify_url", 回調地址); data.put("trade_type","JSAPI"); data.put("openid", openid); try { Map<String, String> rMap = wxpay.unifiedOrder(data); String return_code = (String) rMap.get("return_code"); String result_code = (String) rMap.get("result_code"); String nonceStr = WXPayUtil.generateNonceStr(); resultMap.put("nonceStr", nonceStr); Long timeStamp = System.currentTimeMillis() / 1000; if ("SUCCESS".equals(return_code) && return_code.equals(result_code)) { String prepayid = rMap.get("prepay_id"); resultMap.put("package", "prepay_id="+prepayid); resultMap.put("signType", "MD5"); //這邊要將返回的時間戳轉化成字符串,不然小程序端調用wx.requestPayment方法會報簽名錯誤 resultMap.put("timeStamp", timeStamp + ""); //再次簽名,這個簽名用于小程序端調用wx.requesetPayment方法 resultMap.put("appId",myConfig.getAppID()); String sign = WXPayUtil.generateSignature(resultMap, myConfig.getKey()); resultMap.put("paySign", sign); resultMap.put("out_trade_no",out_trade_no); ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信支付",resultMap); return responseBean; }else{ ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信支付失敗","微信支付失敗"); return responseBean; } } catch (Exception e) { ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信支付失敗","微信支付失敗"); e.printStackTrace(); return responseBean; } }
微信退款代碼(需要傳退款的訂單id)
public ResponseBean refund(Map<String, Object> req) { Map resultMap=new HashMap(); String buyId = (String) req.get("buyId"); WXPay wxpay =null; try { myConfig.initCert();//初始化證書 wxpay= new WXPay(myConfig); } catch (Exception e) { e.printStackTrace(); } //生成的隨機字符串 String nonce_str = WXPayUtil.generateNonceStr(); //獲取客戶端的ip地址 //獲取本機的ip地址 InetAddress addr = null; try { addr = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } //支付金額,需要轉成字符串類型,否則后面的簽名會失敗 // int total_fee= (int) req.get("money"); String payOutMoney = WxMoney.changeY2F(String.valueOf(req.get("money"))); // String body = (String) req.get("z"); //商戶訂單號 String out_trade_no= WXPayUtil.generateNonceStr(); //統一下單接口參數 HashMap<String, String> data = new HashMap<String, String>(); try { data.put("appid", myConfig.getAppID()); data.put("mch_id", myConfig.getMchID()); data.put("nonce_str", nonce_str); data.put("sign_type", "MD5"); data.put("out_trade_no",buyId);//微信訂單號 data.put("out_refund_no", out_trade_no);//商戶退款單號 data.put("total_fee",payOutMoney);//支付金額,微信支付提交的金額是不能帶小數點的,且是以分為單位,這邊需要轉成字符串類型,否則后面的簽名會失敗 data.put("refund_fee",payOutMoney);//退款總金額,訂單總金額,單位為分,只能為整數 //MD5運算生成簽名,這里是第一次簽名,用于調用統一下單接口 String sign = WXPayUtil.generateSignature(data, myConfig.getKey()); data.put("sign", sign); Map<String, String> rMap = wxpay.refund(data); String return_code = (String) rMap.get("return_code"); String result_code = (String) rMap.get("result_code"); Long timeStamp = System.currentTimeMillis() / 1000; if ("SUCCESS".equals(return_code) && return_code.equals(result_code)) { ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信退款成功",rMap); return responseBean; }else{ ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信退款失敗","微信支付失敗"); return responseBean; } } catch (Exception e) { ResponseBean responseBean = new ResponseBean(HttpStatus.OK.value(),"微信退款失敗","微信支付失敗"); e.printStackTrace(); return responseBean; } }
需要用到的工具類
然后在MyConfig中配置一下你們的小程序參數就行了,如果需要添加退款功能必須配置商戶證書(對應在你項目中的位置)。不需要的話就把initCert方法去掉。
public class MyConfig extends WXPayConfig { @Value("${res.imgPath}") public String imgPath; private byte[] certData; @Override public String getAppID() { return "你的小程序id"; } @Override public String getMchID() { return "商戶id"; } @Override public String getKey() { return "商戶秘鑰"; } @Override public InputStream getCertStream() { ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData); return certBis; } @Override public int getHttpConnectTimeoutMs() { return 8000; } @Override public int getHttpReadTimeoutMs() { return 10000; } @Override IWXPayDomain getWXPayDomain() { return new IWXPayDomain() { @Override public void report(String domain, long elapsedTimeMillis, Exception ex) { } @Override public DomainInfo getDomain(WXPayConfig config) { return new DomainInfo("api.mch.weixin.qq.com", false); } }; } @Override public void initCert() throws Exception { String certPath = this.imgPath+"/upload/cert/"+"apiclient_cert.p12";//從微信商戶平臺下載的安全證書存放的目錄 System.out.println(certPath); File file = new File(certPath); InputStream certStream = new FileInputStream(file); this.certData = new byte[(int) file.length()]; certStream.read(this.certData); certStream.close(); } }
看完上述內容,你們對使用Java怎么實現一個微信支付和微信退款功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。