123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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.*;
- 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;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author
- * @since 2025-04-15
- */
- @RestController
- @RequestMapping("/service/pay/stripe")
- @Slf4j
- public class StripeController {
- @Autowired
- IStripeWebhookLogService stripeWebhookLogService;
- @Autowired
- IOrderService orderService;
- @Autowired
- IAutopayOrderService autopayOrderService;
- @Autowired
- IAutopayOrderSonService autopayOrderSonService;
- @Autowired
- RabbitMqProducer rabbitMqProducer;
- @PostMapping("/webhook")
- public ResultData webhook(@RequestBody JSONObject webhookObj){
- StripeWebhookLog stripeWebhookLog = new StripeWebhookLog();
- String type = webhookObj.getString("type");
- stripeWebhookLog.setEventType(type);
- 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 = webhookObj.getJSONObject("data");
- jsonObject1 = jsonObject.getJSONObject("object");
- String id = jsonObject1.getString("id");
- trade_no = jsonObject1.getString("payment_intent");
- String mode = jsonObject1.getString("mode");
- subscriptionId = jsonObject1.getString("subscription");
- status = jsonObject1.getString("status");
- JSONObject jsonObject2 = jsonObject1.getJSONObject("metadata");
- orderId = jsonObject2.getString("orderId");
- log.info("订单号为:id:{},trade_no:{},orderId:{}",id,trade_no,orderId);
- Order order = orderService.getByOrderSn(orderId);
- if( order != null){
- orderService.payResult(order,true,trade_no,null);
- }
- break;
- case "checkout.session.expired"://过期
- break;
- case "payment_intent.created"://创建订单 这里事件就是图二选着的事件
- break;
- case "payment_intent.canceled"://取消订单
- break;
- case "payment_intent.succeeded"://支付成功
- 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();
- }
- }
|