From 55440b1275e573281992dfc8202fed2cb9c79634 Mon Sep 17 00:00:00 2001 From: Fang Date: Wed, 8 Feb 2023 23:15:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=9B=B4=E8=BF=9E=E5=95=86=E6=88=B7-?= =?UTF-8?q?=E5=A7=94=E6=89=98=E8=90=A5=E9=94=80=E7=9B=B8=E5=85=B3API?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wechat/enumeration/BusinessType.java | 35 +++++ .../wechat/enumeration/PartnerType.java | 35 +++++ .../wechat/enumeration/PartnershipState.java | 35 +++++ .../wechat/enumeration/WechatPayV3Type.java | 14 ++ .../payment/wechat/v3/WechatApiProvider.java | 43 +++--- .../v3/WechatMarketingPartnershipApi.java | 127 ++++++++++++++++++ .../v3/model/partnership/AuthorizedData.java | 31 +++++ .../wechat/v3/model/partnership/Partner.java | 67 +++++++++ .../model/partnership/PartnershipParams.java | 43 ++++++ .../partnership/PartnershipQueryParams.java | 40 ++++++ 10 files changed, 453 insertions(+), 17 deletions(-) create mode 100644 payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/BusinessType.java create mode 100644 payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/PartnerType.java create mode 100644 payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/PartnershipState.java create mode 100644 payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatMarketingPartnershipApi.java create mode 100644 payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/AuthorizedData.java create mode 100644 payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/Partner.java create mode 100644 payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/PartnershipParams.java create mode 100644 payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/PartnershipQueryParams.java diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/BusinessType.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/BusinessType.java new file mode 100644 index 0000000..5548a50 --- /dev/null +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/BusinessType.java @@ -0,0 +1,35 @@ +/* + * 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.enumeration; + +/** + * The enum Business type. + * + * @author felord.cn + * @since 1.0.16.RELEASE + */ +public enum BusinessType { + /** + * 代金券批次(暂不支持合作方为商户的场景) + */ + FAVOR_STOCK, + /** + * 商家券批次 + */ + BUSIFAVOR_STOCK +} diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/PartnerType.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/PartnerType.java new file mode 100644 index 0000000..e163872 --- /dev/null +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/PartnerType.java @@ -0,0 +1,35 @@ +/* + * 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.enumeration; + +/** + * The enum Partner type. + * + * @author felord.cn + * @since 1.0.16.RELEASE + */ +public enum PartnerType { + /** + * 合作方为APPID + */ + APPID, + /** + * 合作方为商户 + */ + MERCHANT +} diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/PartnershipState.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/PartnershipState.java new file mode 100644 index 0000000..613c595 --- /dev/null +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/enumeration/PartnershipState.java @@ -0,0 +1,35 @@ +/* + * 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.enumeration; + +/** + * 合作状态 + * + * @author felord.cn + * @since 1.0.16.RELEASE + */ +public enum PartnershipState { + /** + * 已建立合作状态 + */ + ESTABLISHED, + /** + * 已终止合作状态 + */ + TERMINATED +} 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 bed36c7..2dd3496 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 @@ -493,6 +493,20 @@ public enum WechatPayV3Type { * @since 1.0.13.RELEASES */ MARKETING_BUSI_FAVOR_SUBSIDY_QUERY(HttpMethod.GET, "%s/v3/marketing/busifavor/subsidy/pay-receipts/{subsidy_receipt_id}"), + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + /** + * 建立合作关系API. + * + * @since 1.0.16.RELEASES + */ + MARKETING_PARTNERSHIPS_BUILD(HttpMethod.POST, "%s/v3/marketing/partnerships/build"), + /** + * 查询合作关系列表API. + * + * @since 1.0.16.RELEASES + */ + MARKETING_PARTNERSHIPS_GET(HttpMethod.GET, "%s/v3/marketing/partnerships"), + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * 发起批量转账API. diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatApiProvider.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatApiProvider.java index 5e3ad3b..9989623 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatApiProvider.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatApiProvider.java @@ -42,17 +42,6 @@ public class WechatApiProvider { this.wechatPayClient = wechatPayClient; } - /** - * 代金券. - * - * @param tenantId the tenant id - * @return the wechat marketing favor api - * @since 1.0.0.RELEASE - */ - public WechatMarketingFavorApi favorApi(String tenantId) { - return new WechatMarketingFavorApi(this.wechatPayClient, tenantId); - } - /** * 普通支付-直连模式. * @@ -64,7 +53,6 @@ public class WechatApiProvider { return new WechatDirectPayApi(wechatPayClient, tenantId); } - /** * 普通支付-服务商模式. * @@ -120,6 +108,17 @@ public class WechatApiProvider { return new WechatDiscountCardApi(wechatPayClient, tenantId); } + /** + * 代金券. + * + * @param tenantId the tenant id + * @return the wechat marketing favor api + * @since 1.0.0.RELEASE + */ + public WechatMarketingFavorApi favorApi(String tenantId) { + return new WechatMarketingFavorApi(this.wechatPayClient, tenantId); + } + /** * 微信支付商家券. * @@ -131,6 +130,16 @@ public class WechatApiProvider { return new WechatMarketingBusiFavorApi(wechatPayClient, tenantId); } + /** + * 委托营销-直连商户 + * + * @param tenantId the tenant id + * @return the wechat marketing partnership api + */ + public WechatMarketingPartnershipApi marketingshipApi(String tenantId) { + return new WechatMarketingPartnershipApi(this.wechatPayClient, tenantId); + } + /** * 批量转账到零钱. *

@@ -198,7 +207,7 @@ public class WechatApiProvider { } /** - * 服务商微信支付分账,基于V3 + * 服务商-微信支付分账,基于V3 * * @param tenantId the tenant id * @return the wechat partner profitsharing api @@ -208,7 +217,7 @@ public class WechatApiProvider { } /** - * 服务商品牌分账 + * 服务商-品牌分账 * * @param tenantId the tenant id * @return the wechat brand profitsharing api @@ -262,7 +271,7 @@ public class WechatApiProvider { } /** - * 服务商智慧商圈 + * 服务商-智慧商圈 * * @param tenantId the tenant id * @return the wechat business circle api @@ -273,7 +282,7 @@ public class WechatApiProvider { } /** - * 直连商户智慧商圈 + * 直连商户-智慧商圈 * * @param tenantId the tenant id * @return the wechat business circle api @@ -311,7 +320,7 @@ public class WechatApiProvider { * @param tenantId the tenant id * @return the wechat complaints api */ - public WechatComplaintsApi complaintsApi(String tenantId){ + public WechatComplaintsApi complaintsApi(String tenantId) { return new WechatComplaintsApi(wechatPayClient, tenantId); } } diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatMarketingPartnershipApi.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatMarketingPartnershipApi.java new file mode 100644 index 0000000..28acbc8 --- /dev/null +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatMarketingPartnershipApi.java @@ -0,0 +1,127 @@ +/* + * Copyright 2019-2023 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 cn.felord.payment.PayException; +import cn.felord.payment.wechat.enumeration.WeChatServer; +import cn.felord.payment.wechat.enumeration.WechatPayV3Type; +import cn.felord.payment.wechat.v3.model.partnership.Partner; +import cn.felord.payment.wechat.v3.model.partnership.PartnershipParams; +import cn.felord.payment.wechat.v3.model.partnership.PartnershipQueryParams; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.springframework.http.HttpHeaders; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.util.UriComponentsBuilder; +import org.springframework.web.util.UriUtils; + +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.Objects; + +/** + * 委托营销-直连商户 + * + * @author felord.cn + * @since 1.0.16.RELEASE + */ +public class WechatMarketingPartnershipApi extends AbstractApi { + /** + * Instantiates a new Abstract api. + * + * @param wechatPayClient the wechat pay client + * @param tenantId the tenant id + */ + public WechatMarketingPartnershipApi(WechatPayClient wechatPayClient, String tenantId) { + super(wechatPayClient, tenantId); + } + + /** + * 建立合作关系API + *

+ * 该接口主要为商户提供营销资源的授权能力,可授权给其他商户或小程序,方便商户间的互利合作。 + * + * @param params the params + * @return the wechat response entity + */ + public WechatResponseEntity build(PartnershipParams params) { + WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); + this.client().withType(WechatPayV3Type.MARKETING_PARTNERSHIPS_BUILD, params) + .function((type, partnershipParams) -> { + URI uri = UriComponentsBuilder.fromHttpUrl(type.uri(WeChatServer.CHINA)) + .build() + .toUri(); + String idempotencyKey = partnershipParams.getIdempotencyKey(); + partnershipParams.clearKey(); + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("Idempotency-Key", idempotencyKey); + return Post(uri, partnershipParams, httpHeaders); + }) + .consumer(wechatResponseEntity::convert) + .request(); + return wechatResponseEntity; + } + + /** + * 查询合作关系列表API + *

+ * 该接口主要为商户提供合作关系列表的查询能力。 + * + * @param params the params + * @return the wechat response entity + */ + public WechatResponseEntity query(PartnershipQueryParams params) { + WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); + this.client().withType(WechatPayV3Type.MARKETING_PARTNERSHIPS_GET, params) + .function((type, partnershipParams) -> { + MultiValueMap queryParams = new LinkedMultiValueMap<>(); + Partner partner = params.getPartner(); + + try { + ObjectMapper mapper = this.getMapper(); + if (Objects.nonNull(partner)) { + String partnerJson = mapper.writeValueAsString(partner); + queryParams.add("partner", UriUtils.encode(partnerJson, StandardCharsets.UTF_8)); + } + String authorizedDataJson = mapper.writeValueAsString(params.getAuthorizedData()); + + queryParams.add("authorized_data", UriUtils.encode(authorizedDataJson, StandardCharsets.UTF_8)); + } catch (JsonProcessingException e) { + throw new PayException(e); + } + Integer limit = params.getLimit(); + if (Objects.nonNull(limit)) { + queryParams.add("limit", limit.toString()); + } + Integer offset = params.getOffset(); + if (Objects.nonNull(offset)) { + queryParams.add("offset", offset.toString()); + } + URI uri = UriComponentsBuilder.fromHttpUrl(type.uri(WeChatServer.CHINA)) + .queryParams(queryParams) + .build(false) + .toUri(); + return Get(uri); + }) + .consumer(wechatResponseEntity::convert) + .request(); + return wechatResponseEntity; + } +} diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/AuthorizedData.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/AuthorizedData.java new file mode 100644 index 0000000..1dd7db3 --- /dev/null +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/AuthorizedData.java @@ -0,0 +1,31 @@ +/* + * 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.model.partnership; + +import cn.felord.payment.wechat.enumeration.BusinessType; +import lombok.Data; + +/** + * @author felord.cn + * @since 1.0.16.RELEASE + */ +@Data +public class AuthorizedData { + private final BusinessType businessType; + private final String stockId; +} diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/Partner.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/Partner.java new file mode 100644 index 0000000..537e883 --- /dev/null +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/Partner.java @@ -0,0 +1,67 @@ +/* + * 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.model.partnership; + +import cn.felord.payment.wechat.enumeration.PartnerType; +import lombok.Getter; + +/** + * The type Partner. + * + * @author felord.cn + * @since 1.0.16.RELEASE + */ +@Getter +public class Partner { + private final PartnerType type; + private final String appid; + private final String merchantId; + + /** + * Instantiates a new Partner. + * + * @param type the type + * @param appid the appid + * @param merchantId the merchant id + */ + Partner(PartnerType type, String appid, String merchantId) { + this.type = type; + this.appid = appid; + this.merchantId = merchantId; + } + + /** + * 建立APP合作 + * + * @param appid the appid + * @return the partner + */ + public static Partner app(String appid) { + return new Partner(PartnerType.APPID, appid, null); + } + + /** + * 建立商户合作 + * + * @param merchantId the merchant id + * @return the partner + */ + public static Partner merchant(String merchantId) { + return new Partner(PartnerType.MERCHANT, null, merchantId); + } +} diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/PartnershipParams.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/PartnershipParams.java new file mode 100644 index 0000000..fd33cbb --- /dev/null +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/PartnershipParams.java @@ -0,0 +1,43 @@ +/* + * 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.model.partnership; + +import lombok.Getter; +import lombok.ToString; + +/** + * @author felord.cn + * @since 1.0.16.RELEASE + */ +@ToString +@Getter +public class PartnershipParams { + private String idempotencyKey; + private final Partner partner; + private final AuthorizedData authorizedData; + + public PartnershipParams(String idempotencyKey, Partner partner, AuthorizedData authorizedData) { + this.idempotencyKey = idempotencyKey; + this.partner = partner; + this.authorizedData = authorizedData; + } + + public void clearKey() { + this.idempotencyKey = null; + } +} diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/PartnershipQueryParams.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/PartnershipQueryParams.java new file mode 100644 index 0000000..8670a37 --- /dev/null +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/model/partnership/PartnershipQueryParams.java @@ -0,0 +1,40 @@ +/* + * 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.model.partnership; + +import lombok.Data; + +/** + * @author felord.cn + * @since 1.0.16.RELEASE + */ +@Data +public class PartnershipQueryParams { + + private final Partner partner; + private final AuthorizedData authorizedData; + private Integer limit; + private Integer offset; + + public PartnershipQueryParams(Partner partner, AuthorizedData authorizedData) { + + this.partner = partner; + this.authorizedData = authorizedData; + } + +}