Skip to content

Commit

Permalink
refactor type extension
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Sep 9, 2024
1 parent bf745c9 commit b1d6dd5
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 7 deletions.
3 changes: 1 addition & 2 deletions config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyClientSecretListener;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\CompleteTypeExtension;
use CommerceWeavers\SyliusTpayPlugin\Form\Extension\CompleteTypeExtension;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayGatewayConfigurationType;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;
use Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType;

return function(ContainerConfigurator $container): void {
$services = $container->services();
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/OrderLastNewPaymentAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Entity;

use Sylius\Component\Core\Model\PaymentInterface;

interface OrderLastNewPaymentAwareInterface
{
public function getLastNewPayment(): ?PaymentInterface;
}
15 changes: 15 additions & 0 deletions src/Entity/OrderLastNewPaymentAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Entity;

use Sylius\Component\Core\Model\PaymentInterface;

trait OrderLastNewPaymentAwareTrait
{
public function getLastNewPayment(): ?PaymentInterface
{
return $this->getLastPayment('cart');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Form\Type;
namespace CommerceWeavers\SyliusTpayPlugin\Form\Extension;

use CommerceWeavers\SyliusTpayPlugin\Form\Type\PaymentDetailsType;
use Sylius\Bundle\CoreBundle\Form\Type\Checkout\CompleteType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
Expand All @@ -21,7 +22,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->add('others', PaymentDetailsType::class, [
'label' => 'commerce_weavers_sylius_tpay.payment.blik.token',
// TODO missing validation
'property_path' => 'payments[0].details', // TODO looks awfull and what about other payments?
'property_path' => 'last_new_payment.details["tpay"]',
'required' => false,
]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Payum/Action/Api/CreateBlik0TransactionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function execute($request): void
$billingAddress = $order->getBillingAddress();
$notifyToken = $this->createNotifyToken($model, $request->getToken(), $localeCode);

$blikToken = $model->getDetails()['blik'];
$blikToken = $model->getDetails()['tpay']['blik'];

$response = $this->api->transactions()->createTransaction([
'amount' => number_format($model->getAmount() / 100, 2, thousands_separator: ''),
Expand All @@ -71,7 +71,6 @@ public function execute($request): void
],
]);

unset($details['blik']);
$details['tpay']['transaction_id'] = $response['transactionId'];
$details['tpay']['status'] = $response['status'];

Expand Down
4 changes: 3 additions & 1 deletion src/Payum/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function supports($request): bool

private function transactionIsBlik($model): bool
{
return array_key_exists('blik', $model->getDetails());
return array_key_exists('tpay', $model->getDetails())
&& array_key_exists('blik', $model->getDetails()['tpay'])
;
}
}
6 changes: 6 additions & 0 deletions tests/Application/config/packages/_sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ sylius_shop:

sylius_api:
enabled: true

sylius_order:
resources:
order:
classes:
model: Tests\CommerceWeavers\SyliusTpayPlugin\Application\Entity\Order
17 changes: 17 additions & 0 deletions tests/Application/src/Entity/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Entity;

use CommerceWeavers\SyliusTpayPlugin\Entity\OrderLastNewPaymentAwareInterface;
use CommerceWeavers\SyliusTpayPlugin\Entity\OrderLastNewPaymentAwareTrait;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\Order as BaseOrder;

#[ORM\Entity]
#[ORM\Table(name: 'sylius_order')]
class Order extends BaseOrder implements OrderLastNewPaymentAwareInterface
{
use OrderLastNewPaymentAwareTrait;
}
161 changes: 161 additions & 0 deletions tests/Unit/Payum/Action/Api/CreateBlik0TransactionActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

declare(strict_types=1);

namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Payum\Action\Api;

use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateBlik0TransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateBlik0Transaction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction;
use Payum\Core\Request\Sync;
use Payum\Core\Security\GenericTokenFactoryInterface;
use Payum\Core\Security\TokenInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Tpay\OpenApi\Api\TpayApi;
use Tpay\OpenApi\Api\Transactions\TransactionsApi;

final class CreateBlik0TransactionActionTest extends TestCase
{
use ProphecyTrait;

private CreateBlik0Transaction|ObjectProphecy $request;
private PaymentInterface|ObjectProphecy $model;
private TpayApi|ObjectProphecy $api;
private RouterInterface|ObjectProphecy $router;
private GenericTokenFactoryInterface|ObjectProphecy $tokenFactory;

protected function setUp(): void
{
$this->request = $this->prophesize(CreateTransaction::class);
$this->model = $this->prophesize(PaymentInterface::class);
$this->api = $this->prophesize(TpayApi::class);
$this->router = $this->prophesize(RouterInterface::class);
$this->tokenFactory = $this->prophesize(GenericTokenFactoryInterface::class);

$this->request->getModel()->willReturn($this->model->reveal());
}

public function test_it_supports_only_create_blik0_transaction_request(): void
{
$action = $this->createTestSubject();

$this->assertFalse($action->supports(new Sync($this->model->reveal())));
$this->assertTrue($action->supports(new CreateBlik0Transaction($this->model->reveal())));
}

public function test_it_supports_only_payment_interface_based_models(): void
{
$action = $this->createTestSubject();

$this->assertFalse($action->supports(new CreateBlik0Transaction(new \stdClass())));
$this->assertTrue($action->supports(new CreateBlik0Transaction($this->model->reveal())));
}

public function test_it_creates_blik0_transaction(): void
{
$createTransactionToken = $this->prophesize(TokenInterface::class);
$createTransactionToken->getGatewayName()->willReturn('tpay');

$this->request->getToken()->willReturn($createTransactionToken);

$customer = $this->prophesize(CustomerInterface::class);
$customer->getEmail()->willReturn('[email protected]');

$billingAddress = $this->prophesize(AddressInterface::class);
$billingAddress->getFullName()->willReturn('Domino Jahas');

$order = $this->prophesize(OrderInterface::class);
$order->getLocaleCode()->willReturn('en_US');
$order->getCustomer()->willReturn($customer);
$order->getBillingAddress()->willReturn($billingAddress);
$order->getNumber()->willReturn('00000001');

$this->model->getAmount()->willReturn(123);
$this->model->getOrder()->willReturn($order);
$blikCode = '777456';
$this->model->getDetails()->willReturn([
'blik' => $blikCode,
]);
$this->model->setDetails([
'tpay' => [
'transaction_id' => '123awsd',
'transaction_payment_url' => 'https://tpay.pay',
],
])->shouldBeCalled();

$NOTIFY_URL = 'https://cw.org/notify';
$THANK_YOU_URL = 'https://cw.org/thank-you';

$this->tokenFactory->createToken(
'tpay',
$this->model,
$NOTIFY_URL,
)->willReturn($token = $this->prophesize(TokenInterface::class));
$token->getTargetUrl()->willReturn($NOTIFY_URL);

$this->router
->generate('sylius_shop_order_thank_you', ['_locale' => 'en_US', UrlGeneratorInterface::ABSOLUTE_URL])
->willReturn($THANK_YOU_URL)
;

$this->router
->generate('commerce_weavers_tpay_payment_notification', ['_locale' => 'en_US'], UrlGeneratorInterface::ABSOLUTE_URL)
->willReturn($NOTIFY_URL)
;

$transactionsApi = $this->prophesize(TransactionsApi::class);
$transactionsApi->createTransaction([
'amount' => 12.3,
'description' => 'zamówienie #00000001',
'payer' => [
'email' => '[email protected]',
'name' => 'Domino Jahas',
],
'pay' => [
'groupId' => 150,
'blikPaymentData' => [
'blikToken' => $blikCode,
],
],
'callbacks' => [
'payerUrls' => [
'success' => $THANK_YOU_URL,
'error' => $THANK_YOU_URL,
],
'notification' => [
'url' => $NOTIFY_URL,
],
],
])->shouldBeCalled()->willReturn([
'transactionId' => '123awsd',
'transactionPaymentUrl' => 'https://tpay.pay',
]);

$this->api->transactions()->willReturn($transactionsApi);
$this->createTestSubject()->execute($this->request->reveal());
}

private function createTestSubject(): CreateBlik0TransactionAction
{
$action = new CreateBlik0TransactionAction(
$this->router->reveal(),
'sylius_shop_order_thank_you',
'sylius_shop_order_thank_you',
'commerce_weavers_tpay_payment_notification',
);

$action->setApi($this->api->reveal());
$action->setGenericTokenFactory($this->tokenFactory->reveal());

return $action;
}
}

0 comments on commit b1d6dd5

Please sign in to comment.