Skip to content

Commit

Permalink
Refactoring creating a transaction and add handling a credit card tra…
Browse files Browse the repository at this point in the history
…nsactions
  • Loading branch information
jakubtobiasz committed Sep 10, 2024
1 parent c032571 commit ef3bfe8
Show file tree
Hide file tree
Showing 27 changed files with 1,259 additions and 282 deletions.
26 changes: 20 additions & 6 deletions config/services/payum/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateCardTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\NotifyAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\PayWithCardAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\GetStatusAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\RefundAction;
Expand All @@ -24,20 +26,32 @@
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.capture'])
;

$services->set(CreateTransactionAction::class)
$services->set(CreateCardTransactionAction::class)
->args([
service('router'),
param('commerce_weavers_tpay.payum.create_transaction.success_route'),
param('commerce_weavers_tpay.payum.create_transaction.error_route'),
param('commerce_weavers_tpay.payum.create_transaction.notify_route'),
service('commerce_weavers_tpay.tpay.factory.create_card_payment_payload'),
service('commerce_weavers_tpay.payum.factory.token.notify'),
])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_transaction'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_card_transaction'])
;

$services->set(CreateRedirectBasedTransactionAction::class)
->args([
service('router'),
service('commerce_weavers_tpay.tpay.factory.create_redirect_based_payment_payload'),
service('commerce_weavers_tpay.payum.factory.token.notify'),
])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_redirect_based_transaction'])
;

$services->set(NotifyAction::class)
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.notify'])
;

$services->set(PayWithCardAction::class)
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.pay_with_card'])
;

$services->set(GetStatusAction::class)
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.get_status'])
;
Expand Down
11 changes: 11 additions & 0 deletions config/services/payum/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\CreateTransactionFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\NotifyFactory;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\NotifyFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\Token\NotifyTokenFactory;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\Token\NotifyTokenFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;
use Payum\Core\Bridge\Symfony\Builder\GatewayFactoryBuilder;

Expand All @@ -28,4 +30,13 @@
$services->set('commerce_weavers.tpay.payum.factory.create_transaction', CreateTransactionFactory::class)
->alias(CreateTransactionFactoryInterface::class, 'commerce_weavers.tpay.payum.factory.create_transaction')
;

$services->set('commerce_weavers_tpay.payum.factory.token.notify', NotifyTokenFactory::class)
->args([
service('payum'),
service('router'),
param('commerce_weavers_tpay.payum.create_transaction.notify_route'),
])
->alias(NotifyTokenFactoryInterface::class, 'commerce_weavers_tpay.payum.factory.token.notify')
;
};
30 changes: 30 additions & 0 deletions config/services/tpay.php
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')
;
};
11 changes: 11 additions & 0 deletions src/Form/Type/TpayPaymentDetailsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

final class TpayPaymentDetailsType extends AbstractType
{
Expand All @@ -21,5 +23,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'property_path' => '[card]'
]
);

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData() ?? [];
$form = $event->getForm();

if (!isset($data['card'])) {
$form->remove('card');
}
});
}
}
46 changes: 46 additions & 0 deletions src/Payum/Action/Api/AbstractCreateTransactionAction.php
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');
}
}
68 changes: 68 additions & 0 deletions src/Payum/Action/Api/CreateCardTransactionAction.php
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 src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php
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']);
}
}
Loading

0 comments on commit ef3bfe8

Please sign in to comment.