-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace webhook controller with payum notifynull and notify implement…
…ation
- Loading branch information
1 parent
886df2e
commit 2f6daf7
Showing
13 changed files
with
318 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusPausePayPlugin\Payum\Action; | ||
|
||
use Payum\Core\Action\ActionInterface; | ||
use Payum\Core\Exception\RequestNotSupportedException; | ||
use Payum\Core\GatewayAwareInterface; | ||
use Payum\Core\GatewayAwareTrait; | ||
use Payum\Core\Reply\HttpResponse; | ||
use Payum\Core\Request\GetHttpRequest; | ||
use Payum\Core\Request\Notify; | ||
use Payum\Core\Security\TokenInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface as SyliusPaymentInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
use Webgriffe\SyliusPausePayPlugin\Client\PaymentState; | ||
use Webgriffe\SyliusPausePayPlugin\Helper\PaymentDetailsHelper; | ||
use Webgriffe\SyliusPausePayPlugin\Payum\PausePayApi; | ||
use Webgriffe\SyliusPausePayPlugin\ValueObject\WebhookPayload; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @psalm-suppress PropertyNotSetInConstructor | ||
* | ||
* @psalm-type PaymentDetails array{uuid: string, redirect_url: string, created_at: string, status?: string} | ||
*/ | ||
class NotifyAction implements ActionInterface, GatewayAwareInterface | ||
{ | ||
use GatewayAwareTrait; | ||
|
||
public function __construct( | ||
private SerializerInterface $serializer, | ||
private LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
/** | ||
* @param Notify|mixed $request | ||
*/ | ||
public function execute($request): void | ||
{ | ||
RequestNotSupportedException::assertSupports($this, $request); | ||
Assert::isInstanceOf($request, Notify::class); | ||
|
||
/** @var SyliusPaymentInterface $syliusPayment */ | ||
$syliusPayment = $request->getModel(); | ||
|
||
$this->logInfo($syliusPayment, 'Start notify action'); | ||
|
||
$notifyToken = $request->getToken(); | ||
Assert::isInstanceOf($notifyToken, TokenInterface::class); | ||
|
||
$this->gateway->execute($httpRequest = new GetHttpRequest()); | ||
$content = $httpRequest->content; | ||
|
||
$this->logInfo($syliusPayment, sprintf('Received notify action call with payload: %s', $content)); | ||
|
||
$payload = $this->serializer->deserialize($content, WebhookPayload::class, 'json'); | ||
Assert::isInstanceOf($payload, WebhookPayload::class); | ||
|
||
$this->assertPausePayPayment($syliusPayment); | ||
|
||
$paymentDetails = $syliusPayment->getDetails(); | ||
if (!PaymentDetailsHelper::areValid($paymentDetails)) { | ||
// todo | ||
throw new HttpResponse('Not found', 404); | ||
} | ||
|
||
$status = $payload->isSuccessful() ? PaymentState::SUCCESS : PaymentState::CANCELLED; | ||
$syliusPayment->setDetails(PaymentDetailsHelper::addPaymentStatus($paymentDetails, $status)); | ||
|
||
$this->logInfo($syliusPayment, sprintf('Saved payment status: %s', $status)); | ||
} | ||
|
||
public function supports($request): bool | ||
{ | ||
return $request instanceof Notify && $request->getModel() instanceof SyliusPaymentInterface; | ||
} | ||
|
||
private function assertPausePayPayment(PaymentInterface $syliusPayment): void | ||
{ | ||
$paymentMethod = $syliusPayment->getMethod(); | ||
if (!$paymentMethod instanceof PaymentMethodInterface) { | ||
throw new HttpResponse('Access denied', 403); | ||
} | ||
|
||
$paymentGatewayConfig = $paymentMethod->getGatewayConfig(); | ||
if (!$paymentGatewayConfig instanceof GatewayConfigInterface) { | ||
throw new HttpResponse('Access denied', 403); | ||
} | ||
/** @psalm-suppress DeprecatedMethod */ | ||
if ($paymentGatewayConfig->getFactoryName() !== PausePayApi::GATEWAY_CODE) { | ||
throw new HttpResponse('Access denied', 403); | ||
} | ||
} | ||
|
||
private function logInfo(SyliusPaymentInterface $payment, string $message, array $context = []): void | ||
{ | ||
$this->logger->info(sprintf('[Payment #%s]: %s.', (string) $payment->getId(), $message, ), $context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusPausePayPlugin\Payum\Action; | ||
|
||
use Payum\Core\Action\ActionInterface; | ||
use Payum\Core\Exception\RequestNotSupportedException; | ||
use Payum\Core\GatewayAwareInterface; | ||
use Payum\Core\GatewayAwareTrait; | ||
use Payum\Core\Reply\HttpResponse; | ||
use Payum\Core\Request\GetHttpRequest; | ||
use Payum\Core\Request\Notify; | ||
use Psr\Log\LoggerInterface; | ||
use Sylius\Bundle\PayumBundle\Model\PaymentSecurityTokenInterface; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
use Webgriffe\SyliusPausePayPlugin\Entity\PaymentOrderInterface; | ||
use Webgriffe\SyliusPausePayPlugin\Repository\PaymentOrderRepositoryInterface; | ||
use Webgriffe\SyliusPausePayPlugin\ValueObject\WebhookPayload; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @psalm-suppress PropertyNotSetInConstructor | ||
* | ||
* @psalm-type PaymentDetails array{uuid: string, redirect_url: string, created_at: string, status?: string} | ||
*/ | ||
class NotifyNullAction implements ActionInterface, GatewayAwareInterface | ||
{ | ||
use GatewayAwareTrait; | ||
|
||
public function __construct( | ||
private SerializerInterface $serializer, | ||
private PaymentOrderRepositoryInterface $paymentOrderRepository, | ||
private LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
/** | ||
* @param Notify|mixed $request | ||
*/ | ||
public function execute($request): void | ||
{ | ||
RequestNotSupportedException::assertSupports($this, $request); | ||
Assert::isInstanceOf($request, Notify::class); | ||
|
||
$this->logger->info('Start notify null action'); | ||
$this->gateway->execute($httpRequest = new GetHttpRequest()); | ||
$notifyToken = $this->retrieveNotifyTokenFromRequest($httpRequest); | ||
$this->logger->info(sprintf('Calling notify action with token: %s', $notifyToken->getHash())); | ||
$this->gateway->execute(new Notify($notifyToken)); | ||
} | ||
|
||
public function supports($request): bool | ||
{ | ||
return $request instanceof Notify && null === $request->getModel(); | ||
} | ||
|
||
private function retrieveNotifyTokenFromRequest(GetHttpRequest $httpRequest): PaymentSecurityTokenInterface | ||
{ | ||
// todo: validate signature in X-Pausepay-Signature | ||
$content = $httpRequest->content; | ||
|
||
$this->logger->info(sprintf('Received notify null action call with payload: %s', $content)); | ||
|
||
$payload = $this->serializer->deserialize($content, WebhookPayload::class, 'json'); | ||
Assert::isInstanceOf($payload, WebhookPayload::class); | ||
|
||
$paymentOrder = $this->paymentOrderRepository->findOneByPausePayOrderId($payload->getOrderID()); | ||
if (!$paymentOrder instanceof PaymentOrderInterface) { | ||
// todo | ||
throw new HttpResponse('Not found', 404); | ||
} | ||
|
||
return $paymentOrder->getPaymentToken(); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.