79 lines
2.5 KiB
Java
79 lines
2.5 KiB
Java
|
package com.greenorange.promotion.config;
|
|||
|
|
|||
|
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
|||
|
import com.wechat.pay.java.core.util.IOUtil;
|
|||
|
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
|||
|
import com.wechat.pay.java.service.refund.RefundService;
|
|||
|
import lombok.Data;
|
|||
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||
|
import org.springframework.context.annotation.Bean;
|
|||
|
import org.springframework.context.annotation.Configuration;
|
|||
|
import org.springframework.core.io.ClassPathResource;
|
|||
|
import org.springframework.stereotype.Component;
|
|||
|
|
|||
|
import java.io.IOException;
|
|||
|
|
|||
|
@Data
|
|||
|
@Slf4j
|
|||
|
@Configuration
|
|||
|
@Component("WxPayConfig")
|
|||
|
@ConfigurationProperties(prefix = "wx.pay")
|
|||
|
public class WxPayConfig {
|
|||
|
|
|||
|
private String appId;
|
|||
|
|
|||
|
private String apiV3Key;
|
|||
|
|
|||
|
private String notifyUrl;
|
|||
|
|
|||
|
private String merchantId;
|
|||
|
|
|||
|
private String privateKeyPath;
|
|||
|
|
|||
|
private String merchantSerialNumber;
|
|||
|
|
|||
|
// RSA配置
|
|||
|
private RSAAutoCertificateConfig RSAConfig;
|
|||
|
|
|||
|
// JSAPI支付
|
|||
|
private JsapiServiceExtension jsapiServiceExtension;
|
|||
|
|
|||
|
// 退款
|
|||
|
private RefundService refundService;
|
|||
|
|
|||
|
/**
|
|||
|
* 初始化配置
|
|||
|
*/
|
|||
|
@Bean
|
|||
|
public boolean initWxPayConfig() throws IOException {
|
|||
|
this.RSAConfig = buildRSAAutoCertificateConfig();
|
|||
|
this.jsapiServiceExtension = buildJsapiServiceExtension(RSAConfig);
|
|||
|
this.refundService = buildRefundService(RSAConfig);
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
// 构建并使用自动更新平台证书的RSA配置,一个商户号只能初始化一个配置,否则会因为重复的下载任务报错
|
|||
|
private RSAAutoCertificateConfig buildRSAAutoCertificateConfig() throws IOException {
|
|||
|
// 将 resource 目录下的文件转为 InputStream,然后利用 IOUtil.toString(inputStream) 转化为密钥
|
|||
|
String privateKey = IOUtil.toString(new ClassPathResource(privateKeyPath).getInputStream());
|
|||
|
return new RSAAutoCertificateConfig.Builder()
|
|||
|
.merchantId(merchantId)
|
|||
|
.privateKey(privateKey)
|
|||
|
.merchantSerialNumber(merchantSerialNumber)
|
|||
|
.apiV3Key(apiV3Key)
|
|||
|
.build();
|
|||
|
}
|
|||
|
|
|||
|
// 构建JSAPI支付
|
|||
|
private JsapiServiceExtension buildJsapiServiceExtension(RSAAutoCertificateConfig config) {
|
|||
|
return new JsapiServiceExtension.Builder().config(config).build();
|
|||
|
}
|
|||
|
|
|||
|
// 构建退款
|
|||
|
private RefundService buildRefundService(RSAAutoCertificateConfig config) {
|
|||
|
return new RefundService.Builder().config(config).build();
|
|||
|
}
|
|||
|
|
|||
|
}
|