mirror of
https://github.com/dromara/payment-spring-boot.git
synced 2026-03-13 21:33:41 +08:00
init 2.0.0
This commit is contained in:
@@ -64,6 +64,42 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>retrofit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>adapter-rxjava3</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>converter-jaxb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>converter-jackson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>logging-interceptor</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk15to18</artifactId>
|
||||
|
||||
@@ -73,7 +73,7 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Getter
|
||||
public abstract class BaseModel {
|
||||
public static final String HMAC_SHA256="HMAC-SHA256";
|
||||
public static final String HMAC_SHA256 = "HMAC-SHA256";
|
||||
private static final XmlMapper XML_MAPPER = new XmlMapper();
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
@@ -84,8 +84,7 @@ public abstract class BaseModel {
|
||||
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
|
||||
OBJECT_MAPPER
|
||||
// .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
|
||||
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
|
||||
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
|
||||
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -165,7 +164,7 @@ public abstract class BaseModel {
|
||||
@SneakyThrows
|
||||
private String hmacSha256(String src) {
|
||||
String algorithm = "HmacSHA256";
|
||||
Mac sha256HMAC = Mac.getInstance(algorithm,"BC");
|
||||
Mac sha256HMAC = Mac.getInstance(algorithm, "BC");
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(appSecret.getBytes(), algorithm);
|
||||
sha256HMAC.init(secretKeySpec);
|
||||
byte[] bytes = sha256HMAC.doFinal(src.getBytes(StandardCharsets.UTF_8));
|
||||
@@ -182,7 +181,7 @@ public abstract class BaseModel {
|
||||
@SneakyThrows
|
||||
private <T> String link(T t) {
|
||||
Assert.hasText(appSecret, "wechat pay appSecret is required");
|
||||
String json = OBJECT_MAPPER
|
||||
String json = OBJECT_MAPPER
|
||||
.writeValueAsString(t);
|
||||
|
||||
TreeMap<String, String> map = OBJECT_MAPPER.readValue(json, new TypeReference<TreeMap<String, String>>() {
|
||||
@@ -200,8 +199,8 @@ public abstract class BaseModel {
|
||||
public JsonNode request(String mchId, HttpMethod method, String url) {
|
||||
String xml = this.xml();
|
||||
RequestEntity<String> body = RequestEntity.method(method, UriComponentsBuilder.fromHttpUrl(url)
|
||||
.build()
|
||||
.toUri())
|
||||
.build()
|
||||
.toUri())
|
||||
.contentType(MediaType.valueOf("application/x-www-form-urlencoded;charset=UTF-8"))
|
||||
.body(xml);
|
||||
ResponseEntity<String> responseEntity = this.getRestTemplateClientAuthentication(mchId)
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright 2019-2022 felord.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* Website:
|
||||
* https://felord.cn
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.felord.payment.wechat.v3.client;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
|
||||
import retrofit2.converter.jackson.JacksonConverterFactory;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* The type Retrofit factory.
|
||||
*
|
||||
* @author dax
|
||||
* @since 2023 /5/21
|
||||
*/
|
||||
public final class RetrofitFactory {
|
||||
|
||||
/**
|
||||
* The constant JACKSON_CONVERTER_FACTORY.
|
||||
*/
|
||||
public static final JacksonConverterFactory JACKSON_CONVERTER_FACTORY = JacksonConverterFactoryBuilder.build();
|
||||
|
||||
/**
|
||||
* Create retrofit.
|
||||
*
|
||||
* @param baseUrl the base url
|
||||
* @return the retrofit
|
||||
*/
|
||||
public static Retrofit create(String baseUrl) {
|
||||
return create(baseUrl, new ConnectionPool());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create retrofit.
|
||||
*
|
||||
* @param baseUrl the base url
|
||||
* @param connectionPool the connection pool
|
||||
* @return the retrofit
|
||||
*/
|
||||
public static Retrofit create(String baseUrl, ConnectionPool connectionPool) {
|
||||
return create(baseUrl, connectionPool, HttpLoggingInterceptor.Level.NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create retrofit.
|
||||
*
|
||||
* @param baseUrl the base url
|
||||
* @param connectionPool the connection pool
|
||||
* @param level the level
|
||||
* @return the retrofit
|
||||
*/
|
||||
public static Retrofit create(String baseUrl, ConnectionPool connectionPool, HttpLoggingInterceptor.Level level) {
|
||||
return new Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.client(okHttpClient(connectionPool, level))
|
||||
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
|
||||
.addConverterFactory(JACKSON_CONVERTER_FACTORY)
|
||||
.build();
|
||||
}
|
||||
|
||||
private static OkHttpClient okHttpClient(ConnectionPool connectionPool, HttpLoggingInterceptor.Level level) {
|
||||
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
|
||||
httpLoggingInterceptor.level(level);
|
||||
return new OkHttpClient.Builder()
|
||||
.connectionPool(connectionPool)
|
||||
.addInterceptor(httpLoggingInterceptor)
|
||||
.retryOnConnectionFailure(true)
|
||||
.connectTimeout(30, TimeUnit.SECONDS)
|
||||
.readTimeout(30, TimeUnit.SECONDS)
|
||||
.writeTimeout(30, TimeUnit.SECONDS)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The type Jackson converter factory builder.
|
||||
*/
|
||||
static final class JacksonConverterFactoryBuilder {
|
||||
private JacksonConverterFactoryBuilder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Build jackson converter factory.
|
||||
*
|
||||
* @return the jackson converter factory
|
||||
*/
|
||||
public static JacksonConverterFactory build() {
|
||||
ObjectMapper objectMapper = new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||
// empty string error
|
||||
.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
|
||||
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
|
||||
.registerModule(new JavaTimeModule());
|
||||
return JacksonConverterFactory.create(objectMapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
78
pom.xml
78
pom.xml
@@ -91,6 +91,11 @@
|
||||
<spring-boot.version>2.7.7</spring-boot.version>
|
||||
<alipay-sdk.version>4.31.7.ALL</alipay-sdk.version>
|
||||
<bcprov.version>1.69</bcprov.version>
|
||||
<retrofit.version>2.9.0</retrofit.version>
|
||||
<okhttp3.version>4.10.0</okhttp3.version>
|
||||
<lombok.version>1.18.26</lombok.version>
|
||||
<jackson.version>2.13.5</jackson.version>
|
||||
<slf4j.version>2.0.7</slf4j.version>
|
||||
</properties>
|
||||
|
||||
<distributionManagement>
|
||||
@@ -116,7 +121,6 @@
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alipay.sdk</groupId>
|
||||
<artifactId>alipay-sdk-java</artifactId>
|
||||
@@ -132,6 +136,78 @@
|
||||
<artifactId>payment-spring-boot-autoconfigure</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>retrofit</artifactId>
|
||||
<version>${retrofit.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>adapter-rxjava3</artifactId>
|
||||
<version>${retrofit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>converter-jaxb</artifactId>
|
||||
<version>${retrofit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>converter-jackson</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<version>${retrofit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>${okhttp3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>logging-interceptor</artifactId>
|
||||
<version>${okhttp3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jdk8</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-parameter-names</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user