您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)使用Java怎么實(shí)現(xiàn)一個(gè)微信支付和微信退款功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
/** * 微信統(tǒng)一下單接口 * @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 { //初始化證書(shū) myConfig.initCert(); wxpay= new WXPay(myConfig); } catch (Exception e) { e.printStackTrace(); } //生成的隨機(jī)字符串 String nonce_str = WXPayUtil.generateNonceStr(); //獲取客戶端的ip地址 //獲取本機(jī)的ip地址 InetAddress addr = null; try { addr = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } //支付金額,需要轉(zhuǎn)成字符串類(lèi)型,否則后面的簽名會(huì)失敗 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; //商戶訂單號(hào) String out_trade_no= WXPayUtil.generateNonceStr(); //統(tǒng)一下單接口參數(shù) 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", 回調(diào)地址); 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"); //這邊要將返回的時(shí)間戳轉(zhuǎn)化成字符串,不然小程序端調(diào)用wx.requestPayment方法會(huì)報(bào)簽名錯(cuò)誤 resultMap.put("timeStamp", timeStamp + ""); //再次簽名,這個(gè)簽名用于小程序端調(diào)用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();//初始化證書(shū) wxpay= new WXPay(myConfig); } catch (Exception e) { e.printStackTrace(); } //生成的隨機(jī)字符串 String nonce_str = WXPayUtil.generateNonceStr(); //獲取客戶端的ip地址 //獲取本機(jī)的ip地址 InetAddress addr = null; try { addr = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } //支付金額,需要轉(zhuǎn)成字符串類(lèi)型,否則后面的簽名會(huì)失敗 // int total_fee= (int) req.get("money"); String payOutMoney = WxMoney.changeY2F(String.valueOf(req.get("money"))); // String body = (String) req.get("z"); //商戶訂單號(hào) String out_trade_no= WXPayUtil.generateNonceStr(); //統(tǒng)一下單接口參數(shù) 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);//微信訂單號(hào) data.put("out_refund_no", out_trade_no);//商戶退款單號(hào) data.put("total_fee",payOutMoney);//支付金額,微信支付提交的金額是不能帶小數(shù)點(diǎn)的,且是以分為單位,這邊需要轉(zhuǎn)成字符串類(lèi)型,否則后面的簽名會(huì)失敗 data.put("refund_fee",payOutMoney);//退款總金額,訂單總金額,單位為分,只能為整數(shù) //MD5運(yùn)算生成簽名,這里是第一次簽名,用于調(diào)用統(tǒng)一下單接口 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; } }
需要用到的工具類(lèi)
然后在MyConfig中配置一下你們的小程序參數(shù)就行了,如果需要添加退款功能必須配置商戶證書(shū)(對(duì)應(yīng)在你項(xiàng)目中的位置)。不需要的話就把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";//從微信商戶平臺(tái)下載的安全證書(shū)存放的目錄 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(); } }
看完上述內(nèi)容,你們對(duì)使用Java怎么實(shí)現(xiàn)一個(gè)微信支付和微信退款功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。