springboot使用com.github.binarywang包實現(xiàn)微信網(wǎng)頁上的支付和退款
前提
微信小程序中實現(xiàn)微信支付是從小程序中調去微信支付的界面直接進行支付,那么在pc端需要實現(xiàn)微信的支付呢,是需要出現(xiàn)一個二維碼讓用戶使用掃碼支付的。
注意:
需要實現(xiàn)pc端的微信支付,需要在微信商戶平臺開通native支付,并且下載并配置商戶證書


設置好這些之后,直接看看在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 是 支付回調地址
refundNotifyUrl是 退款回調地址
這兩個地址都必須是外網(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()); //金額,轉換成分 ,至少支付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();
}
}
//支付回調函數(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è)務
//因為會重復調用,如果你這里是錢包這種,那么一定要判斷是否支付過了
return WxPayNotifyResponse.success("成功_" + orderInfo.getId());
} catch (WxPayException e) {
e.printStackTrace();
return WxPayNotifyResponse.fail(e.getMessage());
}
}
//退款訂單請求
//RefundInfo 退款的訂單,這里單筆訂單,如果是多筆,你可以構建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)內部業(yè)務邏輯
*/
logger.info("wechatPayConfig.redRefundNotify():{}", refund.toString());
return "退款中";
}
return "退款失敗";
} catch (Exception e) {
// 處理異常
e.printStackTrace();
return e.getMessage();
}
}
//退款回調函數(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è)務處理邏輯
return WxPayNotifyResponse.success("成功_" + orderInfo.getId());
} catch (WxPayException e) {
e.printStackTrace();
return WxPayNotifyResponse.fail(e.getMessage());
}
}
}到此這篇關于springboot使用com.github.binarywang包實現(xiàn)微信網(wǎng)頁上的支付和退款的文章就介紹到這了,更多相關springboot微信支付和退款內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- java?Springboot對接開發(fā)微信支付詳細流程
- UniApp?+?SpringBoot?實現(xiàn)微信支付和退款功能
- springboot對接微信支付的完整流程(附前后端代碼)
- SpringBoot + 微信公眾號JSAPI支付功能的實現(xiàn)
- SpringBoot微信掃碼支付的實現(xiàn)示例
- SpringCloud解決Feign異步回調問題(SpringBoot+Async+Future實現(xiàn))
- 詳解springboot通過Async注解實現(xiàn)異步任務及回調的方法
- SpringBoot實現(xiàn)微信支付接口調用及回調函數(shù)(商戶參數(shù)獲取)
相關文章
如何在springboot中配置和使用mybatis-plus
這篇文章主要給大家介紹了關于如何在springboot中配置和使用mybatis-plus的相關資料,MyBatis?Plus是MyBatis的增強版,旨在提供更多便捷的特性,減少開發(fā)工作,同時保留了MyBatis的靈活性和強大性能,需要的朋友可以參考下2023-11-11
Java非靜態(tài)成員變量之死循環(huán)(詳解)
下面小編就為大家?guī)硪黄狫ava非靜態(tài)成員變量之死循環(huán)(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
java中利用List的subList方法實現(xiàn)對List分頁(簡單易學)
本篇文章主要介紹了java中l(wèi)ist數(shù)據(jù)拆分為sublist實現(xiàn)頁面分頁的簡單代碼,具有一定的參考價值,有需要的可以了解一下。2016-11-11

