lyhzzz пре 1 година
родитељ
комит
3a98adeefb

+ 21 - 0
src/main/java/com/fdkankan/pay/controller/AutopayOrderController.java

@@ -0,0 +1,21 @@
+package com.fdkankan.pay.controller;
+
+
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * <p>
+ *  前端控制器
+ * </p>
+ *
+ * @author 
+ * @since 2023-09-26
+ */
+@RestController
+@RequestMapping("/pay/autopayOrder")
+public class AutopayOrderController {
+
+}
+

+ 61 - 0
src/main/java/com/fdkankan/pay/entity/AutopayOrder.java

@@ -0,0 +1,61 @@
+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.util.Date;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author 
+ * @since 2023-09-26
+ */
+@Getter
+@Setter
+@TableName("t_autopay_order")
+public class AutopayOrder implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    /**
+     * 订单号
+     */
+    @TableField("order_sn")
+    private String orderSn;
+
+    /**
+     * 商品Id
+     */
+    @TableField("product_id")
+    private String productId;
+
+    /**
+     * 计划Id
+     */
+    @TableField("plan_id")
+    private String planId;
+
+    /**
+     * 订阅Id
+     */
+    @TableField("subscription_id")
+    private String subscriptionId;
+
+    @TableField("create_time")
+    private Date createTime;
+
+    @TableField("update_time")
+    private Date updateTime;
+
+
+}

+ 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_config"
+                "t_autopay_order"
         }));
 
 //        generate(path,"goods", getTables(new String[]{

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

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

+ 17 - 0
src/main/java/com/fdkankan/pay/service/IAutopayOrderService.java

@@ -0,0 +1,17 @@
+package com.fdkankan.pay.service;
+
+import com.fdkankan.pay.entity.AutopayOrder;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author 
+ * @since 2023-09-26
+ */
+public interface IAutopayOrderService extends IService<AutopayOrder> {
+
+    void saveAutoOrder(String orderSn, String productId, String planId, String subscriptionId);
+}

+ 40 - 0
src/main/java/com/fdkankan/pay/service/impl/AutopayOrderServiceImpl.java

@@ -0,0 +1,40 @@
+package com.fdkankan.pay.service.impl;
+import java.util.Date;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.fdkankan.pay.entity.AutopayOrder;
+import com.fdkankan.pay.mapper.IAutopayOrderMapper;
+import com.fdkankan.pay.service.IAutopayOrderService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author 
+ * @since 2023-09-26
+ */
+@Service
+public class AutopayOrderServiceImpl extends ServiceImpl<IAutopayOrderMapper, AutopayOrder> implements IAutopayOrderService {
+
+    @Override
+    public void saveAutoOrder(String orderSn, String productId, String planId, String subscriptionId) {
+        AutopayOrder autopayOrder = this.getByOrderSn(orderSn);
+        if(autopayOrder == null){
+            autopayOrder = new AutopayOrder();
+        }
+        autopayOrder.setOrderSn(orderSn);
+        autopayOrder.setProductId(productId);
+        autopayOrder.setPlanId(planId);
+        autopayOrder.setSubscriptionId(subscriptionId);
+        this.save(autopayOrder);
+    }
+
+    private AutopayOrder getByOrderSn(String orderSn) {
+        LambdaQueryWrapper<AutopayOrder> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(AutopayOrder::getOrderSn,orderSn);
+        return this.getOne(wrapper);
+    }
+}

+ 8 - 4
src/main/java/com/fdkankan/pay/util/paypal/restApi/RestApiPaypalService.java

@@ -1,6 +1,7 @@
 package com.fdkankan.pay.util.paypal.restApi;
 
 
+import cn.hutool.http.Header;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
 import com.alibaba.fastjson.JSONObject;
@@ -102,8 +103,8 @@ public class RestApiPaypalService {
             paymentPreferences.setPaymentFailureThreshold(3);
             paymentPreferences.setSetupFeeFailureAction("CANCEL");
             SetupFee setupFee = new SetupFee();
-            setupFee.setCurrencyCode("USD");
-            setupFee.setValue("0");
+            setupFee.setCurrencyCode(planVo.getCurrencyCode());
+            setupFee.setValue(planVo.getPrice());
             paymentPreferences.setSetupFee(setupFee);
             // 税率
             Taxes taxes = new Taxes();
@@ -175,7 +176,7 @@ public class RestApiPaypalService {
     /**
      * 创建订阅
      */
-    public String createSubscription(PaypalConfig paypalConfig,String planId) {
+    public SubscriptionVo createSubscription(PaypalConfig paypalConfig,String planId) {
         Map<String,String> map = new HashMap<>(4);
         map.put("Content-Type","application/json");
         map.put("Authorization",getToken(paypalConfig));
@@ -195,10 +196,13 @@ public class RestApiPaypalService {
                 .execute().body();
         JSONObject jsonObject = JSONObject.parseObject(body);
         log.info("createSubscription-resp:{}",jsonObject);
+        String id = jsonObject.getString("id");
         List<com.paypal.api.payments.Links> links = JSONObject.parseArray(jsonObject.get("links").toString(), com.paypal.api.payments.Links.class);
         Links links1 = links.stream().filter(o -> "approve".equals(o.getRel())).findFirst()
                 .orElseThrow(() -> new RuntimeException("创建订阅失败"));
-        return links1.getHref();
+        String href = links1.getHref();
+        SubscriptionVo vo = new SubscriptionVo(id, href);
+        return vo;
     }
 
     /**

+ 14 - 0
src/main/java/com/fdkankan/pay/util/paypal/restApi/vo/subscription/SubscriptionVo.java

@@ -0,0 +1,14 @@
+package com.fdkankan.pay.util.paypal.restApi.vo.subscription;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.ToString;
+
+@Data
+@AllArgsConstructor
+@ToString
+public class SubscriptionVo {
+
+    private String subscriptionId;
+    private String subscriptionHref;
+}

+ 9 - 3
src/main/java/com/fdkankan/pay/util/paypal/sdk/PaypalService.java

@@ -8,6 +8,7 @@ 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.IOrderService;
 import com.fdkankan.pay.service.IPaypalConfigService;
 import com.fdkankan.pay.util.CacheUtil;
@@ -17,6 +18,7 @@ import com.fdkankan.pay.util.paypal.restApi.vo.Product;
 import com.fdkankan.pay.util.paypal.restApi.vo.plan.PlanVo;
 import com.fdkankan.pay.util.paypal.restApi.vo.subscription.ApplicationContext;
 import com.fdkankan.pay.util.paypal.restApi.vo.subscription.PaymentMethod;
+import com.fdkankan.pay.util.paypal.restApi.vo.subscription.SubscriptionVo;
 import com.paypal.api.payments.*;
 import com.paypal.base.codec.binary.Base64;
 import com.paypal.base.rest.APIContext;
@@ -425,6 +427,8 @@ public class PaypalService {
 
     @Autowired
     RestApiPaypalService restApiPaypalService;
+    @Autowired
+    IAutopayOrderService autopayOrderService;
 
     public Object autoPay(Order order, PaypalConfig paypalConfig) {
 
@@ -444,11 +448,13 @@ public class PaypalService {
         String planId = restApiPaypalService.createPlan(paypalConfig, plan);
         log.info("创建订阅计划:"+planId);
 
-        String subscription = restApiPaypalService.createSubscription(paypalConfig, planId);
+        SubscriptionVo subscription = restApiPaypalService.createSubscription(paypalConfig, planId);
         log.info("创建订阅支付链接:"+subscription);
+        autopayOrderService.saveAutoOrder(order.getOrderSn(),productId,planId,subscription.getSubscriptionId());
+
         OpenPayResponse openPayResponse = new OpenPayResponse();
-        openPayResponse.setH5Url(subscription);
-        openPayResponse.setQrCodeUrl(subscription);
+        openPayResponse.setH5Url(subscription.getSubscriptionHref());
+        openPayResponse.setQrCodeUrl(subscription.getSubscriptionHref());
         openPayResponse.setOrderSn(order.getOrderSn());
         openPayResponse.setPayType(5);
         openPayResponse.setAutoPay(1);

+ 5 - 0
src/main/resources/mapper/pay/AutopayOrderMapper.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.IAutopayOrderMapper">
+
+</mapper>