微信开发文档

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)

  1. <dependency>
  2. <groupId>com.github.binarywang</groupId>
  3. <artifactId>weixin-java-pay</artifactId>
  4. <version>3.7.0</version>
  5. </dependency>

配置文件

application.yml

  1. weixin:
  2. pay:
  3. jsapi:
  4. jsapiAppId:
  5. jsapiAppSecret:
  6. jsapiTradeType: JSAPI
  7. mini:
  8. miniAppId:
  9. miniAppSecret:
  10. miniTradeType: JSAPI
  11. # 商户ID
  12. mchId:
  13. # 商户key:api秘钥(32位)
  14. mchKey:
  15. # 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除
  16. # subAppId:
  17. #服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除
  18. # subMchId:
  19. # apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定:
  20. # 从微信商户平台下载的安全证书存放的路径、我放在resources下面,切记一定要看看target目录下的class文件下有没有打包apiclient_cert.p12文件
  21. certPath: classpath:wx/apiclient_cert.p12

证书申请参考:https://www.yuque.com/hntool/dq84f3/siug8v

配置类

WxConfig.java

  1. import com.github.binarywang.wxpay.config.WxPayConfig;
  2. import com.github.binarywang.wxpay.service.WxPayService;
  3. import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. public class WxConfig {
  9. @Autowired
  10. private WxProperties properties;
  11. @Bean
  12. public WxPayConfig wxPayConfig() {
  13. WxPayConfig payConfig = new WxPayConfig();
  14. payConfig.setAppId(properties.getMiniAppId());
  15. payConfig.setMchId(properties.getMchId());
  16. payConfig.setMchKey(properties.getMchKey());
  17. payConfig.setNotifyUrl(properties.getNotifyUrl());
  18. payConfig.setKeyPath(properties.getCertPath());
  19. payConfig.setTradeType("JSAPI");
  20. payConfig.setSignType("MD5");
  21. return payConfig;
  22. }
  23. @Bean
  24. public WxPayService wxPayService(WxPayConfig payConfig) {
  25. WxPayService wxPayService = new WxPayServiceImpl();
  26. wxPayService.setConfig(payConfig);
  27. return wxPayService;
  28. }
  29. }

WxProperties.java

  1. import lombok.Getter;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. @Getter
  6. public class WxProperties {
  7. /**
  8. * 小程序aapid
  9. */
  10. @Value("${weixin.pay.mini.miniAppId}")
  11. private String miniAppId;
  12. /**
  13. * jsapiappid
  14. */
  15. @Value("${weixin.pay.jsapi.jsapiAppId}")
  16. private String jsapiAppId;
  17. /**
  18. * 回调地址
  19. */
  20. @Value("${weixin.pay.notifyUrl}")
  21. private String notifyUrl;
  22. @Value("${weixin.pay.jsapi.jsapiTradeType}")
  23. private String jsapiTradeType;
  24. @Value("${weixin.pay.mini.miniTradeType}")
  25. private String miniTradeType;
  26. /**
  27. * 微信支付商户号
  28. */
  29. @Value("${weixin.pay.mchId}")
  30. private String mchId;
  31. /**
  32. * 微信支付商户密钥
  33. */
  34. @Value("${weixin.pay.mchKey}")
  35. private String mchKey;
  36. /**
  37. * apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定
  38. */
  39. @Value("${weixin.pay.certPath}")
  40. private String certPath;
  41. }

付款到零钱工具类

WxPayTransfer.java

  1. @Component
  2. public class WxPayTransfer {
  3. @Resource
  4. private WxProperties wxProperties;
  5. @Resource
  6. private WxPayService wxPayService;
  7. public void transfer(String openId, BigDecimal amount) {
  8. String amt = amount.multiply(new BigDecimal(String.valueOf(100))).setScale(0, BigDecimal.ROUND_DOWN).toPlainString();
  9. EntPayRequest entPayRequest = new EntPayRequest();
  10. // 注意小程序的话就传小程序的appid,公众号就传公众号的appid
  11. entPayRequest.setAppid(wxProperties.getMiniAppId());
  12. entPayRequest.setMchId(wxProperties.getMchId());
  13. entPayRequest.setPartnerTradeNo(RandomUtil.randomString(32));
  14. entPayRequest.setOpenid(openId);
  15. entPayRequest.setCheckName("NO_CHECK");
  16. entPayRequest.setAmount(Integer.valueOf(amt));
  17. entPayRequest.setDescription("提现到账");
  18. entPayRequest.setSpbillCreateIp(IpUtils.getHostIp());
  19. try {
  20. EntPayResult entPayResult = wxPayService.getEntPayService().entPay(entPayRequest);
  21. System.out.println("entPayResult : " + entPayResult);
  22. } catch (WxPayException e) {
  23. System.out.println("付款失败,返回报文" + e);
  24. throw new CustomException("付款失败,返回报文" + e);
  25. }
  26. }
  27. }

调用

  1. @Service
  2. public class DemoServiceImpl implements DemoService{
  3. @Autowired
  4. private WxPayTransfer wxPayTransfer;
  5. public void trafer(String openId, BigDecimal amount){
  6. wxPayTransfer.transfer(openId,amount);
  7. }
  8. }


常见问题

最低打款金额不能小于1元

注意openId必须是在appid下面的
比如小程序的用户openid就填小程序的appid

如何开通企业付款到零钱

https://mp.weixin.qq.com/s/GztyVrHtaZuAdqCPUX1eJA