@blocklet/payment-react 1.20.5 → 1.20.7
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.
- package/.aigne/doc-smith/config.yaml +2 -2
- package/.aigne/doc-smith/upload-cache.yaml +381 -0
- package/docs/components-business-auto-topup.md +128 -179
- package/docs/components-business-overdue-invoice-payment.md +108 -143
- package/docs/components-business-resume-subscription.md +117 -104
- package/docs/components-business.md +12 -36
- package/docs/components-checkout-checkout-donate.md +209 -149
- package/docs/components-checkout-checkout-form.md +115 -136
- package/docs/components-checkout-checkout-table.md +92 -172
- package/docs/components-checkout.md +43 -109
- package/docs/components-history-credit-grants-list.md +45 -70
- package/docs/components-history-credit-transactions-list.md +57 -67
- package/docs/components-history-invoice-list.md +58 -52
- package/docs/components-history-payment-list.md +19 -40
- package/docs/components-history.md +42 -67
- package/docs/components-ui-form-elements-address-form.md +37 -65
- package/docs/components-ui-form-elements-country-select.md +80 -59
- package/docs/components-ui-form-elements-currency-selector.md +57 -73
- package/docs/components-ui-form-elements-phone-input.md +90 -112
- package/docs/components-ui-form-elements.md +46 -80
- package/docs/components-ui-payment-summary.md +71 -119
- package/docs/components-ui-pricing-table.md +117 -204
- package/docs/components-ui.md +59 -32
- package/docs/components.md +89 -62
- package/docs/getting-started.md +36 -63
- package/docs/guides-theming.md +122 -84
- package/docs/guides-utilities.md +107 -145
- package/docs/guides.md +7 -84
- package/docs/hooks-use-mobile.md +50 -36
- package/docs/hooks-use-subscription.md +72 -89
- package/docs/hooks.md +12 -82
- package/docs/overview.md +45 -52
- package/docs/providers-donate-provider.md +73 -95
- package/docs/providers-payment-provider.md +115 -169
- package/docs/providers.md +27 -86
- package/es/locales/en.js +7 -0
- package/es/locales/zh.js +8 -1
- package/es/payment/index.js +3 -0
- package/es/payment/progress-item.d.ts +12 -0
- package/es/payment/progress-item.js +78 -0
- package/es/payment/success.d.ts +4 -1
- package/es/payment/success.js +55 -3
- package/lib/locales/en.js +7 -0
- package/lib/locales/zh.js +8 -1
- package/lib/payment/index.js +3 -0
- package/lib/payment/progress-item.d.ts +12 -0
- package/lib/payment/progress-item.js +107 -0
- package/lib/payment/success.d.ts +4 -1
- package/lib/payment/success.js +59 -3
- package/package.json +3 -3
- package/src/locales/en.tsx +7 -0
- package/src/locales/zh.tsx +8 -1
- package/src/payment/index.tsx +6 -0
- package/src/payment/progress-item.tsx +107 -0
- 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
|
|
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
|
-
|
|
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
|
|
9
|
+
The component orchestrates the entire checkout process:
|
|
10
10
|
|
|
11
|
-
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
25
|
+
Application: {
|
|
26
|
+
label: "Your React Application"
|
|
43
27
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
32
|
+
Payment-Component: {
|
|
33
|
+
label: "Payment Component"
|
|
34
|
+
}
|
|
61
35
|
|
|
62
|
-
|
|
36
|
+
Donation-Component: {
|
|
37
|
+
label: "DonationForm Component"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
63
40
|
|
|
64
|
-
|
|
41
|
+
Payment-API: {
|
|
42
|
+
label: "Payment Backend API"
|
|
43
|
+
shape: cylinder
|
|
44
|
+
}
|
|
65
45
|
|
|
66
|
-
|
|
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
|
-
|
|
50
|
+
alt "if formType is 'payment'" {
|
|
51
|
+
Application.CheckoutForm-Component -> Application.Payment-Component: "4. Renders Payment UI"
|
|
52
|
+
}
|
|
69
53
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
78
|
-
|
|
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
|
-
|
|
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
|
-
|
|
94
|
-
function MyCheckoutPage() {
|
|
95
|
-
const { session } = useSessionContext();
|
|
96
|
-
const paymentLinkId = 'plink_xxxxxxxxxxxx';
|
|
63
|
+
## Props
|
|
97
64
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
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
|
-
###
|
|
82
|
+
### Basic Inline Payment Form
|
|
114
83
|
|
|
115
|
-
This is the
|
|
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
|
-
<
|
|
123
|
-
<
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
</
|
|
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
|
|
119
|
+
### Standalone Full-Page Checkout
|
|
138
120
|
|
|
139
|
-
|
|
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
|
-
<
|
|
147
|
-
id="
|
|
148
|
-
|
|
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
|
-
|
|
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
|
-
<
|
|
169
|
-
<
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
7
|
+
## How it Works
|
|
8
8
|
|
|
9
|
-
The
|
|
9
|
+
The checkout flow managed by `CheckoutTable` is straightforward:
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
16
|
-
shape: step
|
|
17
|
-
}
|
|
16
|
+
```d2 CheckoutTable Flow Diagram
|
|
17
|
+
direction: down
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
shape: step
|
|
19
|
+
User: {
|
|
20
|
+
shape: c4-person
|
|
22
21
|
}
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
label: "
|
|
26
|
-
shape:
|
|
27
|
-
}
|
|
23
|
+
CheckoutTable-Component: {
|
|
24
|
+
label: "CheckoutTable Component"
|
|
25
|
+
shape: rectangle
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
43
|
-
|
|
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
|
-
|
|
61
|
-
label: "
|
|
62
|
-
shape:
|
|
36
|
+
Payment-API: {
|
|
37
|
+
label: "Payment API"
|
|
38
|
+
shape: cylinder
|
|
63
39
|
}
|
|
64
40
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
111
|
-
|
|
112
|
-
|
|
|
113
|
-
|
|
114
|
-
| `
|
|
115
|
-
| `
|
|
116
|
-
| `
|
|
117
|
-
| `
|
|
118
|
-
| `
|
|
119
|
-
| `
|
|
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
|
-
|
|
65
|
+
## Usage
|
|
130
66
|
|
|
131
|
-
|
|
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
|
-
```
|
|
134
|
-
import {
|
|
135
|
-
import { useSessionContext } from '
|
|
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
|
|
73
|
+
export default function MyCheckoutPage() {
|
|
138
74
|
const { session, connectApi } = useSessionContext();
|
|
139
75
|
|
|
140
|
-
|
|
141
|
-
|
|
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}
|
|
146
|
-
<div style={{ height: '
|
|
86
|
+
<PaymentProvider session={session} connectApi={connectApi}>
|
|
87
|
+
<div style={{ height: '700px', width: '100%' }}>
|
|
147
88
|
<CheckoutTable
|
|
148
|
-
id="
|
|
149
|
-
|
|
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
|
-
|
|
98
|
+
## Modes of Operation
|
|
158
99
|
|
|
159
|
-
### Embedded Mode
|
|
100
|
+
### Embedded Mode (Default)
|
|
160
101
|
|
|
161
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
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
|
-
|
|
117
|
+
### Handling Back Navigation
|
|
213
118
|
|
|
214
|
-
|
|
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
|
-
|
|
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
|
-
|
|
127
|
+
<CheckoutTable
|
|
128
|
+
id="prctbl_xxxxxxxxxxxxxxxx"
|
|
129
|
+
onPaid={handlePaymentSuccess}
|
|
130
|
+
goBack={handleGoBack}
|
|
131
|
+
/>
|
|
132
|
+
```
|
|
219
133
|
|
|
220
|
-
|
|
134
|
+
### Passing Extra Parameters
|
|
221
135
|
|
|
222
|
-
|
|
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
|
-
|
|
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
|
-
|
|
146
|
+
## Customization
|
|
227
147
|
|
|
228
|
-
|
|
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).
|