Skip to content

Commit

Permalink
untested applepay address format
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermer committed Dec 12, 2023
1 parent a96dc2a commit 077870f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/lib/src/components/ApplePay/ApplePay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import defaultProps from './defaultProps';
import { httpPost } from '../../core/Services/http';
import { APPLEPAY_SESSION_ENDPOINT } from './config';
import { preparePaymentRequest } from './payment-request';
import { resolveSupportedVersion, mapBrands } from './utils';
import { resolveSupportedVersion, mapBrands, formatApplePayContactToAdyenAddressFormat } from './utils';
import {
ApplePayConfiguration,
ApplePayElementData,
Expand Down Expand Up @@ -53,10 +53,14 @@ class ApplePayElement extends UIElement<ApplePayConfiguration> {
* Formats the component data output
*/
protected formatData(): ApplePayElementData {
const { applePayToken, billingAddress, shippingAddress } = this.state;

return {
paymentMethod: {
type: ApplePayElement.type,
applePayToken: this.state.applePayToken
applePayToken,
...(billingAddress && { billingAddress }),
...(shippingAddress && { shippingAddress })
}
};
}
Expand Down Expand Up @@ -96,9 +100,14 @@ class ApplePayElement extends UIElement<ApplePayConfiguration> {
onShippingContactSelected,
onValidateMerchant: onValidateMerchant || this.validateMerchant,
onPaymentAuthorized: (resolve, reject, event) => {
const billingAddress = formatApplePayContactToAdyenAddressFormat(event.payment.billingContact);
const shippingAddress = formatApplePayContactToAdyenAddressFormat(event.payment.shippingContact);

this.setState({
applePayToken: btoa(JSON.stringify(event.payment.token.paymentData)),
authorizedEvent: event
authorizedEvent: event,
...(billingAddress && { billingAddress }),
...(shippingAddress && { shippingAddress })
});

this.makePaymentsCall()
Expand Down
4 changes: 4 additions & 0 deletions packages/lib/src/components/ApplePay/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { UIElementProps } from '../internal/UIElement/types';
import { AddressSchema } from '../internal/Address/types';

Check failure on line 2 in packages/lib/src/components/ApplePay/types.ts

View workflow job for this annotation

GitHub Actions / size

'AddressSchema' is defined but never used

Check failure on line 2 in packages/lib/src/components/ApplePay/types.ts

View workflow job for this annotation

GitHub Actions / build (18.15)

'AddressSchema' is defined but never used

Check failure on line 2 in packages/lib/src/components/ApplePay/types.ts

View workflow job for this annotation

GitHub Actions / build-then-e2e (18.15)

'AddressSchema' is defined but never used
import { AddressData } from '../../types/global-types';

declare global {
interface Window {
Expand Down Expand Up @@ -198,6 +200,8 @@ export interface ApplePayElementData {
paymentMethod: {
type: string;
applePayToken: string;
billingAddress?: AddressData;
shippingAddress?: AddressData;
};
}

Expand Down
47 changes: 47 additions & 0 deletions packages/lib/src/components/ApplePay/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AddressData } from '../../types/global-types';

export function resolveSupportedVersion(latestVersion: number): number | null {
const versions = [];
for (let i = latestVersion; i > 0; i--) {
Expand Down Expand Up @@ -36,3 +38,48 @@ export function mapBrands(brands) {
return accumulator;
}, []);
}

/**
* ApplePay formats address into two lines (US format). First line includes house number and street name.
* Second line includes unit/suite/apartment number if applicable.
* This function formats it into Adyen's Address format (house number separate from street).
*/
export function formatApplePayContactToAdyenAddressFormat(
paymentContact: ApplePayJS.ApplePayPaymentContact
): AddressData | undefined {
if (!paymentContact) {
return;
}

let street = '';
let houseNumberOrName = '';
if (paymentContact.addressLines && paymentContact.addressLines.length) {
const splitAddress = splitAddressLine(paymentContact.addressLines[0]);
street = splitAddress.streetAddress;
houseNumberOrName = splitAddress.houseNumber;
}

if (paymentContact.addressLines && paymentContact.addressLines.length > 1) {
street += ` ${paymentContact.addressLines[1]}`;
}

return {
city: paymentContact.locality,
country: paymentContact.countryCode,
houseNumberOrName,
postalCode: paymentContact.postalCode,
stateOrProvince: paymentContact.administrativeArea,
street: street
};
}

const splitAddressLine = (addressLine: string) => {
// The \d+ captures the digits of the house number, and \w* allows for any letter suffixes (like "123B")
// Everything after the space is considered the street address.
const parts = addressLine.match(/^(\d+\w*)\s+(.+)/);
if (parts) {
return { houseNumber: parts[1] || '', streetAddress: parts[2] || addressLine };
} else {
return { houseNumber: '', streetAddress: addressLine };
}
};

0 comments on commit 077870f

Please sign in to comment.