Skip to content

Commit

Permalink
Fixes after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Oct 17, 2024
1 parent 41f4bd9 commit db2a61b
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 64 deletions.
41 changes: 16 additions & 25 deletions assets/shop/js/_visa_mobile.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import 'intl-tel-input/build/css/intlTelInput.css';

import intlTelInput from 'intl-tel-input';

const inputElement = '#sylius_checkout_complete_tpay_visa_mobile_phone_number';

const input = document.querySelector(inputElement);
const locale = input.dataset.locale;

const iti = intlTelInput(input, {
initialCountry: locale,
strictMode: true,
nationalMode: true,
utilsScript: "/intl-tel-input/js/utils.js?1727952657388",
loadUtilsOnInit: () => import("intl-tel-input/utils"),
});

document.addEventListener('DOMContentLoaded', () => {
const COUNTRY_CODED_MOBILE_PHONE_LENGTH = 11;

let form = document.querySelector('[name="sylius_checkout_complete"]');
let phoneNumber = form.querySelector('[data-visa-mobile-phone-number]');

form.addEventListener('submit', (event) => {
validateVisaMobilePhoneNumber();
validateVisaMobilePhoneNumber(phoneNumber);

const isValid = form.querySelectorAll('.sylius-validation-error').length === 0;

Expand All @@ -33,17 +18,23 @@ document.addEventListener('DOMContentLoaded', () => {
return;
}

phoneNumber.value = iti.getNumber().replace(/\D/g, '');

form.submit();
});

function validateVisaMobilePhoneNumber() {
if (iti.isValidNumber()) {
function validateVisaMobilePhoneNumber(field) {
let fieldLength = field.value.length;

if (fieldLength === COUNTRY_CODED_MOBILE_PHONE_LENGTH) {
clearErrors(phoneNumber);
return;
}

if (fieldLength === 0) {
addError(phoneNumber, 'validationErrorRequired');
return;
}

addError(phoneNumber);
}

Expand All @@ -54,15 +45,15 @@ document.addEventListener('DOMContentLoaded', () => {
errorContainer.innerHTML = '';
}

function addError(field) {
function addError(field, validationErrorName = 'validationErrorLength') {
const tpayField = field.closest('[data-tpay-field]');
const errorContainer = tpayField.querySelector('[data-tpay-error-container]');

errorContainer.innerHTML = createErrorElement(field);
errorContainer.innerHTML = createErrorElement(field, validationErrorName);
}

function createErrorElement(field) {
const errorMessage = field.dataset.validationError;
function createErrorElement(field, validationErrorName = 'validationErrorLength') {
const errorMessage = field.dataset[validationErrorName];

return `
<div class="ui red pointing label sylius-validation-error">
Expand Down
4 changes: 2 additions & 2 deletions config/config/sylius_template_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_card.html.twig',
'priority' => 5,
],
'pay-by-link' => [
'pay_by_link' => [
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_payByLink.html.twig',
'priority' => 5,
],
'visa-mobile' => [
'visa_mobile' => [
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_visaMobile.html.twig',
'priority' => 5,
]
Expand Down
7 changes: 7 additions & 0 deletions config/validation/Pay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
</constraint>
</property>
<property name="tpayChannelId">
<constraint name="CommerceWeavers\SyliusTpayPlugin\Api\Validator\Constraint\NotBlankIfGatewayConfigTypeEquals">
<option name="paymentMethodType">pay_by_link</option>
<option name="fieldRequiredErrorMessage">commerce_weavers_sylius_tpay.shop.pay.encoded_card_data.required</option>
<option name="groups">
<value>commerce_weavers_sylius_tpay:shop:order:pay</value>
</option>
</constraint>
<constraint name="CommerceWeavers\SyliusTpayPlugin\Api\Validator\Constraint\TpayChannelIdEligibility">
<option name="groups">
<value>commerce_weavers_sylius_tpay:shop:order:pay</value>
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"webpack-cli": "^4.10.0",
"webpack-notifier": "^1.15.0"
},
"dependencies": {
"intl-tel-input": "^24.6.0"
},
"license": "UNLICENSED",
"private": true,
"scripts": {
Expand Down
16 changes: 1 addition & 15 deletions src/Api/Command/PayByVisaMobileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use CommerceWeavers\SyliusTpayPlugin\Model\PaymentDetails;
use Sylius\Component\Core\Model\PaymentInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Webmozart\Assert\Assert;

#[AsMessageHandler]
final class PayByVisaMobileHandler extends AbstractPayByHandler
Expand All @@ -19,7 +18,7 @@ public function __invoke(PayByVisaMobile $command): PayResult
$this->setTransactionData($payment);
$this->createTransaction($payment);

return $this->createResultFrom($payment);
return $this->createResultFrom($payment, false);
}

private function setTransactionData(PaymentInterface $payment): void
Expand All @@ -29,17 +28,4 @@ private function setTransactionData(PaymentInterface $payment): void

$payment->setDetails($paymentDetails->toArray());
}

private function createResultFrom(PaymentInterface $payment): PayResult
{
$paymentDetails = PaymentDetails::fromArray($payment->getDetails());

Assert::notNull($paymentDetails->getStatus(), 'Payment status is required to create a result.');
Assert::notNull($paymentDetails->getPaymentUrl(), 'Payment URL is required to create a result.');

return new PayResult(
$paymentDetails->getStatus(),
$paymentDetails->getPaymentUrl(),
);
}
}
3 changes: 2 additions & 1 deletion src/ContextProvider/BankListContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CommerceWeavers\SyliusTpayPlugin\ContextProvider;

use CommerceWeavers\SyliusTpayPlugin\Model\OrderLastNewPaymentAwareInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiBankListProviderInterface;
use Sylius\Bundle\UiBundle\ContextProvider\ContextProviderInterface;
use Sylius\Bundle\UiBundle\Registry\TemplateBlock;
Expand Down Expand Up @@ -32,7 +33,7 @@ public function provide(array $templateContext, TemplateBlock $templateBlock): a
$paymentMethod = $payment->getMethod();
Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class);

if (($paymentMethod->getGatewayConfig()?->getConfig()['type'] ?? null) === 'pay-by-link') {
if (($paymentMethod->getGatewayConfig()?->getConfig()['type'] ?? null) === PaymentType::PAY_BY_LINK) {
$templateContext['banks'] = $this->bankListProvider->provide();
}

Expand Down
4 changes: 4 additions & 0 deletions src/Form/Type/TpayPaymentDetailsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
TelType::class,
[
'property_path' => '[visa_mobile_phone_number]',
'attr' => [
'placeholder' => 'commerce_weavers_sylius_tpay.shop.order_summary.visa_mobile.placeholder',
'maxLength' => 11,
],
],
)
;
Expand Down
1 change: 1 addition & 0 deletions src/Model/PaymentDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function getType(): string
null !== $this->getBlikToken() => PaymentType::BLIK,
null !== $this->getTpayChannelId() => PaymentType::PAY_BY_LINK,
null !== $this->getGooglePayToken() => PaymentType::GOOGLE_PAY,
null !== $this->getVisaMobilePhoneNumber() => PaymentType::VISA_MOBILE,
default => PaymentType::REDIRECT,
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/Payum/Action/Api/CreateVisaMobileTransactionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\Token\NotifyTokenFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateVisaMobilePaymentPayloadFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Payum\Core\Security\GenericTokenFactoryAwareTrait;
use Sylius\Component\Core\Model\PaymentInterface;

Expand Down Expand Up @@ -58,8 +59,8 @@ public function supports($request): bool
return false;
}

$details = $model->getDetails();
$paymentDetails = PaymentDetails::fromArray($model->getDetails());

return isset($details['tpay']['visa_mobile_phone_number']);
return $paymentDetails->getType() === PaymentType::VISA_MOBILE;
}
}
2 changes: 1 addition & 1 deletion src/Tpay/PaymentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PaymentType

public const BLIK = 'blik';

public const PAY_BY_LINK = 'pay-by-link';
public const PAY_BY_LINK = 'pay_by_link';

public const REDIRECT = 'redirect';

Expand Down
2 changes: 1 addition & 1 deletion templates/shop/cart/complete/_payByLink.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% set payment = order.lastCartPayment() %}

{% if cw_tpay_get_gateway_config_value(payment.method.gatewayConfig, 'type') == 'pay-by-link' %}
{% if cw_tpay_get_gateway_config_value(payment.method.gatewayConfig, 'type') == 'pay_by_link' %}
<div class="bank-container">
{% for bank in banks %}
<div class="bank-tile" data-bank-id="{{ bank.id }}">
Expand Down
6 changes: 3 additions & 3 deletions templates/shop/cart/complete/_visaMobile.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% set payment = order.lastCartPayment() %}

{% if cw_tpay_get_gateway_config_value(payment.method.gatewayConfig, 'type') == 'visa-mobile' %}
{% if cw_tpay_get_gateway_config_value(payment.method.gatewayConfig, 'type') == 'visa_mobile' %}
<div class="ui stackable grid">
<div class="ui column eight wide">
<div class="ui small icon message">
Expand All @@ -12,8 +12,8 @@
{{ form_widget(form.tpay.visa_mobile_phone_number, {
attr: {
'data-locale': app.request.locale|slice(3, 2),
'data-validation-error': 'commerce_weavers_sylius_tpay.shop.pay.visa_mobile.number'|trans({}, 'validators'),
'data-visa-mobile-phone-number': ''
'data-validation-error-length': 'commerce_weavers_sylius_tpay.shop.pay.visa_mobile.length'|trans({}, 'validators'),
'data-validation-error-required': 'commerce_weavers_sylius_tpay.shop.pay.visa_mobile.required'|trans({}, 'validators'),
}
}) }}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Sylius\Bundle\PayumBundle\Model\GatewayConfig:
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
type: 'pay-by-link'
type: 'pay_by_link'
production_mode: false
encrypted: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Sylius\Bundle\PayumBundle\Model\GatewayConfig:
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
type: 'pay-by-link'
type: 'pay_by_link'
production_mode: false
encrypted: true

Expand Down
2 changes: 1 addition & 1 deletion tests/E2E/Resources/fixtures/common/payment_method.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Sylius\Bundle\PayumBundle\Model\GatewayConfig:
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
type: 'pay-by-link'
type: 'pay_by_link'
production_mode: false
encrypted: true

Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/Api/Command/PayByLinkHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public function test_it_throws_an_exception_if_payment_details_does_not_have_a_s
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'tpay_channel_id' => '1'
'tpay_channel_id' => '1',
'visa_mobile_phone_number' => null,
],
])->shouldBeCalled();

Expand Down Expand Up @@ -127,7 +128,8 @@ public function test_it_creates_a_pay_by_link_based_transaction(): void
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'tpay_channel_id' => '1'
'tpay_channel_id' => '1',
'visa_mobile_phone_number' => null,
],
])->shouldBeCalled();

Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Api/Command/PayByVisaMobileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function test_it_throws_an_exception_if_payment_details_does_not_have_a_s
'payment_url' => null,
'success_url' => null,
'failure_url' => null,
'tpay_channel_id' => null,
'visa_mobile_phone_number' => null,
],
])->shouldBeCalled();
Expand Down Expand Up @@ -127,6 +128,7 @@ public function test_it_creates_a_visa_mobile_based_transaction(): void
'payment_url' => 'https://cw.org/pay',
'success_url' => null,
'failure_url' => null,
'tpay_channel_id' => null,
'visa_mobile_phone_number' => '44123456789',
],
])->shouldBeCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function test_it_throws_an_exception_if_a_constraint_has_an_invalid_type(
public function test_it_builds_violation_if_tpay_channel_id_does_not_exist(): void
{
$gatewayConfig = $this->prophesize(GatewayConfigInterface::class);
$gatewayConfig->getConfig()->willReturn(['type' => 'pay-by-link']);
$gatewayConfig->getConfig()->willReturn(['type' => 'pay_by_link']);

$paymentMethod = $this->prophesize(PaymentMethodInterface::class);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig->reveal());
Expand All @@ -111,7 +111,7 @@ public function test_it_builds_violation_if_tpay_channel_id_does_not_exist(): vo
public function test_it_builds_violation_if_tpay_channel_id_is_not_available(): void
{
$gatewayConfig = $this->prophesize(GatewayConfigInterface::class);
$gatewayConfig->getConfig()->willReturn(['type' => 'pay-by-link']);
$gatewayConfig->getConfig()->willReturn(['type' => 'pay_by_link']);

$paymentMethod = $this->prophesize(PaymentMethodInterface::class);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig->reveal());
Expand All @@ -132,7 +132,7 @@ public function test_it_builds_violation_if_tpay_channel_id_is_not_available():
public function test_it_builds_violation_if_tpay_channel_id_is_not_a_bank(): void
{
$gatewayConfig = $this->prophesize(GatewayConfigInterface::class);
$gatewayConfig->getConfig()->willReturn(['type' => 'pay-by-link']);
$gatewayConfig->getConfig()->willReturn(['type' => 'pay_by_link']);

$paymentMethod = $this->prophesize(PaymentMethodInterface::class);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig->reveal());
Expand Down Expand Up @@ -160,7 +160,7 @@ public function test_it_does_nothing_if_tpay_channel_id_is_not_provided(): void
public function test_it_does_nothing_if_tpay_channel_id_is_eligible(): void
{
$gatewayConfig = $this->prophesize(GatewayConfigInterface::class);
$gatewayConfig->getConfig()->willReturn(['type' => 'pay-by-link']);
$gatewayConfig->getConfig()->willReturn(['type' => 'pay_by_link']);

$paymentMethod = $this->prophesize(PaymentMethodInterface::class);
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig->reveal());
Expand Down
2 changes: 2 additions & 0 deletions translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ commerce_weavers_sylius_tpay:
year_placeholder: 'Choose a year'
holder_name: 'Cardholder name'
number: 'Number'
visa_mobile:
placeholder: 'Phone number with country code'
payment_failed:
retry_payment: 'Retry payment'
subtitle: 'We were unable to process it successfully'
Expand Down
2 changes: 2 additions & 0 deletions translations/messages.pl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ commerce_weavers_sylius_tpay:
year_placeholder: 'Wybierz rok'
holder_name: 'Imię i nazwisko właściciela'
number: 'Numer'
visa_mobile:
placeholder: 'Numer telefonu z kodem kierunkowym'
payment_failed:
retry_payment: 'Ponów płatność'
subtitle: 'Nie byliśmy w stanie jej pomyślnie przetworzyć'
Expand Down
1 change: 0 additions & 1 deletion translations/validators.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ commerce_weavers_sylius_tpay:
name: 'The cardholder name is required.'
visa_mobile:
required: 'The mobile phone number is required.'
number: 'The mobile phone number is not valid.'
length: 'The mobile phone must be composed of 11 digits (national code and phone number).'
1 change: 0 additions & 1 deletion translations/validators.pl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ commerce_weavers_sylius_tpay:
number: 'Numer karty musi składać się z 16 cyfr.'
name: 'Imię i nazwisko właściciela karty są wymagane.'
visa_mobile:
number: 'Numer telefonu nie jest prawidłowy.'
required: 'Numer telefonu jest wymagany.'
length: 'Numer telefonu musi składać się z 11 cyfr (numer kierunkowy oraz numer telefonu).'

0 comments on commit db2a61b

Please sign in to comment.