Skip to content

Commit

Permalink
Add support for UUID for CreditCard
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed Oct 30, 2024
1 parent 02f7073 commit c79729e
Show file tree
Hide file tree
Showing 24 changed files with 109 additions and 50 deletions.
15 changes: 12 additions & 3 deletions config/api_resources/credit_card.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
'%commerce_weavers_sylius_tpay.model.credit_card.class%':
collectionOperations:
get:
shop_get:
method: 'GET'
path: /shop/credit-cards
normalization_context:
groups:
- 'commerce_weavers_sylius_tpay:shop:credit_card:index'
itemOperations:
get:
shop_get:
method: 'GET'
path: /shop/credit-cards/{id}
delete:
normalization_context:
groups:
- 'commerce_weavers_sylius_tpay:shop:credit_card:show'
shop_delete:
method: 'DELETE'
path: /shop/credit-cards/{id}
properties:
id:
Expand Down
4 changes: 1 addition & 3 deletions config/doctrine/CreditCard.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
>
<mapped-superclass name="CommerceWeavers\SyliusTpayPlugin\Entity\CreditCard" table="cw_sylius_tpay_credt_card">
<id name="id" column="id" type="integer">
<generator />
</id>
<id name="id" column="id" />

<field name="token" column="token" />
<field name="brand" column="brand" />
Expand Down
1 change: 1 addition & 0 deletions config/routes_shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
$routes->add(Routing::SHOP_ACCOUNT_CREDIT_CARD_DELETE, Routing::SHOP_ACCOUNT_CREDIT_CARD_DELETE_PATH)
->controller('commerce_weavers_sylius_tpay.controller.credit_card::deleteAction')
->methods([Request::METHOD_DELETE])
->requirements(['id' => '[0-9a-f]{8}-[0-9a-f]{4}-[1-6][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}'])
->defaults([
'_sylius' => [
'section' => 'shop_account',
Expand Down
25 changes: 25 additions & 0 deletions config/serialization/CreditCard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" ?>

<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="CommerceWeavers\SyliusTpayPlugin\Entity\CreditCard">
<attribute name="id">
<group>commerce_weavers_sylius_tpay:shop:credit_card:index</group>
<group>commerce_weavers_sylius_tpay:shop:credit_card:show</group>
</attribute>
<attribute name="brand">
<group>commerce_weavers_sylius_tpay:shop:credit_card:index</group>
<group>commerce_weavers_sylius_tpay:shop:credit_card:show</group>
</attribute>
<attribute name="tail">
<group>commerce_weavers_sylius_tpay:shop:credit_card:index</group>
<group>commerce_weavers_sylius_tpay:shop:credit_card:show</group>
</attribute>
<attribute name="expirationDate">
<group>commerce_weavers_sylius_tpay:shop:credit_card:index</group>
<group>commerce_weavers_sylius_tpay:shop:credit_card:show</group>
</attribute>
</class>
</serializer>
2 changes: 2 additions & 0 deletions migrations/Version20241029160137.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function up(Schema $schema): void
$this->addSql('ALTER TABLE cw_sylius_tpay_credt_card ADD channel_id INT NOT NULL');
$this->addSql('ALTER TABLE cw_sylius_tpay_credt_card ADD CONSTRAINT FK_9FF1996C72F5A1AA FOREIGN KEY (channel_id) REFERENCES sylius_channel (id) ON DELETE CASCADE');
$this->addSql('CREATE INDEX IDX_9FF1996C72F5A1AA ON cw_sylius_tpay_credt_card (channel_id)');
$this->addSql('ALTER TABLE cw_sylius_tpay_credt_card CHANGE id id VARCHAR(255) NOT NULL');
}

public function down(Schema $schema): void
Expand All @@ -30,5 +31,6 @@ public function down(Schema $schema): void
$this->addSql('ALTER TABLE cw_sylius_tpay_credt_card DROP FOREIGN KEY FK_9FF1996C72F5A1AA');
$this->addSql('DROP INDEX IDX_9FF1996C72F5A1AA ON cw_sylius_tpay_credt_card');
$this->addSql('ALTER TABLE cw_sylius_tpay_credt_card DROP channel_id');
$this->addSql('ALTER TABLE cw_sylius_tpay_credt_card CHANGE id id INT AUTO_INCREMENT NOT NULL');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241030113142 extends AbstractMigration
final class Version20241030214842 extends AbstractMigration
{
public function getDescription(): string
{
Expand All @@ -20,12 +20,11 @@ public function getDescription(): string
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE cw_sylius_tpay_blik_alias CHANGE registered registered TINYINT(1) DEFAULT false NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE cw_sylius_tpay_blik_alias CHANGE registered registered TINYINT(1) DEFAULT 0 NOT NULL');
$this->addSql('ALTER TABLE cw_sylius_tpay_credt_card CHANGE id id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\'');
}
}
2 changes: 1 addition & 1 deletion src/Api/Command/Pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
public readonly ?string $blikAliasApplicationCode = null,
public readonly ?string $googlePayToken = null,
public readonly ?string $encodedCardData = null,
public readonly ?int $savedCardId = null,
public readonly ?string $savedCardId = null,
public readonly ?bool $saveCard = null,
public readonly ?string $tpayChannelId = null,
public readonly ?string $visaMobilePhoneNumber = null,
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Command/PayBySavedCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class PayBySavedCard
{
public function __construct(
public readonly int $paymentId,
public readonly int $savedCardId,
public readonly string $savedCardId,
) {
}
}
2 changes: 1 addition & 1 deletion src/Api/Command/PayBySavedCardHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __invoke(PayBySavedCard $command): PayResult
return $this->createResultFrom($payment);
}

private function setTransactionData(PaymentInterface $payment, int $savedCardId): void
private function setTransactionData(PaymentInterface $payment, string $savedCardId): void
{
$paymentDetails = PaymentDetails::fromArray($payment->getDetails());
$paymentDetails->setUseSavedCreditCard($savedCardId);
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Factory/NextCommand/PayBySavedCardFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function create(Pay $command, PaymentInterface $payment): PayBySavedCard

/** @var int $paymentId */
$paymentId = $payment->getId();
/** @var int $savedCardId */
/** @var string $savedCardId */
$savedCardId = $command->savedCardId;

return new PayBySavedCard($paymentId, $savedCardId);
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use CommerceWeavers\SyliusTpayPlugin\Entity\CreditCard;
use CommerceWeavers\SyliusTpayPlugin\Entity\CreditCardInterface;
use CommerceWeavers\SyliusTpayPlugin\Factory\BlikAliasFactory;
use CommerceWeavers\SyliusTpayPlugin\Factory\CreditCardFactory;
use CommerceWeavers\SyliusTpayPlugin\Repository\BlikAliasRepository;
use CommerceWeavers\SyliusTpayPlugin\Repository\CreditCardRepository;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
Expand Down Expand Up @@ -62,7 +63,7 @@ private function addResourcesSection(ArrayNodeDefinition $node): void
->scalarNode('model')->defaultValue(CreditCard::class)->cannotBeEmpty()->end()
->scalarNode('interface')->defaultValue(CreditCardInterface::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(CreditCardFactory::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(CreditCardRepository::class)->cannotBeEmpty()->end()
->end()
->end()
Expand Down
18 changes: 4 additions & 14 deletions src/Entity/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

class CreditCard implements CreditCardInterface
{
private ?int $id = null;

private ?string $uid = null;
public function __construct(private ?string $id = null)
{
}

private ?string $token = null;

Expand All @@ -25,21 +25,11 @@ class CreditCard implements CreditCardInterface

private ?ChannelInterface $channel = null;

public function getId(): ?int
public function getId(): ?string
{
return $this->id;
}

public function getUid(): ?string
{
return $this->uid;
}

public function setUid(?string $uid): void
{
$this->uid = $uid;
}

public function getToken(): ?string
{
return $this->token;
Expand Down
4 changes: 0 additions & 4 deletions src/Entity/CreditCardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

interface CreditCardInterface extends ResourceInterface
{
public function getUid(): ?string;

public function setUid(?string $uid): void;

public function getToken(): ?string;

public function setToken(?string $token): void;
Expand Down
29 changes: 29 additions & 0 deletions src/Factory/CreditCardFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Factory;

use CommerceWeavers\SyliusTpayPlugin\Entity\CreditCardInterface;
use Symfony\Component\Uid\Uuid;

final class CreditCardFactory implements CreditCardFactoryInterface
{
/**
* @param class-string<CreditCardInterface> $className
*/
public function __construct(private readonly string $className)
{
if (!is_a($className, CreditCardInterface::class, true)) {
throw new \DomainException(sprintf(
'This factory requires %s or its descend to be used as resource',
CreditCardInterface::class,
));
}
}

public function createNew(): CreditCardInterface
{
return new $this->className(Uuid::v4()->toRfc4122());
}
}
11 changes: 11 additions & 0 deletions src/Factory/CreditCardFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace CommerceWeavers\SyliusTpayPlugin\Factory;

use CommerceWeavers\SyliusTpayPlugin\Entity\CreditCardInterface;
use Sylius\Resource\Factory\FactoryInterface;

interface CreditCardFactoryInterface extends FactoryInterface
{
public function createNew(): CreditCardInterface;
}
1 change: 1 addition & 0 deletions src/Form/EventListener/AddSavedCreditCardsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Component\VarDumper\VarDumper;
use Symfony\Contracts\Translation\TranslatorInterface;

final class AddSavedCreditCardsListener
Expand Down
6 changes: 3 additions & 3 deletions src/Model/PaymentDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
#[\SensitiveParameter]
private ?string $encodedCardData = null,
private bool $saveCreditCardForLater = false,
private ?int $useSavedCreditCard = null,
private ?string $useSavedCreditCard = null,
#[\SensitiveParameter]
private ?string $applePaySession = null,
private ?string $paymentUrl = null,
Expand Down Expand Up @@ -136,12 +136,12 @@ public function setSaveCreditCardForLater(bool $saveCreditCardForLater): void
$this->saveCreditCardForLater = $saveCreditCardForLater;
}

public function getUseSavedCreditCard(): ?int
public function getUseSavedCreditCard(): ?string
{
return $this->useSavedCreditCard;
}

public function setUseSavedCreditCard(?int $useSavedCreditCard): void
public function setUseSavedCreditCard(?string $useSavedCreditCard): void
{
$this->useSavedCreditCard = $useSavedCreditCard;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Payum/Action/Api/SaveCreditCardAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Resource\Factory\FactoryInterface;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final class SaveCreditCardAction extends BasePaymentAwareAction implements GatewayAwareInterface
Expand All @@ -37,7 +36,6 @@ protected function doExecute(Generic $request, PaymentInterface $model, PaymentD
/** @var CreditCardInterface $creditCard */
$creditCard = $this->creditCardFactory->createNew();

$creditCard->setUid(Uuid::v4()->toRfc4122());
$creditCard->setToken($request->getCardToken());
$creditCard->setBrand($request->getCardBrand());
$creditCard->setTail($request->getCardTail());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CommerceWeavers\SyliusTpayPlugin\Entity\CreditCard:
single_credit_card:
__construct: ['e0f79275-18ef-4edf-b8fc-adc40fdcbcc0']
brand: 'Visa'
expirationDate: "<(new \\DateTime('+1 year'))>"
tail: '1234'
Expand Down
4 changes: 2 additions & 2 deletions tests/Api/Shop/PayingForOrdersByCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function test_paying_with_a_valid_encrypted_card_data_for_an_order(): voi

public function test_paying_with_a_saved_card_data_for_an_order(): void
{
$loadFixtures = $this->loadFixturesFromDirectory('shop/paying_for_orders_by_card');
$this->loadFixturesFromDirectory('shop/paying_for_orders_by_card');

$order = $this->doPlaceOrder('t0k3n', paymentMethodCode: 'tpay_card');

Expand All @@ -68,7 +68,7 @@ public function test_paying_with_a_saved_card_data_for_an_order(): void
content: json_encode([
'successUrl' => 'https://example.com/success',
'failureUrl' => 'https://example.com/failure',
'savedCardId' => $loadFixtures['single_credit_card']->getId(),
'savedCardId' => 'e0f79275-18ef-4edf-b8fc-adc40fdcbcc0',
]),
);

Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Api/Command/PayBySavedCardHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ public function test_it_throw_an_exception_if_a_payment_cannot_be_found(): void

$this->paymentRepository->find(1)->willReturn(null);

$this->createTestSubject()->__invoke(new PayBySavedCard(1, 1));
$this->createTestSubject()->__invoke(new PayBySavedCard(1, 'e0f79275-18ef-4edf-b8fc-adc40fdcbcc0'));
}

public function test_it_creates_a_card_based_transaction(): void
{
$payment = $this->prophesize(PaymentInterface::class);
$payment->getDetails()->willReturn([], ['tpay' => ['status' => 'pending', 'payment_url' => 'https://cw.org/pay']]);
$payment->setDetails(
$this->getExpectedDetails(use_saved_credit_card: 1),
$this->getExpectedDetails(use_saved_credit_card: 'e0f79275-18ef-4edf-b8fc-adc40fdcbcc0'),
)->shouldBeCalled();

$this->paymentRepository->find(1)->willReturn($payment);

$result = $this->createTestSubject()->__invoke(new PayBySavedCard(1, 1));
$result = $this->createTestSubject()->__invoke(new PayBySavedCard(1, 'e0f79275-18ef-4edf-b8fc-adc40fdcbcc0'));

$this->assertSame('pending', $result->status);
$this->assertSame('https://cw.org/pay', $result->transactionPaymentUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private function createCommand(?string $token = null): Pay
'https://cw.nonexisting/success',
'https://cw.nonexisting/failure',
encodedCardData: 'card_data',
savedCardId: 1,
savedCardId: 'e0f79275-18ef-4edf-b8fc-adc40fdcbcc0',
);
}

Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Api/Factory/NextCommand/PayBySavedCardFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ public function test_it_does_not_support_a_command_without_a_payment_with_id():
{
$factory = $this->createTestSubject();

$this->assertFalse($factory->supports($this->createCommand(savedCardId: 1), new Payment()));
$this->assertFalse($factory->supports($this->createCommand(savedCardId: 'e0f79275-18ef-4edf-b8fc-adc40fdcbcc0'), new Payment()));
}

public function test_it_supports_a_command_with_an_saved_card_data(): void
{
$factory = $this->createTestSubject();

$this->assertTrue($factory->supports($this->createCommand(savedCardId: 1), $this->createPayment()));
$this->assertTrue($factory->supports($this->createCommand(savedCardId: 'e0f79275-18ef-4edf-b8fc-adc40fdcbcc0'), $this->createPayment()));
}

public function test_it_creates_a_pay_by_saved_card_command(): void
{
$command = $this->createTestSubject()->create($this->createCommand(savedCardId: 1), $this->createPayment());
$command = $this->createTestSubject()->create($this->createCommand(savedCardId: 'e0f79275-18ef-4edf-b8fc-adc40fdcbcc0'), $this->createPayment());

$this->assertInstanceOf(PayBySavedCard::class, $command);
$this->assertSame(1, $command->savedCardId);
$this->assertSame('e0f79275-18ef-4edf-b8fc-adc40fdcbcc0', $command->savedCardId);
}

public function test_it_throws_an_exception_when_trying_to_create_a_command_with_unsupported_factory(): void
Expand All @@ -54,7 +54,7 @@ public function test_it_throws_an_exception_when_trying_to_create_a_command_with
$this->createTestSubject()->create($this->createCommand(), new Payment());
}

private function createCommand(?string $token = null, ?int $savedCardId = null, bool $saveCard = false): Pay
private function createCommand(?string $token = null, ?string $savedCardId = null, bool $saveCard = false): Pay
{
return new Pay(
$token ?? 'token',
Expand Down
Loading

0 comments on commit c79729e

Please sign in to comment.