SpringBoot集成微信服务商支付(多子商户集成)
前言
在网上搜了很多,都没有说如何对接微信支付-服务商。于是参考官方代码示例琢磨了一个版本
微信官方api文档网站:https://pay.weixin.qq.com/docs/partner/apis/partner-mini-program-payment/partner-mini-prepay.html
官方示例demo:https://github.com/wechatpay-apiv3/wechatpay-java
一、前置工作
1、获取商户平台的xxx核心参数
商户号(merchantId)、秘钥(apiV3Key)、商户证书(apiclient_key.pem)、证书序列号(merchantSerialNumber)
2、关联对应的小程序(appid)
二、SpringBoot集成微信小程序
1、引入pom依赖
<!-- 微信支付 -->
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.12</version>
</dependency>
2、yml配置
pay:
# 所关联小程序id
appId: wxaxxxxxxxxxxxxxxx
merchantId: 1xxxxxxxx5
merchantSerialNumber: xxxxxxxxxx
apiV3Key: xxxxxxxxx
#支付成功回调地址
notifyUrl: https://xxxxxx
# 商户平台私钥证书路径
privateKeyPath: /xxxxxx/apiclient_key.pem
商户号(merchantId)、秘钥(apiV3Key)、商户证书(apiclient_key.pem)、证书序列号(merchantSerialNumber)
3、java代码文件
3.1、Properties 配置类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "pay")
public class WxPayServiceProperties {
private String appId;
private String merchantId;
private String merchantSerialNumber;
private String apiV3Key;
private String notifyUrl;
private String privateKeyPath;
}
3.2 Configuration 服务类
import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.util.PemUtil;
import com.wechat.pay.java.service.partnerpayments.jsapi.JsapiService;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.RefundService.Builder;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.Base64;
/**
* @author tm
* date 2023/5/15
* @version 1.0
*/
@Slf4j
@Configuration
@EnableConfigurationProperties(WxPayServiceProperties.class)
public class WxPayServiceConfiguration {
private final WxPayServiceProperties properties;
public static JsapiService service;
private static String privateKeyPath;
public static RefundService refundService;
@Autowired
public WxPayServiceConfiguration(WxPayServiceProperties properties) {
this.properties = properties;
}
@PostConstruct
public void init() {
WxPayServiceProperties properties = this.properties