♻️ Simplify the address extraction logic

This commit is contained in:
Philipp Stracker 2024-10-07 14:56:09 +02:00
parent 7c56d58fe4
commit 3d271586a3
No known key found for this signature in database

View file

@ -1017,62 +1017,39 @@ class ApplePayButton extends PaymentButton {
}; };
} }
fillBillingContact( data ) { #extractContactInfo( data, primaryPrefix, fallbackPrefix ) {
if ( ! data || typeof data !== 'object' ) {
data = {};
}
const getValue = ( key ) =>
data[ `${ primaryPrefix }_${ key }` ] ||
data[ `${ fallbackPrefix }_${ key }` ] ||
'';
return { return {
givenName: data.billing_first_name ?? '', givenName: getValue( 'first_name' ),
familyName: data.billing_last_name ?? '', familyName: getValue( 'last_name' ),
emailAddress: data.billing_email ?? '', emailAddress: getValue( 'email' ),
phoneNumber: data.billing_phone ?? '', phoneNumber: getValue( 'phone' ),
addressLines: [ data.billing_address_1, data.billing_address_2 ], addressLines: [ getValue( 'address_1' ), getValue( 'address_2' ) ],
locality: data.billing_city ?? '', locality: getValue( 'city' ),
postalCode: data.billing_postcode ?? '', postalCode: getValue( 'postcode' ),
countryCode: data.billing_country ?? '', countryCode: getValue( 'country' ),
administrativeArea: data.billing_state ?? '', administrativeArea: getValue( 'state' ),
}; };
} }
fillBillingContact( data ) {
return this.#extractContactInfo( data, 'billing', '' );
}
fillShippingContact( data ) { fillShippingContact( data ) {
if ( data.shipping_first_name === '' ) { if ( ! data?.shipping_first_name ) {
return this.fillBillingContact( data ); return this.fillBillingContact( data );
} }
return {
givenName: return this.#extractContactInfo( data, 'shipping', 'billing' );
data?.shipping_first_name && data.shipping_first_name !== ''
? data.shipping_first_name
: data?.billing_first_name,
familyName:
data?.shipping_last_name && data.shipping_last_name !== ''
? data.shipping_last_name
: data?.billing_last_name,
emailAddress:
data?.shipping_email && data.shipping_email !== ''
? data.shipping_email
: data?.billing_email,
phoneNumber:
data?.shipping_phone && data.shipping_phone !== ''
? data.shipping_phone
: data?.billing_phone,
addressLines: [
data.shipping_address_1 ?? '',
data.shipping_address_2 ?? '',
],
locality:
data?.shipping_city && data.shipping_city !== ''
? data.shipping_city
: data?.billing_city,
postalCode:
data?.shipping_postcode && data.shipping_postcode !== ''
? data.shipping_postcode
: data?.billing_postcode,
countryCode:
data?.shipping_country && data.shipping_country !== ''
? data.shipping_country
: data?.billing_country,
administrativeArea:
data?.shipping_state && data.shipping_state !== ''
? data.shipping_state
: data?.billing_state,
};
} }
fillApplicationData( data ) { fillApplicationData( data ) {