mirror of
https://github.com/dromara/payment-spring-boot.git
synced 2026-03-14 05:43:46 +08:00
hotfix
This commit is contained in:
@@ -11,6 +11,6 @@ import java.lang.annotation.*;
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Import(WechatPayConfiguration.class)
|
||||
public @interface EnableWechatPay {
|
||||
@Import(PayConfiguration.class)
|
||||
public @interface EnableMobilePay {
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.enongm.dianji.payment.autoconfigure;
|
||||
|
||||
import com.enongm.dianji.payment.wechat.v2.WechatPayV2Service;
|
||||
import com.enongm.dianji.payment.wechat.v3.KeyPairFactory;
|
||||
import com.enongm.dianji.payment.wechat.v3.SignatureProvider;
|
||||
import com.enongm.dianji.payment.wechat.v3.WechatPayV3Service;
|
||||
import com.enongm.dianji.payment.wechat.v3.model.WechatMetaBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Dax
|
||||
* @since 14:47
|
||||
*/
|
||||
@Configuration
|
||||
|
||||
public class PayConfiguration {
|
||||
/**
|
||||
* The type Wechat pay configuration.
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "wechat.pay", name = "v3.app-id")
|
||||
@EnableConfigurationProperties(WechatPayProperties.class)
|
||||
static class WechatPayConfiguration {
|
||||
private static final String CERT_ALIAS = "Tenpay Certificate";
|
||||
|
||||
/**
|
||||
* 微信支付公私钥 以及序列号等元数据.
|
||||
*
|
||||
* @param wechatPayProperties the wechat pay properties
|
||||
* @return the wechat cert bean
|
||||
*/
|
||||
@Bean
|
||||
WechatMetaBean wechatMetaBean(WechatPayProperties wechatPayProperties) {
|
||||
|
||||
String certPath = wechatPayProperties.getV3().getCertPath();
|
||||
String mchId = wechatPayProperties.getV3().getMchId();
|
||||
WechatMetaBean wechatMetaBean = new KeyPairFactory().createPKCS12(certPath, CERT_ALIAS, mchId);
|
||||
wechatMetaBean.setWechatPayProperties(wechatPayProperties);
|
||||
return wechatMetaBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付V3签名工具.
|
||||
*
|
||||
* @param wechatMetaBean the wechat meta bean
|
||||
* @return the signature provider
|
||||
*/
|
||||
@Bean
|
||||
SignatureProvider signatureProvider(WechatMetaBean wechatMetaBean) {
|
||||
return new SignatureProvider(wechatMetaBean);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付V2 只实现V3支付没有的支付业务。
|
||||
*
|
||||
* @param wechatPayProperties the wechat pay properties
|
||||
* @return the wechat pay v 2 service
|
||||
*/
|
||||
@Bean
|
||||
public WechatPayV2Service wechatPayV2Service(WechatPayProperties wechatPayProperties) {
|
||||
System.out.println("wechatPayProperties = " + wechatPayProperties);
|
||||
return new WechatPayV2Service(wechatPayProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付V3 全量支持.
|
||||
*
|
||||
* @param signatureProvider the signature provider
|
||||
* @return the wechat pay service
|
||||
*/
|
||||
@Bean
|
||||
public WechatPayV3Service wechatPayService(SignatureProvider signatureProvider) {
|
||||
return new WechatPayV3Service(signatureProvider);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package com.enongm.dianji.payment.autoconfigure;
|
||||
|
||||
|
||||
import com.enongm.dianji.payment.wechat.v3.KeyPairFactory;
|
||||
import com.enongm.dianji.payment.wechat.v3.WechatPayV3Service;
|
||||
import com.enongm.dianji.payment.wechat.v2.WechatPayV2Service;
|
||||
import com.enongm.dianji.payment.wechat.v3.SignatureProvider;
|
||||
import com.enongm.dianji.payment.wechat.v3.model.WechatMetaBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* The type Wechat pay configuration.
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(WechatPayProperties.class)
|
||||
public class WechatPayConfiguration {
|
||||
private static final String CERT_ALIAS = "Tenpay Certificate";
|
||||
|
||||
/**
|
||||
* 微信支付公私钥 以及序列号等元数据.
|
||||
*
|
||||
* @param wechatPayProperties the wechat pay properties
|
||||
* @return the wechat cert bean
|
||||
*/
|
||||
@Bean
|
||||
WechatMetaBean wechatMetaBean(WechatPayProperties wechatPayProperties) {
|
||||
|
||||
String certPath = wechatPayProperties.getV3().getCertPath();
|
||||
String mchId = wechatPayProperties.getV3().getMchId();
|
||||
WechatMetaBean wechatMetaBean = new KeyPairFactory().createPKCS12(certPath, CERT_ALIAS, mchId);
|
||||
wechatMetaBean.setWechatPayProperties(wechatPayProperties);
|
||||
return wechatMetaBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付V3签名工具.
|
||||
*
|
||||
* @param wechatMetaBean the wechat meta bean
|
||||
* @return the signature provider
|
||||
*/
|
||||
@Bean
|
||||
SignatureProvider signatureProvider(WechatMetaBean wechatMetaBean) {
|
||||
return new SignatureProvider(wechatMetaBean);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付V2 只实现V3支付没有的支付业务。
|
||||
*
|
||||
* @param wechatPayProperties the wechat pay properties
|
||||
* @return the wechat pay v 2 service
|
||||
*/
|
||||
@Bean
|
||||
public WechatPayV2Service wechatPayV2Service(WechatPayProperties wechatPayProperties) {
|
||||
System.out.println("wechatPayProperties = " + wechatPayProperties);
|
||||
return new WechatPayV2Service(wechatPayProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付V3 全量支持.
|
||||
*
|
||||
* @param signatureProvider the signature provider
|
||||
* @return the wechat pay service
|
||||
*/
|
||||
@Bean
|
||||
public WechatPayV3Service wechatPayService(SignatureProvider signatureProvider) {
|
||||
return new WechatPayV3Service(signatureProvider);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,14 @@ public class WechatPayProperties {
|
||||
|
||||
@Data
|
||||
public static class V3 {
|
||||
/**
|
||||
* set as true in develop mode
|
||||
*/
|
||||
private boolean sandboxMode;
|
||||
/**
|
||||
* set as true in partner mode
|
||||
*/
|
||||
private boolean partnerMode;
|
||||
/**
|
||||
* app id for wechat pay is required
|
||||
*/
|
||||
|
||||
@@ -56,6 +56,10 @@ public class BaseModel {
|
||||
private String appSecret;
|
||||
@JsonIgnore
|
||||
private V2PayType payType;
|
||||
@JsonIgnore
|
||||
private final WeChatServer weChatServer = WeChatServer.CHINA;
|
||||
private boolean sandboxMode;
|
||||
private boolean partnerMode;
|
||||
|
||||
|
||||
public BaseModel appId(String appId) {
|
||||
@@ -136,9 +140,16 @@ public class BaseModel {
|
||||
@SneakyThrows
|
||||
public WechatResponseBody request() {
|
||||
Assert.notNull(payType, "wechat pay payType is required");
|
||||
|
||||
String url = payType.defaultUri(this.weChatServer);
|
||||
if (sandboxMode) {
|
||||
url = partnerMode ? this.payType.partnerSandboxUri(this.weChatServer) :
|
||||
this.payType.defaultSandboxUri(this.weChatServer);
|
||||
}
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.method(payType.method(), RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), this.xml()))
|
||||
.url(payType.defaultUri(WeChatServer.CHINA))
|
||||
.url(url)
|
||||
.build();
|
||||
System.out.println("request.toString() = " + request.toString());
|
||||
Response response = CLIENT.newCall(request).execute();
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.enongm.dianji.payment.wechat.v3;
|
||||
|
||||
import com.enongm.dianji.payment.wechat.enumeration.V3PayType;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* @author Dax
|
||||
* @since 15:27
|
||||
*/
|
||||
public class V3PayTypeBodyAndConsumer {
|
||||
private final V3PayType payType;
|
||||
private final String jsonBody;
|
||||
private final Consumer<String> responseBodyConsumer;
|
||||
|
||||
public V3PayTypeBodyAndConsumer(V3PayType payType, String jsonBody, Consumer<String> responseBodyConsumer) {
|
||||
this.payType = payType;
|
||||
this.jsonBody = jsonBody;
|
||||
this.responseBodyConsumer = responseBodyConsumer;
|
||||
}
|
||||
|
||||
public V3PayType getPayType() {
|
||||
return payType;
|
||||
}
|
||||
|
||||
public String getJsonBody() {
|
||||
return jsonBody;
|
||||
}
|
||||
|
||||
public Consumer<String> getResponseBodyConsumer() {
|
||||
return responseBodyConsumer;
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,8 @@ import java.util.function.Consumer;
|
||||
@Getter
|
||||
public class WechatPayRequest {
|
||||
private V3PayType v3PayType;
|
||||
private boolean isSandbox;
|
||||
private boolean isPartner;
|
||||
private WeChatServer weChatServer;
|
||||
private String body;
|
||||
private final Map<String, String> headers = new HashMap<>();
|
||||
@@ -35,6 +37,28 @@ public class WechatPayRequest {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is sandbox wechat pay request.
|
||||
*
|
||||
* @param isSandbox the is sandbox
|
||||
* @return the wechat pay request
|
||||
*/
|
||||
public WechatPayRequest isSandbox(boolean isSandbox) {
|
||||
this.isSandbox = isSandbox;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is partner wechat pay request.
|
||||
*
|
||||
* @param isPartner the is partner
|
||||
* @return the wechat pay request
|
||||
*/
|
||||
public WechatPayRequest isPartner(boolean isPartner) {
|
||||
this.isPartner = isPartner;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* We chat server.
|
||||
*
|
||||
@@ -85,6 +109,10 @@ public class WechatPayRequest {
|
||||
* @return the string
|
||||
*/
|
||||
public String url() {
|
||||
if (isSandbox) {
|
||||
return isPartner ? this.v3PayType.partnerSandboxUri(this.weChatServer):
|
||||
this.v3PayType.defaultSandboxUri(this.weChatServer);
|
||||
}
|
||||
return this.v3PayType.defaultUri(this.weChatServer);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,7 @@ package com.enongm.dianji.payment.wechat.v3;
|
||||
|
||||
|
||||
|
||||
import com.enongm.dianji.payment.wechat.v3.filter.BodyMergeFilter;
|
||||
import com.enongm.dianji.payment.wechat.v3.filter.HeaderFilter;
|
||||
import com.enongm.dianji.payment.wechat.v3.filter.HttpRequestFilter;
|
||||
import com.enongm.dianji.payment.wechat.v3.filter.WechatServerFilter;
|
||||
import com.enongm.dianji.payment.wechat.v3.filter.*;
|
||||
import com.enongm.dianji.payment.wechat.v3.model.BaseModel;
|
||||
import com.enongm.dianji.payment.wechat.v3.model.WechatMetaBean;
|
||||
|
||||
@@ -57,7 +54,7 @@ public class WechatPayV3Service {
|
||||
* @param requestFunction the request function
|
||||
* @return the executor
|
||||
*/
|
||||
public <M extends BaseModel, R extends WechatPayRequest> Executor<M, R> request(Function<M, R> requestFunction) {
|
||||
public <M extends BaseModel, R extends V3PayTypeBodyAndConsumer> Executor<M, R> request(Function<M, R> requestFunction) {
|
||||
return new Executor<>(this.payFilterChain, requestFunction);
|
||||
}
|
||||
|
||||
@@ -68,7 +65,7 @@ public class WechatPayV3Service {
|
||||
* @param <M> the type parameter
|
||||
* @param <R> the type parameter
|
||||
*/
|
||||
public static class Executor<M extends BaseModel, R extends WechatPayRequest> {
|
||||
public static class Executor<M extends BaseModel, R extends V3PayTypeBodyAndConsumer> {
|
||||
/**
|
||||
* The Pay filter chain.
|
||||
*/
|
||||
@@ -95,7 +92,11 @@ public class WechatPayV3Service {
|
||||
* @param model the model
|
||||
*/
|
||||
public void withModel(M model) {
|
||||
payFilterChain.doChain(requestFunction.apply(model));
|
||||
R apply = requestFunction.apply(model);
|
||||
WechatPayRequest wechatPayRequest = new WechatPayRequest().v3PayType(apply.getPayType())
|
||||
.body(apply.getJsonBody())
|
||||
.responseConsumer(apply.getResponseBodyConsumer());
|
||||
payFilterChain.doChain(wechatPayRequest);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.enongm.dianji.payment.wechat.v3.service;
|
||||
|
||||
|
||||
import com.enongm.dianji.payment.wechat.enumeration.V3PayType;
|
||||
import com.enongm.dianji.payment.wechat.v3.WechatPayRequest;
|
||||
import com.enongm.dianji.payment.wechat.v3.model.AppPayModel;
|
||||
|
||||
/**
|
||||
* The type Wechat pay v 3 api.
|
||||
*
|
||||
* @author Dax
|
||||
* @since 17 :47
|
||||
*/
|
||||
public class WechatPayV3Api {
|
||||
|
||||
/**
|
||||
* APP 支付.
|
||||
*
|
||||
* @param appPayModel the app pay model
|
||||
* @return the wechat pay request
|
||||
*/
|
||||
public static WechatPayRequest appPay(AppPayModel appPayModel) {
|
||||
|
||||
return new WechatPayRequest().responseConsumer(System.out::println)
|
||||
// 如果需要支持容灾 需要对server 进行一些处理
|
||||
.v3PayType(V3PayType.APP)
|
||||
.body(appPayModel.jsonBody());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user