123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- 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.config.PayConfig;
- 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;
- /**
- * <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;
- @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<String,String >map = new HashMap<>();
- map.put("subscriptionOrderSn",orderSn);
- map.put("subscriptionId",subscriptionId);
- map.put("eventType",event_type);
- map.put("tradeNo",tradeNo);
- map.put("state",state);
- 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();
- }
- @GetMapping("/cancel/{subscriptionId}")
- public ResultData cancel(@PathVariable String subscriptionId){
- AutopayOrder autopayOrder = autopayOrderService.getBySubscriptionId(subscriptionId);
- if(autopayOrder == null){
- throw new BusinessException(ResultCode.ORDER_NOT_EXIST);
- }
- Order order = orderService.getByOrderSn(autopayOrder.getOrderSn());
- if(order == null){
- throw new BusinessException(ResultCode.ORDER_NOT_EXIST);
- }
- PaypalConfig paypalConfig = PayConfig.paypalConfigMap.get(order.getServeId());
- if(paypalConfig == null){
- throw new BusinessException(ResultCode.PAYPAL_CONFIG_ERROR);
- }
- RestApiPaypalService.cancelSubscriptions(paypalConfig,autopayOrder.getSubscriptionId());
- return ResultData.ok();
- }
- }
|