Skip to content

Commit

Permalink
Extract credit cards to separate form type and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed Nov 4, 2024
1 parent 5347811 commit 3610926
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 65 deletions.
9 changes: 9 additions & 0 deletions config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use CommerceWeavers\SyliusTpayPlugin\Form\Extension\CompleteTypeExtension;
use CommerceWeavers\SyliusTpayPlugin\Form\Extension\PaymentTypeExtension;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayCardType;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayCreditCardChoiceType;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayGatewayConfigurationType;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayPaymentDetailsType;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;
Expand Down Expand Up @@ -50,6 +51,13 @@
->args([
service('commerce_weavers_sylius_tpay.form.event_listener.remove_unnecessary_payment_details_fields'),
service('security.token_storage'),
])
->tag('form.type')
;

$services->set(TpayCreditCardChoiceType::class)
->args([
service('security.token_storage'),
service('translator'),
service('commerce_weavers_sylius_tpay.repository.credit_card'),
service('sylius.context.cart'),
Expand Down Expand Up @@ -77,6 +85,7 @@

$services
->set('commerce_weavers_sylius_tpay.form.event_listener.add_saved_credit_cards', AddSavedCreditCardsListener::class)
->deprecate('commerce-weavers/sylius-tpay-plugin', '2.0', 'The "%service_id%" service is deprecated and will be removed in the version 2.0 as it is considered daed code.')
->args([
service('security.token_storage'),
service('translator'),
Expand Down
18 changes: 0 additions & 18 deletions config/services/payum/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateApplePayTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateBlikLevelZeroTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateCardTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateGooglePayTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreatePayByLinkTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateVisaMobileTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\GetTpayTransactionsChannelsAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\InitializeApplePayPaymentAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\NotifyAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\PayWithCardAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\SaveCreditCardAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\GetStatusAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\RefundAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\ResolveNextRouteAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\Payum\Mapper\PayWithCardActionPayloadMapper;
use CommerceWeavers\SyliusTpayPlugin\Payum\Mapper\PayWithCardActionPayloadMapperInterface;

return static function(ContainerConfigurator $container): void {
$services = $container->services();
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Command/PayByCardHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __invoke(PayByCard $command): PayResult
return $this->createResultFrom($payment);
}

private function setTransactionData(PaymentInterface $payment, string $encodedCardData, bool $saveCard = false): void
private function setTransactionData(PaymentInterface $payment, string $encodedCardData, bool $saveCard): void
{
$paymentDetails = PaymentDetails::fromArray($payment->getDetails());
$paymentDetails->setEncodedCardData($encodedCardData);
Expand Down
3 changes: 3 additions & 0 deletions src/Form/EventListener/AddSavedCreditCardsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @deprecated Deprecated since CommerceWeavers/TpayPlugin 1.0.1, will be removed in 2.0.0. Use \CommerceWeavers\SyliusTpayPlugin\Form\Type\TpayCreditCardChoiceType instead.
*/
final class AddSavedCreditCardsListener
{
public function __construct(
Expand Down
91 changes: 91 additions & 0 deletions src/Form/Type/TpayCreditCardChoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Form\Type;

use CommerceWeavers\SyliusTpayPlugin\Repository\CreditCardRepositoryInterface;
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\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Contracts\Translation\TranslatorInterface;

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

public function configureOptions(OptionsResolver $resolver): void
{
$token = $this->tokenStorage->getToken();

$user = $token?->getUser();

if (!$user instanceof ShopUserInterface) {
return;
}

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

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

$choices = [];

if (count($creditCards) !== 0) {
$choices = $this->mapChoices($creditCards);
}

$resolver->setDefaults([
'choices' => $choices,
'required' => false,
'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'),
]);
}

public function getParent(): string
{
return ChoiceType::class;
}

public function getBlockPrefix(): string
{
return 'cw_tpay_credit_card_choice';
}

private function mapChoices(array $creditCards): array
{
$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();
}

return $choices;
}
}
46 changes: 1 addition & 45 deletions src/Form/Type/TpayPaymentDetailsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,25 @@

namespace CommerceWeavers\SyliusTpayPlugin\Form\Type;

use CommerceWeavers\SyliusTpayPlugin\Repository\CreditCardRepositoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\EncodedGooglePayToken;
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ValidTpayChannel;
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 TokenStorageInterface $tokenStorage,
private readonly TranslatorInterface $translator,
private readonly CreditCardRepositoryInterface $creditCardRepository,
private readonly CartContextInterface $cartContext,
) {
}

Expand Down Expand Up @@ -135,44 +125,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
)
;

/** @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,
],
TpayCreditCardChoiceType::class,
)
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Sylius\Component\Core\Model\ShopUser:
username: '[email protected]'
usernameCanonical: '[email protected]'


Sylius\Component\Core\Model\Product:
product_mug:
code: 'MUG'
Expand Down
Loading

0 comments on commit 3610926

Please sign in to comment.