|
@@ -0,0 +1,108 @@
|
|
|
+package com.kdan.compdf.client;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import cn.kdan.compdf.exception.CommonException;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.kdan.compdf.dto.SubscriptionPlansDTO;
|
|
|
+import com.kdan.compdf.resultmap.PaddleResultMap;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.boot.web.client.RestTemplateBuilder;
|
|
|
+import org.springframework.core.ParameterizedTypeReference;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.http.client.ClientHttpRequestExecution;
|
|
|
+import org.springframework.http.client.ClientHttpRequestInterceptor;
|
|
|
+import org.springframework.http.client.ClientHttpResponse;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.time.Duration;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author ComPDFKit-WPH 2022/11/8
|
|
|
+ * <p>
|
|
|
+ * Paddle 客服端
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class PaddleClient {
|
|
|
+ /**
|
|
|
+ * 请求地址
|
|
|
+ */
|
|
|
+ private final String address;
|
|
|
+ /**
|
|
|
+ * vendor_id
|
|
|
+ */
|
|
|
+ private final String vendorId;
|
|
|
+ /**
|
|
|
+ * vendor_auth_code
|
|
|
+ */
|
|
|
+ private final String vendorAuthCode;
|
|
|
+ private final RestTemplate restTemplate;
|
|
|
+
|
|
|
+ public PaddleClient(String address, String vendorId, String vendorAuthCode) {
|
|
|
+ if (!address.endsWith("/")) {
|
|
|
+ address += "/";
|
|
|
+ }
|
|
|
+ this.address = address;
|
|
|
+ this.restTemplate = new RestTemplateBuilder()
|
|
|
+ .build();
|
|
|
+ this.vendorId = vendorId;
|
|
|
+ this.vendorAuthCode = vendorAuthCode;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询出订阅计划列表
|
|
|
+ *
|
|
|
+ * @param planId 订阅计划id,为空则查询所有订阅计划
|
|
|
+ * @return 订阅计划列表
|
|
|
+ */
|
|
|
+ public List<SubscriptionPlansDTO> getSubscriptionPlans(Integer planId) {
|
|
|
+ String url = address + "subscription/plans";
|
|
|
+ HttpHeaders headers = getHeaders();
|
|
|
+ MultiValueMap<String, String> paramMap = getParamMap();
|
|
|
+ if (planId != null) {
|
|
|
+ paramMap.add("plan", planId.toString());
|
|
|
+ }
|
|
|
+ ResponseEntity<Map> exchange = restTemplate.exchange(
|
|
|
+ url,
|
|
|
+ HttpMethod.POST,
|
|
|
+ new HttpEntity<>(paramMap, headers),
|
|
|
+ Map.class);
|
|
|
+ Map<String, Object> result = exchange.getBody();
|
|
|
+ resultIsTrue(result);
|
|
|
+
|
|
|
+ List<SubscriptionPlansDTO> data = JSONUtil.parseArray(exchange.getBody().get("response")).toList(SubscriptionPlansDTO.class);
|
|
|
+ return Objects.requireNonNull(data);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private HttpHeaders getHeaders() {
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ private MultiValueMap<String, String> getParamMap() {
|
|
|
+ MultiValueMap<String, String> param = new LinkedMultiValueMap<String, String>();
|
|
|
+ param.add("vendor_id", "159972");
|
|
|
+ param.add("vendor_auth_code", "7ce557336b096a1bb5719fcb92e8122236f0522b1cde4e8b1d");
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断返回对象是否为true
|
|
|
+ *
|
|
|
+ * @param result 返回值
|
|
|
+ */
|
|
|
+ private void resultIsTrue(Map<String, Object> result) {
|
|
|
+ if (Boolean.FALSE.equals(result.get("success"))){
|
|
|
+ throw new CommonException("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|