-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring creating a transaction and add handling a credit card tra…
…nsactions
- Loading branch information
1 parent
c032571
commit ef3bfe8
Showing
27 changed files
with
1,259 additions
and
282 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateCardPaymentPayloadFactory; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateCardPaymentPayloadFactoryInterface; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateRedirectBasedPaymentPayloadFactory; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateRedirectBasedPaymentPayloadFactoryInterface; | ||
|
||
return function(ContainerConfigurator $container): void { | ||
$services = $container->services(); | ||
|
||
$services->set('commerce_weavers_tpay.tpay.factory.create_card_payment_payload', CreateCardPaymentPayloadFactory::class) | ||
->args([ | ||
service('commerce_weavers_tpay.tpay.factory.create_redirect_based_payment_payload'), | ||
]) | ||
->alias(CreateCardPaymentPayloadFactoryInterface::class, 'commerce_weavers_tpay.factory.create_card_payment_payload') | ||
; | ||
|
||
$services->set('commerce_weavers_tpay.tpay.factory.create_redirect_based_payment_payload', CreateRedirectBasedPaymentPayloadFactory::class) | ||
->args([ | ||
service('router'), | ||
param('commerce_weavers_tpay.payum.create_transaction.success_route'), | ||
param('commerce_weavers_tpay.payum.create_transaction.error_route'), | ||
]) | ||
->alias(CreateRedirectBasedPaymentPayloadFactoryInterface::class, 'commerce_weavers_tpay.factory.create_redirect_based_payment_payload') | ||
; | ||
}; |
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,46 @@ | ||
<?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 Sylius\Component\Core\Model\PaymentInterface; | ||
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, | ||
) { | ||
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'); | ||
} | ||
} |
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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\Token\NotifyTokenFactoryInterface; | ||
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; | ||
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\PayWithCard; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateCardPaymentPayloadFactoryInterface; | ||
use Payum\Core\GatewayAwareInterface; | ||
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 implements GatewayAwareInterface | ||
{ | ||
use GenericTokenFactoryAwareTrait; | ||
use GatewayAwareTrait; | ||
|
||
public function __construct( | ||
private RouterInterface $router, | ||
private CreateCardPaymentPayloadFactoryInterface $createCardPaymentPayloadFactory, | ||
private NotifyTokenFactoryInterface $notifyTokenFactory, | ||
) { | ||
parent::__construct($router); | ||
} | ||
|
||
/** | ||
* @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->notifyTokenFactory->create($model, $token->getGatewayName(), $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(); | ||
|
||
if (!$request instanceof CreateTransaction) { | ||
return false; | ||
} | ||
|
||
if (!$model instanceof PaymentInterface) { | ||
return false; | ||
} | ||
|
||
$details = $model->getDetails(); | ||
|
||
return isset($details['tpay']['card']); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php
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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\Token\NotifyTokenFactoryInterface; | ||
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, | ||
private CreateRedirectBasedPaymentPayloadFactoryInterface $createRedirectBasedPaymentPayloadFactory, | ||
private NotifyTokenFactoryInterface $notifyTokenFactory, | ||
) { | ||
parent::__construct($router); | ||
} | ||
|
||
/** | ||
* @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->notifyTokenFactory->create($model, $token->getGatewayName(), $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(); | ||
|
||
if (!$request instanceof CreateTransaction) { | ||
return false; | ||
} | ||
|
||
if (!$model instanceof PaymentInterface) { | ||
return false; | ||
} | ||
|
||
$details = $model->getDetails(); | ||
|
||
return !isset($details['tpay']['card']) && !isset($details['tpay']['blik']); | ||
} | ||
} |
Oops, something went wrong.