Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Sep 10, 2024
1 parent c032571 commit e8ade80
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 99 deletions.
22 changes: 19 additions & 3 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,34 @@
->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'),
])
->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'),
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'),
])
->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
58 changes: 58 additions & 0 deletions src/Payum/Action/Api/AbstractCreateTransactionAction.php
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

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine

Cannot call method getLocaleCode() on Sylius\Component\Core\Model\OrderInterface|null.

Check failure on line 47 in src/Payum/Action/Api/AbstractCreateTransactionAction.php

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow

Cannot call method getLocaleCode() on Sylius\Component\Core\Model\OrderInterface|null.
}

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),
);
}
}
66 changes: 66 additions & 0 deletions src/Payum/Action/Api/CreateCardTransactionAction.php
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

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine

Property CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateCardTransactionAction::$router is never read, only written.

Check failure on line 22 in src/Payum/Action/Api/CreateCardTransactionAction.php

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow

Property CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateCardTransactionAction::$router is never read, only written.
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

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine

Cannot call method getModel() on mixed.

Check failure on line 52 in src/Payum/Action/Api/CreateCardTransactionAction.php

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow

Cannot call method getModel() on mixed.

if (!$request instanceof CreateTransaction) {
return false;
}

if (!$model instanceof PaymentInterface) {
return false;
}

$details = $model->getDetails();

return isset($details['tpay']['card']);
}
}
66 changes: 66 additions & 0 deletions src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php
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

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine

Property CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction::$router is never read, only written.

Check failure on line 20 in src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow

Property CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction::$router is never read, only written.
private CreateRedirectBasedPaymentPayloadFactoryInterface $createRedirectBasedPaymentPayloadFactory,
private string $notifyRoute,

Check failure on line 22 in src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine

Property CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction::$notifyRoute is never read, only written.

Check failure on line 22 in src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow

Property CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction::$notifyRoute is never read, only written.
) {
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

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine

Cannot call method getModel() on mixed.

Check failure on line 52 in src/Payum/Action/Api/CreateRedirectBasedTransactionAction.php

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow

Cannot call method getModel() on mixed.

if (!$request instanceof CreateTransaction) {
return false;
}

if (!$model instanceof PaymentInterface) {
return false;
}

$details = $model->getDetails();

return !isset($details['tpay']['card']) && !isset($details['tpay']['blik']);
}
}
90 changes: 0 additions & 90 deletions src/Payum/Action/Api/CreateTransactionAction.php

This file was deleted.

42 changes: 42 additions & 0 deletions src/Payum/Action/Api/PayWithCardAction.php
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;
}
}
4 changes: 1 addition & 3 deletions src/Payum/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public function execute($request): void
$this->createTransactionFactory->createNewWithModel($request->getToken()),
);

$paymentDetails = $model->getDetails();

throw new HttpRedirect($paymentDetails['tpay']['transaction_payment_url']);
throw new HttpRedirect($request->getToken()->getAfterUrl());

Check failure on line 36 in src/Payum/Action/CaptureAction.php

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.12.*, PHP 8.0, Symfony 5.4.*, MySQL 8.0, State Machine winzou_state_machine

Cannot call method getAfterUrl() on Payum\Core\Security\TokenInterface|null.

Check failure on line 36 in src/Payum/Action/CaptureAction.php

View workflow job for this annotation

GitHub Actions / CI / Sylius 1.13.*, PHP 8.3, Symfony 6.4.*, MySQL 8.0, State Machine symfony_workflow

Cannot call method getAfterUrl() on Payum\Core\Security\TokenInterface|null.
}

public function supports($request): bool
Expand Down
11 changes: 11 additions & 0 deletions src/Payum/Request/Api/PayWithCard.php
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
{
}
Loading

0 comments on commit e8ade80

Please sign in to comment.