Skip to content

Commit

Permalink
Add address element types (#352)
Browse files Browse the repository at this point in the history
* add address types

* address comment

* run prettier

* update all usage to AddressElement
  • Loading branch information
graceg-stripe committed Sep 21, 2022
1 parent c647923 commit dea611e
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 5 deletions.
3 changes: 3 additions & 0 deletions tests/types/src/invalid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ paymentElement.on('change', (e) => {
}
});

// @ts-expect-error: AddressElement requires a mode
elements.create('address');

stripe
.confirmPayment({elements, confirmParams: {return_url: ''}})
.then((res) => {
Expand Down
73 changes: 73 additions & 0 deletions tests/types/src/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
StripeLinkAuthenticationElement,
StripeShippingAddressElementChangeEvent,
StripeShippingAddressElement,
StripeAddressElementChangeEvent,
StripeAddressElement,
StripeElementType,
CanMakePaymentResult,
VerificationSession,
Expand Down Expand Up @@ -491,6 +493,77 @@ const retrievedLinkAuthenticationElement: StripeLinkAuthenticationElement | null
'linkAuthentication'
);

let addressElementDefaults: StripeAddressElement = elements.create('address', {
mode: 'shipping',
});

addressElementDefaults = elements.create('address', {mode: 'billing'});

const addressElement = elements.create('address', {
mode: 'shipping',
allowedCountries: ['US'],
autocomplete: {mode: 'disabled'},
contacts: [
{
name: 'Jane Doe',
address: {
line1: '513 Townsend St',
city: 'San Francisco',
state: 'CA',
postal_code: '92122',
country: 'US',
},
},
],
blockPoBox: true,
defaultValues: {
name: 'Jane Doe',
address: {
line1: '513 Townsend St',
city: 'San Francisco',
state: 'CA',
postal_code: '92122',
country: 'US',
},
},
fields: {
phone: 'always',
},
validation: {
phone: {
required: 'never',
},
},
});

addressElement
.on('ready', (e: {elementType: 'address'}) => {})
.on('focus', (e: {elementType: 'address'}) => {})
.on('blur', (e: {elementType: 'address'}) => {})
.on('change', (e: StripeAddressElementChangeEvent) => {})
.on('loaderstart', (e: {elementType: 'address'}) => {})
.on(
'loaderror',
(e: {
elementType: 'address';
error: {
type: string;
};
}) => {}
);

addressElement.update({
validation: {
phone: {
required: 'always',
},
},
});

const retrievedAddressElement: StripeAddressElement | null = elements.getElement(
'address'
);

let shippingAddressElementDefaults: StripeShippingAddressElement = elements.create(
'shippingAddress'
);
Expand Down
38 changes: 34 additions & 4 deletions types/stripe-js/elements-group.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
StripeAddressElement,
StripeAddressElementOptions,
StripeShippingAddressElement,
StripeShippingAddressElementOptions,
StripePaymentRequestButtonElement,
Expand Down Expand Up @@ -46,6 +48,29 @@ export interface StripeElements {
*/
fetchUpdates(): Promise<{error?: {message: string; status?: string}}>;

/////////////////////////////
/// address
/////////////////////////////

/**
* Requires beta access:
* Contact [Stripe support](https://support.stripe.com/) for more information.
*
* Creates a `AddressElement`.
*/
create(
elementType: 'address',
options: StripeAddressElementOptions
): StripeAddressElement;

/**
* Requires beta access:
* Contact [Stripe support](https://support.stripe.com/) for more information.
*
* Looks up a previously created `Element` by its type.
*/
getElement(elementType: 'address'): StripeAddressElement | null;

/////////////////////////////
/// affirmMessage
/////////////////////////////
Expand Down Expand Up @@ -329,9 +354,13 @@ export interface StripeElements {
elementType: 'paymentRequestButton'
): StripePaymentRequestButtonElement | null;

/////////////////////////////
/// shippingAddress
/////////////////////////////

/**
* Requires beta access:
* Contact [Stripe support](https://support.stripe.com/) for more information.
* @deprecated
* Use `Address` element instead.
*
* Creates a `ShippingAddressElement`.
*/
Expand All @@ -341,8 +370,8 @@ export interface StripeElements {
): StripeShippingAddressElement;

/**
* Requires beta access:
* Contact [Stripe support](https://support.stripe.com/) for more information.
* @deprecated
* Use `Address` element instead.
*
* Looks up a previously created `Element` by its type.
*/
Expand All @@ -352,6 +381,7 @@ export interface StripeElements {
}

export type StripeElementType =
| 'address'
| 'affirmMessage'
| 'afterpayClearpayMessage'
| 'auBankAccount'
Expand Down

0 comments on commit dea611e

Please sign in to comment.