2025-04-27 17:50:45 +08:00
|
|
|
|
package com.cdzy.payment.config;
|
|
|
|
|
|
|
|
|
|
|
|
import com.wechat.pay.java.core.Config;
|
2025-05-12 18:10:11 +08:00
|
|
|
|
import com.wechat.pay.java.core.RSAPublicKeyConfig;
|
2025-04-27 17:50:45 +08:00
|
|
|
|
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;
|
|
|
|
|
|
/**
|
2025-05-12 18:10:11 +08:00
|
|
|
|
* 微信支付商户私钥文件路径
|
2025-04-27 17:50:45 +08:00
|
|
|
|
*/
|
|
|
|
|
|
private String privateKeyPath;
|
2025-05-12 18:10:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 微信支付商户公钥文件路径
|
|
|
|
|
|
*/
|
|
|
|
|
|
private String publicKeyPath;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 微信支付商户公钥ID
|
|
|
|
|
|
*/
|
|
|
|
|
|
private String publicKeyId;
|
2025-04-27 17:50:45 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 微信支付商户证书序列号
|
|
|
|
|
|
*/
|
|
|
|
|
|
private String merchantSerialNumber;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 微信支付商户 APIv3 密钥
|
|
|
|
|
|
*/
|
|
|
|
|
|
private String apiV3Key;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 微信支付回调地址
|
|
|
|
|
|
*/
|
|
|
|
|
|
private String payNotifyUrl;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 微信支付退款回调地址
|
|
|
|
|
|
*/
|
|
|
|
|
|
private String refundNotifyUrl;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 支付、退款过期时间(分钟)
|
2025-04-29 14:31:26 +08:00
|
|
|
|
* 默认5分钟,超过5分钟订单会自动关闭
|
2025-04-27 17:50:45 +08:00
|
|
|
|
*/
|
2025-04-29 14:31:26 +08:00
|
|
|
|
private Integer expireMinute = 5;
|
2025-04-27 17:50:45 +08:00
|
|
|
|
|
2025-05-12 18:10:11 +08:00
|
|
|
|
@Bean
|
2025-04-29 14:31:26 +08:00
|
|
|
|
public Config certificateConfig() {
|
2025-05-12 18:10:11 +08:00
|
|
|
|
return new RSAPublicKeyConfig.Builder()
|
2025-04-27 17:50:45 +08:00
|
|
|
|
.merchantId(merchantId)
|
|
|
|
|
|
// 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
|
|
|
|
|
|
.privateKeyFromPath(privateKeyPath)
|
2025-05-12 18:10:11 +08:00
|
|
|
|
.publicKeyFromPath(publicKeyPath)
|
2025-04-27 17:50:45 +08:00
|
|
|
|
.merchantSerialNumber(merchantSerialNumber)
|
|
|
|
|
|
.apiV3Key(apiV3Key)
|
2025-05-12 18:10:11 +08:00
|
|
|
|
.publicKeyId(publicKeyId)
|
2025-04-27 17:50:45 +08:00
|
|
|
|
.build();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
2025-04-29 14:31:26 +08:00
|
|
|
|
public JsapiService wxJsapiService(Config certificateConfig) {
|
2025-04-27 17:50:45 +08:00
|
|
|
|
return new JsapiService.Builder().config(certificateConfig).build();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
2025-04-29 14:31:26 +08:00
|
|
|
|
public RefundService wxRefundService(Config certificateConfig) {
|
2025-04-27 17:50:45 +08:00
|
|
|
|
return new RefundService.Builder().config(certificateConfig).build();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
public RSASigner wxRsaSigner(){
|
|
|
|
|
|
PrivateKey privateKey = PemUtil.loadPrivateKeyFromPath(privateKeyPath);
|
|
|
|
|
|
return new RSASigner(merchantSerialNumber, privateKey);
|
|
|
|
|
|
}
|
2025-04-29 14:31:26 +08:00
|
|
|
|
|
2025-04-27 17:50:45 +08:00
|
|
|
|
}
|