lyhzzz 4 月之前
父節點
當前提交
65ddf71ba3

+ 27 - 0
src/main/java/com/fdkankan/pay/common/AutoPayUtil.java

@@ -0,0 +1,27 @@
+package com.fdkankan.pay.common;
+
+public class AutoPayUtil {
+
+
+    public static String  getAutoEven(String even){
+        switch (even){
+            case "PAYMENT.SALE.COMPLETED" :         //paypal支付成功
+            case "checkout.session.completed" :     //stripe支付成功
+                return "PAY.COMPLETED";
+
+            case "BILLING.SUBSCRIPTION.PAYMENT.FAILED" :         //paypal支付失败
+            case "payment_intent.payment_failed" :               //stripe支付失败
+                return "PAY.FAILED";
+
+            case "BILLING.SUBSCRIPTION.CANCELLED" :         //paypal取消订阅
+            case "customer.subscription.deleted" :          //stripe取消订阅
+                return "SUBSCRIPTION.CANCELLED";
+
+            case "BILLING.SUBSCRIPTION.SUSPENDED" :         //paypal订阅暂停
+                return "SUBSCRIPTION.CANCELLED";
+
+            default:
+                return even;
+        }
+    }
+}

+ 16 - 6
src/main/java/com/fdkankan/pay/controller/AutopayOrderController.java

@@ -11,7 +11,10 @@ import com.fdkankan.pay.service.*;
 import com.fdkankan.pay.util.CacheUtil;
 import com.fdkankan.pay.util.OrderSnUtil;
 import com.fdkankan.pay.util.paypal.restApi.RestApiPaypalService;
+import com.fdkankan.pay.util.stripe.StripeService;
 import com.fdkankan.rabbitmq.util.RabbitMqProducer;
+import com.stripe.model.Subscription;
+import com.stripe.param.SubscriptionCancelParams;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -47,6 +50,8 @@ public class AutopayOrderController {
     IOrderService orderService;
     @Autowired
     IAutopayOrderSonService autopayOrderSonService;
+    @Autowired
+    StripeService stripeService;
 
     @PostMapping("/webhook")
     public ResultData webhook(@RequestBody JSONObject webhookObj){
@@ -106,7 +111,7 @@ public class AutopayOrderController {
 
         AutopayOrderSon orderSnSon = null;
         if("PAYMENT.SALE.COMPLETED".equals(event_type)){
-            orderSnSon = autopayOrderSonService.addOrderByOrder(autopayOrder.getId(),resource);
+            orderSnSon = autopayOrderSonService.addOrderByOrder(autopayOrder.getId(),resource,5);
         }
         HashMap<String,String >map = new HashMap<>();
         map.put("subscriptionOrderSn",orderSn);
@@ -126,7 +131,7 @@ public class AutopayOrderController {
     }
 
     @GetMapping("/cancel/{subscriptionId}")
-    public ResultData cancel(@PathVariable String subscriptionId){
+    public ResultData cancel(@PathVariable String subscriptionId) {
         AutopayOrder autopayOrder = autopayOrderService.getBySubscriptionId(subscriptionId);
         if(autopayOrder == null){
             throw new BusinessException(ResultCode.ORDER_NOT_EXIST);
@@ -135,11 +140,16 @@ public class AutopayOrderController {
         if(order == null){
             throw new BusinessException(ResultCode.ORDER_NOT_EXIST);
         }
-        PaypalConfig paypalConfig = PayConfig.paypalConfigMap.get(order.getServeId());
-        if(paypalConfig == null){
-            throw new BusinessException(ResultCode.PAYPAL_CONFIG_ERROR);
+        if(order.getPayType() == 5){
+            PaypalConfig paypalConfig = PayConfig.paypalConfigMap.get(order.getServeId());
+            if(paypalConfig == null){
+                throw new BusinessException(ResultCode.PAYPAL_CONFIG_ERROR);
+            }
+            RestApiPaypalService.cancelSubscriptions(paypalConfig,autopayOrder.getSubscriptionId());
+        }
+        if(order.getPayType() == 6){
+            stripeService.cancelSubscriptions(subscriptionId);
         }
-        RestApiPaypalService.cancelSubscriptions(paypalConfig,autopayOrder.getSubscriptionId());
         return ResultData.ok();
     }
 }

+ 65 - 8
src/main/java/com/fdkankan/pay/controller/StripeController.java

@@ -3,12 +3,15 @@ package com.fdkankan.pay.controller;
 
 import cn.hutool.log.Log;
 import com.alibaba.fastjson.JSONObject;
+import com.fdkankan.pay.common.AutoPayUtil;
 import com.fdkankan.pay.common.ResultData;
-import com.fdkankan.pay.entity.Order;
-import com.fdkankan.pay.entity.PaypalWebhookLog;
-import com.fdkankan.pay.entity.StripeWebhookLog;
+import com.fdkankan.pay.entity.*;
+import com.fdkankan.pay.service.IAutopayOrderService;
+import com.fdkankan.pay.service.IAutopayOrderSonService;
 import com.fdkankan.pay.service.IOrderService;
 import com.fdkankan.pay.service.IStripeWebhookLogService;
+import com.fdkankan.pay.util.CacheUtil;
+import com.fdkankan.rabbitmq.util.RabbitMqProducer;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -17,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.HashMap;
 import java.util.Map;
 
 /**
@@ -36,6 +40,12 @@ public class StripeController {
     IStripeWebhookLogService stripeWebhookLogService;
     @Autowired
     IOrderService orderService;
+    @Autowired
+    IAutopayOrderService autopayOrderService;
+    @Autowired
+    IAutopayOrderSonService autopayOrderSonService;
+    @Autowired
+    RabbitMqProducer rabbitMqProducer;
 
     @PostMapping("/webhook")
     public ResultData webhook(@RequestBody JSONObject webhookObj){
@@ -45,17 +55,28 @@ public class StripeController {
         stripeWebhookLog.setMsg(webhookObj.toJSONString());
         stripeWebhookLogService.save(stripeWebhookLog);
 
+        JSONObject jsonObject = null;
+        JSONObject jsonObject1 = null;
+
+        String subscriptionId = null;
+        String trade_no = null;
+        String status = null;
+        String orderId = null;
+        AutopayOrder autopayOrder = null;
         switch (type){
             case "checkout.session.completed"://支付完成
-                JSONObject jsonObject = webhookObj.getJSONObject("data");
-                JSONObject jsonObject1 = jsonObject.getJSONObject("object");
+                jsonObject = webhookObj.getJSONObject("data");
+                jsonObject1 = jsonObject.getJSONObject("object");
                 String id = jsonObject1.getString("id");
-                String trade_no = jsonObject1.getString("payment_intent");
+                trade_no = jsonObject1.getString("payment_intent");
+                String mode = jsonObject1.getString("mode");
+                subscriptionId = jsonObject1.getString("subscription");
+                status = jsonObject1.getString("status");
                 JSONObject jsonObject2 = jsonObject1.getJSONObject("metadata");
-                String orderId = jsonObject2.getString("orderId");
+                orderId = jsonObject2.getString("orderId");
                 log.info("订单号为:id:{},trade_no:{},orderId:{}",id,trade_no,orderId);
                 Order order = orderService.getByOrderSn(orderId);
-                if(order != null){
+                if( order != null){
                     orderService.payResult(order,true,trade_no,null);
                 }
                 break;
@@ -69,10 +90,46 @@ public class StripeController {
                 break;
             case "payment_intent.payment_failed"://支付失败
                 break;
+            case "customer.subscription.created"://创建订阅
+                jsonObject = webhookObj.getJSONObject("data");
+                jsonObject1 = jsonObject.getJSONObject("object");
+                subscriptionId = jsonObject1.getString("id");
+
+                break;
+            case "customer.subscription.deleted"://取消订阅
+                jsonObject = webhookObj.getJSONObject("data");
+                jsonObject1 = jsonObject.getJSONObject("object");
+                subscriptionId = jsonObject1.getString("id");
+                break;
 
             default:
                 break;
         }
+        String autoEven = AutoPayUtil.getAutoEven(type);
+
+        autopayOrder = autopayOrderService.getBySubscriptionId(subscriptionId);
+        if(autopayOrder == null ){
+            return ResultData.ok();
+        }
+
+        AutopayOrderSon orderSnSon = null;
+        if("PAY.COMPLETED".equals(autoEven)){
+            orderSnSon = autopayOrderSonService.addOrderByOrder(autopayOrder.getId(),jsonObject1,6);
+        }
+        HashMap<String,String > map = new HashMap<>();
+        map.put("subscriptionOrderSn",autopayOrder.getOrderSn());
+        map.put("subscriptionId",subscriptionId);
+        map.put("eventType",autoEven);
+        map.put("tradeNo",trade_no);
+        map.put("state",status);
+        map.put("orderSn",null);
+        map.put("amount",null);
+        if(orderSnSon != null){
+            map.put("orderSn",orderSnSon.getOrderSn() );
+            map.put("amount",orderSnSon.getAmount());
+        }
+
+        rabbitMqProducer.sendByWorkQueue(CacheUtil.autoPaypalQueue,map);
 
         return ResultData.ok();
     }

+ 3 - 0
src/main/java/com/fdkankan/pay/entity/StripePrice.java

@@ -44,6 +44,9 @@ public class StripePrice implements Serializable {
     @TableField("recurring")
     private Integer recurring;
 
+    @TableField("interval")
+    private String interval;
+
     @TableField("rec_status")
     @TableLogic(value = "A",delval = "I")
     private String recStatus;

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

@@ -15,5 +15,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IAutopayOrderSonService extends IService<AutopayOrderSon> {
 
-    AutopayOrderSon addOrderByOrder(Integer id , JSONObject resource);
+    AutopayOrderSon addOrderByOrder(Integer id , JSONObject resource ,Integer payType);
 }

+ 19 - 6
src/main/java/com/fdkankan/pay/service/impl/AutopayOrderSonServiceImpl.java

@@ -26,12 +26,25 @@ public class AutopayOrderSonServiceImpl extends ServiceImpl<IAutopayOrderSonMapp
     OrderSnUtil orderSnUtil;
 
     @Override
-    public AutopayOrderSon addOrderByOrder(Integer autoPayOrderId, JSONObject resource ) {
-        String state =resource.getString("state");
-        String tradeNo = resource.getString("id");
-        JSONObject amountObj = resource.getJSONObject("amount");
-        String amount = amountObj.getString("total");
-        String currency = amountObj.getString("currency");
+    public AutopayOrderSon addOrderByOrder(Integer autoPayOrderId, JSONObject resource ,Integer payType) {
+        String state =null;
+        String tradeNo =null;
+        JSONObject amountObj =null;
+        String amount =null;
+        String currency =null;
+        if(payType == 5){
+             state =resource.getString("state");
+             tradeNo = resource.getString("id");
+             amountObj = resource.getJSONObject("amount");
+             amount = amountObj.getString("total");
+             currency = amountObj.getString("currency");
+        }
+        if(payType == 6){
+            state =resource.getString("status");
+            tradeNo = resource.getString("invoice");
+            amount = String.valueOf(resource.getInteger("amount_subtotal")/100);
+            currency = amountObj.getString("currency");
+        }
 
         AutopayOrderSon autopayOrderSon = new AutopayOrderSon();
         autopayOrderSon.setAutopayOrderId(autoPayOrderId);

+ 6 - 2
src/main/java/com/fdkankan/pay/service/impl/StripePriceServiceImpl.java

@@ -34,6 +34,9 @@ public class StripePriceServiceImpl extends ServiceImpl<IStripePriceMapper, Stri
             wrapper.eq(StripePrice::getCurrency,currency);
             wrapper.eq(StripePrice::getProductId,productId);
             wrapper.eq(StripePrice::getRecurring,autoPay);
+            if(autoPay == 1){
+                wrapper.eq(StripePrice::getInterval,interval);
+            }
             StripePrice one = this.getOne(wrapper);
             if(one == null){
                 //创建价格 https://stripe.com/docs/api/prices/create
@@ -47,7 +50,7 @@ public class StripePriceServiceImpl extends ServiceImpl<IStripePriceMapper, Stri
                 }
                 priceMap.put("product", productId);
                 Price price = Price.create(priceMap);
-                this.saveByParam(price.getId(),orderMoney,currency,productId,autoPay);
+                this.saveByParam(price.getId(),orderMoney,currency,productId,autoPay,interval);
                 return price.getId();
             }
             return one.getPriceId();
@@ -58,13 +61,14 @@ public class StripePriceServiceImpl extends ServiceImpl<IStripePriceMapper, Stri
         return null;
     }
 
-    private void saveByParam(String priceId,BigDecimal orderMoney, String currency, String productId, Integer autoPay) {
+    private void saveByParam(String priceId,BigDecimal orderMoney, String currency, String productId, Integer autoPay,String interval) {
         StripePrice stripePrice = new StripePrice();
         stripePrice.setPriceId(priceId);
         stripePrice.setUnitAmount(orderMoney);
         stripePrice.setCurrency(currency);
         stripePrice.setProductId(productId);
         stripePrice.setRecurring(autoPay);
+        stripePrice.setInterval(interval);
         this.save(stripePrice);
 
     }

+ 14 - 0
src/main/java/com/fdkankan/pay/util/stripe/StripeService.java

@@ -13,7 +13,9 @@ import com.fdkankan.pay.util.CacheUtil;
 import com.paypal.api.payments.Payment;
 import com.stripe.model.Price;
 import com.stripe.model.Product;
+import com.stripe.model.Subscription;
 import com.stripe.model.checkout.Session;
+import com.stripe.param.SubscriptionCancelParams;
 import com.stripe.param.checkout.SessionCreateParams;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -124,4 +126,16 @@ public class StripeService {
         }
         return  true;
     }
+
+    public void cancelSubscriptions(String subscriptionId) {
+        try {
+            Subscription resource = Subscription.retrieve(subscriptionId);
+            SubscriptionCancelParams params = SubscriptionCancelParams.builder().build();
+            Subscription subscription = resource.cancel(params);
+        }catch (Exception e){
+            log.info("取消订阅失败:{}",e);
+            throw new BusinessException(ResultCode.CANCEL_SUBSCRIPTIONS_ERROR);
+        }
+
+    }
 }