123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package com.fdkankan.pay.controller;
- import com.alibaba.fastjson.JSONObject;
- import com.fdkankan.pay.common.ResultData;
- import com.fdkankan.pay.entity.AutopayOrder;
- import com.fdkankan.pay.entity.Order;
- import com.fdkankan.pay.entity.PaypalConfig;
- import com.fdkankan.pay.entity.PaypalWebhookLog;
- import com.fdkankan.pay.service.IAutopayOrderService;
- import com.fdkankan.pay.service.IOrderService;
- import com.fdkankan.pay.service.IPaypalConfigService;
- import com.fdkankan.pay.service.IPaypalWebhookLogService;
- import com.fdkankan.pay.util.CacheUtil;
- import com.fdkankan.pay.util.paypal.restApi.RestApiPaypalService;
- import com.fdkankan.rabbitmq.util.RabbitMqProducer;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import java.util.HashMap;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @author
- * @since 2023-09-26
- */
- @RestController
- @RequestMapping("/service/pay/paypal")
- @Slf4j
- public class AutopayOrderController {
- @Autowired
- IAutopayOrderService autopayOrderService;
- @Autowired
- IPaypalWebhookLogService paypalWebhookLogService;
- @Autowired
- RabbitMqProducer rabbitMqProducer;
- @Autowired
- RestApiPaypalService restApiPaypalService;
- @Autowired
- IPaypalConfigService paypalConfigService;
- @Autowired
- IOrderService orderService;
- @PostMapping("/webhook")
- public ResultData webhook(@RequestBody JSONObject webhookObj){
- log.info("webhook:{}",webhookObj);
- String event_type = webhookObj.getString("event_type");
- PaypalWebhookLog log = paypalWebhookLogService.saveLog(event_type, webhookObj);
- JSONObject resource = webhookObj.getJSONObject("resource");
- AutopayOrder autopayOrder = null;
- String subscriptionId = null;
- String state =resource.getString("status");
- switch (event_type){
- case "PAYMENT.SALE.COMPLETED" : //每日扣款
- subscriptionId = resource.getString("billing_agreement_id");
- break;
- case "CATALOG.PRODUCT.CREATED" : //创建商品
- break;
- case "BILLING.PLAN.CREATED" : //创建计划
- break;
- case "BILLING.PLAN.ACTIVATED" : //计划激活
- break;
- case "BILLING.SUBSCRIPTION.CREATED" : //创建订阅
- break;
- case "BILLING.SUBSCRIPTION.CANCELLED" : //取消订阅
- subscriptionId = resource.getString("id"); //订阅id
- break;
- case "BILLING.SUBSCRIPTION.EXPIRED" : //订阅过期
- break;
- case "BILLING.SUBSCRIPTION.SUSPENDED" : //订阅暂停
- break;
- default:
- AutopayOrderController.log.info("webhook-default-event:{}",event_type);
- break;
- }
- autopayOrder = autopayOrderService.getBySubscriptionId(subscriptionId);
- if(autopayOrder == null){
- AutopayOrderController.log.info("webhook-error:{},订阅Id:{}","订单不存在",subscriptionId);
- return ResultData.ok();
- }
- String orderSn = autopayOrder.getOrderSn();
- //通知官网支付状态状态
- log.setStatus(1);
- paypalWebhookLogService.updateById(log);
- HashMap<String,String >map = new HashMap<>();
- map.put("orderSn",orderSn);
- map.put("eventType",event_type);
- map.put("state",state);
- rabbitMqProducer.sendByWorkQueue(CacheUtil.autoPaypalQueue,map);
- return ResultData.ok();
- }
- @GetMapping("/cancel/{orderSn}")
- public ResultData cancel(@PathVariable String orderSn){
- AutopayOrder autopayOrder = autopayOrderService.getByOrderSn(orderSn);
- if(autopayOrder != null){
- Order order = orderService.getByOrderSn(orderSn);
- if(order != null){
- PaypalConfig paypalConfig = paypalConfigService.getByServeId(order.getServeId());
- if(paypalConfig != null){
- restApiPaypalService.cancelSubscriptions(paypalConfig,autopayOrder.getSubscriptionId());
- }
- }
- }
- return ResultData.ok();
- }
- }
|