Option to only set missing billing details

This commit is contained in:
Philipp Stracker 2024-08-16 18:45:49 +02:00
parent 63e9c8bf27
commit c007d7909c
No known key found for this signature in database
2 changed files with 8 additions and 4 deletions

View file

@ -110,8 +110,12 @@ export const payerData = () => {
* Updates the DOM with specific payer details.
*
* @param {PayerDetails} newData - New payer details.
* @param {boolean} [overwriteExisting=false] - If set to true, all provided values will replace existing details. If false, or omitted, only undefined fields are updated.
*/
export const setPayerData = ( newData ) => {
export const setPayerData = ( newData, overwriteExisting = false ) => {
// TODO: Check if we can add some kind of "filter" to allow customization of the data.
// Or add JS flags like "onlyUpdateMissing".
const setValue = ( path, field, value ) => {
if ( null === value || undefined === value || ! field ) {
return;
@ -121,7 +125,7 @@ export const setPayerData = ( newData ) => {
value = value.phone_number?.national_number;
}
if ( field.value !== value ) {
if ( overwriteExisting || ! field.value ) {
field.value = value;
}
};

View file

@ -634,8 +634,8 @@ class GooglepayButton extends PaymentButton {
const propagatePayerDataToForm = () => {
const raw = paymentData?.paymentMethodData?.info?.billingAddress;
const payer = {
email_address: paymentData?.email,
name: {
given_name: raw.name.split( ' ' )[ 0 ], // Assuming first name is the first part
surname: raw.name.split( ' ' ).slice( 1 ).join( ' ' ), // Assuming last name is the rest