Explorar el Código

计划存库,避免建立多个重复计划

lyhzzz hace 2 años
padre
commit
bddc5c7176

+ 71 - 0
src/main/java/com/fdkankan/pay/entity/AutopayPlan.java

@@ -0,0 +1,71 @@
+package com.fdkankan.pay.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author 
+ * @since 2023-10-27
+ */
+@Getter
+@Setter
+@TableName("t_autopay_plan")
+public class AutopayPlan implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    @TableField("serve_id")
+    private Integer serveId;
+
+    @TableField("order_type")
+    private String orderType;
+
+    @TableField("pay_type")
+    private Integer payType;
+
+    @TableField("order_money")
+    private BigDecimal orderMoney;
+
+    @TableField("auto_pay_time")
+    private String autoPayTime;
+
+    @TableField("product_id")
+    private String productId;
+
+    @TableField("plan_id")
+    private String planId;
+
+    @TableField("create_time")
+    private Date createTime;
+
+    @TableField("update_time")
+    private Date updateTime;
+
+
+
+    public AutopayPlan(Order order, String productId,String planId) {
+        if(order != null){
+            this.serveId = order.getServeId();
+            this.orderType = order.getOrderType();
+            this.payType = order.getPayType();
+            this.orderMoney = order.getOrderMoney();
+            this.autoPayTime = order.getAutoPayTime();
+        }
+        this.productId = productId;
+        this.planId = planId;
+    }
+}

+ 1 - 1
src/main/java/com/fdkankan/pay/generate/AutoGenerate.java

@@ -18,7 +18,7 @@ public class AutoGenerate {
         String path =System.getProperty("user.dir");
 
         generate(path,"pay", getTables(new String[]{
-                "t_paypal_webhook_log"
+                "t_autopay_plan"
         }));
 
 //        generate(path,"goods", getTables(new String[]{

+ 18 - 0
src/main/java/com/fdkankan/pay/mapper/IAutopayPlanMapper.java

@@ -0,0 +1,18 @@
+package com.fdkankan.pay.mapper;
+
+import com.fdkankan.pay.entity.AutopayPlan;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author 
+ * @since 2023-10-27
+ */
+@Mapper
+public interface IAutopayPlanMapper extends BaseMapper<AutopayPlan> {
+
+}

+ 18 - 0
src/main/java/com/fdkankan/pay/service/IAutopayPlanService.java

@@ -0,0 +1,18 @@
+package com.fdkankan.pay.service;
+
+import com.fdkankan.pay.entity.AutopayPlan;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.fdkankan.pay.entity.Order;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author 
+ * @since 2023-10-27
+ */
+public interface IAutopayPlanService extends IService<AutopayPlan> {
+
+    AutopayPlan getByOrder(Order order);
+}

+ 32 - 0
src/main/java/com/fdkankan/pay/service/impl/AutopayPlanServiceImpl.java

@@ -0,0 +1,32 @@
+package com.fdkankan.pay.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.fdkankan.pay.entity.AutopayPlan;
+import com.fdkankan.pay.entity.Order;
+import com.fdkankan.pay.mapper.IAutopayPlanMapper;
+import com.fdkankan.pay.service.IAutopayPlanService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author 
+ * @since 2023-10-27
+ */
+@Service
+public class AutopayPlanServiceImpl extends ServiceImpl<IAutopayPlanMapper, AutopayPlan> implements IAutopayPlanService {
+
+    @Override
+    public AutopayPlan getByOrder(Order order) {
+        LambdaQueryWrapper<AutopayPlan> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(AutopayPlan::getServeId,order.getServeId());
+        wrapper.eq(AutopayPlan::getOrderType,order.getOrderType());
+        wrapper.eq(AutopayPlan::getPayType,order.getPayType());
+        wrapper.eq(AutopayPlan::getOrderMoney,order.getOrderMoney());
+        wrapper.eq(AutopayPlan::getAutoPayTime,order.getAutoPayTime());
+        return this.getOne(wrapper);
+    }
+}

+ 35 - 21
src/main/java/com/fdkankan/pay/util/paypal/sdk/PaypalService.java

@@ -3,13 +3,12 @@ package com.fdkankan.pay.util.paypal.sdk;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.fdkankan.pay.common.ResultCode;
-import com.fdkankan.pay.entity.AliConfig;
-import com.fdkankan.pay.entity.AutopayOrder;
+import com.fdkankan.pay.entity.*;
 import com.fdkankan.pay.entity.Order;
-import com.fdkankan.pay.entity.PaypalConfig;
 import com.fdkankan.pay.exception.BusinessException;
 import com.fdkankan.pay.response.OpenPayResponse;
 import com.fdkankan.pay.service.IAutopayOrderService;
+import com.fdkankan.pay.service.IAutopayPlanService;
 import com.fdkankan.pay.service.IOrderService;
 import com.fdkankan.pay.service.IPaypalConfigService;
 import com.fdkankan.pay.util.CacheUtil;
@@ -105,7 +104,12 @@ public class PaypalService {
             return false;
         }
         try {
-             String paymentId = request.getParameter("paymentId");//商品名
+            Map<String, String[]> parameterMap = request.getParameterMap();
+            for (String key : parameterMap.keySet()) {
+                log.info("paypal-callBack--request:{},{}",key,request.getParameter(key));
+            }
+
+            String paymentId = request.getParameter("paymentId");//商品名
              String payerId = request.getParameter("PayerID");//购买数量
             PaypalConfig paypalConfig = paypalConfigService.getByServeId(order.getServeId());
             if(paypalConfig == null){
@@ -430,6 +434,8 @@ public class PaypalService {
     RestApiPaypalService restApiPaypalService;
     @Autowired
     IAutopayOrderService autopayOrderService;
+    @Autowired
+    IAutopayPlanService autopayPlanService;
 
     public Object autoPay(Order order, PaypalConfig paypalConfig) {
         AutopayOrder autopayOrder = autopayOrderService.getByOrderSn(order.getOrderSn());
@@ -443,26 +449,34 @@ public class PaypalService {
             openPayResponse.setQrCodeUrl(autopayOrder.getSubscriptionHref());
             return openPayResponse;
         }
+
         autopayOrder = new AutopayOrder();
-        Product product = new Product();
-        product.setName(order.getOrderType());
-        product.setDescription(order.getGoodsInfo().toJSONString());
-        product.setType("SERVICE");
-        String productId = restApiPaypalService.createProduct(paypalConfig, product);
-        log.info("创建商品:"+productId);
-
-        PlanVo plan = new PlanVo();
-        plan.setProductId(productId);
-        plan.setPlanName(order.getOrderType());
-        plan.setTimeUnit(order.getAutoPayTime());
-        plan.setCurrencyCode("USD");
-        plan.setPrice(order.getOrderMoney().toString());
-        String planId = restApiPaypalService.createPlan(paypalConfig, plan);
-        log.info("创建订阅计划:"+planId);
+        AutopayPlan autopayPlan = autopayPlanService.getByOrder(order);
+
+        if(autopayPlan == null){
+            Product product = new Product();
+            product.setName(order.getOrderType());
+            product.setDescription(order.getGoodsInfo().toJSONString());
+            product.setType("SERVICE");
+            String productId = restApiPaypalService.createProduct(paypalConfig, product);
+            log.info("创建商品:"+productId);
+
+            PlanVo plan = new PlanVo();
+            plan.setProductId(productId);
+            plan.setPlanName(order.getOrderType());
+            plan.setTimeUnit(order.getAutoPayTime());
+            plan.setCurrencyCode("USD");
+            plan.setPrice(order.getOrderMoney().toString());
+            String planId = restApiPaypalService.createPlan(paypalConfig, plan);
+            log.info("创建订阅计划:"+planId);
+
+            autopayPlan = new AutopayPlan(order,productId,planId);
+            autopayPlanService.save(autopayPlan);
+        }
 
+        autopayOrder.setProductId(autopayPlan.getProductId());
+        autopayOrder.setPlanId(autopayPlan.getPlanId());
         autopayOrder.setOrderSn(order.getOrderSn());
-        autopayOrder.setProductId(productId);
-        autopayOrder.setPlanId(planId);
 
         SubscriptionVo subscription = restApiPaypalService.createSubscription(paypalConfig, autopayOrder.getPlanId());
         log.info("创建订阅支付链接:"+subscription);

+ 5 - 0
src/main/resources/mapper/pay/AutopayPlanMapper.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.fdkankan.pay.mapper.IAutopayPlanMapper">
+
+</mapper>