From 69016b1ddc4bdcd91561b26cdbb16f013da27d75 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Thu, 27 Jun 2024 11:04:57 +0200 Subject: [PATCH 1/2] NTR: fix #764 --- .../ApplePayDirect/ApplePayDirect.php | 4 +-- .../ApplePayDirectControllerBase.php | 2 ++ .../ApplePayDirectControllerBase.php | 4 ++- .../plugins/apple-pay-direct.plugin.js | 30 ++++++++++++++----- .../component/apple-pay-direct-button.twig | 1 + src/Service/SettingsService.php | 23 +++++++++++++- src/Setting/MollieSettingStruct.php | 16 ++++++++++ src/Subscriber/ApplePayDirectSubscriber.php | 1 + 8 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/Components/ApplePayDirect/ApplePayDirect.php b/src/Components/ApplePayDirect/ApplePayDirect.php index 46e7c38c7..cf2f3171b 100644 --- a/src/Components/ApplePayDirect/ApplePayDirect.php +++ b/src/Components/ApplePayDirect/ApplePayDirect.php @@ -334,7 +334,7 @@ public function restoreCart(SalesChannelContext $context): void * @throws \Exception * @return SalesChannelContext */ - public function prepareCustomer(string $firstname, string $lastname, string $email, string $street, string $zipcode, string $city, string $countryCode, string $paymentToken, SalesChannelContext $context): SalesChannelContext + public function prepareCustomer(string $firstname, string $lastname, string $email, string $street, string $zipcode, string $city, string $countryCode, string $phone, string $paymentToken, SalesChannelContext $context): SalesChannelContext { if (empty($paymentToken)) { throw new \Exception('PaymentToken not found!'); @@ -356,7 +356,7 @@ public function prepareCustomer(string $firstname, string $lastname, string $ema $firstname, $lastname, $email, - '', + $phone, $street, $zipcode, $city, diff --git a/src/Controller/StoreApi/ApplePayDirect/ApplePayDirectControllerBase.php b/src/Controller/StoreApi/ApplePayDirect/ApplePayDirectControllerBase.php index 08af9eaee..aaea3e3ab 100644 --- a/src/Controller/StoreApi/ApplePayDirect/ApplePayDirectControllerBase.php +++ b/src/Controller/StoreApi/ApplePayDirect/ApplePayDirectControllerBase.php @@ -194,6 +194,7 @@ public function pay(RequestDataBag $data, SalesChannelContext $context): StoreAp $city = (string)$data->get('city', ''); $zipcode = (string)$data->get('postalCode', ''); $countryCode = (string)$data->get('countryCode', ''); + $phone = (string)$data->get('phone', ''); $paymentToken = (string)$data->get('paymentToken', ''); $finishUrl = (string)$data->get('finishUrl', ''); @@ -215,6 +216,7 @@ public function pay(RequestDataBag $data, SalesChannelContext $context): StoreAp $zipcode, $city, $countryCode, + $phone, $paymentToken, $context ); diff --git a/src/Controller/Storefront/ApplePayDirect/ApplePayDirectControllerBase.php b/src/Controller/Storefront/ApplePayDirect/ApplePayDirectControllerBase.php index 801b83e50..88815a052 100644 --- a/src/Controller/Storefront/ApplePayDirect/ApplePayDirectControllerBase.php +++ b/src/Controller/Storefront/ApplePayDirect/ApplePayDirectControllerBase.php @@ -234,7 +234,7 @@ public function createPaymentSession(SalesChannelContext $context, Request $requ ] ); - return new JsonResponse(['success' => false,], 500); + return new JsonResponse(['success' => false], 500); } } @@ -342,6 +342,7 @@ public function startPayment(SalesChannelContext $context, Request $request): Re $zipcode = (string)$request->get('postalCode', ''); $city = (string)$request->get('city', ''); $countryCode = (string)$request->get('countryCode', ''); + $phone = (string)$request->get('phone', ''); $paymentToken = (string)$request->get('paymentToken', ''); @@ -358,6 +359,7 @@ public function startPayment(SalesChannelContext $context, Request $request): Re $zipcode, $city, $countryCode, + $phone, $paymentToken, $context ); diff --git a/src/Resources/app/storefront/src/mollie-payments/plugins/apple-pay-direct.plugin.js b/src/Resources/app/storefront/src/mollie-payments/plugins/apple-pay-direct.plugin.js index 93c264ace..c56dabcfc 100644 --- a/src/Resources/app/storefront/src/mollie-payments/plugins/apple-pay-direct.plugin.js +++ b/src/Resources/app/storefront/src/mollie-payments/plugins/apple-pay-direct.plugin.js @@ -126,6 +126,8 @@ export default class MollieApplePayDirect extends Plugin { const countryCode = form.querySelector('input[name="countryCode"]').value; const currency = form.querySelector('input[name="currency"]').value; const mode = form.querySelector('input[name="mode"]').value; + const withPhone = parseInt(form.querySelector('input[name="withPhone"]').value); + // this helps us to figure out if we are in // "product" mode to purchase a single product, or in "cart" mode @@ -152,7 +154,7 @@ export default class MollieApplePayDirect extends Plugin { me.addProductToCart(productId, quantity, shopUrl); } - var session = me.createApplePaySession(isProductMode, countryCode, currency, shopUrl); + var session = me.createApplePaySession(isProductMode, countryCode, currency,withPhone, shopUrl); session.begin(); } @@ -178,20 +180,26 @@ export default class MollieApplePayDirect extends Plugin { * @param country * @param currency * @param shopSlug + * @param withPhone * @returns {ApplePaySession} */ - createApplePaySession(isProductMode, country, currency, shopSlug) { + createApplePaySession(isProductMode, country, currency, withPhone, shopSlug) { const me = this; + var shippingFields = [ + 'name', + 'email', + 'postalAddress', + ]; + + if(withPhone === 1){ + shippingFields.push('phone'); + } var request = { countryCode: country, currencyCode: currency, - requiredShippingContactFields: [ - 'name', - 'email', - 'postalAddress', - ], + requiredShippingContactFields: shippingFields, supportedNetworks: [ 'amex', 'maestro', @@ -216,6 +224,9 @@ export default class MollieApplePayDirect extends Plugin { validationUrl: event.validationURL, }), (validationData) => { + if(validationData.success === false){ + throw new Error('Validation failed for URL: '+ event.validationURL); + } const data = JSON.parse(validationData.session); session.completeMerchantValidation(data); }, @@ -360,6 +371,11 @@ export default class MollieApplePayDirect extends Plugin { form.insertAdjacentElement('beforeend', createInput('street', street)); form.insertAdjacentElement('beforeend', createInput('postalCode', payment.shippingContact.postalCode)); form.insertAdjacentElement('beforeend', createInput('city', payment.shippingContact.locality)); + + if(payment.shippingContact.phoneNumber.length > 0){ + form.insertAdjacentElement('beforeend', createInput('phone', payment.shippingContact.phoneNumber)); + } + form.insertAdjacentElement('beforeend', createInput('countryCode', payment.shippingContact.countryCode)); // also add our payment token form.insertAdjacentElement('beforeend', createInput('paymentToken', paymentToken)); diff --git a/src/Resources/views/mollie/component/apple-pay-direct-button.twig b/src/Resources/views/mollie/component/apple-pay-direct-button.twig index f93375787..c0c12a876 100644 --- a/src/Resources/views/mollie/component/apple-pay-direct-button.twig +++ b/src/Resources/views/mollie/component/apple-pay-direct-button.twig @@ -21,6 +21,7 @@ + {% if productId %} {% set mode = 'productMode' %} diff --git a/src/Service/SettingsService.php b/src/Service/SettingsService.php index 78b13c709..c041ed5f2 100644 --- a/src/Service/SettingsService.php +++ b/src/Service/SettingsService.php @@ -11,12 +11,18 @@ class SettingsService implements PluginSettingsServiceInterface { public const SYSTEM_CONFIG_DOMAIN = 'MolliePayments.config.'; + private const SYSTEM_CORE_CONFIG_DOMAIN = 'core'; + private const PHONE_NUMBER_FIELD_REQUIRED = 'phoneNumberFieldRequired'; const LIVE_API_KEY = 'liveApiKey'; const TEST_API_KEY = 'testApiKey'; const LIVE_PROFILE_ID = 'liveProfileId'; const TEST_PROFILE_ID = 'testProfileId'; + /** + * @var array + */ + private $settings = []; /** * @var SystemConfigService @@ -70,6 +76,11 @@ public function __construct(SystemConfigService $systemConfigService, SalesChann */ public function getSettings(?string $salesChannelId = null): MollieSettingStruct { + $cacheKey = $salesChannelId ?? 'all'; + + if (isset($this->settings[$cacheKey]) && $this->settings[$cacheKey] instanceof MollieSettingStruct) { + return $this->settings[$cacheKey]; + } $structData = []; $systemConfigData = $this->systemConfigService->getDomain(self::SYSTEM_CONFIG_DOMAIN, $salesChannelId, true); @@ -81,7 +92,17 @@ public function getSettings(?string $salesChannelId = null): MollieSettingStruct } } - return (new MollieSettingStruct())->assign($structData); + $coreSettings = $this->systemConfigService->getDomain(self::SYSTEM_CORE_CONFIG_DOMAIN, $salesChannelId, true); + foreach ($coreSettings as $key => $value) { + if (strpos($key, self::PHONE_NUMBER_FIELD_REQUIRED)) { + $structData[self::PHONE_NUMBER_FIELD_REQUIRED] = $value; + break; + } + } + + + $this->settings[$cacheKey] = (new MollieSettingStruct())->assign($structData); + return $this->settings[$cacheKey] ; } /** diff --git a/src/Setting/MollieSettingStruct.php b/src/Setting/MollieSettingStruct.php index 088c1547a..dc9fcc7db 100644 --- a/src/Setting/MollieSettingStruct.php +++ b/src/Setting/MollieSettingStruct.php @@ -243,6 +243,12 @@ class MollieSettingStruct extends Struct */ protected $useShopwareJavascript = false; + + /** + * @var bool + */ + protected $phoneNumberFieldRequired = false; + /** * @return string */ @@ -950,4 +956,14 @@ public function setUseShopwareJavascript(bool $useShopwareJavascript): void { $this->useShopwareJavascript = $useShopwareJavascript; } + + public function isPhoneNumberFieldRequired(): bool + { + return $this->phoneNumberFieldRequired; + } + + public function setPhoneNumberFieldRequired(bool $phoneNumberFieldRequired): void + { + $this->phoneNumberFieldRequired = $phoneNumberFieldRequired; + } } diff --git a/src/Subscriber/ApplePayDirectSubscriber.php b/src/Subscriber/ApplePayDirectSubscriber.php index 5f7d2cda6..872da8521 100644 --- a/src/Subscriber/ApplePayDirectSubscriber.php +++ b/src/Subscriber/ApplePayDirectSubscriber.php @@ -51,6 +51,7 @@ public function onStorefrontRender(StorefrontRenderEvent $event): void $applePayDirectEnabled = $this->applePay->isApplePayDirectEnabled($event->getSalesChannelContext()); + $event->setParameter('mollie_applepaydirect_phonenumber_required', $settings->isPhoneNumberFieldRequired()); $event->setParameter('mollie_applepaydirect_enabled', $applePayDirectEnabled); $event->setParameter('mollie_applepaydirect_restrictions', $settings->getRestrictApplePayDirect()); } From 5c4cb97e22f25b5be52787e92742ac53f4879067 Mon Sep 17 00:00:00 2001 From: Vitalij Mik Date: Thu, 27 Jun 2024 11:08:42 +0200 Subject: [PATCH 2/2] NTR: fix payment failed cypress tests --- .../cypress/e2e/storefront/checkout/checkout-failed.cy.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Cypress/cypress/e2e/storefront/checkout/checkout-failed.cy.js b/tests/Cypress/cypress/e2e/storefront/checkout/checkout-failed.cy.js index ed0908f70..4dd7cc182 100644 --- a/tests/Cypress/cypress/e2e/storefront/checkout/checkout-failed.cy.js +++ b/tests/Cypress/cypress/e2e/storefront/checkout/checkout-failed.cy.js @@ -71,7 +71,7 @@ context("Checkout Failure Tests", () => { // select giro pay and mark it as "paid" mollieSandbox.initSandboxCookie(); - molliePaymentList.selectiDEAL(); + molliePaymentList.selectSOFORT(); molliePaymentStatus.selectPaid(); cy.url().should('include', '/checkout/finish'); @@ -102,7 +102,7 @@ context("Checkout Failure Tests", () => { // select giro pay and mark it as "paid" mollieSandbox.initSandboxCookie(); - molliePaymentList.selectiDEAL(); + molliePaymentList.selectSOFORT(); molliePaymentStatus.selectPaid(); cy.url().should('include', '/checkout/finish'); @@ -173,7 +173,7 @@ context("Checkout Failure Tests", () => { } - paymentAction.switchPaymentMethod('iDEAL'); + paymentAction.switchPaymentMethod('SOFORT'); checkout.placeOrderOnEdit(); @@ -206,7 +206,7 @@ context("Checkout Failure Tests", () => { cy.contains('We received your order, but the payment was aborted'); } - paymentAction.switchPaymentMethod('iDEAL'); + paymentAction.switchPaymentMethod('SOFORT'); checkout.placeOrderOnEdit();