-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apple pay): populate billing/shippingContact with form address f…
…ields Apple Pay on the Web supports supplying a `billingContact` and `shippingContact` when creating the session. If provided, it would populate over the customer's default billing contact on the payment sheet. If the `form` option is supplied, use the address fields that are populated to populate the `billingContact` and the shipping fields that are populated to populate the `shippingContact`. This also cleans up the interaction between the `form` and the token. Currently, if any of the fields on the `form` are present, we prefer that address instead of what is presented and modified on the payment card. This changes that functionality to always prefer the `billingContact` from the payment card when tokenizing the card as that is what the customer sees when authorizing the payment.
- Loading branch information
Showing
8 changed files
with
370 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import isEmpty from 'lodash.isempty'; | ||
import { normalize } from '../../../util/normalize'; | ||
import { ADDRESS_FIELDS, NON_ADDRESS_FIELDS } from '../../token'; | ||
|
||
const BILLING_CONTACT_MAP = { | ||
first_name: 'givenName', | ||
last_name: 'familyName', | ||
address1: { field: 'addressLines', index: 0 }, | ||
address2: { field: 'addressLines', index: 1 }, | ||
city: 'locality', | ||
state: 'administrativeArea', | ||
postal_code: 'postalCode', | ||
country: 'countryCode', | ||
}; | ||
|
||
const SHIPPING_CONTACT_MAP = { | ||
email: 'emailAddress', | ||
phone: 'phoneNumber', | ||
}; | ||
|
||
const CONTACT_MAP = { | ||
...BILLING_CONTACT_MAP, | ||
...SHIPPING_CONTACT_MAP, | ||
}; | ||
|
||
/** | ||
* Transforms a source address type to another address type. | ||
* @param {Object} source either an Address or ApplePayPaymentContact | ||
* @param {Object} options transform options | ||
* @param {string} options.to either 'contact' or 'address' | ||
* @param {string} options.except properties to exclude | ||
* @return {Object} the transform result | ||
*/ | ||
export function transformAddress (source, { to = 'contact', except = [], map = CONTACT_MAP }) { | ||
if (isEmpty(source)) return {}; | ||
|
||
return Object.keys(map).reduce((target, addressField) => { | ||
const contactField = map[addressField]; | ||
const sourceField = to === 'contact' ? addressField : contactField; | ||
const targetField = to === 'address' ? addressField : contactField; | ||
|
||
if (~except.indexOf(sourceField)) return target; | ||
|
||
const sourceValue = typeof sourceField === 'object' | ||
? source[sourceField.field]?.[sourceField.index] | ||
: source[sourceField]; | ||
if (!sourceValue) return target; | ||
|
||
if (typeof targetField === 'object') { | ||
const { field, index } = targetField; | ||
target[field] = target[field] || []; | ||
target[field][index] = sourceValue; | ||
} else { | ||
target[targetField] = sourceValue; | ||
} | ||
|
||
return target; | ||
}, {}); | ||
} | ||
|
||
export function normalizeForm (form) { | ||
if (!form) return {}; | ||
|
||
const address = normalize(form, ADDRESS_FIELDS, { parseCard: false }).values; | ||
const billingContact = transformAddress(address, { map: BILLING_CONTACT_MAP }); | ||
const shippingContact = transformAddress(address, { map: SHIPPING_CONTACT_MAP }); | ||
const tokenData = normalize(form, NON_ADDRESS_FIELDS, { parseCard: false }).values; | ||
|
||
return { | ||
billingContact, | ||
shippingContact, | ||
tokenData, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.