springboot使用com.github.binarywang包實現(xiàn)微信網(wǎng)頁上的支付和退款
前提
微信小程序中實現(xiàn)微信支付是從小程序中調(diào)去微信支付的界面直接進行支付,那么在pc端需要實現(xiàn)微信的支付呢,是需要出現(xiàn)一個二維碼讓用戶使用掃碼支付的。
注意:
需要實現(xiàn)pc端的微信支付,需要在微信商戶平臺開通native支付,并且下載并配置商戶證書
設(shè)置好這些之后,直接看看在springboot 或者springclound 中如何實現(xiàn)。
maven依賴
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-pay</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
步驟
在yml中定義微信的商戶號,密碼等等
wxpay: appId: appId mchId: mchId mchKey: mchKey keyPath: /home/wxpay_cert/apiclient_cert.p12 privateKeyPath: /home/wxpay_cert/apiclient_key.pem privateCertPath: /home/wxpay_cert/apiclient_cert.pem notifyUrl: notifyUrl refundNotifyUrl: refundNotifyUrl serialNo: serialNo
notifuyUrl 是 支付回調(diào)地址
refundNotifyUrl是 退款回調(diào)地址
這兩個地址都必須是外網(wǎng)可訪問的地址
新建WechatPayConfig類來讀取yml文件的中信息
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @Data @ConfigurationProperties(prefix = "wxpay") public class WechatPayConfig { private String appId; private String mchId; private String mchKey; private String keyPath; private String privateKeyPath; private String privateCertPath; private String notifyUrl; private String redUrl; private String refundNotifyUrl; private String serialNo; }
獲取ip地址幫助類
IpUtils
public class IpUtils { public static String getIpAddr() { return getIpAddr(ServletUtils.getRequest()); } }
ServletUtils (客戶端工具類)
public static HttpServletRequest getRequest() { try { return getRequestAttributes().getRequest(); } catch (Exception e) { return null; } } public static ServletRequestAttributes getRequestAttributes() { try { RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); return (ServletRequestAttributes) attributes; } catch (Exception e) { return null; } }
微信支付Service類
@Service @ConditionalOnClass(WxPayService.class) @EnableConfigurationProperties(WechatPayConfig.class) @AllArgsConstructor public class WechatPayService { static Logger logger = LoggerFactory.getLogger(WechatPayService.class); private WechatPayConfig wechatPayConfig; public WxPayService wxPayService() { WxPayConfig payConfig = new WxPayConfig(); payConfig.setAppId(wechatPayConfig.getAppId()); payConfig.setMchId(wechatPayConfig.getMchId()); payConfig.setMchKey(wechatPayConfig.getMchKey()); payConfig.setApiV3Key(wechatPayConfig.getMchKey()); payConfig.setKeyPath(wechatPayConfig.getKeyPath()); payConfig.setUseSandboxEnv(false); logger.info("wechatPayConfig.getPrivateKeyPath():{}",wechatPayConfig.getPrivateKeyPath()); WxPayService wxPayService = new WxPayServiceImpl(); wxPayService.setConfig(payConfig); //微信配置信息 return wxPayService; } //生成支付二維碼 //BookingInfo 是你訂單表 public String generatePayQrCode(BookingInfo booking, String ip) { try { WxPayService wxPayService = wxPayService(); WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest(); orderRequest.setOutTradeNo(booking.getDisplayNo()); //訂單號 orderRequest.setTotalFee(BigDecimal.valueOf(booking.getDisplayPrice()).multiply(new BigDecimal(100)).intValue()); //金額,轉(zhuǎn)換成分 ,至少支付1分錢 orderRequest.setSpbillCreateIp(ip); orderRequest.setNotifyUrl(wechatPayConfig.getRedUrl()); orderRequest.setBody(""); orderRequest.setAttach(""); orderRequest.setTradeType("NATIVE");//交易類型 orderRequest.setProductId(booking.getId().toString()); WxPayNativeOrderResult wxPayNativeOrderResult = (WxPayNativeOrderResult) wxPayService.createOrder(orderRequest); return wxPayNativeOrderResult.getCodeUrl(); } catch (Exception e) { // 處理異常 e.printStackTrace(); return e.getMessage(); } } //支付回調(diào)函數(shù) public String payNotify(String xmlData) { try { WxPayService wxPayService = wxPayService(); WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData); String orderId = notifyResult.getOutTradeNo();//拿到訂單號獲取訂單 logger.info("wechatPayConfig.rePayNotify():{}", xmlData); //處理的業(yè)務(wù) //因為會重復(fù)調(diào)用,如果你這里是錢包這種,那么一定要判斷是否支付過了 return WxPayNotifyResponse.success("成功_" + orderInfo.getId()); } catch (WxPayException e) { e.printStackTrace(); return WxPayNotifyResponse.fail(e.getMessage()); } } //退款訂單請求 //RefundInfo 退款的訂單,這里單筆訂單,如果是多筆,你可以構(gòu)建list 要循環(huán)執(zhí)行 //RefundInfo 中需要包括 支付訂單的訂單號、支付金額、退款的訂單號、退款金額 public String redRefundPay(RefundInfo refund){ try { if (refund == null) { return "訂單獲取失敗"; } WxPayService wxPayService = wxPayService(); WxPayRefundRequest orderRequest = new WxPayRefundRequest(); orderRequest.setOutTradeNo(refund.getDisplayNo()); orderRequest.setNotifyUrl(wechatPayConfig.getRefundNotifyUrl()); orderRequest.setTotalFee(BigDecimal.valueOf(refund.getDisplaySum()).multiply(new BigDecimal(100)).intValue()); orderRequest.setRefundFee(BigDecimal.valueOf(refund.getDisplayPrice()).multiply(new BigDecimal(100)).intValue()); orderRequest.setOutRefundNo(memberRedRefund.getDisplayNo()); WxPayRefundResult wxPayRefundResult = wxPayService.refund(orderRequest); if ("SUCCESS".equals(wxPayRefundResult.getReturnCode()) && "SUCCESS".equals(wxPayRefundResult.getResultCode())) { /** * 系統(tǒng)內(nèi)部業(yè)務(wù)邏輯 */ logger.info("wechatPayConfig.redRefundNotify():{}", refund.toString()); return "退款中"; } return "退款失敗"; } catch (Exception e) { // 處理異常 e.printStackTrace(); return e.getMessage(); } } //退款回調(diào)函數(shù) public String redRefundNotify(String xmlData) { try { WxPayService wxPayService = wxPayService(); WxPayRefundNotifyResult notifyResult = wxPayService.parseRefundNotifyResult(xmlData); String orderId = notifyResult.getReqInfo().getOutRefundNo();//拿到訂單號獲取訂單 logger.info("wechatPayConfig.redRefundNotify():{}", xmlData); //編寫自己的業(yè)務(wù)處理邏輯 return WxPayNotifyResponse.success("成功_" + orderInfo.getId()); } catch (WxPayException e) { e.printStackTrace(); return WxPayNotifyResponse.fail(e.getMessage()); } } }
到此這篇關(guān)于springboot使用com.github.binarywang包實現(xiàn)微信網(wǎng)頁上的支付和退款的文章就介紹到這了,更多相關(guān)springboot微信支付和退款內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java?Springboot對接開發(fā)微信支付詳細(xì)流程
- UniApp?+?SpringBoot?實現(xiàn)微信支付和退款功能
- springboot對接微信支付的完整流程(附前后端代碼)
- SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn)
- SpringBoot微信掃碼支付的實現(xiàn)示例
- SpringCloud解決Feign異步回調(diào)問題(SpringBoot+Async+Future實現(xiàn))
- 詳解springboot通過Async注解實現(xiàn)異步任務(wù)及回調(diào)的方法
- SpringBoot實現(xiàn)微信支付接口調(diào)用及回調(diào)函數(shù)(商戶參數(shù)獲取)
相關(guān)文章
Spring MVC中使用Controller如何進行重定向
這篇文章主要介紹了Spring MVC中使用Controller如何進行重定向操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java數(shù)據(jù)結(jié)構(gòu) 遞歸之迷宮回溯案例講解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)遞歸之迷宮回溯案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08SpringBoot中的JPA(Java?Persistence?API)詳解
這篇文章主要介紹了SpringBoot中的JPA(Java?Persistence?API)詳解,JPA用于將?Java?對象映射到關(guān)系型數(shù)據(jù)庫中,它提供了一種面向?qū)ο蟮姆绞絹聿僮鲾?shù)據(jù)庫,使得開發(fā)者可以更加方便地進行數(shù)據(jù)持久化操作,需要的朋友可以參考下2023-07-07SpringBoot項目實現(xiàn)統(tǒng)一異常處理的最佳方案
在前后端分離的項目開發(fā)過程中,我們通常會對數(shù)據(jù)返回格式進行統(tǒng)一的處理,這樣可以方便前端人員取數(shù)據(jù),后端發(fā)生異常時同樣會使用此格式將異常信息返回給前端,本文介紹了如何在SpringBoot項目中實現(xiàn)統(tǒng)一異常處理,如有錯誤,還望批評指正2024-02-02