-
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 billingContact with form address fields
Apple Pay on the Web version 10 added support for supplying a `billingContact` 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`. This also cleans up the interaction between the `form` and the payment card when the client is not on version 10. 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
302 additions
and
165 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,54 @@ | ||
import isEmpty from 'lodash.isempty'; | ||
|
||
const ADDRESS_MAP = { | ||
address1: { field: 'addressLines', index: 0 }, | ||
address2: { field: 'addressLines', index: 1 }, | ||
city: 'locality', | ||
state: 'administrativeArea', | ||
postal_code: 'postalCode', | ||
country: 'countryCode', | ||
}; | ||
|
||
const CONTACT_MAP = { | ||
email: 'emailAddress', | ||
phone: 'phoneNumber', | ||
first_name: 'givenName', | ||
last_name: 'familyName', | ||
...ADDRESS_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 default function transformAddress (source, { to = 'address', except = [] }) { | ||
if (isEmpty(source)) return {}; | ||
|
||
const target = {}; | ||
|
||
for (const [addressField, contactField] of Object.entries(CONTACT_MAP)) { | ||
const sourceField = to === 'contact' ? addressField : contactField; | ||
const targetField = to === 'address' ? addressField : contactField; | ||
|
||
if (except.includes(sourceField)) continue; | ||
|
||
const sourceValue = typeof sourceField === 'object' | ||
? source[sourceField.field]?.[sourceField.index] | ||
: source[sourceField]; | ||
if (!sourceValue) continue; | ||
|
||
if (typeof targetField === 'object') { | ||
const { field, index } = targetField; | ||
target[field] = target[field] || []; | ||
target[field][index] = sourceValue; | ||
} else { | ||
target[targetField] = sourceValue; | ||
} | ||
} | ||
|
||
return target; | ||
} |
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.