Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions config/services/action.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<service id="bitbag.imoje_plugin.action.notify" class="BitBag\SyliusImojePlugin\Action\NotifyAction">
<argument type="service" id="request_stack"/>
<argument type="service" id="bitbag.imoje_plugin.resolver.signature_resolver"/>
<argument type="service" id="sylius.factory.payment"/>
<argument type="service" id="doctrine.orm.entity_manager"/>
<argument type="service" id="sylius.repository.order"/>
<tag name="payum.action" factory="imoje" alias="payum.action.notify"/>
</service>

Expand Down
32 changes: 32 additions & 0 deletions src/Action/NotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
}

Expand Down
Loading