package com.fdkankan.pay.controller; import com.alibaba.fastjson.JSONObject; import com.fdkankan.pay.common.ResultCode; import com.fdkankan.pay.common.ResultData; import com.fdkankan.pay.entity.*; import com.fdkankan.pay.exception.BusinessException; 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.rabbitmq.util.RabbitMqProducer; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.OrderUtils; import org.springframework.web.bind.annotation.*; import java.util.HashMap; /** *
* 前端控制器 *
* * @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; @Autowired IAutopayOrderSonService autopayOrderSonService; @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 tradeNo = ""; String state =resource.getString("state"); switch (event_type){ case "PAYMENT.SALE.COMPLETED" : //每日扣款 subscriptionId = resource.getString("billing_agreement_id"); tradeNo = resource.getString("id"); break; case "CATALOG.PRODUCT.CREATED" : //创建商品 break; case "BILLING.PLAN.CREATED" : //创建计划 break; case "BILLING.PLAN.ACTIVATED" : //计划激活 break; case "BILLING.PLAN.DEACTIVATED" : //计划停止 break; case "BILLING.SUBSCRIPTION.CREATED" : //创建订阅 //subscriptionId = resource.getString("id"); //订阅id break; case "BILLING.SUBSCRIPTION.CANCELLED" : //取消订阅 subscriptionId = resource.getString("id"); //订阅id break; case "BILLING.SUBSCRIPTION.EXPIRED" : //订阅过期 break; case "BILLING.SUBSCRIPTION.SUSPENDED" : //订阅暂停 subscriptionId = resource.getString("id"); //订阅id break; case "BILLING.SUBSCRIPTION.UPDATED" : //订阅暂停 break; case "BILLING.SUBSCRIPTION.PAYMENT.FAILED" : //订阅付款失败 subscriptionId = resource.getString("id"); //订阅id break; default: AutopayOrderController.log.info("webhook-default-event:{}",event_type); break; } autopayOrder = autopayOrderService.getBySubscriptionId(subscriptionId); if(autopayOrder == null){ AutopayOrderController.log.info("webhook-error:{},event:{},订阅Id:{}","订单不存在",event_type,subscriptionId); return ResultData.ok(); } String orderSn = autopayOrder.getOrderSn(); //通知官网支付状态状态 log.setStatus(1); paypalWebhookLogService.updateById(log); AutopayOrderSon orderSnSon = null; if("PAYMENT.SALE.COMPLETED".equals(event_type)){ orderSnSon = autopayOrderSonService.addOrderByOrder(autopayOrder.getId(),resource); } HashMap