Skip to content

Commit

Permalink
Fix guest customer sync for the Fastlane and virtual cart.
Browse files Browse the repository at this point in the history
  • Loading branch information
NickolasMalovanets committed Jul 29, 2024
1 parent 3441180 commit 1a3cf04
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 55 deletions.
56 changes: 22 additions & 34 deletions view/frontend/web/js/model/bold-frontend-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -40,12 +29,6 @@ define([
});
},

/**
* Get data from Bold API.
*
* @param path
* @return {*}
*/
get: function (path) {
return $.ajax({
url: window.checkoutConfig.bold.url + path,
Expand All @@ -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) {
Expand All @@ -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',
Expand All @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion view/frontend/web/js/model/fastlane.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
define([], function () {
define([
'ko',
], function (
ko
) {
'use strict';

/**
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -123,13 +125,6 @@ define(
this.isVisible(false);
}
},
/**
* @inheritDoc
*/
selectPaymentMethod: function () {
this.renderCardComponent();
return this._super();
},
/**
* @inheritDoc
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 1a3cf04

Please sign in to comment.