lyhzzz 2 tahun lalu
induk
melakukan
4d9fe5d1c8

+ 3 - 1
src/main/java/com/fdkankan/pay/service/IAutopayOrderService.java

@@ -13,5 +13,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IAutopayOrderService extends IService<AutopayOrder> {
 
-    void saveAutoOrder(String orderSn, String productId, String planId, String subscriptionId);
+     AutopayOrder getByOrderSn(String orderSn) ;
+
+     void saveAutoOrder(String orderSn, String productId, String planId, String subscriptionId);
 }

+ 3 - 2
src/main/java/com/fdkankan/pay/service/impl/AutopayOrderServiceImpl.java

@@ -29,10 +29,11 @@ public class AutopayOrderServiceImpl extends ServiceImpl<IAutopayOrderMapper, Au
         autopayOrder.setProductId(productId);
         autopayOrder.setPlanId(planId);
         autopayOrder.setSubscriptionId(subscriptionId);
-        this.save(autopayOrder);
+        this.saveOrUpdate(autopayOrder);
     }
 
-    private AutopayOrder getByOrderSn(String orderSn) {
+    @Override
+    public AutopayOrder getByOrderSn(String orderSn) {
         LambdaQueryWrapper<AutopayOrder> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(AutopayOrder::getOrderSn,orderSn);
         return this.getOne(wrapper);

+ 26 - 18
src/main/java/com/fdkankan/pay/util/paypal/sdk/PaypalService.java

@@ -4,6 +4,7 @@ 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.Order;
 import com.fdkankan.pay.entity.PaypalConfig;
 import com.fdkankan.pay.exception.BusinessException;
@@ -431,26 +432,33 @@ public class PaypalService {
     IAutopayOrderService autopayOrderService;
 
     public Object autoPay(Order order, PaypalConfig paypalConfig) {
+        AutopayOrder autopayOrder = autopayOrderService.getByOrderSn(order.getOrderSn());
+        if(autopayOrder == null){
+            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);
+            autopayOrder.setPlanId(planId);
+            autopayOrder.setOrderSn(order.getOrderSn());
+            autopayOrder.setProductId(productId);
+        }
 
-        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);
-
-        SubscriptionVo subscription = restApiPaypalService.createSubscription(paypalConfig, planId);
+        SubscriptionVo subscription = restApiPaypalService.createSubscription(paypalConfig, autopayOrder.getSubscriptionId());
         log.info("创建订阅支付链接:"+subscription);
-        autopayOrderService.saveAutoOrder(order.getOrderSn(),productId,planId,subscription.getSubscriptionId());
+        autopayOrder.setSubscriptionId(subscription.getSubscriptionId());
+        autopayOrderService.saveOrUpdate(autopayOrder);
 
         OpenPayResponse openPayResponse = new OpenPayResponse();
         openPayResponse.setH5Url(subscription.getSubscriptionHref());