Commit f0d680b2b81f9c641116943ac66f969d258bfdd2
1 parent
f877a24e
【功能优化】PAY:微信支付使用 certSerialNo 替代 privateCertContent
Showing
2 changed files
with
19 additions
and
31 deletions
yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/weixin/AbstractWxPayClient.java
| @@ -36,6 +36,7 @@ import java.util.Objects; | @@ -36,6 +36,7 @@ import java.util.Objects; | ||
| 36 | 36 | ||
| 37 | import static cn.hutool.core.date.DatePattern.*; | 37 | import static cn.hutool.core.date.DatePattern.*; |
| 38 | import static cn.iocoder.yudao.framework.pay.core.client.impl.weixin.WxPayClientConfig.API_VERSION_V2; | 38 | import static cn.iocoder.yudao.framework.pay.core.client.impl.weixin.WxPayClientConfig.API_VERSION_V2; |
| 39 | +import static cn.iocoder.yudao.framework.pay.core.client.impl.weixin.WxPayClientConfig.API_VERSION_V3; | ||
| 39 | 40 | ||
| 40 | /** | 41 | /** |
| 41 | * 微信支付抽象类,实现微信统一的接口、以及部分实现(退款) | 42 | * 微信支付抽象类,实现微信统一的接口、以及部分实现(退款) |
| @@ -59,19 +60,14 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -59,19 +60,14 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 59 | protected void doInit(String tradeType) { | 60 | protected void doInit(String tradeType) { |
| 60 | // 创建 config 配置 | 61 | // 创建 config 配置 |
| 61 | WxPayConfig payConfig = new WxPayConfig(); | 62 | WxPayConfig payConfig = new WxPayConfig(); |
| 62 | - BeanUtil.copyProperties(config, payConfig, "keyContent", "privateKeyContent", "privateCertContent"); | 63 | + BeanUtil.copyProperties(config, payConfig, "keyContent", "privateKeyContent"); |
| 63 | payConfig.setTradeType(tradeType); | 64 | payConfig.setTradeType(tradeType); |
| 64 | // weixin-pay-java 无法设置内容,只允许读取文件,所以这里要创建临时文件来解决 | 65 | // weixin-pay-java 无法设置内容,只允许读取文件,所以这里要创建临时文件来解决 |
| 65 | - if (Base64.isBase64(config.getKeyContent())) { | 66 | + if (Objects.equals(config.getApiVersion(), API_VERSION_V2)) { |
| 66 | payConfig.setKeyPath(FileUtils.createTempFile(Base64.decode(config.getKeyContent())).getPath()); | 67 | payConfig.setKeyPath(FileUtils.createTempFile(Base64.decode(config.getKeyContent())).getPath()); |
| 67 | - } | ||
| 68 | - if (StrUtil.isNotEmpty(config.getPrivateKeyContent())) { | 68 | + } else if (Objects.equals(config.getApiVersion(), API_VERSION_V3)) { |
| 69 | payConfig.setPrivateKeyPath(FileUtils.createTempFile(config.getPrivateKeyContent()).getPath()); | 69 | payConfig.setPrivateKeyPath(FileUtils.createTempFile(config.getPrivateKeyContent()).getPath()); |
| 70 | } | 70 | } |
| 71 | - if (StrUtil.isNotEmpty(config.getPrivateCertContent())) { | ||
| 72 | - payConfig.setPrivateCertPath(FileUtils.createTempFile(config.getPrivateCertContent()).getPath()); | ||
| 73 | - } | ||
| 74 | -// payConfig.setCertSerialNo(); | ||
| 75 | 71 | ||
| 76 | // 创建 client 客户端 | 72 | // 创建 client 客户端 |
| 77 | client = new WxPayServiceImpl(); | 73 | client = new WxPayServiceImpl(); |
| @@ -86,7 +82,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -86,7 +82,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 86 | switch (config.getApiVersion()) { | 82 | switch (config.getApiVersion()) { |
| 87 | case API_VERSION_V2: | 83 | case API_VERSION_V2: |
| 88 | return doUnifiedOrderV2(reqDTO); | 84 | return doUnifiedOrderV2(reqDTO); |
| 89 | - case WxPayClientConfig.API_VERSION_V3: | 85 | + case API_VERSION_V3: |
| 90 | return doUnifiedOrderV3(reqDTO); | 86 | return doUnifiedOrderV3(reqDTO); |
| 91 | default: | 87 | default: |
| 92 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); | 88 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); |
| @@ -157,7 +153,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -157,7 +153,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 157 | switch (config.getApiVersion()) { | 153 | switch (config.getApiVersion()) { |
| 158 | case API_VERSION_V2: | 154 | case API_VERSION_V2: |
| 159 | return doParseOrderNotifyV2(body); | 155 | return doParseOrderNotifyV2(body); |
| 160 | - case WxPayClientConfig.API_VERSION_V3: | 156 | + case API_VERSION_V3: |
| 161 | return doParseOrderNotifyV3(body); | 157 | return doParseOrderNotifyV3(body); |
| 162 | default: | 158 | default: |
| 163 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); | 159 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); |
| @@ -192,7 +188,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -192,7 +188,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 192 | switch (config.getApiVersion()) { | 188 | switch (config.getApiVersion()) { |
| 193 | case API_VERSION_V2: | 189 | case API_VERSION_V2: |
| 194 | return doGetOrderV2(outTradeNo); | 190 | return doGetOrderV2(outTradeNo); |
| 195 | - case WxPayClientConfig.API_VERSION_V3: | 191 | + case API_VERSION_V3: |
| 196 | return doGetOrderV3(outTradeNo); | 192 | return doGetOrderV3(outTradeNo); |
| 197 | default: | 193 | default: |
| 198 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); | 194 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); |
| @@ -261,7 +257,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -261,7 +257,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 261 | switch (config.getApiVersion()) { | 257 | switch (config.getApiVersion()) { |
| 262 | case API_VERSION_V2: | 258 | case API_VERSION_V2: |
| 263 | return doUnifiedRefundV2(reqDTO); | 259 | return doUnifiedRefundV2(reqDTO); |
| 264 | - case WxPayClientConfig.API_VERSION_V3: | 260 | + case API_VERSION_V3: |
| 265 | return doUnifiedRefundV3(reqDTO); | 261 | return doUnifiedRefundV3(reqDTO); |
| 266 | default: | 262 | default: |
| 267 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); | 263 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); |
| @@ -321,7 +317,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -321,7 +317,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 321 | switch (config.getApiVersion()) { | 317 | switch (config.getApiVersion()) { |
| 322 | case API_VERSION_V2: | 318 | case API_VERSION_V2: |
| 323 | return doParseRefundNotifyV2(body); | 319 | return doParseRefundNotifyV2(body); |
| 324 | - case WxPayClientConfig.API_VERSION_V3: | 320 | + case API_VERSION_V3: |
| 325 | return parseRefundNotifyV3(body); | 321 | return parseRefundNotifyV3(body); |
| 326 | default: | 322 | default: |
| 327 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); | 323 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); |
| @@ -358,7 +354,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | @@ -358,7 +354,7 @@ public abstract class AbstractWxPayClient extends AbstractPayClient<WxPayClientC | ||
| 358 | switch (config.getApiVersion()) { | 354 | switch (config.getApiVersion()) { |
| 359 | case API_VERSION_V2: | 355 | case API_VERSION_V2: |
| 360 | return doGetRefundV2(outTradeNo, outRefundNo); | 356 | return doGetRefundV2(outTradeNo, outRefundNo); |
| 361 | - case WxPayClientConfig.API_VERSION_V3: | 357 | + case API_VERSION_V3: |
| 362 | return doGetRefundV3(outTradeNo, outRefundNo); | 358 | return doGetRefundV3(outTradeNo, outRefundNo); |
| 363 | default: | 359 | default: |
| 364 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); | 360 | throw new IllegalArgumentException(String.format("未知的 API 版本(%s)", config.getApiVersion())); |
yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/weixin/WxPayClientConfig.java
| 1 | package cn.iocoder.yudao.framework.pay.core.client.impl.weixin; | 1 | package cn.iocoder.yudao.framework.pay.core.client.impl.weixin; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.io.IoUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; | 3 | import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; |
| 5 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; | 4 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; |
| 6 | -import lombok.Data; | ||
| 7 | - | ||
| 8 | import jakarta.validation.Validator; | 5 | import jakarta.validation.Validator; |
| 9 | import jakarta.validation.constraints.NotBlank; | 6 | import jakarta.validation.constraints.NotBlank; |
| 10 | -import java.io.FileInputStream; | ||
| 11 | -import java.io.FileNotFoundException; | 7 | +import lombok.Data; |
| 12 | 8 | ||
| 13 | /** | 9 | /** |
| 14 | * 微信支付的 PayClientConfig 实现类 | 10 | * 微信支付的 PayClientConfig 实现类 |
| @@ -72,15 +68,18 @@ public class WxPayClientConfig implements PayClientConfig { | @@ -72,15 +68,18 @@ public class WxPayClientConfig implements PayClientConfig { | ||
| 72 | @NotBlank(message = "apiclient_key 不能为空", groups = V3.class) | 68 | @NotBlank(message = "apiclient_key 不能为空", groups = V3.class) |
| 73 | private String privateKeyContent; | 69 | private String privateKeyContent; |
| 74 | /** | 70 | /** |
| 75 | - * apiclient_cert.pem 证书文件的对应的字符串 | ||
| 76 | - */ | ||
| 77 | - @NotBlank(message = "apiclient_cert 不能为空", groups = V3.class) | ||
| 78 | - private String privateCertContent; | ||
| 79 | - /** | ||
| 80 | * apiV3 密钥值 | 71 | * apiV3 密钥值 |
| 81 | */ | 72 | */ |
| 82 | @NotBlank(message = "apiV3 密钥值不能为空", groups = V3.class) | 73 | @NotBlank(message = "apiV3 密钥值不能为空", groups = V3.class) |
| 83 | private String apiV3Key; | 74 | private String apiV3Key; |
| 75 | + /** | ||
| 76 | + * 证书序列号 | ||
| 77 | + */ | ||
| 78 | + @NotBlank(message = "证书序列号不能为空", groups = V3.class) | ||
| 79 | + private String certSerialNo; | ||
| 80 | + | ||
| 81 | + @Deprecated // TODO 芋艿:V2.3.0 进行移除 | ||
| 82 | + private String privateCertContent; | ||
| 84 | 83 | ||
| 85 | /** | 84 | /** |
| 86 | * 分组校验 v2版本 | 85 | * 分组校验 v2版本 |
| @@ -100,11 +99,4 @@ public class WxPayClientConfig implements PayClientConfig { | @@ -100,11 +99,4 @@ public class WxPayClientConfig implements PayClientConfig { | ||
| 100 | API_VERSION_V2.equals(this.getApiVersion()) ? V2.class : V3.class); | 99 | API_VERSION_V2.equals(this.getApiVersion()) ? V2.class : V3.class); |
| 101 | } | 100 | } |
| 102 | 101 | ||
| 103 | - public static void main(String[] args) throws FileNotFoundException { | ||
| 104 | - String path = "/Users/yunai/Downloads/wx_pay/apiclient_cert.p12"; | ||
| 105 | - /// String path = "/Users/yunai/Downloads/wx_pay/apiclient_key.pem"; | ||
| 106 | - /// String path = "/Users/yunai/Downloads/wx_pay/apiclient_cert.pem"; | ||
| 107 | - System.out.println(IoUtil.readUtf8(new FileInputStream(path))); | ||
| 108 | - } | ||
| 109 | - | ||
| 110 | } | 102 | } |