From 7be5971ae31323fc9b9859335d1dff55db3d199f Mon Sep 17 00:00:00 2001 From: xucun Date: Wed, 11 Jun 2025 18:05:54 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=A2=9E=E5=8A=A0=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=95=86=E6=88=B7=E6=A8=A1=E5=BC=8F=E4=B8=8BV3?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=9A=84=E4=BB=98=E6=AC=BE=E7=A0=81=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E4=B8=8E=E5=AF=B9=E5=BA=94=E7=9A=84=E6=92=A4=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wechat/enumeration/WechatPayV3Type.java | 15 +++++ .../payment/wechat/v3/WechatDirectPayApi.java | 64 ++++++++++++++++--- .../felord/payment/wechat/v3/model/Payer.java | 2 + 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/WechatPayV3Type.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/WechatPayV3Type.java index c62871d..2195a86 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/WechatPayV3Type.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/WechatPayV3Type.java @@ -66,6 +66,13 @@ public enum WechatPayV3Type { MERCHANT_MEDIA_VIDEO(HttpMethod.POST, "%s/v3/merchant/media/video_upload"), //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + /** + * 付款码支付 + * + * @since 1.0.0.RELEASE + */ + CODE(HttpMethod.POST, "%s/v3/pay/transactions/codepay"), + /** * 微信公众号支付或者小程序支付. * @@ -99,6 +106,13 @@ public enum WechatPayV3Type { * @since 1.0.0.RELEASE */ CLOSE(HttpMethod.POST, "%s/v3/pay/transactions/out-trade-no/{out_trade_no}/close"), + /** + * 关闭订单. + * + * @since 1.0.0.RELEASE + */ + REVERSE(HttpMethod.POST, "%s/v3/pay/transactions/out-trade-no/{out_trade_no}/reverse"), + /** * 微信支付订单号查询API. * @@ -633,6 +647,7 @@ public enum WechatPayV3Type { //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + /** * 服务商APP下单API. * diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatDirectPayApi.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatDirectPayApi.java index 3814f6a..fb53076 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatDirectPayApi.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatDirectPayApi.java @@ -56,12 +56,29 @@ public class WechatDirectPayApi extends AbstractApi { super(wechatPayClient, tenantId); } - /** - * APP下单API - * - * @param payParams the pay params - * @return the wechat response entity - */ + public WechatResponseEntity codePay(PayParams payParams) { + WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); + this.client().withType(WechatPayV3Type.CODE, payParams) + .function(this::payFunction) + .consumer(responseEntity -> { + ObjectNode body = responseEntity.getBody(); + if (Objects.isNull(body)) { + throw new PayException("response body cannot be resolved"); + } + wechatResponseEntity.setHttpStatus(responseEntity.getStatusCodeValue()); + wechatResponseEntity.setBody(body); + }) + .request(); + return wechatResponseEntity; + } + + + /** + * APP下单API + * + * @param payParams the pay params + * @return the wechat response entity + */ public WechatResponseEntity appPay(PayParams payParams) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); this.client().withType(WechatPayV3Type.APP, payParams) @@ -185,8 +202,10 @@ public class WechatDirectPayApi extends AbstractApi { WechatPayProperties.V3 v3 = this.wechatMetaBean().getV3(); payParams.setAppid(v3.getAppId()); payParams.setMchid(v3.getMchId()); - String notifyUrl = v3.getDomain().concat(payParams.getNotifyUrl()); - payParams.setNotifyUrl(notifyUrl); + if (!type.equals(WechatPayV3Type.CODE)){ + String notifyUrl = v3.getDomain().concat(payParams.getNotifyUrl()); + payParams.setNotifyUrl(notifyUrl); + } URI uri = UriComponentsBuilder.fromHttpUrl(type.uri(WeChatServer.CHINA)) .build() .toUri(); @@ -252,6 +271,21 @@ public class WechatDirectPayApi extends AbstractApi { return wechatResponseEntity; } + /** + * 撤销API + * + * @param outTradeNo the out trade no + * @return the wechat response entity + */ + public WechatResponseEntity reverse(String outTradeNo) { + WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); + this.client().withType(WechatPayV3Type.REVERSE, outTradeNo) + .function(this::reverseOutTradeNoFunction) + .consumer(wechatResponseEntity::convert) + .request(); + return wechatResponseEntity; + } + private RequestEntity closeByOutTradeNoFunction(WechatPayV3Type type, String outTradeNo) { WechatPayProperties.V3 v3 = this.wechatMetaBean().getV3(); @@ -265,6 +299,20 @@ public class WechatDirectPayApi extends AbstractApi { return Post(uri, queryParams); } + private RequestEntity reverseOutTradeNoFunction(WechatPayV3Type type, String outTradeNo) { + WechatPayProperties.V3 v3 = this.wechatMetaBean().getV3(); + + Map queryParams = new HashMap<>(1); + queryParams.put("mchid", v3.getMchId()); + queryParams.put("appid", v3.getAppId()); + + URI uri = UriComponentsBuilder.fromHttpUrl(type.uri(WeChatServer.CHINA)) + .build() + .expand(outTradeNo) + .toUri(); + return Post(uri, queryParams); + } + /** * 申请退款API * diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/Payer.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/Payer.java index 1b29fc4..b928056 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/Payer.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/Payer.java @@ -38,4 +38,6 @@ public class Payer { * 用户子标识 */ private String subOpenid; + + private String authCode; }