Skip to content

Commit

Permalink
bump version to 0.2.6 (#139)
Browse files Browse the repository at this point in the history
* Update README.md

* docs: 补充 JsapiServiceExtension/AppServiceExtension, resolve #137

* bump version to 0.2.6

* feat: 增加 `getSha1HexString(byte[])`, resolve #126

* test: 补充测试用例
  • Loading branch information
xy-peng authored Feb 24, 2023
1 parent 49c5db5 commit cb5821c
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 40 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![JavaDoc](http://img.shields.io/badge/javadoc-reference-blue.svg)](https://www.javadoc.io/doc/com.github.wechatpay-apiv3/wechatpay-java/latest/index.html)
![Maven Central](https://img.shields.io/maven-central/v/com.github.wechatpay-apiv3/wechatpay-java?versionPrefix=0.2.5)
![Maven Central](https://img.shields.io/maven-central/v/com.github.wechatpay-apiv3/wechatpay-java?versionPrefix=0.2.6)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=wechatpay-apiv3_wechatpay-java&metric=security_rating)](https://sonarcloud.io/summary/overall?id=wechatpay-apiv3_wechatpay-java)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=wechatpay-apiv3_wechatpay-java&metric=sqale_rating)](https://sonarcloud.io/summary/overall?id=wechatpay-apiv3_wechatpay-java)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=wechatpay-apiv3_wechatpay-java&metric=coverage)](https://sonarcloud.io/summary/overall?id=wechatpay-apiv3_wechatpay-java)
Expand Down Expand Up @@ -36,7 +36,7 @@
在你的 build.gradle 文件中加入如下的依赖

```groovy
implementation 'com.github.wechatpay-apiv3:wechatpay-java:0.2.5'
implementation 'com.github.wechatpay-apiv3:wechatpay-java:0.2.6'
```

#### Maven
Expand All @@ -47,7 +47,7 @@ implementation 'com.github.wechatpay-apiv3:wechatpay-java:0.2.5'
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.5</version>
<version>0.2.6</version>
</dependency>
```

Expand Down Expand Up @@ -234,11 +234,11 @@ Config config =

```java
// 构造 RequestParam
RequestParam requestParam = new Builder()
RequestParam requestParam = new RequestParam.Builder()
.serialNumber(wechatPayCertificateSerialNumber)
.nonce(nonce)
.signature(signature)
.timestamp(timstamp)
.timestamp(timestamp)
// 若未设置signType,默认值为 WECHATPAY2-SHA256-RSA2048
.signType(signType)
.body(requestBody)
Expand Down
69 changes: 43 additions & 26 deletions core/src/main/java/com/wechat/pay/java/core/util/ShaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,53 @@ private ShaUtil() {}
* @throws IOException 读取输入流失字节、关闭流失败等
*/
public static String getSha1HexString(InputStream inputStream) throws IOException {
try {
return getShaHexString(inputStream, SHA1);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return getShaHexString(inputStream, SHA1);
}

/**
* 生成SHA1的HEX编码消息摘要字符串
*
* @param source 消息输入
* @return HEX编码消息摘要字符串
*/
public static String getSha1HexString(byte[] source) {
return getShaHexString(source, SHA1);
}

/**
* 生成SHA256的HEX编码消息摘要字符串
*
* @param inputStream 消息输入流
* @return HEX编码消息摘要字符串
* @throws IOException 读取输入流失字节、关闭流失败等
*/
public static String getSha256HexString(InputStream inputStream) throws IOException {
try {
return getShaHexString(inputStream, SHA256);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return getShaHexString(inputStream, SHA256);
}

/**
* 生成SHA256的HEX编码消息摘要字符串
*
* @param source 消息输入
* @return HEX编码消息摘要字符串
*/
public static String getSha256HexString(byte[] source) {
try {
return getShaHexString(source, SHA256);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return getShaHexString(source, SHA256);
}

private static String getShaHexString(InputStream inputStream, String algorithm)
throws NoSuchAlgorithmException, IOException {
MessageDigest digest = MessageDigest.getInstance(algorithm);
throws IOException {
byte[] data = new byte[BUFFER_SIZE];
int nRead;
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
digest.update(data, 0, nRead);
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
digest.update(data, 0, nRead);
}
return toHexString(digest.digest());
} catch (NoSuchAlgorithmException e) {
throw new SecurityException(e);
}
return toHexString(digest.digest());
}

/**
Expand All @@ -64,14 +79,16 @@ private static String getShaHexString(InputStream inputStream, String algorithm)
* @param source 消息字节数组
* @param algorithm 具体的SHA算法,例如SHA-1、SHA-256
* @return HEX编码消息摘要字符串
* @throws NoSuchAlgorithmException 没有对应的SHA算法
*/
private static String getShaHexString(byte[] source, String algorithm)
throws NoSuchAlgorithmException {
private static String getShaHexString(byte[] source, String algorithm) {
requireNonNull(source);
MessageDigest digest = MessageDigest.getInstance(algorithm);
digest.update(source);
return toHexString(digest.digest());
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
digest.update(source);
return toHexString(digest.digest());
} catch (NoSuchAlgorithmException e) {
throw new SecurityException(e);
}
}

/**
Expand Down
39 changes: 36 additions & 3 deletions core/src/test/java/com/wechat/pay/java/core/util/ShaUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,67 @@
package com.wechat.pay.java.core.util;

import static org.mockito.ArgumentMatchers.anyString;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

public class ShaUtilTest {

private static final String MESSAGE = "message";
private static final String SHA1 = "6f9b9af3cd6e8b8a73c2cdced37fe9f59226e27d";
private static final String SHA256 =
"ab530a13e45914982b79f9b7e3fba994cfd1f3fb22f71cea1afbf02b460c6d1d";

@Test
public void testInputStreamGetSha1HexString() throws IOException {
ByteArrayInputStream inputStream =
new ByteArrayInputStream(MESSAGE.getBytes(StandardCharsets.UTF_8));
String sha = ShaUtil.getSha1HexString(inputStream);
Assert.assertNotNull(sha);
Assert.assertEquals(SHA1, sha);
}

@Test
public void testInputStreamGetSha256HexString() throws IOException {
ByteArrayInputStream inputStream =
new ByteArrayInputStream(MESSAGE.getBytes(StandardCharsets.UTF_8));
String sha = ShaUtil.getSha256HexString(inputStream);
Assert.assertNotNull(sha);
Assert.assertEquals(SHA256, sha);
}

@Test
public void testBytesGetSha256HexString() {
String sha = ShaUtil.getSha256HexString(MESSAGE.getBytes(StandardCharsets.UTF_8));
Assert.assertNotNull(sha);
Assert.assertEquals(SHA256, sha);
}

@Test
public void testBytesGetSha1HexString() {
String sha = ShaUtil.getSha1HexString(MESSAGE.getBytes(StandardCharsets.UTF_8));
Assert.assertEquals(SHA1, sha);
}

@Test
public void testNoSuchAlgorithmException() {
try (MockedStatic<MessageDigest> mockDigest = Mockito.mockStatic(MessageDigest.class)) {
mockDigest
.when(() -> MessageDigest.getInstance(anyString()))
.thenThrow(new NoSuchAlgorithmException());

Assert.assertThrows(
SecurityException.class,
() -> ShaUtil.getSha1HexString("test".getBytes(StandardCharsets.UTF_8)));
Assert.assertThrows(
SecurityException.class,
() ->
ShaUtil.getSha1HexString(
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8))));
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
projectPropGroup=com.github.wechatpay-apiv3
projectPropVersion=0.2.5
projectPropVersion=0.2.6

slf4jVersion=1.7.36
junitVersion=4.13.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* APP 支付的扩展类。
*
* <p>它封装了 AppService,并提供了一个增强的 APP 下单方法 prepayWithRequestPayment。
*/
public class AppServiceExtension {
private final Signer signer;
private final AppService appService;
Expand All @@ -36,11 +41,14 @@ private AppServiceExtension(Config config, HttpClient httpClient, HostName hostN
this.appService = builder.build();
}
/**
* APP支付下单,并返回APP调起支付数据
* APP 支付下单,并返回 APP 调起支付数据。推荐使用!
*
* <p>请求成功后,该方法返回预支付交易会话标识 prepay_id 和客户端 APP 调起支付所需参数。 它相比 AppService.prepay
* 更简单易用,因为无需开发者自行计算调起支付签名。
*
* @param request 请求参数
* @param requestPaymentAppid 商户申请的公众号对应的appid
* @return PrepayResponse
* @return PrepayWithRequestPaymentResponse
* @throws HttpException 发送HTTP请求失败。例如构建请求参数失败、发送请求失败、I/O错误等。包含请求信息。
* @throws ValidationException 发送HTTP请求成功,验证微信支付返回签名失败。
* @throws ServiceException 发送HTTP请求成功,服务返回异常。例如返回状态码小于200或大于等于300。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* JSAPI 支付的扩展类。
*
* <p>它封装了 JsapiService,并提供了一个增强的 JSAPI 下单方法 prepayWithRequestPayment。
*/
public class JsapiServiceExtension {
private final Signer signer;
private final String signType;
Expand All @@ -40,7 +45,10 @@ private JsapiServiceExtension(
}

/**
* JSAPI支付下单,并返回JSAPI调起支付数据
* JSAPI 支付下单,并返回 JSAPI 调起支付数据。推荐使用!
*
* <p>请求成功后,该方法返回预支付交易会话标识 prepay_id 和客户端 JSAPI 调起支付所需参数。 它相比 JsapiService.prepay
* 更简单易用,因为无需开发者自行计算调起支付签名。
*
* @param request 请求参数 商户申请的公众号对应的appid
* @param requestPaymentAppid 商户申请的公众号对应的appid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* APP 支付的扩展类。
*
* <p>它封装了 AppService,并提供了一个增强的 APP 下单方法 prepayWithRequestPayment。
*/
public class AppServiceExtension {
private final Signer signer;
private final AppService appService;
Expand All @@ -35,8 +40,12 @@ private AppServiceExtension(Config config, HttpClient httpClient, HostName hostN
}
this.appService = builder.build();
}

/**
* APP支付预下单,并返回APP调起支付数据
* APP 支付下单,并返回 APP 调起支付数据。推荐使用!
*
* <p>请求成功后,该方法返回预支付交易会话标识 prepay_id 和客户端 APP 调起支付所需参数。 它相比 AppService.prepay
* 更简单易用,因为无需开发者自行计算调起支付签名。
*
* @param request 请求参数
* @return PrepayWithRequestPaymentResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* JSAPI 支付的扩展类。
*
* <p>它封装了 JsapiService,并提供了一个增强的 JSAPI 下单方法 prepayWithRequestPayment。
*/
public class JsapiServiceExtension {
private final Signer signer;
private final String signType;
Expand All @@ -39,7 +44,10 @@ private JsapiServiceExtension(
this.jsapiService = builder.build();
}
/**
* JSAPI支付下单,并返回JSAPI调起支付数据
* JSAPI 支付下单,并返回 JSAPI 调起支付数据。推荐使用!
*
* <p>请求成功后,该方法返回预支付交易会话标识 prepay_id 和客户端 JSAPI 调起支付所需参数。 它相比 JsApiService.prepay
* 更简单易用,因为无需开发者自行计算调起支付签名。
*
* @param request 请求参数
* @return PrepayWithRequestPaymentResponse
Expand Down

0 comments on commit cb5821c

Please sign in to comment.