| | |
| | | import com.github.binarywang.wxpay.constant.WxPayConstants; |
| | | import com.github.binarywang.wxpay.service.WxPayService; |
| | | import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; |
| | | import com.wechat.pay.java.core.Config; |
| | | import com.wechat.pay.java.core.RSAAutoCertificateConfig; |
| | | import com.wechat.pay.java.core.RSAPublicKeyConfig; |
| | | import com.wechat.pay.java.core.notification.NotificationParser; |
| | | import com.wechat.pay.java.core.notification.RSAPublicKeyNotificationConfig; |
| | | import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; |
| | | import com.wechat.pay.java.service.refund.RefundService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.core.io.ClassPathResource; |
| | | import org.springframework.util.StreamUtils; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.io.InputStream; |
| | | import java.nio.charset.StandardCharsets; |
| | | |
| | | /** |
| | | * 微信小程序组件 |
| | |
| | | @PostConstruct |
| | | void init() { |
| | | this.load_WxMaService(); |
| | | this.load_wxPayService(); |
| | | // this.load_wxPayService(); |
| | | this.load_wxPayV3Service(); |
| | | // this.load_wxAppPayService(); |
| | | } |
| | |
| | | /** |
| | | * 初始化微信小程序支付 V2 |
| | | */ |
| | | public void load_wxPayService() { |
| | | WxPayConfig payConfig = new WxPayConfig(); |
| | | payConfig.setTradeType(WxPayConstants.TradeType.JSAPI); |
| | | payConfig.setSignType(WxPayConstants.SignType.MD5); |
| | | payConfig.setAppId(StringUtils.trimToNull(wxPayProperties.getAppId())); |
| | | payConfig.setMchId(StringUtils.trimToNull(wxPayProperties.getMchId())); |
| | | payConfig.setMchKey(StringUtils.trimToNull(wxPayProperties.getMchKey())); |
| | | payConfig.setKeyPath(StringUtils.trimToNull(wxPayProperties.getKeyPath())); |
| | | payConfig.setNotifyUrl(StringUtils.trimToNull(wxPayProperties.getNotifyUrl())); |
| | | WxPayService wxPayService = new WxPayServiceImpl(); |
| | | wxPayService.setConfig(payConfig); |
| | | this.wxPayService = wxPayService; |
| | | } |
| | | // public void load_wxPayService() { |
| | | // WxPayConfig payConfig = new WxPayConfig(); |
| | | // payConfig.setTradeType(WxPayConstants.TradeType.JSAPI); |
| | | // payConfig.setSignType(WxPayConstants.SignType.MD5); |
| | | // payConfig.setAppId(StringUtils.trimToNull(wxPayProperties.getAppId())); |
| | | // payConfig.setMchId(StringUtils.trimToNull(wxPayProperties.getMchId())); |
| | | // payConfig.setMchKey(StringUtils.trimToNull(wxPayProperties.getMchKey())); |
| | | // payConfig.setKeyPath(StringUtils.trimToNull(wxPayProperties.getKeyPath())); |
| | | // payConfig.setNotifyUrl(StringUtils.trimToNull(wxPayProperties.getNotifyUrl())); |
| | | // WxPayService wxPayService = new WxPayServiceImpl(); |
| | | // wxPayService.setConfig(payConfig); |
| | | // this.wxPayService = wxPayService; |
| | | // } |
| | | |
| | | /** |
| | | * 初始化微信支付 V3(JSAPI + 退款 + 回调验签) |
| | | * 使用平台证书模式(自动下载和管理微信平台证书) |
| | | */ |
| | | public void load_wxPayV3Service() { |
| | | try { |
| | | Config config = |
| | | new RSAPublicKeyConfig.Builder() |
| | | .merchantId(wxPayProperties.getMchId()) //微信支付的商户号 |
| | | .privateKeyFromPath(wxPayProperties.getPrivateKeyPath()) // 商户API证书私钥的存放路径 |
| | | .merchantSerialNumber(wxPayProperties.getSerialNumer()) //商户API证书序列号 |
| | | .publicKeyFromPath(wxPayProperties.getPubKeyPath()) //微信支付公钥的存放路径 |
| | | .publicKeyId(wxPayProperties.getPublicKeyId()) //微信支付公钥ID |
| | | .apiV3Key(wxPayProperties.getApiV3Key()) //APIv3密钥 |
| | | .build(); |
| | | // 从 classpath 读取商户私钥 |
| | | ClassPathResource keyResource = new ClassPathResource(StringUtils.trimToNull(wxPayProperties.getPrivateKeyPath())); |
| | | InputStream keyStream = keyResource.getInputStream(); |
| | | String privateKey = StreamUtils.copyToString(keyStream, StandardCharsets.UTF_8); |
| | | keyStream.close(); |
| | | |
| | | |
| | | // 支付公钥配置(用于回调验签) |
| | | RSAPublicKeyNotificationConfig notifyConfig = new RSAPublicKeyNotificationConfig.Builder() |
| | | .publicKeyFromPath(wxPayProperties.getPubKeyPath()) |
| | | .publicKeyId(wxPayProperties.getPublicKeyId()) |
| | | .apiV3Key(wxPayProperties.getApiV3Key()) |
| | | RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder() |
| | | .merchantId(StringUtils.trimToNull(wxPayProperties.getMchId())) |
| | | .privateKey(privateKey) |
| | | .merchantSerialNumber(StringUtils.trimToNull(wxPayProperties.getSerialNumer())) |
| | | .apiV3Key(StringUtils.trimToNull(wxPayProperties.getApiV3Key())) |
| | | .build(); |
| | | |
| | | v3JsapiService = new JsapiServiceExtension.Builder().config(config).build(); |
| | | v3RefundService = new RefundService.Builder().config(config).build(); |
| | | v3NotificationParser = new NotificationParser(notifyConfig); |
| | | v3NotificationParser = new NotificationParser(config); |
| | | |
| | | log.info("微信支付V3初始化成功"); |
| | | log.info("微信支付V3初始化成功(平台证书模式)"); |
| | | } catch (Exception e) { |
| | | log.error("微信支付V3初始化失败: {}", e.getMessage(), e); |
| | | } |