feat: 直连商户-委托营销相关API实现

This commit is contained in:
Fang
2023-02-08 23:15:48 +08:00
parent 59be1e96ad
commit 55440b1275
10 changed files with 453 additions and 17 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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.

View File

@@ -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);
}
/**
* 批量转账到零钱.
* <p>
@@ -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);
}
}

View File

@@ -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
* <p>
* 该接口主要为商户提供营销资源的授权能力,可授权给其他商户或小程序,方便商户间的互利合作。
*
* @param params the params
* @return the wechat response entity
*/
public WechatResponseEntity<ObjectNode> build(PartnershipParams params) {
WechatResponseEntity<ObjectNode> 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
* <p>
* 该接口主要为商户提供合作关系列表的查询能力。
*
* @param params the params
* @return the wechat response entity
*/
public WechatResponseEntity<ObjectNode> query(PartnershipQueryParams params) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
this.client().withType(WechatPayV3Type.MARKETING_PARTNERSHIPS_GET, params)
.function((type, partnershipParams) -> {
MultiValueMap<String, String> 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;
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}