|
|
@@ -0,0 +1,104 @@
|
|
|
+package com.izouma.nineth.service;
|
|
|
+
|
|
|
+import com.alibaba.fastjson15.JSON;
|
|
|
+import com.alibaba.fastjson15.JSONObject;
|
|
|
+import com.alibaba.fastjson15.parser.Feature;
|
|
|
+import com.izouma.nineth.config.RedisKeys;
|
|
|
+import com.izouma.nineth.dto.BindCardRequest;
|
|
|
+import com.izouma.nineth.exception.BusinessException;
|
|
|
+import com.upay.sdk.CipherWrapper;
|
|
|
+import com.upay.sdk.ConfigurationUtils;
|
|
|
+import com.upay.sdk.HttpClientUtils;
|
|
|
+import com.upay.sdk.cashier.bindcard.builder.BindCardBuilder;
|
|
|
+import com.upay.sdk.cashier.bindcard.builder.BindCardConfirmBuilder;
|
|
|
+import com.upay.sdk.cashier.bindcard.builder.BindCardKaptchaBuilder;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.Duration;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+@AllArgsConstructor
|
|
|
+public class PayEaseService {
|
|
|
+
|
|
|
+ public static final String merchantId = "896593123";
|
|
|
+
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
+ public JSONObject request(String url, JSONObject requestData) {
|
|
|
+ log.info("requestData: {}", JSON.toJSONString(requestData, true));
|
|
|
+ String responseStr = HttpClientUtils.post3(url, requestData);
|
|
|
+ log.info("responseStr: \n{}", responseStr);
|
|
|
+ JSONObject responseData = JSONObject.parseObject(responseStr, Feature.SortFeidFastMatch);
|
|
|
+ responseData = CipherWrapper.bothDecryptWrap(responseData);
|
|
|
+ log.info("responseStr: {}", JSON.toJSONString(responseData, true));
|
|
|
+ return responseData;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String bindCard(BindCardRequest bindCardRequest) {
|
|
|
+ BindCardBuilder builder = new BindCardBuilder(merchantId);
|
|
|
+ builder.setMerchantUserId(bindCardRequest.getUserId())
|
|
|
+ .setBankCardNumber(bindCardRequest.getBankNo())
|
|
|
+ .setPhoneNumber(bindCardRequest.getPhone())
|
|
|
+ .setName(bindCardRequest.getName())
|
|
|
+ .setIdCardNum(bindCardRequest.getIdNo());
|
|
|
+ JSONObject requestData = builder.bothEncryptBuild();
|
|
|
+ JSONObject responseData = request(ConfigurationUtils.getCashierBindCardUrl(), requestData);
|
|
|
+
|
|
|
+ if (!"SUCCESS".equals(responseData.getString("status"))) {
|
|
|
+ String error = responseData.getString("error");
|
|
|
+ String cause = responseData.getString("cause");
|
|
|
+ throw new BusinessException(error);
|
|
|
+ } else {
|
|
|
+ String bindStatus = responseData.getString("bindStatus");
|
|
|
+ String bindCardId = responseData.getString("bindCardId");
|
|
|
+ String bankCardNumber = responseData.getString("bankCardNumber");
|
|
|
+ bindCardRequest.setBindCardId(bindCardId);
|
|
|
+ redisTemplate.opsForValue().set(RedisKeys.BIND_CARD + bindCardId,
|
|
|
+ bindCardRequest, Duration.ofMinutes(30));
|
|
|
+ return bindCardId;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public BindCardRequest bindCardConfirm(String bindCardId, String code) {
|
|
|
+ Object obj = redisTemplate.opsForValue().get(RedisKeys.BIND_CARD + bindCardId);
|
|
|
+ Objects.requireNonNull(obj, "绑卡信息不存在");
|
|
|
+ BindCardRequest bindCardRequest = (BindCardRequest) obj;
|
|
|
+
|
|
|
+
|
|
|
+ BindCardConfirmBuilder builder = new BindCardConfirmBuilder(merchantId);
|
|
|
+ builder.setBindCardId(bindCardId)
|
|
|
+ .setMerchantUserId(bindCardRequest.getUserId())
|
|
|
+ .setKaptchaCode(code);
|
|
|
+
|
|
|
+ JSONObject requestData = builder.bothEncryptBuild();
|
|
|
+ JSONObject responseData = request(ConfigurationUtils.getCashierBindCardConfirmUrl(), requestData);
|
|
|
+
|
|
|
+ String status = responseData.getString("status");
|
|
|
+ if (!"SUCCESS".equals(status)) {
|
|
|
+ String error = responseData.getString("error");
|
|
|
+ String cause = responseData.getString("cause");
|
|
|
+ throw new BusinessException(error);
|
|
|
+ }
|
|
|
+ String bindStatus = responseData.getString("bindStatus");
|
|
|
+ if (!"SUCCESS".equals(bindStatus)) {
|
|
|
+ String errorMsg = responseData.getString("errorMsg");
|
|
|
+ throw new BusinessException(errorMsg);
|
|
|
+ }
|
|
|
+ return bindCardRequest;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void bindCardCaptcha(String bindCardId) {
|
|
|
+ BindCardKaptchaBuilder builder = new BindCardKaptchaBuilder(merchantId);
|
|
|
+ builder.setBindCardId(bindCardId);
|
|
|
+ JSONObject response = request(ConfigurationUtils.getCashierBindCardKaptchaUrl(), builder.bothEncryptBuild());
|
|
|
+ if (!"SUCCESS".equals(response.getString("status"))) {
|
|
|
+ String error = response.getString("error");
|
|
|
+ throw new BusinessException(error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|