微信开发文档
https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
代码
引入三方jar包
pom.xml 引入 WxJava(https://www.yuque.com/hntool/dq84f3/hn6qc0)
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>3.7.0</version>
</dependency>
配置文件
application.yml
weixin:
pay:
jsapi:
jsapiAppId:
jsapiAppSecret:
jsapiTradeType: JSAPI
mini:
miniAppId:
miniAppSecret:
miniTradeType: JSAPI
# 商户ID
mchId:
# 商户key:api秘钥(32位)
mchKey:
# 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除
# subAppId:
#服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除
# subMchId:
# apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定:
# 从微信商户平台下载的安全证书存放的路径、我放在resources下面,切记一定要看看target目录下的class文件下有没有打包apiclient_cert.p12文件
certPath: classpath:wx/apiclient_cert.p12
配置类
WxConfig.java
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WxConfig {
@Autowired
private WxProperties properties;
@Bean
public WxPayConfig wxPayConfig() {
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(properties.getMiniAppId());
payConfig.setMchId(properties.getMchId());
payConfig.setMchKey(properties.getMchKey());
payConfig.setNotifyUrl(properties.getNotifyUrl());
payConfig.setKeyPath(properties.getCertPath());
payConfig.setTradeType("JSAPI");
payConfig.setSignType("MD5");
return payConfig;
}
@Bean
public WxPayService wxPayService(WxPayConfig payConfig) {
WxPayService wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(payConfig);
return wxPayService;
}
}
WxProperties.java
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
@Getter
public class WxProperties {
/**
* 小程序aapid
*/
@Value("${weixin.pay.mini.miniAppId}")
private String miniAppId;
/**
* jsapiappid
*/
@Value("${weixin.pay.jsapi.jsapiAppId}")
private String jsapiAppId;
/**
* 回调地址
*/
@Value("${weixin.pay.notifyUrl}")
private String notifyUrl;
@Value("${weixin.pay.jsapi.jsapiTradeType}")
private String jsapiTradeType;
@Value("${weixin.pay.mini.miniTradeType}")
private String miniTradeType;
/**
* 微信支付商户号
*/
@Value("${weixin.pay.mchId}")
private String mchId;
/**
* 微信支付商户密钥
*/
@Value("${weixin.pay.mchKey}")
private String mchKey;
/**
* apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定
*/
@Value("${weixin.pay.certPath}")
private String certPath;
}
付款到零钱工具类
WxPayTransfer.java
@Component
public class WxPayTransfer {
@Resource
private WxProperties wxProperties;
@Resource
private WxPayService wxPayService;
public void transfer(String openId, BigDecimal amount) {
String amt = amount.multiply(new BigDecimal(String.valueOf(100))).setScale(0, BigDecimal.ROUND_DOWN).toPlainString();
EntPayRequest entPayRequest = new EntPayRequest();
// 注意小程序的话就传小程序的appid,公众号就传公众号的appid
entPayRequest.setAppid(wxProperties.getMiniAppId());
entPayRequest.setMchId(wxProperties.getMchId());
entPayRequest.setPartnerTradeNo(RandomUtil.randomString(32));
entPayRequest.setOpenid(openId);
entPayRequest.setCheckName("NO_CHECK");
entPayRequest.setAmount(Integer.valueOf(amt));
entPayRequest.setDescription("提现到账");
entPayRequest.setSpbillCreateIp(IpUtils.getHostIp());
try {
EntPayResult entPayResult = wxPayService.getEntPayService().entPay(entPayRequest);
System.out.println("entPayResult : " + entPayResult);
} catch (WxPayException e) {
System.out.println("付款失败,返回报文" + e);
throw new CustomException("付款失败,返回报文" + e);
}
}
}
调用
@Service
public class DemoServiceImpl implements DemoService{
@Autowired
private WxPayTransfer wxPayTransfer;
public void trafer(String openId, BigDecimal amount){
wxPayTransfer.transfer(openId,amount);
}
}
常见问题
最低打款金额不能小于1元
注意openId必须是在appid下面的
比如小程序的用户openid就填小程序的appid