src/EventSubscribers/Main/CallbacksSubscriber.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscribers\Main;
  3. use App\Entity\Main\BillingRefundV2Callback;
  4. use App\Entity\Main\CallbackLog;
  5. use App\Events\Main\Callbacks\ChargebackCallbackReceivedEvent;
  6. use App\Events\Main\Callbacks\PaypalCallbackReceivedEvent;
  7. use App\Events\Main\Callbacks\RebillManagerCallbackReceivedEvent;
  8. use App\Events\Main\Callbacks\RefundCallbackReceivedEvent;
  9. use App\Events\Main\Callbacks\ThreeDSCallbackReceivedEvent;
  10. use App\Events\Main\Callbacks\TransactionCallbackReceivedEvent;
  11. use App\Exceptions\Main\Callbacks\IgnoredByThisCallbackException;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Exception\HttpException;
  17. use Symfony\Component\Validator\Validator\ValidatorInterface;
  18. /**
  19.  * Class CallbacksSubscriber
  20.  * @package App\EventSubscribers\Main
  21.  */
  22. class CallbacksSubscriber implements EventSubscriberInterface
  23. {
  24.     const
  25.         CALLBACK_CHARGEBACK 'callback.chargeback',
  26.         CALLBACK_TRANSACTION 'callback.transaction',
  27.         CALLBACK_REBILLMANAGER 'callback.rebillmanager',
  28.         CALLBACK_3DS 'callback.threeds',
  29.         CALLBACK_REFUND 'callback.refund',
  30.         CALLBACK_PAYPAL 'callback.paypal'
  31.     ;
  32.     /**
  33.      * @var EntityManagerInterface
  34.      */
  35.     private $entityManager;
  36.     /**
  37.      * @var ValidatorInterface
  38.      */
  39.     private $validator;
  40.     private $errors;
  41.     /**
  42.      * CoachingUserSubscriber constructor.
  43.      * @param EntityManagerInterface $entityManager
  44.      */
  45.     public function __construct(EntityManagerInterface $entityManagerValidatorInterface $validator)
  46.     {
  47.         $this->entityManager $entityManager;
  48.         $this->validator $validator;
  49.     }
  50.     /**
  51.      * @return array
  52.      */
  53.     public static function getSubscribedEvents()
  54.     {
  55.         return array(
  56.             self::CALLBACK_CHARGEBACK => "callbackChargeback",
  57.             self::CALLBACK_TRANSACTION => "callbackTransaction",
  58.             self::CALLBACK_REBILLMANAGER => "callbackRebillManager",
  59.             self::CALLBACK_3DS => "callbackThreeDS",
  60.             self::CALLBACK_REFUND => "callbackRefund",
  61.             self::CALLBACK_PAYPAL => "callbackPaypal"
  62.         );
  63.     }
  64.     /**
  65.      * @param ChargebackCallbackReceivedEvent $event
  66.      * @throws \Exception
  67.      */
  68.     public function callbackChargeback(ChargebackCallbackReceivedEvent $event)
  69.     {
  70.         // We ignore some actions but we still log them.
  71.         $ignoredActions = ["""transaction"];
  72.         $isValid $this->validate($event->getCallback());
  73.         $this->createLog($event->getRequest(), CallbackLog::TYPE_CHARGEBACK$isValid$event->getCallback());
  74.         if (in_array($event->getCallback()->getAction(), $ignoredActions)) {
  75.             throw new IgnoredByThisCallbackException();
  76.         }
  77.         if (!$isValid) {
  78.             $this->throwError($event->getRequest());
  79.         }
  80.     }
  81.     /**
  82.      * @param TransactionCallbackReceivedEvent $event
  83.      * @throws \Exception
  84.      */
  85.     public function callbackTransaction(TransactionCallbackReceivedEvent $event)
  86.     {
  87.         // TODO update that if we have a transaction callback.
  88.         $this->createLog($event->getRequest(), CallbackLog::TYPE_TRANSACTIONtrue);
  89.     }
  90.     /**
  91.      * @param RebillManagerCallbackReceivedEvent $event
  92.      * @throws \Exception
  93.      */
  94.     public function callbackRebillManager(RebillManagerCallbackReceivedEvent $event)
  95.     {
  96.         $isValid $this->validate($event->getCallback());
  97.         $this->createLog($event->getRequest(), CallbackLog::TYPE_REBILL_MANAGER$isValid$event->getCallback());
  98.         // return a 500 error like it was.
  99.         if (!$isValid) {
  100.             $this->throwError($event->getRequest());
  101.         }
  102.     }
  103.     /**
  104.      * @param ThreeDSCallbackReceivedEvent $event
  105.      * @throws \Exception
  106.      */
  107.     public function callbackThreeDS(ThreeDSCallbackReceivedEvent $event)
  108.     {
  109.         $this->createLog($event->getRequest(), CallbackLog::TYPE_3DStrue);
  110.     }
  111.     /**
  112.      * @param PaypalCallbackReceivedEvent $event
  113.      * @throws \Exception
  114.      */
  115.     public function callbackPaypal(PaypalCallbackReceivedEvent $event)
  116.     {
  117.         $this->createLog($event->getRequest(), CallbackLog::TYPE_PAYPALtruenull$event->getPutData());
  118.     }
  119.     /**
  120.      * @param RefundCallbackReceivedEvent $event
  121.      * @throws \Exception
  122.      */
  123.     public function callbackRefund(RefundCallbackReceivedEvent $event)
  124.     {
  125.         $callBack $event->getCallBack();
  126.         $request $event->getRequest();
  127.         $isValid $this->validate($callBack);
  128.         $this->createLog($requestCallbackLog::TYPE_REFUND$isValid$callBack);
  129.         // return a 500 error like it was.
  130.         if (!$isValid) {
  131.             $this->throwError($request);
  132.         }
  133.     }
  134.     /**
  135.      * @param Request $request
  136.      * @param $logType
  137.      * @param $isValid
  138.      * @param null $object
  139.      * @throws \Exception
  140.      */
  141.     protected function createLog(Request $request$logType$isValid$object null$putData null)
  142.     {
  143.         $log = new CallbackLog();
  144.         $log->setContent("POST data : " json_encode($request->request->all()) . " \n GET data : " json_encode($request->query->all()));
  145.         if (null != $putData) {
  146.             $log->setContent($log->getContent() . " Put data : " print_r($putDatatrue));
  147.         }
  148.         $log->setDate(new \DateTime());
  149.         $log->setOriginIp($request->getClientIp());
  150.         $log->setType($logType);
  151.         $log->setPassedValidation($isValid);
  152.         if (null != $object) {
  153.             $log->setObjectData(print_r($objecttrue));
  154.         }
  155.         $this->entityManager->persist($log);
  156.         $this->entityManager->flush();
  157.     }
  158.     /**
  159.      * @param $objectToValidate
  160.      * @return bool
  161.      */
  162.     protected function validate($objectToValidate)
  163.     {
  164.         $this->errors $this->validator->validate($objectToValidate);
  165.         if (count($this->errors)) {
  166.             return false;
  167.         }
  168.         return true;
  169.     }
  170.     /**
  171.      * @param Request $request
  172.      */
  173.     protected function throwError(Request $request)
  174.     {
  175.         throw new HttpException(
  176.             Response::HTTP_INTERNAL_SERVER_ERROR,
  177.             'Errors found in: ' . (string) $this->errors ' - Request : ' print_r($request->request->all(), true)
  178.         );
  179.     }
  180. }