介绍
搞微信退款的时候发现一个好用的SDK,不止微信支付相关,包含很多。
码云地址
文档
微信支付demo
退款
maven
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>4.0.0</version>
</dependency>
yml配置
wx:
pay:
appId: #微信公众号或者小程序等的appid
mchId: #微信支付商户号
mchKey: #微信支付商户密钥
subAppId: #服务商模式下的子商户公众账号ID
subMchId: #服务商模式下的子商户号
keyPath: # p12证书的位置,可以指定绝对路径,也可以指定类路径(以classpath:开头)
两个config类
package com.github.binarywang.demo.wx.pay.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* wxpay pay properties.
*
* @author Binary Wang
*/
@Data
@ConfigurationProperties(prefix = "wx.pay")
public class WxPayProperties {
/**
* 设置微信公众号或者小程序等的appid
*/
private String appId;
/**
* 微信支付商户号
*/
private String mchId;
/**
* 微信支付商户密钥
*/
private String mchKey;
/**
* 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除
*/
private String subAppId;
/**
* 服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除
*/
private String subMchId;
/**
* apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定
*/
private String keyPath;
}
package com.github.binarywang.demo.wx.pay.config;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Binary Wang
*/
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
@AllArgsConstructor
public class WxPayConfiguration {
private WxPayProperties properties;
@Bean
@ConditionalOnMissingBean
public WxPayService wxService() {
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
// 可以指定是否使用沙箱环境
payConfig.setUseSandboxEnv(false);
WxPayService wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(payConfig);
return wxPayService;
}
}
实现方法
/**
* 微信退款
*
* @param outTradeNo 支付流水号
* @param outRefundNo 退款流水号
* @param totalFee 支付金额
* @param refundFee 退款金额
*/
public static Map<String, Object> doRefund(String outTradeNo, String outRefundNo, int totalFee, int refundFee) {
Map<String, Object> map = new HashMap<>();
try {
WxPayRefundRequest request = new WxPayRefundRequest();
request.setOutTradeNo(outTradeNo);
request.setOutRefundNo(outRefundNo);
request.setTotalFee(totalFee);
request.setRefundFee(refundFee);
WxPayRefundResult refundRequest = wxService.refund(request);
if (refundRequest.getReturnCode().equals("SUCCESS")) {
map.put("code", 200);
map.put("msg", "调用成功!");
return map;
} else {
map.put("code", 500);
map.put("msg", "调用失败!");
return map;
}
} catch (WxPayException e) {
e.printStackTrace();
map.put("code", 500);
map.put("msg", "调用失败!");
return map;
}
}