Skip to content
Merged
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
29 changes: 27 additions & 2 deletions paypal/js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,11 @@
}

if ( 'string' === typeof err ) {
return parsePayPalErrorString( err ) || err;
const parsed = parsePayPalErrorString( err );
if ( parsed ) {
return parsed;
}
return mapPayPalErrorCode( err ) || err;
}

// PayPal SDK sometimes nests the payload under `err.data` or `err.response`.
Expand All @@ -1581,12 +1585,33 @@
}

if ( err.message ) {
return parsePayPalErrorString( err.message ) || err.message;
const parsed = parsePayPalErrorString( err.message );
if ( parsed ) {
return parsed;
}
return mapPayPalErrorCode( err.message ) || err.message;
}

return fallback;
}

/**
* Map PayPal error codes to user-friendly messages.
*
* @param {string} code The PayPal error code (e.g. INVALID_CVV).
* @return {string} The user-friendly message, or empty string if not mapped.
*/
function mapPayPalErrorCode( code ) {
const codeMap = {
INVALID_CVV: 'Please enter a valid CVV code.',
INVALID_CARD_NUMBER: 'Please enter a valid card number.',
INVALID_EXPIRY: 'Please enter a valid expiry date.',
};

const upperCode = code.toUpperCase();
return codeMap[ upperCode ] || '';
}

/**
* Extract the first `description` from a PayPal `details` array.
*
Expand Down
Loading