Skip to content

Commit 3fc837d

Browse files
committed
Handle failed and pending payment statuses
1 parent 0d58a6b commit 3fc837d

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

config/services/action.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<service id="bitbag.imoje_plugin.action.notify" class="BitBag\SyliusImojePlugin\Action\NotifyAction">
1919
<argument type="service" id="request_stack"/>
2020
<argument type="service" id="bitbag.imoje_plugin.resolver.signature_resolver"/>
21+
<argument type="service" id="sylius.factory.payment"/>
22+
<argument type="service" id="doctrine.orm.entity_manager"/>
23+
<argument type="service" id="sylius.repository.order"/>
2124
<tag name="payum.action" factory="imoje" alias="payum.action.notify"/>
2225
</service>
2326

src/Action/NotifyAction.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212

1313
use BitBag\SyliusImojePlugin\Api\ImojeApi;
1414
use BitBag\SyliusImojePlugin\Resolver\SignatureResolverInterface;
15+
use Doctrine\ORM\EntityManagerInterface;
1516
use Payum\Core\Action\ActionInterface;
1617
use Payum\Core\ApiAwareInterface;
1718
use Payum\Core\ApiAwareTrait;
1819
use Payum\Core\Bridge\Spl\ArrayObject;
1920
use Payum\Core\Exception\RequestNotSupportedException;
2021
use Payum\Core\Request\Notify;
22+
use Sylius\Component\Core\Model\PaymentInterface;
23+
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
24+
use Sylius\Component\Payment\Factory\PaymentFactoryInterface;
2125
use Symfony\Component\HttpFoundation\Request;
2226
use Symfony\Component\HttpFoundation\RequestStack;
2327

@@ -30,11 +34,15 @@ final class NotifyAction implements ActionInterface, ApiAwareInterface
3034
public function __construct(
3135
private RequestStack $requestStack,
3236
private SignatureResolverInterface $signatureResolver,
37+
private PaymentFactoryInterface $paymentFactory,
38+
private EntityManagerInterface $entityManager,
39+
private OrderRepositoryInterface $orderRepository,
3340
) {
3441
$this->request = $requestStack->getCurrentRequest();
3542
$this->apiClass = ImojeApi::class;
3643
}
3744

45+
/** @param Notify $request */
3846
public function execute($request): void
3947
{
4048
RequestNotSupportedException::assertSupports($this, $request);
@@ -43,14 +51,38 @@ public function execute($request): void
4351
throw new \Exception('Request is empty');
4452
}
4553

54+
/** @var PaymentInterface $payment */
55+
$payment = $request->getFirstModel();
56+
4657
/** @var string $content */
4758
$content = $this->request->getContent();
4859
$notificationData = json_decode($content, true);
60+
if ($notificationData['payment']['status'] === 'pending') {
61+
return;
62+
}
63+
4964
$transactionData = $notificationData['transaction'];
5065

5166
$model = $request->getModel();
5267
$model['statusImoje'] = $transactionData['status'];
5368

69+
if ($notificationData['payment']['status'] === 'error') {
70+
/** @var PaymentInterface $newPayment */
71+
$newPayment = $this->paymentFactory->createNew();
72+
$newPayment->setState('new');
73+
$newPayment->setCurrencyCode($payment->getCurrencyCode());
74+
$newPayment->setAmount($payment->getAmount());
75+
$this->entityManager->persist($newPayment);
76+
77+
$order = $payment->getOrder();
78+
$reloadedOrder = $this->orderRepository->findOneBy(['id' => $order->getId()]);
79+
80+
$reloadedOrder->getLastPayment()->setState('failed');
81+
$reloadedOrder->addPayment($newPayment);
82+
83+
$this->entityManager->flush();
84+
}
85+
5486
$request->setModel($model);
5587
}
5688

0 commit comments

Comments
 (0)