src/EventSubscriber/PaymentGateway/PaymentGatewaySubscriber.php line 158

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\PaymentGateway;
  3. use App\DTO\MMPZ\PaymentMethod\PaymentGatewayAuthorizationDTO;
  4. use App\DTO\MMPZ\PaymentMethod\Paypal\PaypalCurrencyDTO;
  5. use App\DTO\MMPZ\PaymentMethod\Paypal\PaypalProductDTO;
  6. use App\DTO\MMPZ\PaymentMethod\Paypal\Plan\PaypalPlanBillingCyclesDTO;
  7. use App\DTO\MMPZ\PaymentMethod\Paypal\Plan\PaypalPlanDTO;
  8. use App\DTO\MMPZ\PaymentMethod\Paypal\Plan\PaypalPlanFrequencyDTO;
  9. use App\DTO\MMPZ\PaymentMethod\Paypal\Plan\PaypalPlanPaymentPreferenceDTO;
  10. use App\DTO\MMPZ\PaymentMethod\Paypal\Plan\PayPalPlanPricingSchemeDTO;
  11. use App\Entity\Logs\EventLog;
  12. use App\Entity\Package\Package;
  13. use App\Enums\Constants;
  14. use App\Enums\PackageType;
  15. use App\Enums\TransactionPlatform;
  16. use App\Event\LogEvent;
  17. use App\Event\PaymentGateway\PaymentGatewayCreatedEvent;
  18. use App\Event\PaymentGateway\PaymentGatewayEditedEvent;
  19. use App\Repository\Package\PackageRepository;
  20. use App\Service\AuthenticationService;
  21. use App\Service\PaymentMethod\Paypal\PaypalService;
  22. use App\Service\PaymentMethod\Stripe\StripeService;
  23. use App\Service\ProfileService;
  24. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. class PaymentGatewaySubscriber implements EventSubscriberInterface
  27. {
  28.     /** @var PackageRepository */
  29.     private $packageRepository;
  30.     /** @var PaypalService */
  31.     private $paypalService;
  32.     /** @var StripeService */
  33.     private $stripeService;
  34.     /** @var ProfileService */
  35.     private $profileService;
  36.     /** @var EventDispatcherInterface */
  37.     private $eventDispatcher;
  38.     /**
  39.      * @var AuthenticationService
  40.      */
  41.     private $authenticationService;
  42.     public function __construct(
  43.         AuthenticationService $authenticationService,
  44.         EventDispatcherInterface $eventDispatcher,
  45.         PackageRepository $packageRepository,
  46.         PaypalService $paypalService,
  47.         StripeService $stripeService,
  48.         ProfileService $profileService
  49.     )
  50.     {
  51.         $this->authenticationService $authenticationService;
  52.         $this->eventDispatcher $eventDispatcher;
  53.         $this->packageRepository $packageRepository;
  54.         $this->stripeService $stripeService;
  55.         $this->paypalService $paypalService;
  56.         $this->profileService $profileService;
  57.     }
  58.     /**
  59.      * @inheritDoc
  60.      */
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return[
  64.             PaymentGatewayCreatedEvent::class   => "onPaymentGatewayCreatedEvent",
  65.             PaymentGatewayEditedEvent::class      => "onPaymentGatewayEditedEvent",
  66.         ];
  67.     }
  68.     /**
  69.      * @param PaymentGatewayCreatedEvent $paymentGatewayCreatedEvent
  70.      * @return void
  71.      * @throws \GuzzleHttp\Exception\GuzzleException
  72.      * @throws \Stripe\Exception\ApiErrorException
  73.      */
  74.     public function onPaymentGatewayCreatedEvent(PaymentGatewayCreatedEvent $paymentGatewayCreatedEvent): void
  75.     {
  76.         $profile $paymentGatewayCreatedEvent->getProfile();
  77.         $paymentGateway $paymentGatewayCreatedEvent->getPaymentGateway();
  78.         $type EventLog::TYPE_PAYMENT_GATEWAY_CREATED;
  79.         if($profile->getProfileType()->getName() === Constants::PROFILE_TYPE_PUBLISHER_ADMINISTRATOR){
  80.             $credentials $this->profileService->getParentPaymentGateways($profile);
  81.         }else if($profile->getProfileType()->getName() === Constants::PROFILE_TYPE_PUBLISHER
  82.             || $profile->getProfileType()->getName() === Constants::PROFILE_TYPE_SONGWRITER){
  83.             $publisherAdministrator $this->profileService->findPublisherAdministratorForProfile($profile);
  84.             $credentials $this->profileService->getParentPaymentGateways($publisherAdministrator);
  85.         }
  86.         /** @var Package $package */
  87.         foreach ($this->packageRepository->findBy(['profile' => $profile]) as $package){
  88.             if($paymentGateway->getPaymentType()->getName() === TransactionPlatform::PAYPAL &&
  89.                 is_null($package->getPaypalProductId())
  90.             ){
  91.                 $type .= TransactionPlatform::PAYPAL;
  92.                 /** @var PaymentGatewayAuthorizationDTO $paymentGatewayAuthorizationDTO */
  93.                 $paymentGatewayAuthorizationDTO $this->paypalService->authPaypal($credentials[TransactionPlatform::PAYPAL]);
  94.                 $paypalProductDTO = (new PaypalProductDTO())
  95.                     ->setName($package->getName())
  96.                     ->setDescription($package->getDescription())
  97.                     ->setCategory("OTHER")
  98.                     ->setType("SERVICE");
  99.                 $paypalProductDTO $this->paypalService->createProduct(
  100.                     $paypalProductDTO,
  101.                     $paymentGatewayAuthorizationDTO
  102.                 );
  103.                 if($package->getType() === PackageType::SUBSCRIPTION){
  104.                     $paypalPlanDTO $this->paypalCreatePlanProcess(
  105.                         $package,
  106.                         $paypalProductDTO,
  107.                         $paymentGatewayAuthorizationDTO
  108.                     );
  109.                     $package->setPaypalPlanId($paypalPlanDTO->getId());
  110.                     $this->paypalService->activePlan($paypalPlanDTO$paymentGatewayAuthorizationDTO);
  111.                 }else{
  112.                     $package->setPaypalPlanId("");
  113.                 }
  114.                 $package->setPaypalProductId($paypalProductDTO->getId());
  115.             }else if($paymentGateway->getPaymentType()->getName() === TransactionPlatform::STRIPE &&
  116.                 is_null($package->getStripeProductId())
  117.             ){
  118.                 $type .= TransactionPlatform::STRIPE;
  119.                 /** @var PaymentGatewayAuthorizationDTO $paymentGatewayAuthorizationDTO */
  120.                 $paymentGatewayAuthorizationDTO $this->stripeService->authStripe($credentials[TransactionPlatform::STRIPE]);
  121.                 $stripeProduct $this->stripeService->createProduct($package$paymentGatewayAuthorizationDTO);
  122.                 $stripePrice $this->stripeService->createPrice($package$stripeProduct$paymentGatewayAuthorizationDTO);
  123.                 if($package->getType() === PackageType::SUBSCRIPTION){
  124.                     $package->setStripePlanId($stripePrice->id);
  125.                 }else{
  126.                     $package->setStripePlanId("");
  127.                 }
  128.                 $package->setStripeProductId($stripeProduct->id);
  129.             }
  130.             $this->packageRepository->flush($package);
  131.         }
  132.         $this->eventDispatcher->dispatch(new LogEvent($paymentGateway$this->authenticationService->getUser(), $type));
  133.     }
  134.     public function onPaymentGatewayEditedEvent(PaymentGatewayEditedEvent $paymentGatewayEditedEvent)
  135.     {
  136.         $profile $paymentGatewayEditedEvent->getProfile();
  137.         $paymentGateway $paymentGatewayEditedEvent->getPaymentGateway();
  138.         $type EventLog::TYPE_PAYMENT_GATEWAY_EDITED;
  139.         if($profile->getProfileType()->getName() === Constants::PROFILE_TYPE_PUBLISHER_ADMINISTRATOR){
  140.             $credentials $this->profileService->getParentPaymentGateways($profile);
  141.         }else if($profile->getProfileType()->getName() === Constants::PROFILE_TYPE_PUBLISHER
  142.             || $profile->getProfileType()->getName() === Constants::PROFILE_TYPE_SONGWRITER){
  143.             $publisherAdministrator $this->profileService->findPublisherAdministratorForProfile($profile);
  144.             $credentials $this->profileService->getParentPaymentGateways($publisherAdministrator);
  145.         }
  146.         /** @var Package $package */
  147.         foreach ($this->packageRepository->findBy(['profile' => $profile]) as $package){
  148.             if($paymentGateway->getPaymentType()->getName() === TransactionPlatform::PAYPAL){
  149.                 $type .= TransactionPlatform::PAYPAL;
  150.                 /** @var PaymentGatewayAuthorizationDTO $paymentGatewayAuthorizationDTO */
  151.                 $paymentGatewayAuthorizationDTO $this->paypalService->authPaypal($credentials[TransactionPlatform::PAYPAL]);
  152.                 $paypalProductDTO = (new PaypalProductDTO())
  153.                     ->setName($package->getName())
  154.                     ->setDescription($package->getDescription())
  155.                     ->setCategory("OTHER")
  156.                     ->setType("SERVICE");
  157.                 $paypalProductDTO $this->paypalService->createProduct(
  158.                     $paypalProductDTO,
  159.                     $paymentGatewayAuthorizationDTO
  160.                 );
  161.                 if($package->getType() === PackageType::SUBSCRIPTION){
  162.                     $paypalPlanDTO $this->paypalCreatePlanProcess(
  163.                         $package,
  164.                         $paypalProductDTO,
  165.                         $paymentGatewayAuthorizationDTO
  166.                     );
  167.                     $package->setPaypalPlanId($paypalPlanDTO->getId());
  168.                     $this->paypalService->activePlan($paypalPlanDTO$paymentGatewayAuthorizationDTO);
  169.                 }else{
  170.                     $package->setPaypalPlanId("");
  171.                 }
  172.                 $package->setPaypalProductId($paypalProductDTO->getId());
  173.             }else if($paymentGateway->getPaymentType()->getName() === TransactionPlatform::STRIPE){
  174.                 $type .= TransactionPlatform::STRIPE;
  175.                 /** @var PaymentGatewayAuthorizationDTO $paymentGatewayAuthorizationDTO */
  176.                 $paymentGatewayAuthorizationDTO $this->stripeService->authStripe($credentials[TransactionPlatform::STRIPE]);
  177.                 $stripeProduct $this->stripeService->createProduct($package$paymentGatewayAuthorizationDTO);
  178.                 $stripePrice $this->stripeService->createPrice($package$stripeProduct$paymentGatewayAuthorizationDTO);
  179.                 if($package->getType() === PackageType::SUBSCRIPTION){
  180.                     $package->setStripePlanId($stripePrice->id);
  181.                 }else{
  182.                     $package->setStripePlanId("");
  183.                 }
  184.                 $package->setStripeProductId($stripeProduct->id);
  185.             }
  186.             $this->packageRepository->flush($package);
  187.         }
  188.         $this->eventDispatcher->dispatch(new LogEvent($paymentGateway$this->authenticationService->getUser(), $type));
  189.     }
  190.     /**
  191.      * @param Package $package
  192.      * @param PaypalProductDTO $paypalProductDTO
  193.      * @param PaymentGatewayAuthorizationDTO $paymentGatewayAuthorizationDTO
  194.      * @return array|object
  195.      */
  196.     private function paypalCreatePlanProcess(
  197.         Package $package,
  198.         PaypalProductDTO $paypalProductDTO,
  199.         PaymentGatewayAuthorizationDTO $paymentGatewayAuthorizationDTO
  200.     )
  201.     {
  202.         $totalAmount = (float)$package->getPrice() - ((float)$package->getPrice() * (float)$package->getDiscount());
  203.         $frequencyUnit "YEAR";
  204.         $totalCycle 0;
  205.         if($package->isMonthly()){
  206.             $frequencyUnit "MONTH";
  207.         }
  208.         $frequency = (new PaypalPlanFrequencyDTO())
  209.             ->setIntervalUnit($frequencyUnit);
  210.         $fixedPrice = (new PaypalCurrencyDTO())
  211.             ->setValue($totalAmount)
  212.             ->setCurrencyCode("GBP");
  213.         $pricingScheme = (new PayPalPlanPricingSchemeDTO())
  214.             ->setFixedPrice($fixedPrice);
  215.         $billingCycles = (new PaypalPlanBillingCyclesDTO())
  216.             ->setTenureType("REGULAR")
  217.             ->setSequence(1)
  218.             ->setFrequency($frequency)
  219.             ->setTotalCycles($totalCycle)
  220.             ->setPricingScheme($pricingScheme);
  221.         $setupFee = (new PaypalCurrencyDTO())
  222.             ->setValue(0)//TODO: this would change
  223.             ->setCurrencyCode("GBP");
  224.         $paymentPreferences = (new PaypalPlanPaymentPreferenceDTO())
  225.             ->setAutoBillOutstanding(true)
  226.             ->setSetupFeeFailureAction("CANCEL")
  227.             ->setPaymentFailureThreshold(1)
  228.             ->setSetupFee($setupFee);
  229.         $paypalPlanDTO = (new PaypalPlanDTO())
  230.             ->setProductId($paypalProductDTO->getId())
  231.             ->setName($package->getName())
  232.             ->setBillingCycles([$billingCycles])
  233.             ->setPaymentPreferences($paymentPreferences);
  234.         return $this->paypalService->createPlan(
  235.             $paypalPlanDTO,
  236.             $paymentGatewayAuthorizationDTO
  237.         );
  238.     }
  239. }