StripeController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.fdkankan.pay.controller;
  2. import cn.hutool.log.Log;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.fdkankan.pay.common.AutoPayUtil;
  5. import com.fdkankan.pay.common.ResultData;
  6. import com.fdkankan.pay.entity.*;
  7. import com.fdkankan.pay.service.IAutopayOrderService;
  8. import com.fdkankan.pay.service.IAutopayOrderSonService;
  9. import com.fdkankan.pay.service.IOrderService;
  10. import com.fdkankan.pay.service.IStripeWebhookLogService;
  11. import com.fdkankan.pay.util.CacheUtil;
  12. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. /**
  22. * <p>
  23. * 前端控制器
  24. * </p>
  25. *
  26. * @author
  27. * @since 2025-04-15
  28. */
  29. @RestController
  30. @RequestMapping("/service/pay/stripe")
  31. @Slf4j
  32. public class StripeController {
  33. @Autowired
  34. IStripeWebhookLogService stripeWebhookLogService;
  35. @Autowired
  36. IOrderService orderService;
  37. @Autowired
  38. IAutopayOrderService autopayOrderService;
  39. @Autowired
  40. IAutopayOrderSonService autopayOrderSonService;
  41. @Autowired
  42. RabbitMqProducer rabbitMqProducer;
  43. @PostMapping("/webhook")
  44. public ResultData webhook(@RequestBody JSONObject webhookObj){
  45. StripeWebhookLog stripeWebhookLog = new StripeWebhookLog();
  46. String type = webhookObj.getString("type");
  47. stripeWebhookLog.setEventType(type);
  48. stripeWebhookLog.setMsg(webhookObj.toJSONString());
  49. stripeWebhookLogService.save(stripeWebhookLog);
  50. JSONObject jsonObject = null;
  51. JSONObject jsonObject1 = null;
  52. String subscriptionId = null;
  53. String trade_no = null;
  54. String status = null;
  55. String orderId = null;
  56. AutopayOrder autopayOrder = null;
  57. switch (type){
  58. case "checkout.session.completed"://支付完成
  59. jsonObject = webhookObj.getJSONObject("data");
  60. jsonObject1 = jsonObject.getJSONObject("object");
  61. String id = jsonObject1.getString("id");
  62. trade_no = jsonObject1.getString("payment_intent");
  63. String mode = jsonObject1.getString("mode");
  64. subscriptionId = jsonObject1.getString("subscription");
  65. status = jsonObject1.getString("status");
  66. JSONObject jsonObject2 = jsonObject1.getJSONObject("metadata");
  67. orderId = jsonObject2.getString("orderId");
  68. log.info("订单号为:id:{},trade_no:{},orderId:{}",id,trade_no,orderId);
  69. Order order = orderService.getByOrderSn(orderId);
  70. if( order != null){
  71. orderService.payResult(order,true,trade_no,null);
  72. }
  73. break;
  74. case "checkout.session.expired"://过期
  75. break;
  76. case "payment_intent.created"://创建订单 这里事件就是图二选着的事件
  77. break;
  78. case "payment_intent.canceled"://取消订单
  79. break;
  80. case "payment_intent.succeeded"://支付成功
  81. break;
  82. case "payment_intent.payment_failed"://支付失败
  83. break;
  84. case "customer.subscription.created"://创建订阅
  85. jsonObject = webhookObj.getJSONObject("data");
  86. jsonObject1 = jsonObject.getJSONObject("object");
  87. subscriptionId = jsonObject1.getString("id");
  88. break;
  89. case "customer.subscription.deleted"://取消订阅
  90. jsonObject = webhookObj.getJSONObject("data");
  91. jsonObject1 = jsonObject.getJSONObject("object");
  92. subscriptionId = jsonObject1.getString("id");
  93. break;
  94. default:
  95. break;
  96. }
  97. String autoEven = AutoPayUtil.getAutoEven(type);
  98. autopayOrder = autopayOrderService.getBySubscriptionId(subscriptionId);
  99. if(autopayOrder == null ){
  100. return ResultData.ok();
  101. }
  102. AutopayOrderSon orderSnSon = null;
  103. if("PAY.COMPLETED".equals(autoEven)){
  104. orderSnSon = autopayOrderSonService.addOrderByOrder(autopayOrder.getId(),jsonObject1,6);
  105. }
  106. HashMap<String,String > map = new HashMap<>();
  107. map.put("subscriptionOrderSn",autopayOrder.getOrderSn());
  108. map.put("subscriptionId",subscriptionId);
  109. map.put("eventType",autoEven);
  110. map.put("tradeNo",trade_no);
  111. map.put("state",status);
  112. map.put("orderSn",null);
  113. map.put("amount",null);
  114. if(orderSnSon != null){
  115. map.put("orderSn",orderSnSon.getOrderSn() );
  116. map.put("amount",orderSnSon.getAmount());
  117. }
  118. rabbitMqProducer.sendByWorkQueue(CacheUtil.autoPaypalQueue,map);
  119. return ResultData.ok();
  120. }
  121. }