@xpayeg/react 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,239 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode, FC } from 'react';
3
+ import * as _xpayeg_sdk from '@xpayeg/sdk';
4
+ import { CheckoutSession, CheckoutActions, XPayInstance, ElementsOptions, Elements, PaymentElementOptions, PaymentElementChangeEvent, CardElementOptions, CardElementChangeEvent, CheckoutOptions } from '@xpayeg/sdk';
5
+ export { ActionResult, Appearance, CardElementChangeEvent, CheckoutActions, CheckoutSession, ConfirmPaymentOptions, ElementsOptions, PaymentElementChangeEvent, PaymentMethodInfo, XPayError } from '@xpayeg/sdk';
6
+
7
+ /**
8
+ * The checkout object returned by `useCheckout()` on success.
9
+ * Merges session data with action methods for a single unified API.
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * const state = useCheckout();
14
+ * if (state.type === "success") {
15
+ * const checkout: Checkout = state.checkout;
16
+ * console.log(checkout.amountTotal); // session data
17
+ * await checkout.confirm(); // action method
18
+ * }
19
+ * ```
20
+ */
21
+ type Checkout = CheckoutSession & CheckoutActions;
22
+ type UseCheckoutResult = {
23
+ type: "loading";
24
+ } | {
25
+ type: "error";
26
+ error: {
27
+ message: string;
28
+ };
29
+ } | {
30
+ type: "success";
31
+ checkout: Checkout;
32
+ };
33
+ interface XPayProviderProps {
34
+ /**
35
+ * XPay instance or a Promise resolving to one.
36
+ * Use `loadXPay()` from `@xpayeg/sdk` — call it at module level, not inside a component.
37
+ */
38
+ xpay: XPayInstance | Promise<XPayInstance | null> | null;
39
+ /** Elements options. Must include `clientSecret`. */
40
+ options?: ElementsOptions;
41
+ children: ReactNode;
42
+ }
43
+ /**
44
+ * Provides XPay context to all child components.
45
+ *
46
+ * Accepts either a resolved XPay instance or a Promise (from `loadXPay()`).
47
+ * When `options` (with `clientSecret`) is provided, creates an Elements instance
48
+ * and fetches session data. Use `useCheckout()` in child components to access
49
+ * session data and action methods.
50
+ *
51
+ * @example
52
+ * ```tsx
53
+ * import { loadXPay } from "@xpayeg/sdk";
54
+ * import { XPayProvider, useCheckout, PaymentElement } from "@xpayeg/react";
55
+ *
56
+ * const xpayPromise = loadXPay("pk_test_xxx");
57
+ *
58
+ * function App() {
59
+ * return (
60
+ * <XPayProvider xpay={xpayPromise} options={{ clientSecret }}>
61
+ * <CheckoutForm />
62
+ * </XPayProvider>
63
+ * );
64
+ * }
65
+ * ```
66
+ */
67
+ declare function XPayProvider({ xpay: xpayProp, options, children }: XPayProviderProps): react_jsx_runtime.JSX.Element;
68
+ /**
69
+ * Access the checkout state — a tagged union of `loading`, `error`, or `success`.
70
+ *
71
+ * On success, returns a `checkout` object (type `Checkout`) that merges
72
+ * session data with action methods (confirm, promo codes, quantities, etc.).
73
+ *
74
+ * @returns `UseCheckoutResult` — narrow the type by checking `result.type`
75
+ * @throws Error if used outside of `<XPayProvider>`
76
+ *
77
+ * @example
78
+ * ```tsx
79
+ * function CheckoutForm() {
80
+ * const state = useCheckout();
81
+ *
82
+ * if (state.type === "loading") return <Skeleton />;
83
+ * if (state.type === "error") return <p>{state.error.message}</p>;
84
+ *
85
+ * const { checkout } = state;
86
+ * return <button onClick={() => checkout.confirm()}>
87
+ * Pay {checkout.currency} {checkout.amountTotal}
88
+ * </button>;
89
+ * }
90
+ * ```
91
+ */
92
+ declare function useCheckout(): UseCheckoutResult;
93
+ /**
94
+ * Access the raw XPay SDK instance. Returns `null` while the SDK is loading.
95
+ *
96
+ * Most merchants should use `useCheckout()` instead. Use `useXPay()` only when
97
+ * you need direct access to `xpay.confirmPayment()` or `xpay.checkout()`.
98
+ */
99
+ declare function useXPay(): XPayInstance | null;
100
+ /**
101
+ * Access the Elements instance. Returns `null` while the session is loading.
102
+ *
103
+ * Use this for direct element management. In most cases, use `useCheckout()` instead
104
+ * which provides both session data and action methods.
105
+ */
106
+ declare function useElements(): Elements | null;
107
+
108
+ /**
109
+ * Convenience hook for payment confirmation.
110
+ *
111
+ * @example
112
+ * ```tsx
113
+ * const { confirmPayment, isConfirming } = useConfirmPayment();
114
+ * ```
115
+ */
116
+ declare function useConfirmPayment(): {
117
+ confirmPayment: () => Promise<{
118
+ type: "error";
119
+ error: {
120
+ message: string;
121
+ code: null;
122
+ };
123
+ }>;
124
+ isConfirming: boolean;
125
+ } | {
126
+ confirmPayment: (options?: Omit<_xpayeg_sdk.ConfirmPaymentOptions, "elements">) => Promise<_xpayeg_sdk.ActionResult>;
127
+ isConfirming: boolean;
128
+ };
129
+
130
+ interface PaymentElementProps {
131
+ /** Element configuration options (layout, default payment method, ordering) */
132
+ options?: PaymentElementOptions;
133
+ /** Fired when the element is mounted and the iframe content is ready */
134
+ onReady?: () => void;
135
+ /**
136
+ * Fired on every state change — payment method selection, card field input, completion.
137
+ * Use `event.complete` to enable/disable your pay button.
138
+ */
139
+ onChange?: (event: PaymentElementChangeEvent) => void;
140
+ /** Fired when the element's iframe starts loading — show a custom loading skeleton */
141
+ onLoaderStart?: () => void;
142
+ /** Fired when the element fails to load (network error, invalid client secret) */
143
+ onLoadError?: (event: {
144
+ type: string;
145
+ message: string;
146
+ code?: string;
147
+ }) => void;
148
+ /** CSS class name for the container `<div>` */
149
+ className?: string;
150
+ /** HTML id for the container `<div>` */
151
+ id?: string;
152
+ }
153
+ /**
154
+ * Renders the XPay Payment Element — a full payment method selector with card form.
155
+ *
156
+ * Must be used inside `<XPayProvider>` with an `options` prop containing `clientSecret`.
157
+ * Handles mount/unmount lifecycle, StrictMode, and SSR automatically.
158
+ *
159
+ * @example
160
+ * ```tsx
161
+ * <XPayProvider xpay={xpayPromise} options={{ clientSecret }}>
162
+ * <PaymentElement
163
+ * onChange={(e) => setPaymentReady(e.complete)}
164
+ * onLoadError={(e) => console.error(e.message)}
165
+ * />
166
+ * </XPayProvider>
167
+ * ```
168
+ */
169
+ declare const PaymentElement: FC<PaymentElementProps>;
170
+
171
+ interface CardElementProps {
172
+ /** Element configuration options */
173
+ options?: CardElementOptions;
174
+ /** Fired when the element is mounted and the iframe content is ready */
175
+ onReady?: () => void;
176
+ /** Fired on every card field change — use `event.complete` to enable/disable your pay button */
177
+ onChange?: (event: CardElementChangeEvent) => void;
178
+ /** Fired when the element's iframe starts loading */
179
+ onLoaderStart?: () => void;
180
+ /** Fired when the element fails to load */
181
+ onLoadError?: (event: {
182
+ type: string;
183
+ message: string;
184
+ code?: string;
185
+ }) => void;
186
+ /** CSS class name for the container `<div>` */
187
+ className?: string;
188
+ /** HTML id for the container `<div>` */
189
+ id?: string;
190
+ }
191
+ /**
192
+ * Renders the XPay Card Element — a card-only form (number, expiry, CVV).
193
+ *
194
+ * Use this when you handle payment method selection yourself.
195
+ * Must be used inside `<XPayProvider>` with an `options` prop containing `clientSecret`.
196
+ *
197
+ * @example
198
+ * ```tsx
199
+ * <CardElement
200
+ * onChange={(e) => setCardReady(e.complete)}
201
+ * />
202
+ * ```
203
+ */
204
+ declare const CardElement: FC<CardElementProps>;
205
+
206
+ interface CheckoutButtonProps {
207
+ /** Checkout session client secret */
208
+ clientSecret: string;
209
+ /** Button content */
210
+ children?: ReactNode;
211
+ /** Checkout options (callbacks, appearance, locale) */
212
+ checkoutOptions?: Omit<CheckoutOptions, "clientSecret" | "mode">;
213
+ className?: string;
214
+ disabled?: boolean;
215
+ }
216
+ /**
217
+ * Button that opens the drop-in checkout modal on click.
218
+ *
219
+ * Must be inside XPayProvider.
220
+ *
221
+ * @example
222
+ * ```tsx
223
+ * <XPayProvider xpay={xpay}>
224
+ * <CheckoutButton
225
+ * clientSecret="cs_test_abc_secret_xyz"
226
+ * checkoutOptions={{
227
+ * onComplete: (result) => router.push('/success'),
228
+ * onClose: () => console.log('Closed'),
229
+ * }}
230
+ * >
231
+ * Pay Now
232
+ * </CheckoutButton>
233
+ * </XPayProvider>
234
+ * ```
235
+ */
236
+ declare const CheckoutButton: FC<CheckoutButtonProps>;
237
+
238
+ export { CardElement, CheckoutButton, PaymentElement, XPayProvider, useCheckout, useConfirmPayment, useElements, useXPay };
239
+ export type { CardElementProps, Checkout, CheckoutButtonProps, PaymentElementProps, UseCheckoutResult };
@@ -0,0 +1,239 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode, FC } from 'react';
3
+ import * as _xpayeg_sdk from '@xpayeg/sdk';
4
+ import { CheckoutSession, CheckoutActions, XPayInstance, ElementsOptions, Elements, PaymentElementOptions, PaymentElementChangeEvent, CardElementOptions, CardElementChangeEvent, CheckoutOptions } from '@xpayeg/sdk';
5
+ export { ActionResult, Appearance, CardElementChangeEvent, CheckoutActions, CheckoutSession, ConfirmPaymentOptions, ElementsOptions, PaymentElementChangeEvent, PaymentMethodInfo, XPayError } from '@xpayeg/sdk';
6
+
7
+ /**
8
+ * The checkout object returned by `useCheckout()` on success.
9
+ * Merges session data with action methods for a single unified API.
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * const state = useCheckout();
14
+ * if (state.type === "success") {
15
+ * const checkout: Checkout = state.checkout;
16
+ * console.log(checkout.amountTotal); // session data
17
+ * await checkout.confirm(); // action method
18
+ * }
19
+ * ```
20
+ */
21
+ type Checkout = CheckoutSession & CheckoutActions;
22
+ type UseCheckoutResult = {
23
+ type: "loading";
24
+ } | {
25
+ type: "error";
26
+ error: {
27
+ message: string;
28
+ };
29
+ } | {
30
+ type: "success";
31
+ checkout: Checkout;
32
+ };
33
+ interface XPayProviderProps {
34
+ /**
35
+ * XPay instance or a Promise resolving to one.
36
+ * Use `loadXPay()` from `@xpayeg/sdk` — call it at module level, not inside a component.
37
+ */
38
+ xpay: XPayInstance | Promise<XPayInstance | null> | null;
39
+ /** Elements options. Must include `clientSecret`. */
40
+ options?: ElementsOptions;
41
+ children: ReactNode;
42
+ }
43
+ /**
44
+ * Provides XPay context to all child components.
45
+ *
46
+ * Accepts either a resolved XPay instance or a Promise (from `loadXPay()`).
47
+ * When `options` (with `clientSecret`) is provided, creates an Elements instance
48
+ * and fetches session data. Use `useCheckout()` in child components to access
49
+ * session data and action methods.
50
+ *
51
+ * @example
52
+ * ```tsx
53
+ * import { loadXPay } from "@xpayeg/sdk";
54
+ * import { XPayProvider, useCheckout, PaymentElement } from "@xpayeg/react";
55
+ *
56
+ * const xpayPromise = loadXPay("pk_test_xxx");
57
+ *
58
+ * function App() {
59
+ * return (
60
+ * <XPayProvider xpay={xpayPromise} options={{ clientSecret }}>
61
+ * <CheckoutForm />
62
+ * </XPayProvider>
63
+ * );
64
+ * }
65
+ * ```
66
+ */
67
+ declare function XPayProvider({ xpay: xpayProp, options, children }: XPayProviderProps): react_jsx_runtime.JSX.Element;
68
+ /**
69
+ * Access the checkout state — a tagged union of `loading`, `error`, or `success`.
70
+ *
71
+ * On success, returns a `checkout` object (type `Checkout`) that merges
72
+ * session data with action methods (confirm, promo codes, quantities, etc.).
73
+ *
74
+ * @returns `UseCheckoutResult` — narrow the type by checking `result.type`
75
+ * @throws Error if used outside of `<XPayProvider>`
76
+ *
77
+ * @example
78
+ * ```tsx
79
+ * function CheckoutForm() {
80
+ * const state = useCheckout();
81
+ *
82
+ * if (state.type === "loading") return <Skeleton />;
83
+ * if (state.type === "error") return <p>{state.error.message}</p>;
84
+ *
85
+ * const { checkout } = state;
86
+ * return <button onClick={() => checkout.confirm()}>
87
+ * Pay {checkout.currency} {checkout.amountTotal}
88
+ * </button>;
89
+ * }
90
+ * ```
91
+ */
92
+ declare function useCheckout(): UseCheckoutResult;
93
+ /**
94
+ * Access the raw XPay SDK instance. Returns `null` while the SDK is loading.
95
+ *
96
+ * Most merchants should use `useCheckout()` instead. Use `useXPay()` only when
97
+ * you need direct access to `xpay.confirmPayment()` or `xpay.checkout()`.
98
+ */
99
+ declare function useXPay(): XPayInstance | null;
100
+ /**
101
+ * Access the Elements instance. Returns `null` while the session is loading.
102
+ *
103
+ * Use this for direct element management. In most cases, use `useCheckout()` instead
104
+ * which provides both session data and action methods.
105
+ */
106
+ declare function useElements(): Elements | null;
107
+
108
+ /**
109
+ * Convenience hook for payment confirmation.
110
+ *
111
+ * @example
112
+ * ```tsx
113
+ * const { confirmPayment, isConfirming } = useConfirmPayment();
114
+ * ```
115
+ */
116
+ declare function useConfirmPayment(): {
117
+ confirmPayment: () => Promise<{
118
+ type: "error";
119
+ error: {
120
+ message: string;
121
+ code: null;
122
+ };
123
+ }>;
124
+ isConfirming: boolean;
125
+ } | {
126
+ confirmPayment: (options?: Omit<_xpayeg_sdk.ConfirmPaymentOptions, "elements">) => Promise<_xpayeg_sdk.ActionResult>;
127
+ isConfirming: boolean;
128
+ };
129
+
130
+ interface PaymentElementProps {
131
+ /** Element configuration options (layout, default payment method, ordering) */
132
+ options?: PaymentElementOptions;
133
+ /** Fired when the element is mounted and the iframe content is ready */
134
+ onReady?: () => void;
135
+ /**
136
+ * Fired on every state change — payment method selection, card field input, completion.
137
+ * Use `event.complete` to enable/disable your pay button.
138
+ */
139
+ onChange?: (event: PaymentElementChangeEvent) => void;
140
+ /** Fired when the element's iframe starts loading — show a custom loading skeleton */
141
+ onLoaderStart?: () => void;
142
+ /** Fired when the element fails to load (network error, invalid client secret) */
143
+ onLoadError?: (event: {
144
+ type: string;
145
+ message: string;
146
+ code?: string;
147
+ }) => void;
148
+ /** CSS class name for the container `<div>` */
149
+ className?: string;
150
+ /** HTML id for the container `<div>` */
151
+ id?: string;
152
+ }
153
+ /**
154
+ * Renders the XPay Payment Element — a full payment method selector with card form.
155
+ *
156
+ * Must be used inside `<XPayProvider>` with an `options` prop containing `clientSecret`.
157
+ * Handles mount/unmount lifecycle, StrictMode, and SSR automatically.
158
+ *
159
+ * @example
160
+ * ```tsx
161
+ * <XPayProvider xpay={xpayPromise} options={{ clientSecret }}>
162
+ * <PaymentElement
163
+ * onChange={(e) => setPaymentReady(e.complete)}
164
+ * onLoadError={(e) => console.error(e.message)}
165
+ * />
166
+ * </XPayProvider>
167
+ * ```
168
+ */
169
+ declare const PaymentElement: FC<PaymentElementProps>;
170
+
171
+ interface CardElementProps {
172
+ /** Element configuration options */
173
+ options?: CardElementOptions;
174
+ /** Fired when the element is mounted and the iframe content is ready */
175
+ onReady?: () => void;
176
+ /** Fired on every card field change — use `event.complete` to enable/disable your pay button */
177
+ onChange?: (event: CardElementChangeEvent) => void;
178
+ /** Fired when the element's iframe starts loading */
179
+ onLoaderStart?: () => void;
180
+ /** Fired when the element fails to load */
181
+ onLoadError?: (event: {
182
+ type: string;
183
+ message: string;
184
+ code?: string;
185
+ }) => void;
186
+ /** CSS class name for the container `<div>` */
187
+ className?: string;
188
+ /** HTML id for the container `<div>` */
189
+ id?: string;
190
+ }
191
+ /**
192
+ * Renders the XPay Card Element — a card-only form (number, expiry, CVV).
193
+ *
194
+ * Use this when you handle payment method selection yourself.
195
+ * Must be used inside `<XPayProvider>` with an `options` prop containing `clientSecret`.
196
+ *
197
+ * @example
198
+ * ```tsx
199
+ * <CardElement
200
+ * onChange={(e) => setCardReady(e.complete)}
201
+ * />
202
+ * ```
203
+ */
204
+ declare const CardElement: FC<CardElementProps>;
205
+
206
+ interface CheckoutButtonProps {
207
+ /** Checkout session client secret */
208
+ clientSecret: string;
209
+ /** Button content */
210
+ children?: ReactNode;
211
+ /** Checkout options (callbacks, appearance, locale) */
212
+ checkoutOptions?: Omit<CheckoutOptions, "clientSecret" | "mode">;
213
+ className?: string;
214
+ disabled?: boolean;
215
+ }
216
+ /**
217
+ * Button that opens the drop-in checkout modal on click.
218
+ *
219
+ * Must be inside XPayProvider.
220
+ *
221
+ * @example
222
+ * ```tsx
223
+ * <XPayProvider xpay={xpay}>
224
+ * <CheckoutButton
225
+ * clientSecret="cs_test_abc_secret_xyz"
226
+ * checkoutOptions={{
227
+ * onComplete: (result) => router.push('/success'),
228
+ * onClose: () => console.log('Closed'),
229
+ * }}
230
+ * >
231
+ * Pay Now
232
+ * </CheckoutButton>
233
+ * </XPayProvider>
234
+ * ```
235
+ */
236
+ declare const CheckoutButton: FC<CheckoutButtonProps>;
237
+
238
+ export { CardElement, CheckoutButton, PaymentElement, XPayProvider, useCheckout, useConfirmPayment, useElements, useXPay };
239
+ export type { CardElementProps, Checkout, CheckoutButtonProps, PaymentElementProps, UseCheckoutResult };