diff --git a/composer.json b/composer.json index 18a58a7..b9edcbd 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "license": "MIT", "require": { "php": "^8.0", - "doctrine/annotations": "^1.14", + "doctrine/annotations": "^1.14 || 2.*", "sylius/sylius": "~1.12.0 || ~1.13.0", "sylius/mailer-bundle": "^1.8 || ^2.0@beta", "symfony/webpack-encore-bundle": "^1.15" diff --git a/config/services/action.xml b/config/services/action.xml index 1f1b2fc..c7110ed 100644 --- a/config/services/action.xml +++ b/config/services/action.xml @@ -18,6 +18,9 @@ + + + diff --git a/src/Action/NotifyAction.php b/src/Action/NotifyAction.php index 7a1be8b..0f435e5 100644 --- a/src/Action/NotifyAction.php +++ b/src/Action/NotifyAction.php @@ -12,12 +12,16 @@ use BitBag\SyliusImojePlugin\Api\ImojeApi; use BitBag\SyliusImojePlugin\Resolver\SignatureResolverInterface; +use Doctrine\ORM\EntityManagerInterface; use Payum\Core\Action\ActionInterface; use Payum\Core\ApiAwareInterface; use Payum\Core\ApiAwareTrait; use Payum\Core\Bridge\Spl\ArrayObject; use Payum\Core\Exception\RequestNotSupportedException; use Payum\Core\Request\Notify; +use Sylius\Component\Core\Model\PaymentInterface; +use Sylius\Component\Core\Repository\OrderRepositoryInterface; +use Sylius\Component\Payment\Factory\PaymentFactoryInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; @@ -30,11 +34,15 @@ final class NotifyAction implements ActionInterface, ApiAwareInterface public function __construct( private RequestStack $requestStack, private SignatureResolverInterface $signatureResolver, + private PaymentFactoryInterface $paymentFactory, + private EntityManagerInterface $entityManager, + private OrderRepositoryInterface $orderRepository, ) { $this->request = $requestStack->getCurrentRequest(); $this->apiClass = ImojeApi::class; } + /** @param Notify $request */ public function execute($request): void { RequestNotSupportedException::assertSupports($this, $request); @@ -43,14 +51,38 @@ public function execute($request): void throw new \Exception('Request is empty'); } + /** @var PaymentInterface $payment */ + $payment = $request->getFirstModel(); + /** @var string $content */ $content = $this->request->getContent(); $notificationData = json_decode($content, true); + if ($notificationData['payment']['status'] === 'pending') { + return; + } + $transactionData = $notificationData['transaction']; $model = $request->getModel(); $model['statusImoje'] = $transactionData['status']; + if ($notificationData['payment']['status'] === 'error') { + /** @var PaymentInterface $newPayment */ + $newPayment = $this->paymentFactory->createNew(); + $newPayment->setState('new'); + $newPayment->setCurrencyCode($payment->getCurrencyCode()); + $newPayment->setAmount($payment->getAmount()); + $this->entityManager->persist($newPayment); + + $order = $payment->getOrder(); + $reloadedOrder = $this->orderRepository->findOneBy(['id' => $order->getId()]); + + $reloadedOrder->getLastPayment()->setState('failed'); + $reloadedOrder->addPayment($newPayment); + + $this->entityManager->flush(); + } + $request->setModel($model); }