Skip to content

Commit

Permalink
Improve fields definitions and fix flow
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed Oct 31, 2024
1 parent 4d352a5 commit 1d4859a
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 15 deletions.
4 changes: 3 additions & 1 deletion config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
$services->set(TpayPaymentDetailsType::class)
->args([
service('commerce_weavers_sylius_tpay.form.event_listener.remove_unnecessary_payment_details_fields'),
service('commerce_weavers_sylius_tpay.form.event_listener.add_saved_credit_cards'),
service('security.token_storage'),
service('translator'),
service('commerce_weavers_sylius_tpay.repository.credit_card'),
service('sylius.context.cart'),
])
->tag('form.type')
;
Expand Down
5 changes: 5 additions & 0 deletions src/Entity/CreditCardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CommerceWeavers\SyliusTpayPlugin\Entity;

use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Resource\Model\ResourceInterface;

Expand All @@ -28,4 +29,8 @@ public function setExpirationDate(?\DateTimeInterface $expirationDate): void;
public function getCustomer(): ?CustomerInterface;

public function setCustomer(?CustomerInterface $customer): void;

public function getChannel(): ?ChannelInterface;

public function setChannel(?ChannelInterface $channel): void;
}
1 change: 1 addition & 0 deletions src/Form/EventListener/AddSavedCreditCardsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(
public function __invoke(FormEvent $event): void
{
$form = $event->getForm();

/** @var FormInterface $form */
$form = $form->getParent();

Expand Down
59 changes: 52 additions & 7 deletions src/Form/Type/TpayPaymentDetailsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@

namespace CommerceWeavers\SyliusTpayPlugin\Form\Type;

use CommerceWeavers\SyliusTpayPlugin\Repository\CreditCardRepositoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\EncodedGooglePayToken;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Contracts\Translation\TranslatorInterface;

final class TpayPaymentDetailsType extends AbstractType
{
public function __construct(
private readonly object $removeUnnecessaryPaymentDetailsFieldsListener,
private readonly object $addSavedCreditCardsListener,
private readonly TokenStorageInterface $tokenStorage,
private readonly TranslatorInterface $translator,
private readonly CreditCardRepositoryInterface $creditCardRepository,
private readonly CartContextInterface $cartContext,
) {
}

Expand Down Expand Up @@ -107,24 +116,60 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
[$this->removeUnnecessaryPaymentDetailsFieldsListener, '__invoke'],
);

$builder->addEventListener(
FormEvents::PRE_SET_DATA,
[$this->addSavedCreditCardsListener, '__invoke'],
);

$token = $this->tokenStorage->getToken();
$user = $token?->getUser();

if ($user instanceof ShopUserInterface) {
$builder
->add(
'saveCreditCardForLater',
'save_credit_card_for_later',
CheckboxType::class,
[
'label' => 'commerce_weavers_sylius_tpay.shop.order_summary.card.save_credit_card_for_later.label',
],
)
;

/** @var OrderInterface $order */
$order = $this->cartContext->getCart();
$channel = $order->getChannel();
/** @var CustomerInterface $customer */
$customer = $user->getCustomer();

$creditCards = $this->creditCardRepository->findByCustomerAndChannel($customer, $channel);

if (count($creditCards) === 0) {
return;
}

$choices = [];

foreach ($creditCards as $creditCard) {
$stringifiedCard = $this->translator->trans(
'commerce_weavers_sylius_tpay.shop.credit_card.card_selection_one_liner',
[
'%brand%' => $creditCard->getBrand(),
'%tail%' => $creditCard->getTail(),
'%expires%' => $creditCard->getExpirationDate()->format('m-Y'),
],
'messages',
);

$choices[$stringifiedCard] = $creditCard->getId();
}

$builder
->add(
'use_saved_credit_card',
ChoiceType::class,
[
'label' => 'commerce_weavers_sylius_tpay.shop.order_summary.card.use_saved_credit_card.label',
'placeholder' => new TranslatableMessage('commerce_weavers_sylius_tpay.shop.credit_card.use_new_card'),
'required' => false,
'choices' => $choices,
],
)
;
}
}
}
3 changes: 1 addition & 2 deletions src/Model/PaymentDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ public function setTpayChannelId(?string $tpayChannelId): void
public function getType(): string
{
return match (true) {
null !== $this->getEncodedCardData() => PaymentType::CARD,
null !== $this->getUseSavedCreditCard() => PaymentType::CARD,
null !== $this->getEncodedCardData(), null !== $this->getUseSavedCreditCard() => PaymentType::CARD,
null !== $this->getBlikToken() => PaymentType::BLIK,
null !== $this->getTpayChannelId() => PaymentType::PAY_BY_LINK,
null !== $this->getGooglePayToken() => PaymentType::GOOGLE_PAY,
Expand Down
1 change: 1 addition & 0 deletions src/Payum/Action/Api/SaveCreditCardAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected function doExecute(Generic $request, PaymentInterface $model, PaymentD
Assert::isInstanceOf($customer, CustomerInterface::class);

$creditCard->setCustomer($customer);
$creditCard->setChannel($order?->getChannel());

$expiryDate = $request->getTokenExpiryDate();

Expand Down
10 changes: 5 additions & 5 deletions templates/shop/payment/_card.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<div class="ui small icon message">
<i class="payment icon"></i>
<div class="content">
{% if form.tpay.useSavedCreditCard is defined %}
{% if form.tpay.use_saved_credit_card is defined %}
<div class="field">
<div class="field required" data-tpay-field>
{{ form_label(form.tpay.useSavedCreditCard) }}
{{ form_label(form.tpay.use_saved_credit_card) }}
<div class="ui left icon input">
{{ form_widget(form.tpay.useSavedCreditCard, { attr: {'data-tpay-saved-card': ''}}) }}
{{ form_widget(form.tpay.use_saved_credit_card, { attr: {'data-tpay-saved-card': ''}}) }}
</div>
</div>
</div>
Expand Down Expand Up @@ -56,9 +56,9 @@
</div>
</div>
</div>
{% if form.tpay.saveCreditCardForLater is defined %}
{% if form.tpay.save_credit_card_for_later is defined %}
<div class="three wide field" data-tpay-save-card>
{{ form_row(form.tpay.saveCreditCardForLater) }}
{{ form_row(form.tpay.save_credit_card_for_later) }}
<div data-tpay-error-container></div>
</div>
{% endif %}
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Payum/Action/Api/SaveCreditCardActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
Expand All @@ -34,6 +35,8 @@ final class SaveCreditCardActionTest extends TestCase

private CustomerInterface|ObjectProphecy $customer;

private ChannelInterface|ObjectProphecy $channel;

private TpayApi|ObjectProphecy $api;

private BasicPaymentFactoryInterface|ObjectProphecy $factory;
Expand All @@ -53,6 +56,9 @@ protected function setUp(): void
$this->customer = $this->prophesize(CustomerInterface::class);
$order->getCustomer()->willReturn($this->customer->reveal());

$this->channel = $this->prophesize(ChannelInterface::class);
$order->getChannel()->willReturn($this->channel->reveal());

$this->model = $this->prophesize(PaymentInterface::class);
$this->model->getOrder()->willReturn($order->reveal());
$this->model->getDetails()->willReturn([]);
Expand Down Expand Up @@ -92,6 +98,7 @@ public function test_it_saves_returned_credit_card(): void
$creditCard->setToken('card_token')->shouldBeCalled();
$creditCard->setExpirationDate(new \DateTimeImmutable('01-11-2028'))->shouldBeCalled();
$creditCard->setCustomer($this->customer)->shouldBeCalled();
$creditCard->setChannel($this->channel)->shouldBeCalled();

$this->repository->add($creditCard->reveal())->shouldBeCalled();

Expand Down

0 comments on commit 1d4859a

Please sign in to comment.