|
@@ -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();
|
|
|
}
|