Environment details
SiteGenesis or Storefront Reference Architecture: SFRA
Klaviyo Cartridge version: 24.1.0
Anything else that would help a developer reproduce the bug?
Steps to reproduce
Go through the checkout flow, inputting a phone number that does not start with +1.
Follow the code in the subscribeUser()
function within utils.js
and inspect the returned result on line 257.
Notice the text of the error message indicates a bad phone number, but the pointer field has a value of "/data".
Notice the code that is meant to remove a bad phone number from the data and resend the service call does not run, because the pointer doesn't have a value of "phone_number"
Expected result
When a bad phone number is passed in, it should be removed from the data and the service call made again, so that the bad phone number doesn't cause the whole call to fail.
Actual result
When a phone number without a country prefix (i.e. "+1") is passed in, the API returns a "bad phone number" message, but the pointer field doesn't have a value of "phone_number".
Example returned error JSON:
{
"errors": [
{
"id": "6a679e76-11aa-456c-b53b-4541b00bf37e",
"status": 400,
"code": "invalid",
"title": "Invalid input.",
"detail": "The phone number provided either does not exist or is ineligible to receive SMS",
"source": {
"pointer": "/data"
},
"links": {},
"meta": {}
}
]
}
Additional information
The check on line 267 of utils.js doesn't allow the phone number to be removed in this instance, as the API call returns different data than is checked for.
Code in question:
if (session.custom.KLEmailSubscribe && emailListID) {
data = {
data: {
type : 'profile-subscription-bulk-create-job',
attributes : {
list_id : emailListID,
custom_source : 'SFCC Checkout',
subscriptions : [{
channels : { email: ['MARKETING'] },
email : email,
phone_number : phone
}]
}
}
};
result = klaviyoServices.KlaviyoSubscribeProfilesService.call(data);
if (result == null) {
logger.error('klaviyoServices.KlaviyoSubscribeProfilesService subscribe call for email returned null result');
}
if (!result.ok === true) {
logger.error('klaviyoServices.KlaviyoSubscribeProfilesService subscribe call for email error: ' + result.errorMessage);
// check to see if the reason the call failed was because of Klaviyo's internal phone number validation. if so, resend without phone number
var errObj = JSON.parse(result.errorMessage);
if (result.error == 400 && errObj.errors[0].source.pointer == 'phone_number') {
data.data.attributes.subscriptions[0].phone_number = null;
result = klaviyoServices.KlaviyoSubscribeProfilesService.call(data);
if (result == null) {
logger.error('klaviyoServices.KlaviyoSubscribeProfilesService subscribe call for email returned null result on second attempt without phone number');
}
if (!result.ok === true) {
logger.error('klaviyoServices.KlaviyoSubscribeProfilesService subscribe call for email on second attempt without phone number, error: ' + result.errorMessage);
}
}
}
}