package com.cdzy.payment.config; import com.wechat.pay.java.core.Config; import com.wechat.pay.java.core.RSAPublicKeyConfig; import com.wechat.pay.java.core.cipher.RSASigner; import com.wechat.pay.java.core.util.PemUtil; import com.wechat.pay.java.service.payments.jsapi.JsapiService; import com.wechat.pay.java.service.refund.RefundService; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.security.PrivateKey; /** * 微信支付配置参数 * * @author dingchao * @date 2025/4/24 * @modified by: */ @Data @Configuration @ConfigurationProperties(prefix = "payment.wx-pay") public class WxPayConfig { /** * 微信小程序 appId */ private String appId; /** * 微信支付商户号 */ private String merchantId; /** * 微信支付商户私钥文件路径 */ private String privateKeyPath; /** * 微信支付商户公钥文件路径 */ private String publicKeyPath; /** * 微信支付商户公钥ID */ private String publicKeyId; /** * 微信支付商户证书序列号 */ private String merchantSerialNumber; /** * 微信支付商户 APIv3 密钥 */ private String apiV3Key; /** * 微信支付回调地址 */ private String payNotifyUrl; /** * 微信支付退款回调地址 */ private String refundNotifyUrl; /** * 支付、退款过期时间(分钟) * 默认5分钟,超过5分钟订单会自动关闭 */ private Integer expireMinute = 5; @Bean public Config certificateConfig() { return new RSAPublicKeyConfig.Builder() .merchantId(merchantId) // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名 .privateKeyFromPath(privateKeyPath) .publicKeyFromPath(publicKeyPath) .merchantSerialNumber(merchantSerialNumber) .apiV3Key(apiV3Key) .publicKeyId(publicKeyId) .build(); } @Bean public JsapiService wxJsapiService(Config certificateConfig) { return new JsapiService.Builder().config(certificateConfig).build(); } @Bean public RefundService wxRefundService(Config certificateConfig) { return new RefundService.Builder().config(certificateConfig).build(); } @Bean public RSASigner wxRsaSigner(){ PrivateKey privateKey = PemUtil.loadPrivateKeyFromPath(privateKeyPath); return new RSASigner(merchantSerialNumber, privateKey); } }