-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api; | ||
|
||
use Payum\Core\GatewayAwareTrait; | ||
use Payum\Core\Security\GenericTokenFactoryAwareInterface; | ||
use Payum\Core\Security\GenericTokenFactoryAwareTrait; | ||
use Payum\Core\Security\TokenInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
abstract class AbstractCreateTransactionAction extends BaseApiAwareAction implements GenericTokenFactoryAwareInterface | ||
{ | ||
use GenericTokenFactoryAwareTrait; | ||
use GatewayAwareTrait; | ||
|
||
public function __construct( | ||
private RouterInterface $router, | ||
private string $notifyRoute, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function createTransaction(PaymentInterface $payment, array $payload): void | ||
{ | ||
$details = $payment->getDetails(); | ||
|
||
$order = $payment->getOrder(); | ||
Assert::notNull($order); | ||
$localeCode = $order->getLocaleCode(); | ||
Assert::notNull($localeCode); | ||
|
||
$response = $this->api->transactions()->createTransaction($payload); | ||
|
||
$details['tpay']['transaction_id'] = $response['transactionId']; | ||
$details['tpay']['transaction_payment_url'] = $response['transactionPaymentUrl']; | ||
|
||
$payment->setDetails($details); | ||
} | ||
|
||
protected function getLocaleCodeFrom(PaymentInterface $payment): string | ||
{ | ||
return $payment->getOrder()->getLocaleCode() ?? throw new \InvalidArgumentException('Cannot determine locale code for a given payment'); | ||
Check failure on line 47 in src/Payum/Action/Api/AbstractCreateTransactionAction.php GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine
Check failure on line 47 in src/Payum/Action/Api/AbstractCreateTransactionAction.php GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow
|
||
} | ||
|
||
protected function createNotifyToken(PaymentInterface $payment, TokenInterface $token, string $localeCode): TokenInterface | ||
{ | ||
return $this->tokenFactory->createToken( | ||
$token->getGatewayName(), | ||
$payment, | ||
$this->router->generate($this->notifyRoute, ['_locale' => $localeCode], UrlGeneratorInterface::ABSOLUTE_URL), | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; | ||
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\PayWithCard; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateCardPaymentPayloadFactoryInterface; | ||
use Payum\Core\GatewayAwareTrait; | ||
use Payum\Core\Security\GenericTokenFactoryAwareTrait; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class CreateCardTransactionAction extends AbstractCreateTransactionAction | ||
{ | ||
use GenericTokenFactoryAwareTrait; | ||
use GatewayAwareTrait; | ||
|
||
public function __construct( | ||
private RouterInterface $router, | ||
Check failure on line 22 in src/Payum/Action/Api/CreateCardTransactionAction.php GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine
Check failure on line 22 in src/Payum/Action/Api/CreateCardTransactionAction.php GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow
|
||
private CreateCardPaymentPayloadFactoryInterface $createCardPaymentPayloadFactory, | ||
private string $notifyRoute, | ||
) { | ||
parent::__construct($router, $this->notifyRoute); | ||
} | ||
|
||
/** | ||
* @param CreateTransaction $request | ||
*/ | ||
public function execute($request): void | ||
{ | ||
/** @var PaymentInterface $model */ | ||
$model = $request->getModel(); | ||
$token = $request->getToken(); | ||
Assert::notNull($token); | ||
|
||
$localeCode = $this->getLocaleCodeFrom($model); | ||
$notifyToken = $this->createNotifyToken($model, $token, $localeCode); | ||
|
||
$this->createTransaction( | ||
$model, | ||
$this->createCardPaymentPayloadFactory->createFrom($model, $notifyToken->getTargetUrl(), $localeCode), | ||
); | ||
|
||
$this->gateway->execute(new PayWithCard($token)); | ||
} | ||
|
||
public function supports($request): bool | ||
{ | ||
$model = $request->getModel(); | ||
Check failure on line 52 in src/Payum/Action/Api/CreateCardTransactionAction.php GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine
|
||
|
||
if (!$request instanceof CreateTransaction) { | ||
return false; | ||
} | ||
|
||
if (!$model instanceof PaymentInterface) { | ||
return false; | ||
} | ||
|
||
$details = $model->getDetails(); | ||
|
||
return isset($details['tpay']['card']); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateRedirectBasedPaymentPayloadFactoryInterface; | ||
use Payum\Core\Reply\HttpRedirect; | ||
use Payum\Core\Security\GenericTokenFactoryAwareTrait; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
class CreateRedirectBasedTransactionAction extends AbstractCreateTransactionAction | ||
{ | ||
use GenericTokenFactoryAwareTrait; | ||
|
||
public function __construct( | ||
private RouterInterface $router, | ||
Check failure on line 20 in src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine
Check failure on line 20 in src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow
|
||
private CreateRedirectBasedPaymentPayloadFactoryInterface $createRedirectBasedPaymentPayloadFactory, | ||
private string $notifyRoute, | ||
Check failure on line 22 in src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine
Check failure on line 22 in src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow
|
||
) { | ||
parent::__construct($router, $notifyRoute); | ||
} | ||
|
||
/** | ||
* @param CreateTransaction $request | ||
*/ | ||
public function execute($request): void | ||
{ | ||
/** @var PaymentInterface $model */ | ||
$model = $request->getModel(); | ||
$token = $request->getToken(); | ||
Assert::notNull($token); | ||
|
||
$localeCode = $this->getLocaleCodeFrom($model); | ||
$notifyToken = $this->createNotifyToken($model, $token, $localeCode); | ||
|
||
$this->createTransaction( | ||
$model, | ||
$this->createRedirectBasedPaymentPayloadFactory->createFrom($model, $notifyToken->getTargetUrl(), $localeCode), | ||
); | ||
|
||
$details = $model->getDetails(); | ||
|
||
throw new HttpRedirect($details['tpay']['transaction_payment_url']); | ||
} | ||
|
||
public function supports($request): bool | ||
{ | ||
$model = $request->getModel(); | ||
Check failure on line 52 in src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine
|
||
|
||
if (!$request instanceof CreateTransaction) { | ||
return false; | ||
} | ||
|
||
if (!$model instanceof PaymentInterface) { | ||
return false; | ||
} | ||
|
||
$details = $model->getDetails(); | ||
|
||
return !isset($details['tpay']['card']) && !isset($details['tpay']['blik']); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\PayWithCard; | ||
use Payum\Core\Reply\HttpRedirect; | ||
use Sylius\Component\Core\Model\PaymentInterface; | ||
|
||
class PayWithCardAction extends BaseApiAwareAction | ||
{ | ||
/** | ||
* @param PayWithCard $request | ||
*/ | ||
public function execute($request): void | ||
{ | ||
/** @var PaymentInterface $model */ | ||
$model = $request->getModel(); | ||
$details = $model->getDetails(); | ||
|
||
$response = $this->api->transactions()->createPaymentByTransactionId([ | ||
'groupId' => 103, | ||
'cardPaymentData' => [ | ||
'card' => $details['tpay']['card'], | ||
], | ||
], $details['tpay']['transaction_id']); | ||
|
||
$details['tpay']['transaction_payment_url'] = $response['transactionPaymentUrl']; | ||
|
||
$model->setDetails($details); | ||
|
||
if ($response['status'] === 'pending') { | ||
throw new HttpRedirect($details['tpay']['transaction_payment_url']); | ||
} | ||
} | ||
|
||
public function supports($request): bool | ||
{ | ||
return $request instanceof PayWithCard && $request->getModel() instanceof PaymentInterface; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api; | ||
|
||
use Payum\Core\Request\Generic; | ||
|
||
class PayWithCard extends Generic | ||
{ | ||
} |