Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for visa mobile (on-site) #85

Merged
merged 10 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions assets/shop/checkout_complete_entrypoint.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import './img/hourglass.gif';
import './scss/style.scss';
import './js/_pay_by_link';
import './js/_visa_mobile';
import {CardForm} from "./js/card_form";

document.addEventListener('DOMContentLoaded', () => {
if (!document.querySelector('[name="sylius_checkout_complete"]').querySelector('[data-tpay-card-holder-name]')) {
return;
}
new CardForm('[name="sylius_checkout_complete"]');
});
79 changes: 79 additions & 0 deletions assets/shop/js/_visa_mobile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
document.addEventListener('DOMContentLoaded', () => {
const MOBILE_PHONE_MIN_LENGTH = 7;
const MOBILE_PHONE_MAX_LENGTH = 15;

let form = document.querySelector('[name="sylius_checkout_complete"]');
let phoneNumber = form.querySelector('#sylius_checkout_complete_tpay_visa_mobile_phone_number');

if (null === phoneNumber) {
return;
}

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

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

if (!isValid) {
event.preventDefault();
event.stopPropagation();

form.classList.remove('loading');

return;
}


form.submit();
});

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

if (MOBILE_PHONE_MIN_LENGTH <= fieldLength && fieldLength <= MOBILE_PHONE_MAX_LENGTH) {
clearErrors(phoneNumber);
return;
}

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

if (fieldLength < MOBILE_PHONE_MIN_LENGTH) {
addError(phoneNumber, 'validationErrorMinLength');
return;
}

if (fieldLength > MOBILE_PHONE_MAX_LENGTH) {
addError(phoneNumber, 'validationErrorMaxLength');
return;
}

addError(phoneNumber);
}

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

errorContainer.innerHTML = '';
}

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

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

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

return `
<div class="ui red pointing label sylius-validation-error">
${errorMessage}
</div>
`;
}
});
13 changes: 12 additions & 1 deletion config/config/sylius_fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Symfony\Config\SyliusFixturesConfig;

return function(SyliusFixturesConfig $fixtures): void {
return static function(SyliusFixturesConfig $fixtures): void {
$defaultSuite = $fixtures->suites('default');
$defaultSuite->fixtures('shipping_method', [
'options' => [
Expand Down Expand Up @@ -136,6 +136,17 @@
],
'enabled' => true,
],
'visa_mobile' => [
'code' => 'tpay_visa_mobile',
'name' => 'Visa mobile (Tpay)',
'gatewayFactory' => 'tpay',
'gatewayName' => 'tpay',
'gatewayConfig' => $tpayConfig + ['type' => PaymentType::VISA_MOBILE],
'channels' => [
'FASHION_WEB',
],
'enabled' => true,
],
],
],
]);
Expand Down
6 changes: 5 additions & 1 deletion config/config/sylius_template_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@
'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' => [
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_visaMobile.html.twig',
'priority' => 5,
]
],
],
Expand Down
3 changes: 3 additions & 0 deletions config/serialization/Pay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@
<attribute name="tpayChannelId">
<group>commerce_weavers_sylius_tpay:shop:order:pay</group>
</attribute>
<attribute name="visaMobilePhoneNumber">
<group>commerce_weavers_sylius_tpay:shop:order:pay</group>
</attribute>
</class>
</serializer>
8 changes: 7 additions & 1 deletion config/services/api/command.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByLinkHandler;
use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByGooglePayHandler;
use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByRedirectHandler;
use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByVisaMobileHandler;
use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayHandler;
use CommerceWeavers\SyliusTpayPlugin\Command\CancelLastPaymentHandler;

return function(ContainerConfigurator $container): void {
return static function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set('commerce_weavers_sylius_tpay.api.command.cancel_last_payment_handler', CancelLastPaymentHandler::class)
Expand Down Expand Up @@ -73,4 +74,9 @@
->parent('commerce_weavers_sylius_tpay.api.command.abstract_pay_by_handler')
->tag('messenger.message_handler')
;

$services->set('commerce_weavers_sylius_tpay.api.command.pay_by_visa_mobile_handler', PayByVisaMobileHandler::class)
->parent('commerce_weavers_sylius_tpay.api.command.abstract_pay_by_handler')
->tag('messenger.message_handler')
;
};
7 changes: 6 additions & 1 deletion config/services/api/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand\PayByGooglePayFactory;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand\PayByLinkFactory;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand\PayByRedirectFactory;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommand\PayByVisaMobileFactory;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommandFactory;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommandFactoryInterface;

return function(ContainerConfigurator $container): void {
return static function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set('commerce_weavers_sylius_tpay.api.factory.next_command', NextCommandFactory::class)
Expand Down Expand Up @@ -49,4 +50,8 @@
$services->set('commerce_weavers_sylius_tpay.api.factory.next_command.pay_by_link', PayByLinkFactory::class)
->tag('commerce_weavers_sylius_tpay.api.factory.next_command')
;

$services->set('commerce_weavers_sylius_tpay.api.factory.next_command.pay_by_visa_mobile', PayByVisaMobileFactory::class)
->tag('commerce_weavers_sylius_tpay.api.factory.next_command')
;
};
1 change: 1 addition & 0 deletions config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\CardTypeDataTransformer;
use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\VisaMobilePhoneDataTransformer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed. Should be removed

use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\DecryptGatewayConfigListener;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\EncryptGatewayConfigListener;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyPasswordFieldsListener;
Expand Down
11 changes: 10 additions & 1 deletion config/services/payum/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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\NotifyAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\PayWithCardAction;
Expand All @@ -19,7 +20,7 @@
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\ResolveNextRouteAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;

return function(ContainerConfigurator $container): void {
return static function(ContainerConfigurator $container): void {
$services = $container->services();
$services->defaults()
->public()
Expand Down Expand Up @@ -72,6 +73,14 @@
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_redirect_based_transaction'])
;

$services->set(CreateVisaMobileTransactionAction::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload'),
service('commerce_weavers_sylius_tpay.payum.factory.token.notify'),
])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_visa_mobile_transaction'])
;

$services->set(NotifyAction::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.security.notification.factory.basic_payment'),
Expand Down
18 changes: 16 additions & 2 deletions config/services/tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateRedirectBasedPaymentPayloadFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiBankListProvider;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiBankListProviderInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiChannelListProvider;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\TpayApiChannelListProviderInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateVisaMobilePaymentPayloadFactory;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateVisaMobilePaymentPayloadFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Resolver\CachedTpayTransactionChannelResolver;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Resolver\TpayTransactionChannelResolver;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Security\Notification\Factory\BasicPaymentFactory;
Expand Down Expand Up @@ -66,6 +66,13 @@
->alias(CreateGooglePayPaymentPayloadFactory::class, 'commerce_weavers_sylius_tpay.tpay.factory.create_google_pay_payment_payload')
;

$services->set('commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload', CreateVisaMobilePaymentPayloadFactory::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.factory.create_redirect_based_payment_payload'),
])
->alias(CreateVisaMobilePaymentPayloadFactoryInterface::class, 'commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload')
;

$services->set('commerce_weavers_sylius_tpay.tpay.factory.create_redirect_based_payment_payload', CreateRedirectBasedPaymentPayloadFactory::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.routing.generator.callback_url'),
Expand All @@ -81,6 +88,13 @@
->alias(CreatePayByLinkPayloadFactoryInterface::class, 'commerce_weavers_sylius_tpay.tpay.factory.create_pay_by_link_payment_payload')
;

$services->set('commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload', CreateVisaMobilePaymentPayloadFactory::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.factory.create_redirect_based_payment_payload'),
])
->alias(CreateVisaMobilePaymentPayloadFactoryInterface::class, 'commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload')
;

$services->set('commerce_weavers_sylius_tpay.tpay.security.notification.factory.basic_payment', BasicPaymentFactory::class)
->alias(BasicPaymentFactory::class, 'commerce_weavers_sylius_tpay.tpay.security.notification.factory.basic_payment')
;
Expand Down
32 changes: 32 additions & 0 deletions config/validation/Pay.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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 All @@ -63,5 +70,30 @@
</option>
</constraint>
</property>
<property name="visaMobilePhoneNumber">
<constraint name="CommerceWeavers\SyliusTpayPlugin\Api\Validator\Constraint\NotBlankIfGatewayConfigTypeEquals">
<option name="paymentMethodType">visa_mobile</option>
<option name="fieldRequiredErrorMessage">commerce_weavers_sylius_tpay.shop.pay.visa_mobile.required</option>
<option name="groups">
<value>commerce_weavers_sylius_tpay:shop:order:pay</value>
</option>
</constraint>
<constraint name="Length">
<option name="min">7</option>
<option name="max">15</option>
<option name="minMessage">commerce_weavers_sylius_tpay.shop.pay.visa_mobile.min_length</option>
<option name="maxMessage">commerce_weavers_sylius_tpay.shop.pay.visa_mobile.max_length</option>
<option name="groups">
<value>commerce_weavers_sylius_tpay:shop:order:pay</value>
</option>
</constraint>
<constraint name="Regex">
<option name="pattern">/^\d+$/</option>
<option name="message">commerce_weavers_sylius_tpay.shop.pay.visa_mobile.regex</option>
<option name="groups">
<value>commerce_weavers_sylius_tpay:shop:order:pay</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>
1 change: 1 addition & 0 deletions src/Api/Command/Pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
public readonly ?string $googlePayToken = null,
public readonly ?string $encodedCardData = null,
public readonly ?string $tpayChannelId = null,
public readonly ?string $visaMobilePhoneNumber = null,
) {
}

Expand Down
14 changes: 14 additions & 0 deletions src/Api/Command/PayByVisaMobile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Api\Command;

final class PayByVisaMobile
{
public function __construct(
public readonly int $paymentId,
public readonly string $visaMobilePhoneNumber,
) {
}
}
31 changes: 31 additions & 0 deletions src/Api/Command/PayByVisaMobileHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Api\Command;

use CommerceWeavers\SyliusTpayPlugin\Model\PaymentDetails;
use Sylius\Component\Core\Model\PaymentInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final class PayByVisaMobileHandler extends AbstractPayByHandler
{
public function __invoke(PayByVisaMobile $command): PayResult
{
$payment = $this->findOr404($command->paymentId);

$this->setTransactionData($payment);
$this->createTransaction($payment);

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

private function setTransactionData(PaymentInterface $payment): void
{
$paymentDetails = PaymentDetails::fromArray($payment->getDetails());
$paymentDetails->setVisaMobilePhoneNumber($paymentDetails->getVisaMobilePhoneNumber());

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