From 1a3cf049ea29672086d27b93759e6eb426164714 Mon Sep 17 00:00:00 2001 From: Nickolas Malovanets Date: Mon, 29 Jul 2024 19:46:56 +0300 Subject: [PATCH] Fix guest customer sync for the Fastlane and virtual cart. --- .../web/js/model/bold-frontend-client.js | 56 ++++++++----------- view/frontend/web/js/model/fastlane.js | 9 ++- .../view/form/element/email/fastlane-mixin.js | 6 +- .../method-renderer/bold-fastlane-method.js | 29 ++++------ 4 files changed, 45 insertions(+), 55 deletions(-) diff --git a/view/frontend/web/js/model/bold-frontend-client.js b/view/frontend/web/js/model/bold-frontend-client.js index 95eb3ca..50061ee 100644 --- a/view/frontend/web/js/model/bold-frontend-client.js +++ b/view/frontend/web/js/model/bold-frontend-client.js @@ -4,30 +4,19 @@ define([ 'Bold_CheckoutPaymentBooster/js/model/address', 'Bold_CheckoutPaymentBooster/js/model/customer' ], function ( - $, - _, - boldAddress, - boldCustomer + $, + _, + boldAddress, + boldCustomer ) { 'use strict'; - /** - * Bold http client. - * @type {object} - */ return { requestInProgress: false, requestQueue: [], synchronizedGuestData: {}, synchronizedAddressData: {}, - /** - * Post data to Bold API. - * - * @param path string - * @param body object - * @return {Promise} - */ post: function (path, body = {}) { return new Promise((resolve, reject) => { this.requestQueue.push({ @@ -40,12 +29,6 @@ define([ }); }, - /** - * Get data from Bold API. - * - * @param path - * @return {*} - */ get: function (path) { return $.ajax({ url: window.checkoutConfig.bold.url + path, @@ -56,11 +39,11 @@ define([ } }); }, + /** - * Process next request in queue. + * Process next request in the queue. * - * @return void - * @private + * @return {*} */ processNextRequest: function () { if (this.requestInProgress || this.requestQueue.length === 0) { @@ -69,27 +52,32 @@ define([ this.requestInProgress = true; const nextRequest = this.requestQueue.shift(); let requestData; + let skipRequest = false; + switch (nextRequest.path) { - case 'addresses/billing' : + case 'addresses/billing': requestData = boldAddress.getBillingAddress(); if (!requestData || _.isEqual(requestData, this.synchronizedAddressData)) { - this.requestInProgress = false; - this.processNextRequest(); - return; + skipRequest = true; } break; - case 'customer/guest' : + case 'customer/guest': requestData = boldCustomer.getCustomer(); if (!requestData || _.isEqual(requestData, this.synchronizedGuestData)) { - this.requestInProgress = false; - this.processNextRequest(); - return; + skipRequest = true; } break; default: requestData = nextRequest.body; break; } + + if (skipRequest) { + nextRequest.resolve(); + this.requestInProgress = false; + return this.processNextRequest(); + } + $.ajax({ url: window.checkoutConfig.bold.url + nextRequest.path, type: 'POST', @@ -102,10 +90,10 @@ define([ nextRequest.resolve(result); this.requestInProgress = false; switch (nextRequest.path) { - case 'addresses/billing' : + case 'addresses/billing': this.synchronizedAddressData = requestData; break; - case 'customer/guest' : + case 'customer/guest': this.synchronizedGuestData = requestData; break; default: diff --git a/view/frontend/web/js/model/fastlane.js b/view/frontend/web/js/model/fastlane.js index 5ec00b1..0b42c36 100644 --- a/view/frontend/web/js/model/fastlane.js +++ b/view/frontend/web/js/model/fastlane.js @@ -1,4 +1,8 @@ -define([], function () { +define([ + 'ko', +], function ( + ko +) { 'use strict'; /** @@ -7,6 +11,9 @@ define([], function () { * @type {object} */ return { + memberAuthenticated: ko.observable(false), + profileData: ko.observable(null), + /** * Check if Fastlane flow is enabled and active. * diff --git a/view/frontend/web/js/view/form/element/email/fastlane-mixin.js b/view/frontend/web/js/view/form/element/email/fastlane-mixin.js index 2871e94..9b3a4ea 100644 --- a/view/frontend/web/js/view/form/element/email/fastlane-mixin.js +++ b/view/frontend/web/js/view/form/element/email/fastlane-mixin.js @@ -100,15 +100,15 @@ define( profileData } = await identity.triggerAuthenticationFlow(customerContextId); if (authenticationState === 'succeeded') { - window.checkoutConfig.bold.fastlane.memberAuthenticated = true; - window.checkoutConfig.bold.fastlane.profileData = profileData; + fastlane.memberAuthenticated(true); + fastlane.profileData(profileData); fullScreenLoader.startLoader(); this.setShippingAddress(profileData); this.isPasswordVisible(false); } return; } - window.checkoutConfig.bold.fastlane.memberAuthenticated = false; + fastlane.memberAuthenticated(false); } catch (error) { fullScreenLoader.stopLoader(); console.error("Error:", error); diff --git a/view/frontend/web/js/view/payment/method-renderer/bold-fastlane-method.js b/view/frontend/web/js/view/payment/method-renderer/bold-fastlane-method.js index c5b67d2..fde716e 100644 --- a/view/frontend/web/js/view/payment/method-renderer/bold-fastlane-method.js +++ b/view/frontend/web/js/view/payment/method-renderer/bold-fastlane-method.js @@ -51,14 +51,16 @@ define( this.isVisible(false); return; } - quote.shippingAddress.subscribe(function () { - if (window.checkoutConfig.bold.fastlane.memberAuthenticated !== true - && checkoutData.getSelectedPaymentMethod() === 'bold_fastlane') { - selectPaymentMethodAction(null); - checkoutData.setSelectedPaymentMethod(null); + this.renderPaymentContainer(); + if (fastlane.memberAuthenticated()) { + this.selectPaymentMethod(); + } + fastlane.memberAuthenticated.subscribe(function (authenticated) { + if (authenticated === true) { + this.selectPaymentMethod(); } + this.renderPaymentContainer(); }, this); - this.renderPaymentContainer(); }, /** * Wait for the payment container to be rendered before rendering the Fastlane component. @@ -123,13 +125,6 @@ define( this.isVisible(false); } }, - /** - * @inheritDoc - */ - selectPaymentMethod: function () { - this.renderCardComponent(); - return this._super(); - }, /** * @inheritDoc */ @@ -231,8 +226,8 @@ define( } let fastlaneFirstName; try { - fastlaneFirstName = window.checkoutConfig.bold.fastlane.profileData && window.checkoutConfig.bold.fastlane.profileData.name.firstName - ? window.checkoutConfig.bold.fastlane.profileData.name.firstName + fastlaneFirstName = fastlane.profileData() && fastlane.profileData().name.firstName + ? fastlane.profileData().name.firstName : tokenResponse.paymentSource.card.name.split(' ')[0]; } catch (e) { fastlaneFirstName = quoteAddress.firstname; @@ -242,8 +237,8 @@ define( } let fastlaneLastName; try { - fastlaneLastName = window.checkoutConfig.bold.fastlane.profileData && window.checkoutConfig.bold.fastlane.profileData.name.lastName - ? window.checkoutConfig.bold.fastlane.profileData.name.lastName + fastlaneLastName = fastlane.profileData() && fastlane.profileData().name.lastName + ? fastlane.profileData().name.lastName : tokenResponse.paymentSource.card.name.split(' ')[1]; } catch (e) { fastlaneLastName = quoteAddress.lastname;