Skip to content

Commit 189e1c4

Browse files
Add PBE component
1 parent 5830f3f commit 189e1c4

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

src/components/createElementComponent.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ interface PrivateElementProps {
3434
onNetworksChange?: UnknownCallback;
3535
onCheckout?: UnknownCallback;
3636
onLineItemClick?: UnknownCallback;
37+
onConfirm?: UnknownCallback;
38+
onCancel?: UnknownCallback;
39+
onShippingAddressChange?: UnknownCallback;
40+
onShippingRateChange?: UnknownCallback;
3741
options?: UnknownOptions;
3842
}
3943

@@ -62,6 +66,10 @@ const createElementComponent = (
6266
onNetworksChange = noop,
6367
onCheckout = noop,
6468
onLineItemClick = noop,
69+
onConfirm = noop,
70+
onCancel = noop,
71+
onShippingAddressChange = noop,
72+
onShippingRateChange = noop,
6573
}) => {
6674
const {elements} = useElementsContextWithUseCase(`mounts <${displayName}>`);
6775
const elementRef = React.useRef<stripeJs.StripeElement | null>(null);
@@ -82,6 +90,12 @@ const createElementComponent = (
8290
const callOnNetworksChange = useCallbackReference(onNetworksChange);
8391
const callOnCheckout = useCallbackReference(onCheckout);
8492
const callOnLineItemClick = useCallbackReference(onLineItemClick);
93+
const callOnConfirm = useCallbackReference(onConfirm);
94+
const callOnCancel = useCallbackReference(onCancel);
95+
const callOnShippingAddressChange = useCallbackReference(
96+
onShippingAddressChange
97+
);
98+
const callOnShippingRateChange = useCallbackReference(onShippingRateChange);
8599

86100
React.useLayoutEffect(() => {
87101
if (elementRef.current == null && elements && domNode.current != null) {
@@ -174,6 +188,29 @@ const createElementComponent = (
174188
// just as they could listen for the `lineitemclick` event on any Element,
175189
// but only certain Elements will trigger the event.
176190
(element as any).on('lineitemclick', callOnLineItemClick);
191+
192+
// Users can pass an onConfirm prop on any Element component
193+
// just as they could listen for the `confirm` event on any Element,
194+
// but only certain Elements will trigger the event.
195+
(element as any).on('confirm', callOnConfirm);
196+
197+
// Users can pass an onCancel prop on any Element component
198+
// just as they could listen for the `cancel` event on any Element,
199+
// but only certain Elements will trigger the event.
200+
(element as any).on('cancel', callOnCancel);
201+
202+
// Users can pass an onShippingAddressChange prop on any Element component
203+
// just as they could listen for the `shippingaddresschange` event on any Element,
204+
// but only certain Elements will trigger the event.
205+
(element as any).on(
206+
'shippingaddresschange',
207+
callOnShippingAddressChange
208+
);
209+
210+
// Users can pass an onShippingRateChange prop on any Element component
211+
// just as they could listen for the `shippingratechange` event on any Element,
212+
// but only certain Elements will trigger the event.
213+
(element as any).on('shippingratechange', callOnShippingRateChange);
177214
}
178215
});
179216

@@ -229,6 +266,10 @@ const createElementComponent = (
229266
onNetworksChange: PropTypes.func,
230267
onCheckout: PropTypes.func,
231268
onLineItemClick: PropTypes.func,
269+
onConfirm: PropTypes.func,
270+
onCancel: PropTypes.func,
271+
onShippingAddressChange: PropTypes.func,
272+
onShippingRateChange: PropTypes.func,
232273
options: PropTypes.object as any,
233274
};
234275

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
AffirmMessageElementComponent,
2020
AfterpayClearpayMessageElementComponent,
2121
PaymentMethodMessagingElementComponent,
22+
PayButtonElementComponent,
2223
} from './types';
2324

2425
export * from './types';
@@ -122,6 +123,17 @@ export const PaymentElement: PaymentElementComponent = createElementComponent(
122123
isServer
123124
);
124125

126+
/**
127+
* Requires beta access:
128+
* Contact [Stripe support](https://support.stripe.com/) for more information.
129+
*
130+
* @docs https://stripe.com/docs/stripe-js/react#element-components
131+
*/
132+
export const PayButtonElement: PayButtonElementComponent = createElementComponent(
133+
'payButton',
134+
isServer
135+
);
136+
125137
/**
126138
* @docs https://stripe.com/docs/stripe-js/react#element-components
127139
*/

src/types/index.ts

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,62 @@ export interface PaymentElementProps extends ElementProps {
431431

432432
export type PaymentElementComponent = FunctionComponent<PaymentElementProps>;
433433

434+
export interface PayButtonElementProps extends ElementProps {
435+
/**
436+
* An object containing Element configuration options.
437+
*/
438+
options?: stripeJs.StripePayButtonElementOptions;
439+
440+
/**
441+
* Triggered when the Element is fully rendered and can accept imperative `element.focus()` calls.
442+
* Called with a reference to the underlying [Element instance](https://stripe.com/docs/js/element).
443+
*/
444+
onReady?: (element: stripeJs.StripePayButtonElement) => any;
445+
446+
/**
447+
* Triggered when the escape key is pressed within the Element.
448+
*/
449+
onEscape?: () => any;
450+
451+
/**
452+
* Triggered when the Element fails to load.
453+
*/
454+
onLoadError?: (event: {elementType: 'payButton'; error: StripeError}) => any;
455+
456+
/**
457+
* Triggered when a button on the Element is clicked.
458+
*/
459+
onClick?: (event: stripeJs.StripePayButtonElementClickEvent) => any;
460+
461+
/**
462+
* Triggered when a buyer authorizes a payment within a supported payment method.
463+
*/
464+
onConfirm: (event: stripeJs.StripePayButtonElementConfirmEvent) => any;
465+
466+
/**
467+
* Triggered when a payment interface is dismissed (e.g., a buyer closes the payment interface)
468+
*/
469+
onCancel?: (event: {elementType: 'payButton'}) => any;
470+
471+
/**
472+
* Triggered when a buyer selects a different shipping address.
473+
*/
474+
onShippingAddressChange?: (
475+
event: stripeJs.StripePayButtonElementShippingAddressChangeEvent
476+
) => any;
477+
478+
/**
479+
* Triggered when a buyer selects a different shipping rate.
480+
*/
481+
onShippingRateChange?: (
482+
event: stripeJs.StripePayButtonElementShippingRateChangeEvent
483+
) => any;
484+
}
485+
486+
export type PayButtonElementComponent = FunctionComponent<
487+
PayButtonElementProps
488+
>;
489+
434490
export interface PaymentRequestButtonElementProps extends ElementProps {
435491
/**
436492
* An object containing [Element configuration options](https://stripe.com/docs/js/elements_object/create_element?type=paymentRequestButton).
@@ -707,17 +763,29 @@ declare module '@stripe/stripe-js' {
707763
): stripeJs.StripeEpsBankElement | null;
708764

709765
/**
710-
* Returns the underlying [element instance](https://stripe.com/docs/js/elements_object/create_element?type=card) for the `LinkAuthenticationElement` component in the current [Elements](https://stripe.com/docs/stripe-js/react#elements-provider) provider tree.
766+
* Returns the underlying [element instance](https://stripe.com/docs/js/elements_object/create_link_authentication_element) for the `LinkAuthenticationElement` component in the current [Elements](https://stripe.com/docs/stripe-js/react#elements-provider) provider tree.
711767
* Returns `null` if no `LinkAuthenticationElement` is rendered in the current `Elements` provider tree.
712768
*/
713769
getElement(
714770
component: LinkAuthenticationElementComponent
715771
): stripeJs.StripeLinkAuthenticationElement | null;
716772

773+
/**
774+
* Returns the underlying [element instance](https://stripe.com/docs/js/elements_object/create_payment_element) for the `PaymentElement` component in the current [Elements](https://stripe.com/docs/stripe-js/react#elements-provider) provider tree.
775+
* Returns `null` if no `PaymentElement` is rendered in the current `Elements` provider tree.
776+
*/
717777
getElement(
718778
component: PaymentElementComponent
719779
): stripeJs.StripeElement | null;
720780

781+
/**
782+
* Returns the underlying [element instance](https://stripe.com/docs/js/elements_object/create_pay_button_element) for the `PayButtonElement` component in the current [Elements](https://stripe.com/docs/stripe-js/react#elements-provider) provider tree.
783+
* Returns `null` if no `PayButtonElement` is rendered in the current `Elements` provider tree.
784+
*/
785+
getElement(
786+
component: PayButtonElementComponent
787+
): stripeJs.StripeElement | null;
788+
721789
/**
722790
* Returns the underlying [element instance](https://stripe.com/docs/js/elements_object/create_element?type=card) for the `PaymentRequestButtonElement` component in the current [Elements](https://stripe.com/docs/stripe-js/react#elements-provider) provider tree.
723791
* Returns `null` if no `PaymentRequestButtonElement` is rendered in the current `Elements` provider tree.

0 commit comments

Comments
 (0)