當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
JAVA+微信JSAPI支付
生活随笔
收集整理的這篇文章主要介紹了
JAVA+微信JSAPI支付
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
JAVA+微信JSAPI支付
- 引入依賴
- 獲取請(qǐng)求ip
- 組裝微信支付請(qǐng)求
- 回調(diào)處理
引入依賴
<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-pay</artifactId><version>${weixin-java-pay.version}</version></dependency>獲取請(qǐng)求ip
public static String getIpAddr(HttpServletRequest request){if (request == null){return "unknown";}String ip = request.getHeader("x-forwarded-for");if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getHeader("Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getHeader("X-Forwarded-For");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getHeader("WL-Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getHeader("X-Real-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){ip = request.getRemoteAddr();}return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : EscapeUtil.clean(ip);}組裝微信支付請(qǐng)求
public <T> T createWxOrder(ReqStringParam param) throws WxPayException {WxPayService wxPayService = wxPayConfiguration.wxService();WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();request.setAppid(wxPayService.getConfig().getAppId());request.setMchId(wxPayService.getConfig().getMchId());request.setDetail("交易描述");request.setOutTradeNo(order.getOrderSn());//支付回調(diào)地址request.setNotifyUrl(wxPayService.getConfig().getNotifyUrl());request.setFeeType("CNY");// 貨幣類型 人民幣// 支付金額BigDecimal actualPrice = BigDecimal.valueOf(100L);request.setTotalFee(actualPrice.multiply(BigDecimal.valueOf(100L)).intValue());request.setSpbillCreateIp(IpUtils.getIpAddr(httpServletRequest));// 獲取iprequest.setTradeType("JSAPI");// 交易類型request.setBody("支付");request.setOpenid(user.getWeixinOpenid());// 微信openId//設(shè)置支付方式return wxService.createOrder(request);}回調(diào)處理
public String parseWxOrderNotifyResult(String xmlData) throws WxPayException {WxPayOrderNotifyResult wxPayOrderNotifyResult = wxService.parseOrderNotifyResult(xmlData);String outtradeNo = wxPayOrderNotifyResult.getOutTradeNo();MallOrder order = iMallOrderService.lambdaQuery().eq(MallOrder::getOrderSn, wxPayOrderNotifyResult.getOutTradeNo()).one();if ("FAIL".equals(wxPayOrderNotifyResult.getResultCode())) {// 失敗業(yè)務(wù)邏輯處理} else {// 成功業(yè)務(wù)邏輯處理}return "<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> </xml>";}總結(jié)
以上是生活随笔為你收集整理的JAVA+微信JSAPI支付的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 传感器技术-热电阻式传感器(学习笔记三)
- 下一篇: 给新考上北大软件与微电子学院的同学——利