@blocklet/payment-react 1.20.4 → 1.20.6

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.
Files changed (55) hide show
  1. package/.aigne/doc-smith/config.yaml +2 -2
  2. package/.aigne/doc-smith/upload-cache.yaml +381 -0
  3. package/docs/components-business-auto-topup.md +128 -179
  4. package/docs/components-business-overdue-invoice-payment.md +108 -143
  5. package/docs/components-business-resume-subscription.md +117 -104
  6. package/docs/components-business.md +12 -36
  7. package/docs/components-checkout-checkout-donate.md +209 -149
  8. package/docs/components-checkout-checkout-form.md +115 -136
  9. package/docs/components-checkout-checkout-table.md +92 -172
  10. package/docs/components-checkout.md +43 -109
  11. package/docs/components-history-credit-grants-list.md +45 -70
  12. package/docs/components-history-credit-transactions-list.md +57 -67
  13. package/docs/components-history-invoice-list.md +58 -52
  14. package/docs/components-history-payment-list.md +19 -40
  15. package/docs/components-history.md +42 -67
  16. package/docs/components-ui-form-elements-address-form.md +37 -65
  17. package/docs/components-ui-form-elements-country-select.md +80 -59
  18. package/docs/components-ui-form-elements-currency-selector.md +57 -73
  19. package/docs/components-ui-form-elements-phone-input.md +90 -112
  20. package/docs/components-ui-form-elements.md +46 -80
  21. package/docs/components-ui-payment-summary.md +71 -119
  22. package/docs/components-ui-pricing-table.md +117 -204
  23. package/docs/components-ui.md +59 -32
  24. package/docs/components.md +89 -62
  25. package/docs/getting-started.md +36 -63
  26. package/docs/guides-theming.md +122 -84
  27. package/docs/guides-utilities.md +107 -145
  28. package/docs/guides.md +7 -84
  29. package/docs/hooks-use-mobile.md +50 -36
  30. package/docs/hooks-use-subscription.md +72 -89
  31. package/docs/hooks.md +12 -82
  32. package/docs/overview.md +45 -52
  33. package/docs/providers-donate-provider.md +73 -95
  34. package/docs/providers-payment-provider.md +115 -169
  35. package/docs/providers.md +27 -86
  36. package/es/locales/en.js +7 -0
  37. package/es/locales/zh.js +8 -1
  38. package/es/payment/index.js +3 -0
  39. package/es/payment/progress-item.d.ts +12 -0
  40. package/es/payment/progress-item.js +78 -0
  41. package/es/payment/success.d.ts +4 -1
  42. package/es/payment/success.js +55 -3
  43. package/lib/locales/en.js +7 -0
  44. package/lib/locales/zh.js +8 -1
  45. package/lib/payment/index.js +3 -0
  46. package/lib/payment/progress-item.d.ts +12 -0
  47. package/lib/payment/progress-item.js +107 -0
  48. package/lib/payment/success.d.ts +4 -1
  49. package/lib/payment/success.js +59 -3
  50. package/package.json +7 -7
  51. package/src/locales/en.tsx +7 -0
  52. package/src/locales/zh.tsx +8 -1
  53. package/src/payment/index.tsx +6 -0
  54. package/src/payment/progress-item.tsx +107 -0
  55. package/src/payment/success.tsx +88 -3
@@ -1,185 +1,164 @@
1
1
  # CheckoutForm
2
2
 
3
- The `CheckoutForm` component is the primary entry point for rendering a complete payment or donation flow. It orchestrates the entire checkout process by fetching session details and rendering the appropriate user interface based on the provided configuration. It requires a `paymentLink` ID (starting with `plink_`) or a `checkoutSession` ID (starting with `cs_`) to initialize.
3
+ The `CheckoutForm` component is the primary entry point for handling payment links and checkout sessions. It acts as a high-level wrapper that fetches all necessary data based on a provided ID and renders the appropriate payment or donation interface. This component is the simplest way to integrate a complete checkout flow into your application.
4
4
 
5
- This component is highly flexible and can be rendered in several modes, including as an inline element, a standalone full-page experience, or a popup modal.
5
+ It's essential to wrap `CheckoutForm` or any of its parent components with the `PaymentProvider` to ensure it has access to the necessary context, such as session information and API configuration. For more details, please refer to the [PaymentProvider documentation](./providers-payment-provider.md).
6
6
 
7
7
  ## How It Works
8
8
 
9
- The `CheckoutForm` component follows a clear internal logic to manage the checkout process. It starts by identifying the type of ID provided, fetches the necessary data, and then renders the appropriate form (`Payment` or `DonationForm`) to handle user interaction.
9
+ The component orchestrates the entire checkout process:
10
10
 
11
- ```d2
11
+ 1. **Initialization**: It's mounted with a `paymentLink` ID (prefixed with `plink_`) or a `checkoutSession` ID (prefixed with `cs_`).
12
+ 2. **Data Fetching**: It communicates with the payment backend to retrieve all necessary context, including payment methods, line items, and customer details.
13
+ 3. **UI Rendering**: Based on the `formType` prop, it internally renders either the standard `Payment` component or the specialized `DonationForm` component.
14
+ 4. **State Management**: It handles the entire lifecycle of the payment, including loading states, completion status, and error handling.
15
+
16
+ ```d2 Basic Flow of CheckoutForm icon=lucide:workflow
12
17
  direction: down
18
+ shape: sequence_diagram
13
19
 
14
- A: "CheckoutForm Component Mounted"
15
- B: "Analyze 'id' prop prefix"
16
- C: "Call startFromPaymentLink API"
17
- D: "Call fetchCheckoutSession API"
18
- E: "Receive CheckoutContext"
19
- F: "Check 'formType' prop"
20
- G: "Render <Payment /> component"
21
- H: "Render <DonationForm /> component"
22
- I: "User completes payment flow"
23
- J: "Payment successful?"
24
- K: "Execute onPaid callback"
25
- L: "Execute onError callback"
26
-
27
- A -> B
28
- B -> C: "'plink_...'"
29
- B -> D: "'cs_...'"
30
- C -> E
31
- D -> E
32
- E -> F
33
- F -> G: "'payment' (default)"
34
- F -> H: "'donation'"
35
- G -> I
36
- H -> I
37
- I -> J
38
- J -> K: "Yes"
39
- J -> L: "No"
40
- ```
20
+ User-Action: {
21
+ shape: c4-person
22
+ label: "User"
23
+ }
41
24
 
42
- ## Props
25
+ Application: {
26
+ label: "Your React Application"
43
27
 
44
- The `CheckoutForm` accepts the following props to customize its behavior and appearance.
45
-
46
- | Prop | Type | Description |
47
- |---------------|-----------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
48
- | `id` | `string` | **Required.** The unique identifier for the checkout. Must start with `plink_` for a Payment Link or `cs_` for a Checkout Session. |
49
- | `mode` | `'standalone' \| 'inline' \| 'popup' \| 'inline-minimal' \| 'popup-minimal'` | The display mode. Defaults to `'inline'`. |
50
- | `formType` | `'payment' \| 'donation'` | The type of form to render. Use `'donation'` for a donation-specific UI. Defaults to `'payment'`. |
51
- | `onPaid` | `(res: CheckoutContext) => void` | Callback function executed upon successful payment. It receives the final checkout context. |
52
- | `onError` | `(err: Error) => void` | Callback function executed when an error occurs during the process. |
53
- | `onChange` | `(data: CheckoutFormData) => void` | Optional. Callback executed when form data (e.g., selected currency) changes. |
54
- | `goBack` | `() => void` | Optional. A function to handle a "back" button action, useful in multi-step flows. |
55
- | `extraParams` | `Record<string, any>` | Optional. An object of extra parameters to be sent when initializing a session from a Payment Link. |
56
- | `action` | `string` | Optional. A string to customize button text or other UI elements based on the action being performed (e.g., 'subscribe', 'donate'). |
57
- | `theme` | `'default' \| 'inherit' \| object` | The theme to apply. `'default'` uses the built-in theme, `'inherit'` uses the parent theme, and an object allows for custom Material-UI theme options. |
58
- | `formRender` | `Record<string, any>` | Optional. Used with `formType='donation'` to inject custom render elements or callbacks, such as a custom cancel button. |
28
+ CheckoutForm-Component: {
29
+ label: "CheckoutForm"
30
+ }
59
31
 
60
- ## Usage
32
+ Payment-Component: {
33
+ label: "Payment Component"
34
+ }
61
35
 
62
- ### Prerequisite: Wrapping with PaymentProvider
36
+ Donation-Component: {
37
+ label: "DonationForm Component"
38
+ }
39
+ }
63
40
 
64
- To use `CheckoutForm`, your application must be wrapped with the `PaymentProvider`. This provider is essential as it supplies the necessary context, including API configuration and session information, to all nested payment components.
41
+ Payment-API: {
42
+ label: "Payment Backend API"
43
+ shape: cylinder
44
+ }
65
45
 
66
- A common pattern is to integrate `PaymentProvider` with your application's existing session management. In the example below, `useSessionContext` is a placeholder for your own custom hook that provides the user session. This setup ensures that `CheckoutForm` has access to user data when needed.
46
+ User-Action -> Application.CheckoutForm-Component: "1. Mounts with 'id' prop"
47
+ Application.CheckoutForm-Component -> Payment-API: "2. Fetch session data"
48
+ Payment-API -> Application.CheckoutForm-Component: "3. Return checkout context"
67
49
 
68
- For a complete guide on setting up the `PaymentProvider` and creating your `useSessionContext` hook, please refer to the [PaymentProvider documentation](./providers-payment-provider.md).
50
+ alt "if formType is 'payment'" {
51
+ Application.CheckoutForm-Component -> Application.Payment-Component: "4. Renders Payment UI"
52
+ }
69
53
 
70
- ```tsx
71
- import { PaymentProvider } from '@blocklet/payment-react';
72
- import { CheckoutForm } from '@blocklet/payment-react';
73
- // This is a placeholder for your application's session context hook.
74
- // See the PaymentProvider documentation for implementation details.
75
- import { useSessionContext } from '../hooks/session-context';
54
+ alt "if formType is 'donation'" {
55
+ Application.CheckoutForm-Component -> Application.Donation-Component: "5. Renders Donation UI"
56
+ }
76
57
 
77
- // Your main application component
78
- function App() {
79
- const { session, connectDid, disconnectDid } = useSessionContext();
58
+ User-Action -> Application.Payment-Component: "6. Completes payment"
59
+ User-Action -> Application.Donation-Component: "7. Completes donation"
80
60
 
81
- return (
82
- <PaymentProvider
83
- apiHost="https://your-api-host"
84
- // Pass session and connection handlers from your local context
85
- session={session}
86
- connect={connectDid}
87
- disconnectDid={disconnectDid}>
88
- <MyCheckoutPage />
89
- </PaymentProvider>
90
- );
91
- }
61
+ ```
92
62
 
93
- // A component that renders the checkout form
94
- function MyCheckoutPage() {
95
- const { session } = useSessionContext();
96
- const paymentLinkId = 'plink_xxxxxxxxxxxx';
63
+ ## Props
97
64
 
98
- // Conditionally render based on user login status
99
- if (!session?.user) {
100
- return <p>Please log in to proceed with the payment.</p>;
101
- }
65
+ The `CheckoutForm` component accepts the following props:
66
+
67
+ | Prop | Type | Required | Default | Description |
68
+ |---------------|----------------------------------------------------------------------------|----------|---------------|-------------------------------------------------------------------------------------------------------------------------|
69
+ | `id` | `string` | Yes | - | The unique identifier for the payment link (`plink_...`) or checkout session (`cs_...`). |
70
+ | `mode` | `'standalone'` \| `'inline'` \| `'popup'` \| `'inline-minimal'` \| `'popup-minimal'` | No | `'inline'` | Defines the rendering mode. `'standalone'` for a full-page view, `'inline'` to embed in a container. |
71
+ | `formType` | `'payment'` \| `'donation'` | No | `'payment'` | Determines the type of form to render. Use `'donation'` for the specialized donation flow. |
72
+ | `onPaid` | `(res: CheckoutContext) => void` | No | - | Callback function executed upon successful payment. Receives the final checkout context as an argument. |
73
+ | `onError` | `(err: Error) => void` | No | `console.error` | Callback function executed when an error occurs during the process. |
74
+ | `onChange` | `(data: CheckoutFormData) => void` | No | - | Callback function that fires when any form field value changes. |
75
+ | `goBack` | `() => void` | No | - | If provided, renders a back button and calls this function when clicked. |
76
+ | `extraParams` | `Record<string, any>` | No | `{}` | An object of extra parameters to be passed in the URL when initiating a session from a payment link. |
77
+ | `theme` | `'default'` \| `'inherit'` \| `PaymentThemeOptions` | No | `'default'` | Controls the component's theme. `'inherit'` uses the parent theme, or you can pass a custom theme object. |
78
+ | `action` | `string` | No | `''` | A string identifier used for customizing UI elements like button text or tracking specific flows. |
102
79
 
103
- return (
104
- <CheckoutForm
105
- id={paymentLinkId}
106
- onPaid={() => console.log('Payment successful!')}
107
- onError={(err) => console.error('Payment failed:', err)}
108
- />
109
- );
110
- }
111
- ```
80
+ ## Usage
112
81
 
113
- ### Standard Inline Payment
82
+ ### Basic Inline Payment Form
114
83
 
115
- This is the default behavior. The component renders directly where it's placed in the DOM, which is suitable for embedding the payment form within a product or pricing page.
84
+ This is the most common use case for embedding a payment form directly within your application's UI. The component handles all the logic internally.
116
85
 
117
- ```tsx
118
- import { CheckoutForm } from '@blocklet/payment-react';
86
+ ```tsx MyStorePage.tsx icon=lucide:shopping-cart
87
+ import { PaymentProvider, CheckoutForm } from '@blocklet/payment-react';
88
+ import { useSessionContext } from './hooks/session-context'; // Your app's session hook
89
+
90
+ export default function MyStorePage() {
91
+ const { session, connectApi } = useSessionContext();
92
+
93
+ const handlePaymentSuccess = (result) => {
94
+ console.log('Payment successful!', result);
95
+ alert('Thank you for your purchase!');
96
+ };
97
+
98
+ const handlePaymentError = (error) => {
99
+ console.error('Payment failed:', error);
100
+ alert('Sorry, your payment could not be processed.');
101
+ };
119
102
 
120
- export default function InlinePayment() {
121
103
  return (
122
- <div>
123
- <h2>Complete Your Purchase</h2>
124
- <CheckoutForm
125
- id="plink_xxxxxxxxxxxx"
126
- mode="inline"
127
- onPaid={(data) => {
128
- console.log('Payment successful:', data.checkoutSession.id);
129
- }}
130
- onError={(err) => console.error('Payment failed:', err)}
131
- />
132
- </div>
104
+ <PaymentProvider session={session} connectApi={connectApi}>
105
+ <div style={{ maxWidth: '960px', margin: '0 auto' }}>
106
+ <h1>Checkout</h1>
107
+ <CheckoutForm
108
+ id="plink_xxxxxxxxxxxxxx" // Replace with your Payment Link ID
109
+ mode="inline"
110
+ onPaid={handlePaymentSuccess}
111
+ onError={handlePaymentError}
112
+ />
113
+ </div>
114
+ </PaymentProvider>
133
115
  );
134
116
  }
135
117
  ```
136
118
 
137
- ### Standalone Checkout Page
119
+ ### Standalone Full-Page Checkout
138
120
 
139
- By setting `mode='standalone'`, the component will render a full-page checkout experience, including a header and footer. This is ideal for redirecting users to a dedicated payment page.
121
+ Use `mode="standalone"` to render a dedicated, full-page checkout experience. This is ideal for scenarios where you redirect the user to a separate page to complete their payment.
140
122
 
141
- ```tsx
142
- import { CheckoutForm } from '@blocklet/payment-react';
123
+ ```tsx CheckoutPage.tsx icon=lucide:layout-template
124
+ import { PaymentProvider, CheckoutForm } from '@blocklet/payment-react';
125
+ import { useSessionContext } from './hooks/session-context';
126
+
127
+ export default function CheckoutPage() {
128
+ const { session, connectApi } = useSessionContext();
129
+ const paymentLinkId = 'plink_xxxxxxxxxxxxxx'; // Can be retrieved from URL params
143
130
 
144
- export default function StandaloneCheckoutPage() {
145
131
  return (
146
- <CheckoutForm
147
- id="cs_xxxxxxxxxxxx"
148
- mode="standalone"
149
- onPaid={(data) => {
150
- // Redirect to a success page
151
- window.location.href = `/payment-success?session_id=${data.checkoutSession.id}`;
152
- }}
153
- onError={(err) => console.error('Payment failed:', err)}
154
- />
132
+ <PaymentProvider session={session} connectApi={connectApi}>
133
+ <CheckoutForm id={paymentLinkId} mode="standalone" />
134
+ </PaymentProvider>
155
135
  );
156
136
  }
157
137
  ```
158
138
 
159
139
  ### Donation Form
160
140
 
161
- To render a UI optimized for donations, set `formType='donation'`. This changes the layout and terminology to better suit a donation flow.
141
+ By setting `formType="donation"`, the component renders a specialized UI tailored for donation campaigns, including features like amount presets and benefit displays.
162
142
 
163
- ```tsx
164
- import { CheckoutForm } from '@blocklet/payment-react';
143
+ ```tsx DonationPage.tsx icon=lucide:gift
144
+ import { PaymentProvider, CheckoutForm } from '@blocklet/payment-react';
145
+ import { useSessionContext } from './hooks/session-context';
165
146
 
166
147
  export default function DonationPage() {
148
+ const { session, connectApi } = useSessionContext();
149
+
167
150
  return (
168
- <div>
169
- <h2>Support Our Cause</h2>
170
- <CheckoutForm
171
- id="plink_donation_xxxxxxxx"
172
- formType="donation"
173
- onPaid={(data) => {
174
- console.log(`Thank you for your donation! Invoice ID: ${data.checkoutSession.invoice_id}`);
175
- }}
176
- onError={(err) => {
177
- console.error('Donation failed:', err);
178
- }}
179
- />
180
- </div>
151
+ <PaymentProvider session={session} connectApi={connectApi}>
152
+ <div style={{ padding: '2rem' }}>
153
+ <h2>Support Our Cause</h2>
154
+ <CheckoutForm
155
+ id="plink_yyyyyyyyyyyyyy" // Replace with your Donation Link ID
156
+ formType="donation"
157
+ onPaid={() => alert('Thank you for your generous donation!')}
158
+ />
159
+ </div>
160
+ </PaymentProvider>
181
161
  );
182
162
  }
183
163
  ```
184
164
 
185
- After integrating the checkout form, you may want to display a summary of the items being purchased. To learn more, see the documentation for the [PaymentSummary](./components-ui-payment-summary.md) component.
@@ -1,152 +1,93 @@
1
1
  # CheckoutTable
2
2
 
3
- The `CheckoutTable` component provides a complete, out-of-the-box solution for displaying a pricing table and handling the entire checkout flow. It fetches pricing table data by ID, allows users to select a plan, and then either redirects them to a checkout page or renders an inline payment form.
3
+ The `CheckoutTable` component provides a complete, out-of-the-box solution for subscription checkouts. It fetches and renders a pricing table, allows users to select a plan, and then seamlessly transitions them to a payment form to complete the purchase. This high-level component is the fastest way to integrate a plan-based payment flow into your application.
4
4
 
5
- This component is a high-level abstraction that internally uses [`PricingTable`](./components-ui-pricing-table.md) for display and [`CheckoutForm`](./components-checkout-checkout-form.md) for payment processing, simplifying the integration of subscription or one-time payment flows.
5
+ It internally uses the [`PricingTable`](./components-ui-pricing-table.md) component to display the plans and the [`CheckoutForm`](./components-checkout-checkout-form.md) component to process the payment.
6
6
 
7
- ## How It Works
7
+ ## How it Works
8
8
 
9
- The component follows a clear, logical flow to guide the user from plan selection to payment completion.
9
+ The checkout flow managed by `CheckoutTable` is straightforward:
10
10
 
11
- ```d2
12
- direction: down
11
+ 1. **Fetch Data**: The component fetches the pricing table configuration from the server using the provided `id`.
12
+ 2. **Display Plans**: It renders the subscription plans in a responsive table, allowing users to switch between billing intervals (e.g., monthly/yearly) and currencies.
13
+ 3. **Create Session**: When a user selects a plan, the component communicates with the backend to create a secure checkout session.
14
+ 4. **Process Payment**: It then displays the `CheckoutForm`, pre-filled with the session details, allowing the user to enter their payment information and finalize the transaction.
13
15
 
14
- CheckoutTable: {
15
- label: "CheckoutTable Mounted with 'id'"
16
- shape: step
17
- }
16
+ ```d2 CheckoutTable Flow Diagram
17
+ direction: down
18
18
 
19
- FetchData: {
20
- label: "Fetch Pricing Table Data"
21
- shape: step
19
+ User: {
20
+ shape: c4-person
22
21
  }
23
22
 
24
- DisplayPricing: {
25
- label: "Display PricingTable"
26
- shape: step
27
- }
23
+ CheckoutTable-Component: {
24
+ label: "CheckoutTable Component"
25
+ shape: rectangle
28
26
 
29
- DisplayError: {
30
- label: "Display Error Message"
31
- shape: step
32
- style: {
33
- stroke: "#d32f2f"
27
+ Pricing-Table-View: {
28
+ label: "Pricing Table View"
34
29
  }
35
- }
36
-
37
- HandleSelect: {
38
- label: "User selects a plan (handleSelect)"
39
- shape: step
40
- }
41
30
 
42
- CreateSession: {
43
- label: "Create Checkout Session via API"
44
- shape: step
45
- }
46
-
47
- ModeCheck: {
48
- label: "mode === 'standalone'?"
49
- shape: diamond
50
- }
51
-
52
- Redirect: {
53
- label: "Redirect to Checkout URL"
54
- shape: step
55
- style: {
56
- fill: "#e8f5e9"
31
+ Checkout-Form-View: {
32
+ label: "Checkout Form View"
57
33
  }
58
34
  }
59
35
 
60
- UpdateHash: {
61
- label: "Update URL hash with Session ID"
62
- shape: step
36
+ Payment-API: {
37
+ label: "Payment API"
38
+ shape: cylinder
63
39
  }
64
40
 
65
- RenderForm: {
66
- label: "Render CheckoutForm"
67
- shape: step
68
- }
41
+ User -> CheckoutTable-Component.Pricing-Table-View: "1. Views plans"
42
+ CheckoutTable-Component.Pricing-Table-View -> User: " "
43
+ User -> CheckoutTable-Component.Pricing-Table-View: "2. Selects a plan"
44
+ CheckoutTable-Component.Pricing-Table-View -> Payment-API: "3. Create checkout session"
45
+ Payment-API -> CheckoutTable-Component.Pricing-Table-View: "4. Return session ID"
46
+ CheckoutTable-Component.Pricing-Table-View -> CheckoutTable-Component.Checkout-Form-View: "5. Transition with session ID"
47
+ User -> CheckoutTable-Component.Checkout-Form-View: "6. Fills payment details & pays"
48
+ CheckoutTable-Component.Checkout-Form-View -> User: ""
69
49
 
70
- OnPaid: {
71
- label: "onPaid() callback"
72
- shape: step
73
- style: {
74
- fill: "#e8f5e9"
75
- }
76
- }
77
-
78
- OnError: {
79
- label: "onError() callback"
80
- shape: step
81
- style: {
82
- stroke: "#d32f2f"
83
- }
84
- }
85
-
86
- ToastError: {
87
- label: "Show Toast Error"
88
- shape: step
89
- style: {
90
- stroke: "#d32f2f"
91
- }
92
- }
93
-
94
- CheckoutTable -> FetchData
95
- FetchData -> DisplayPricing: "Success"
96
- FetchData -> DisplayError: "Error"
97
- DisplayPricing -> HandleSelect
98
- HandleSelect -> CreateSession
99
- CreateSession -> ModeCheck: "Success"
100
- CreateSession -> ToastError: "Error"
101
- ModeCheck -> Redirect: "Yes"
102
- ModeCheck -> UpdateHash: "No (Embedded)"
103
- UpdateHash -> RenderForm
104
- RenderForm -> OnPaid: "Payment Success"
105
- RenderForm -> OnError: "Payment Error"
106
50
  ```
107
51
 
108
52
  ## Props
109
53
 
110
- The `CheckoutTable` component requires a `PaymentProvider` to be present in the component tree. For more details, refer to the [`PaymentProvider`](./providers-payment-provider.md) documentation.
111
-
112
- | Prop | Type | Required | Description |
113
- |---|---|---|---|
114
- | `id` | `string` | Yes | The ID of the pricing table to display. This must start with `prctbl_`. |
115
- | `mode` | `'standalone' \| 'embedded'` | No | Determines the checkout behavior. In `'standalone'` mode, the user is redirected to a separate checkout page. In `'embedded'` mode (default), the `CheckoutForm` is rendered inline, replacing the pricing table. |
116
- | `onPaid` | `(data: TCheckout) => void` | No | Callback function executed when the payment is successfully completed. It receives the final checkout session data. |
117
- | `onError` | `(err: Error) => void` | No | Callback function executed if an error occurs during the payment process. |
118
- | `onChange` | `(session: TCheckout) => void` | No | Callback function executed whenever the checkout session state changes (e.g., from `pending` to `processing`). |
119
- | `extraParams` | `object` | No | An object containing extra query parameters to be sent when creating the checkout session. |
120
- | `goBack` | `() => void` | No | A function that is called when the user clicks the "back" button on the `CheckoutForm`. This enables navigation back to the pricing table view in embedded mode. |
121
- | `theme` | `object \| 'inherit'` | No | A custom Material-UI theme object to style the component. Set to `'inherit'` to use the ambient theme without an additional `ThemeProvider`. |
122
-
123
- ## Usage Scenarios
124
-
125
- `CheckoutTable` can be configured for two primary user experiences: a full-page redirect or a seamless embedded flow.
126
-
127
- > **Note on Context:** The following examples assume you have set up a session context in your application. The `PaymentProvider` relies on this context to get authentication and API details. For a complete guide on this setup, please see the [`PaymentProvider`](./providers-payment-provider.md) documentation.
54
+ | Prop | Type | Required | Default | Description |
55
+ |---------------|-----------------------------------|----------|--------------|------------------------------------------------------------------------------------------------------------------------------------------|
56
+ | `id` | `string` | Yes | - | The ID of the pricing table to display (must start with `prctbl_`). |
57
+ | `mode` | `'standalone'` or `'embedded'` | No | `'embedded'` | The display mode. `'standalone'` redirects to a separate page, while `'embedded'` handles the flow inline within the component. |
58
+ | `onPaid` | `(sessionId: string) => void` | No | - | Callback function triggered when the payment is successfully completed. |
59
+ | `onError` | `(error: Error) => void` | No | - | Callback function for handling errors during the checkout process. |
60
+ | `onChange` | `(data: any) => void` | No | - | Callback for any state change within the checkout form. |
61
+ | `extraParams` | `Record<string, any>` | No | `{}` | An object of extra parameters to be sent when creating the checkout session, useful for passing custom data like a user ID. |
62
+ | `goBack` | `() => void` | No | - | A function to handle the back action from the payment form, returning the user to the pricing table view. |
63
+ | `theme` | `object` or `'inherit'` | No | - | Custom Material-UI theme object to style the component. For more details, see the [Theming guide](./guides-theming.md). |
128
64
 
129
- ### Standalone Mode
65
+ ## Usage
130
66
 
131
- In `standalone` mode, the component handles plan selection and then redirects the user to a dedicated, hosted checkout page. This is the simplest way to integrate.
67
+ To use the `CheckoutTable` component, you must wrap it in a `PaymentProvider`. Pass the pricing table `id` and an `onPaid` callback to handle successful transactions.
132
68
 
133
- ```jsx
134
- import { CheckoutTable, PaymentProvider } from '@blocklet/payment-react';
135
- import { useSessionContext } from '../contexts/session'; // Path to your custom session context hook
69
+ ```javascript Basic CheckoutTable Example icon=logos:react
70
+ import { PaymentProvider, CheckoutTable } from '@blocklet/payment-react';
71
+ import { useSessionContext } from './path-to-your-session-context'; // Centralize this context as per guidelines
136
72
 
137
- function MyPaymentPage() {
73
+ export default function MyCheckoutPage() {
138
74
  const { session, connectApi } = useSessionContext();
139
75
 
140
- if (!session.user) {
141
- return <p>Please log in to see pricing.</p>;
76
+ const handlePaymentSuccess = (sessionId) => {
77
+ console.log(`Payment successful for session: ${sessionId}`);
78
+ alert('Thank you for your subscription!');
79
+ };
80
+
81
+ if (!session) {
82
+ return <div>Loading session...</div>;
142
83
  }
143
84
 
144
85
  return (
145
- <PaymentProvider session={session} connect={connectApi}>
146
- <div style={{ height: '600px', width: '100%' }}>
86
+ <PaymentProvider session={session} connectApi={connectApi}>
87
+ <div style={{ height: '700px', width: '100%' }}>
147
88
  <CheckoutTable
148
- id="prctbl_xxxxxxxxxxxx" // Replace with your actual pricing table ID
149
- mode="standalone"
89
+ id="prctbl_xxxxxxxxxxxxxxxx" // Replace with your actual pricing table ID
90
+ onPaid={handlePaymentSuccess}
150
91
  />
151
92
  </div>
152
93
  </PaymentProvider>
@@ -154,75 +95,54 @@ function MyPaymentPage() {
154
95
  }
155
96
  ```
156
97
 
157
- When a user clicks a subscription button in the pricing table, `window.location.replace` is called to navigate them to the checkout URL provided by the payment service API.
98
+ ## Modes of Operation
158
99
 
159
- ### Embedded Mode
100
+ ### Embedded Mode (Default)
160
101
 
161
- In `embedded` mode (the default behavior), the entire checkout process happens within your application. After a plan is selected, the pricing table is replaced by the `CheckoutForm` component.
102
+ By default, `CheckoutTable` operates in `'embedded'` mode. The component manages the entire flow inline, transitioning from the pricing table view to the payment form within its own container. This is ideal for single-page applications where you want the checkout to feel like an integrated part of the page.
162
103
 
163
- This mode relies on the URL hash (`#`) to store and retrieve the checkout session ID, enabling a smooth, single-page application experience.
164
-
165
- ```jsx
166
- import { CheckoutTable, PaymentProvider } from '@blocklet/payment-react';
167
- import { useSessionContext } from '../contexts/session'; // Path to your custom session context hook
168
- import type { TCheckout } from '@blocklet/payment-types';
169
-
170
- function MyEmbeddedCheckout() {
171
- const { session, connectApi } = useSessionContext();
172
-
173
- const handlePaymentSuccess = (checkoutData: TCheckout) => {
174
- console.log('Payment successful!', checkoutData);
175
- alert('Thank you for your payment!');
176
- };
177
-
178
- const handlePaymentError = (error: Error) => {
179
- console.error('Payment failed:', error);
180
- alert('There was an issue with your payment.');
181
- };
182
-
183
- const handleGoBack = () => {
184
- // The component handles clearing the URL hash internally.
185
- // You can add custom logic here if needed.
186
- console.log('User returned to the pricing table view.');
187
- };
104
+ ### Standalone Mode
188
105
 
189
- if (!session.user) {
190
- return <p>Please log in to see pricing.</p>;
191
- }
106
+ By setting `mode='standalone'`, the component's behavior changes. When a user selects a plan, they are redirected to a dedicated, hosted checkout page. This mode is useful for simpler integrations that don't require an embedded payment form.
192
107
 
193
- return (
194
- <PaymentProvider session={session} connect={connectApi}>
195
- <div style={{ height: '800px', width: '100%' }}>
196
- <CheckoutTable
197
- id="prctbl_xxxxxxxxxxxx" // Replace with your actual pricing table ID
198
- onPaid={handlePaymentSuccess}
199
- onError={handlePaymentError}
200
- goBack={handleGoBack}
201
- />
202
- </div>
203
- </PaymentProvider>
204
- );
205
- }
108
+ ```javascript Standalone Mode icon=logos:react
109
+ <CheckoutTable
110
+ id="prctbl_xxxxxxxxxxxxxxxx"
111
+ mode="standalone"
112
+ />
206
113
  ```
207
114
 
208
- In this example:
209
- - The `onPaid` and `onError` callbacks are used to handle the final state of the transaction.
210
- - The `goBack` prop enables a back button on the payment form, allowing users to return to the plan selection screen without losing context.
115
+ ## Advanced Usage
211
116
 
212
- ## `CheckoutTable` vs. `PricingTable`
117
+ ### Handling Back Navigation
213
118
 
214
- It's important to understand the difference between `CheckoutTable` and the lower-level `PricingTable` component to choose the right one for your needs.
119
+ The `goBack` prop allows you to define custom logic when the user navigates back from the payment form to the pricing table. The component handles the view transition automatically, but this callback is useful for synchronizing your application's state.
215
120
 
216
- #### CheckoutTable
121
+ ```javascript goBack Prop Example icon=logos:react
122
+ const handleGoBack = () => {
123
+ console.log('User returned to the pricing table.');
124
+ // You can add custom logic here, like updating your app's state.
125
+ };
217
126
 
218
- A high-level, all-in-one component that manages the entire checkout flow. It handles data fetching, plan selection, and payment form rendering.
127
+ <CheckoutTable
128
+ id="prctbl_xxxxxxxxxxxxxxxx"
129
+ onPaid={handlePaymentSuccess}
130
+ goBack={handleGoBack}
131
+ />
132
+ ```
219
133
 
220
- **Use when:** You need a complete, ready-to-use pricing and checkout solution with minimal configuration.
134
+ ### Passing Extra Parameters
221
135
 
222
- #### PricingTable
136
+ Use the `extraParams` prop to send additional data when creating the checkout session. This data can be associated with the resulting payment and is useful for tracking and reconciliation on your backend.
223
137
 
224
- A low-level, presentational component that only displays pricing data passed to it. It does not manage state or handle the checkout process.
138
+ ```javascript extraParams Example icon=logos:react
139
+ <CheckoutTable
140
+ id="prctbl_xxxxxxxxxxxxxxxx"
141
+ onPaid={handlePaymentSuccess}
142
+ extraParams={{ userId: 'user_123', source: 'marketing_campaign' }}
143
+ />
144
+ ```
225
145
 
226
- **Use when:** You need to build a custom checkout flow or display pricing plans in a non-checkout context.
146
+ ## Customization
227
147
 
228
- For most use cases, **`CheckoutTable` is the recommended starting point** as it provides a complete and integrated experience. If you find you need more granular control over the user flow, you can then use `PricingTable` and `CheckoutForm` as separate building blocks.
148
+ While `CheckoutTable` is a high-level component designed for ease of use, you can achieve more granular control by using its constituent parts. For a fully custom UI, you can use the [`PricingTable`](./components-ui-pricing-table.md) component to display plans and then manually trigger a checkout session that renders in a [`CheckoutForm`](./components-checkout-checkout-form.md).