8 Commits

14 changed files with 124 additions and 128 deletions

View File

@@ -11,7 +11,7 @@
<dependency> <dependency>
<groupId>cn.felord</groupId> <groupId>cn.felord</groupId>
<artifactId>payment-spring-boot-starter</artifactId> <artifactId>payment-spring-boot-starter</artifactId>
<version>1.0.6.RELEASE</version> <version>1.0.7.RELEASE</version>
</dependency> </dependency>
``` ```

View File

@@ -35,7 +35,7 @@
<dependency> <dependency>
<groupId>cn.felord</groupId> <groupId>cn.felord</groupId>
<artifactId>payment-spring-boot-starter</artifactId> <artifactId>payment-spring-boot-starter</artifactId>
<version>1.0.6.RELEASE</version> <version>1.0.7.RELEASE</version>
</dependency> </dependency>
``` ```
## 采用技术 ## 采用技术

View File

@@ -4,7 +4,7 @@
<dependency> <dependency>
<groupId>cn.felord</groupId> <groupId>cn.felord</groupId>
<artifactId>payment-spring-boot-starter</artifactId> <artifactId>payment-spring-boot-starter</artifactId>
<version>1.0.6.RELEASE</version> <version>1.0.7.RELEASE</version>
</dependency> </dependency>
``` ```
> 基于 **Spring Boot 2.x** > 基于 **Spring Boot 2.x**

View File

@@ -5,11 +5,11 @@
<parent> <parent>
<groupId>cn.felord</groupId> <groupId>cn.felord</groupId>
<artifactId>payment-spring-boot</artifactId> <artifactId>payment-spring-boot</artifactId>
<version>1.0.6.RELEASE</version> <version>1.0.7.RELEASE</version>
</parent> </parent>
<artifactId>payment-spring-boot-autoconfigure</artifactId> <artifactId>payment-spring-boot-autoconfigure</artifactId>
<version>1.0.6.RELEASE</version> <version>1.0.7.RELEASE</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -18,46 +18,30 @@
package cn.felord.payment.wechat.enumeration; package cn.felord.payment.wechat.enumeration;
/** /**
* 退款成功事件. * 退款状态.
* *
* @author felord.cn * @author felord.cn
* @since 1.0.6.RELEASE * @since 1.0.7.RELEASE
*/ */
public enum RefundStatus { public enum RefundStatus {
/** /**
* 退款异常事件. * 退款异常.
* *
* @since 1.0.6.RELEASE * @since 1.0.7.RELEASE
*/ */
ABNORMAL("REFUND.ABNORMAL"), ABNORMAL,
/** /**
* 退款关闭事件. * 退款关闭.
* *
* @since 1.0.6.RELEASE * @since 1.0.7.RELEASE
*/ */
CLOSED("REFUND.CLOSED"), CLOSED,
/** /**
* 支付成功事件. * 退款成功.
* *
* @since 1.0.0.RELEASE * @since 1.0.7.RELEASE
*/ */
TRANSACTION("TRANSACTION.SUCCESS"); SUCCESS
/**
* The Event.
*/
private final String refundStatus;
/**
* Instantiates a new Event type.
*
* @param refundStatus the event
*/
RefundStatus(String refundStatus) {
this.refundStatus = refundStatus;
}
public String getRefundStatus() {
return refundStatus;
}
} }

View File

@@ -1,58 +0,0 @@
/*
* Copyright 2019-2021 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.v2;
import cn.felord.payment.wechat.WechatPayProperties;
import cn.felord.payment.wechat.v2.model.RefundModel;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.http.HttpMethod;
/**
* 退款相关API.
*
* @author felord.cn
* @since 1.0.5.RELEASE
*/
@Deprecated
public class WechatPayRefundApi {
private final WechatV2Client wechatV2Client;
/**
* Instantiates a new Wechat pay refund api.
*
* @param wechatV2Client the wechat v 2 client
*/
public WechatPayRefundApi(WechatV2Client wechatV2Client) {
this.wechatV2Client = wechatV2Client;
}
/**
* 退款
*
* @param refundModel the refund model
* @return json node
*/
public JsonNode transfer(RefundModel refundModel) {
WechatPayProperties.V3 v3 = wechatV2Client.getWechatMetaBean().getV3();
refundModel.setAppid(v3.getAppId());
refundModel.setMchId(v3.getMchId());
return wechatV2Client.wechatPayRequest(refundModel,
HttpMethod.POST,
"https://api.mch.weixin.qq.com/secapi/pay/refund");
}
}

View File

@@ -29,6 +29,7 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.RequestEntity; import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -155,6 +156,25 @@ public abstract class AbstractApi {
} }
} }
/**
* 构建Post请求对象.
*
* @param uri the uri
* @param params the params
* @param httpHeaders the http headers
* @return request entity
*/
protected RequestEntity<?> Post(URI uri, Object params, HttpHeaders httpHeaders) {
try {
return RequestEntity.post(uri)
.header("Pay-TenantId", tenantId)
.headers(httpHeaders)
.body(mapper.writeValueAsString(params));
} catch (JsonProcessingException e) {
throw new PayException("wechat app pay json failed");
}
}
/** /**
* 构建Get请求对象. * 构建Get请求对象.
* *
@@ -165,7 +185,19 @@ public abstract class AbstractApi {
return RequestEntity.get(uri).header("Pay-TenantId", tenantId) return RequestEntity.get(uri).header("Pay-TenantId", tenantId)
.build(); .build();
} }
/**
* 构建Get请求对象.
*
* @param uri the uri
* @param httpHeaders the http headers
* @return the request entity
*/
protected RequestEntity<?> Get(URI uri, HttpHeaders httpHeaders) {
return RequestEntity.get(uri)
.header("Pay-TenantId", tenantId)
.headers(httpHeaders)
.build();
}
/** /**
* 对账单内容下载,非流文件。 * 对账单内容下载,非流文件。

View File

@@ -37,7 +37,6 @@ import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import sun.security.x509.X509CertImpl;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
@@ -46,11 +45,16 @@ import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.*; import java.security.*;
import java.security.cert.*;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.util.*; import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -219,7 +223,7 @@ public class SignatureProvider {
ArrayNode certificates = bodyObjectNode.withArray("data"); ArrayNode certificates = bodyObjectNode.withArray("data");
if (certificates.isArray() && certificates.size() > 0) { if (certificates.isArray() && certificates.size() > 0) {
CERTIFICATE_MAP.clear(); CERTIFICATE_MAP.clear();
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509"); final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509",BC_PROVIDER);
certificates.forEach(objectNode -> { certificates.forEach(objectNode -> {
JsonNode encryptCertificate = objectNode.get("encrypt_certificate"); JsonNode encryptCertificate = objectNode.get("encrypt_certificate");
String associatedData = encryptCertificate.get("associated_data").asText(); String associatedData = encryptCertificate.get("associated_data").asText();
@@ -275,6 +279,7 @@ public class SignatureProvider {
* 对请求敏感字段进行加密 * 对请求敏感字段进行加密
* *
* @param message the message * @param message the message
* @param certificate the certificate
* @return encrypt message * @return encrypt message
* @since 1.0.6.RELEASE * @since 1.0.6.RELEASE
*/ */
@@ -284,20 +289,23 @@ public class SignatureProvider {
cipher.init(Cipher.ENCRYPT_MODE, certificate.getPublicKey()); cipher.init(Cipher.ENCRYPT_MODE, certificate.getPublicKey());
byte[] data = message.getBytes(StandardCharsets.UTF_8); byte[] data = message.getBytes(StandardCharsets.UTF_8);
byte[] cipherdata = cipher.doFinal(data); byte[] cipherData = cipher.doFinal(data);
return Base64Utils.encodeToString(cipherdata); return Base64Utils.encodeToString(cipherData);
} catch (Exception e) { } catch (Exception e) {
throw new PayException(e); throw new PayException(e);
} }
} }
public X509CertImpl getCertificate(){ public X509WechatCertificateInfo getCertificate(){
for (String serial : CERTIFICATE_MAP.keySet()) { for (String serial : CERTIFICATE_MAP.keySet()) {
X509CertImpl x509Cert = (X509CertImpl) CERTIFICATE_MAP.get(serial); X509Certificate x509Cert = (X509Certificate) CERTIFICATE_MAP.get(serial);
try { try {
x509Cert.checkValidity(); x509Cert.checkValidity();
return x509Cert; X509WechatCertificateInfo x509WechatCertificateInfo = new X509WechatCertificateInfo();
x509WechatCertificateInfo.setWechatPaySerial(serial);
x509WechatCertificateInfo.setX509Certificate(x509Cert);
return x509WechatCertificateInfo;
} catch (Exception e) { } catch (Exception e) {
log.warn("the wechat certificate is invalid , {}", e.getMessage()); log.warn("the wechat certificate is invalid , {}", e.getMessage());
// Async? // Async?

View File

@@ -19,7 +19,6 @@
package cn.felord.payment.wechat.v3; package cn.felord.payment.wechat.v3;
import cn.felord.payment.wechat.v2.WechatPayRedpackApi; import cn.felord.payment.wechat.v2.WechatPayRedpackApi;
import cn.felord.payment.wechat.v2.WechatPayRefundApi;
import cn.felord.payment.wechat.v2.WechatPayTransfersApi; import cn.felord.payment.wechat.v2.WechatPayTransfersApi;
import cn.felord.payment.wechat.v2.WechatV2Client; import cn.felord.payment.wechat.v2.WechatV2Client;
@@ -136,22 +135,6 @@ public class WechatApiProvider {
return new WechatPayCallback(wechatPayClient.signatureProvider(), tenantId); return new WechatPayCallback(wechatPayClient.signatureProvider(), tenantId);
} }
/**
* 退款基于V2
*
* @param tenantId the tenant id
* @return the wechat pay refund api
* @since 1.0.6.RELEASE
*/
@Deprecated
public WechatPayRefundApi refund(String tenantId) {
WechatMetaBean wechatMeta = wechatPayClient.signatureProvider()
.wechatMetaContainer()
.getWechatMeta(tenantId);
WechatV2Client wechatV2Client = new WechatV2Client(wechatMeta);
return new WechatPayRefundApi(wechatV2Client);
}
/** /**
* 现金红包基于V2 * 现金红包基于V2
* *

View File

@@ -24,6 +24,7 @@ import cn.felord.payment.wechat.v3.model.batchtransfer.QueryBatchTransferDetailP
import cn.felord.payment.wechat.v3.model.batchtransfer.QueryBatchTransferParams; import cn.felord.payment.wechat.v3.model.batchtransfer.QueryBatchTransferParams;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.RequestEntity; import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -31,9 +32,9 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import sun.security.x509.X509CertImpl;
import java.net.URI; import java.net.URI;
import java.security.cert.X509Certificate;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -76,15 +77,16 @@ public class WechatBatchTransferApi extends AbstractApi {
List<CreateBatchTransferParams.TransferDetailListItem> transferDetailList = createBatchTransferParams.getTransferDetailList(); List<CreateBatchTransferParams.TransferDetailListItem> transferDetailList = createBatchTransferParams.getTransferDetailList();
SignatureProvider signatureProvider = this.client().signatureProvider(); SignatureProvider signatureProvider = this.client().signatureProvider();
final X509CertImpl certificate = signatureProvider.getCertificate(); final X509WechatCertificateInfo certificate = signatureProvider.getCertificate();
List<CreateBatchTransferParams.TransferDetailListItem> encrypted = transferDetailList.stream() List<CreateBatchTransferParams.TransferDetailListItem> encrypted = transferDetailList.stream()
.peek(transferDetailListItem -> { .peek(transferDetailListItem -> {
String userName = transferDetailListItem.getUserName(); String userName = transferDetailListItem.getUserName();
String encryptedUserName = signatureProvider.encryptRequestMessage(userName, certificate); X509Certificate x509Certificate = certificate.getX509Certificate();
String encryptedUserName = signatureProvider.encryptRequestMessage(userName, x509Certificate);
transferDetailListItem.setUserName(encryptedUserName); transferDetailListItem.setUserName(encryptedUserName);
String userIdCard = transferDetailListItem.getUserIdCard(); String userIdCard = transferDetailListItem.getUserIdCard();
if (StringUtils.hasText(userIdCard)) { if (StringUtils.hasText(userIdCard)) {
String encryptedUserIdCard = signatureProvider.encryptRequestMessage(userIdCard, certificate); String encryptedUserIdCard = signatureProvider.encryptRequestMessage(userIdCard, x509Certificate);
transferDetailListItem.setUserIdCard(encryptedUserIdCard); transferDetailListItem.setUserIdCard(encryptedUserIdCard);
} }
}).collect(Collectors.toList()); }).collect(Collectors.toList());
@@ -93,7 +95,9 @@ public class WechatBatchTransferApi extends AbstractApi {
URI uri = UriComponentsBuilder.fromHttpUrl(type.uri(WeChatServer.CHINA)) URI uri = UriComponentsBuilder.fromHttpUrl(type.uri(WeChatServer.CHINA))
.build() .build()
.toUri(); .toUri();
return Post(uri, createBatchTransferParams); HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Wechatpay-Serial", certificate.getWechatPaySerial());
return Post(uri, createBatchTransferParams, httpHeaders);
} }
/** /**
@@ -249,7 +253,7 @@ public class WechatBatchTransferApi extends AbstractApi {
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();
String downloadUrl = wechatResponseEntity.getBody().get("download_url").asText(); String downloadUrl = wechatResponseEntity.getBody().get("download_url").asText();
Assert.hasText(downloadUrl,"download url has no text"); Assert.hasText(downloadUrl, "download url has no text");
return this.billResource(downloadUrl); return this.billResource(downloadUrl);
} }
} }

View File

@@ -26,6 +26,7 @@ import cn.felord.payment.wechat.v3.model.ResponseSignVerifyParams;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@@ -225,7 +226,8 @@ public class WechatPayClient {
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// for upload // for upload
if (Objects.isNull(httpHeaders.getContentType())) { if (Objects.isNull(httpHeaders.getContentType())) {
httpHeaders.setContentType(MediaType.APPLICATION_JSON); // 避免出现因为中文导致的 HttpRetryException
httpHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
} }
httpHeaders.add("Authorization", authorization); httpHeaders.add("Authorization", authorization);
httpHeaders.add("User-Agent", "X-Pay-Service"); httpHeaders.add("User-Agent", "X-Pay-Service");
@@ -336,6 +338,7 @@ public class WechatPayClient {
*/ */
private void applyDefaultRestTemplate() { private void applyDefaultRestTemplate() {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
DefaultResponseErrorHandler errorHandler = new WechatPayResponseErrorHandler(); DefaultResponseErrorHandler errorHandler = new WechatPayResponseErrorHandler();
restTemplate.setErrorHandler(errorHandler); restTemplate.setErrorHandler(errorHandler);
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters(); List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2019-2021 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;
import lombok.Data;
import java.security.cert.X509Certificate;
/**
* 微信X509证书
*
* @author felord.cn
* @since 1.0.6.RELEASE
*/
@Data
public class X509WechatCertificateInfo {
/**
* wechatPaySerial
*/
private String wechatPaySerial;
/**
* X509Certificate
*/
private X509Certificate x509Certificate;
}

View File

@@ -5,11 +5,11 @@
<parent> <parent>
<groupId>cn.felord</groupId> <groupId>cn.felord</groupId>
<artifactId>payment-spring-boot</artifactId> <artifactId>payment-spring-boot</artifactId>
<version>1.0.6.RELEASE</version> <version>1.0.7.RELEASE</version>
</parent> </parent>
<artifactId>payment-spring-boot-starter</artifactId> <artifactId>payment-spring-boot-starter</artifactId>
<version>1.0.6.RELEASE</version> <version>1.0.7.RELEASE</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>cn.felord</groupId> <groupId>cn.felord</groupId>
<artifactId>payment-spring-boot</artifactId> <artifactId>payment-spring-boot</artifactId>
<version>1.0.6.RELEASE</version> <version>1.0.7.RELEASE</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@@ -30,7 +30,7 @@
</developers> </developers>
<scm> <scm>
<tag>payment-spring-boot-1.0.6.RELEASE</tag> <tag>payment-spring-boot-1.0.7.RELEASE</tag>
<url>https://github.com/NotFound403/payment-spring-boot</url> <url>https://github.com/NotFound403/payment-spring-boot</url>
<connection>scm:git:https://github.com/NotFound403/payment-spring-boot.git</connection> <connection>scm:git:https://github.com/NotFound403/payment-spring-boot.git</connection>
<developerConnection>scm:git:https://github.com/NotFound403/payment-spring-boot.git</developerConnection> <developerConnection>scm:git:https://github.com/NotFound403/payment-spring-boot.git</developerConnection>