CheckoutDonate
The CheckoutDonate component provides a flexible and easy-to-integrate solution for adding donation functionality to your application. It supports various display modes, from a simple button that opens a checkout dialog to a fully custom UI that you control.
This component must be wrapped within both a PaymentProvider and a DonateProvider to function correctly. The DonateProvider manages the settings and state for donation instances within a specific scope of your application.
How It Works#
The donation flow is orchestrated by a combination of DonateProvider and CheckoutDonate. Here's a high-level overview:
- Initialization:
DonateProviderfetches and caches donation settings (like preset amounts, button text) from the backend, identified by a uniquemountLocation. - Rendering:
CheckoutDonaterenders a button or custom UI based on the retrieved settings and its props. - Interaction: When a user initiates a donation,
CheckoutDonateopens a dialog containing aCheckoutFormpre-configured for the donation. - Payment: The user completes the payment through the
CheckoutForm. - Confirmation: After a successful payment, the
onPaidcallback is triggered, and the component automatically refreshes the list of supporters.
Provider Setup#
Before using CheckoutDonate, you must wrap your component tree with PaymentProvider and DonateProvider.
Provider Setup Example
import {
PaymentProvider,
DonateProvider,
CheckoutDonate,
} from '@blocklet/payment-react';
import { useSessionContext } from '../hooks/session-context'; // Your session context hook
function DonationPage() {
const { session, connect } = useSessionContext();
// Ensure session is loaded before rendering providers
if (!session) {
return <div>Loading...</div>;
}
return (
<PaymentProvider session={session} connect={connect}>
<DonateProvider
mountLocation="unique-page-identifier" // A unique key for this donation context
description="Donation for my awesome blog post"
defaultSettings={{
btnText: 'Support Me',
}}>
{/* Your CheckoutDonate component goes here */}
<CheckoutDonate See all 17 lines
For more details, see the DonateProvider documentation.
Component Props#
DonateProps#
Prop | Type | Description |
|---|---|---|
|
| Required. Configuration for this specific donation instance. |
|
| Optional. Callback function executed after a successful payment. |
|
| Optional. Callback function executed if an error occurs. |
|
| Optional. The rendering mode. Defaults to |
|
| Optional. Overrides the |
|
| Optional. Milliseconds to wait before closing the dialog after payment. Defaults to |
|
| Optional. Theme customization options. See the Theming guide. |
|
| Optional. A render prop function used only when |
CheckoutDonateSettings#
This object is passed to the settings prop and defines the core details of the donation.
Property | Type | Description |
|---|---|---|
|
| Required. A unique identifier for the donation target (e.g., a post ID, a project name). |
|
| Required. The title displayed at the top of the donation dialog. |
|
| Required. A short description displayed in the donation dialog. |
|
| Required. A URL related to the donation, used for reference. |
|
| Required. An array of objects defining who receives the funds. Each object needs an |
|
| Optional. Configures donation amounts, including |
|
| Optional. Customizes the look and feel, including |
Usage Examples#
Default Mode#
This is the simplest way to use CheckoutDonate. It renders a button that opens a donation dialog, along with a summary of recent supporters.
Default Donation Button
import {
PaymentProvider,
DonateProvider,
CheckoutDonate,
} from '@blocklet/payment-react';
import { useSessionContext } from '../hooks/session-context';
function App() {
const { session, connect } = useSessionContext();
if (!session) {
return <div>Loading session...</div>;
}
return (
<PaymentProvider session={session} connect={connect}>
<DonateProvider
mountLocation="blog-post-123"
description="Donations for Blog Post 123"
defaultSettings={{
btnText: 'Buy me a coffee',
historyType: 'avatar',
}}>
<CheckoutDonate
settings={{See all 19 lines
Custom UI Mode#
For full control over the user interface, use mode="custom" and provide a render prop as the children. This function gives you access to the donation state, including the total amount raised and a list of supporters, allowing you to build a completely custom display.
Custom Donation UI
import {
PaymentProvider,
DonateProvider,
CheckoutDonate,
} from '@blocklet/payment-react';
import { useSessionContext } from '../hooks/session-context';
import { CircularProgress, Button } from '@mui/material';
function CustomDonationDisplay() {
const { session, connect } = useSessionContext();
if (!session) {
return <div>Loading session...</div>;
}
const donateSettings = {
target: 'post-123',
title: 'Support the Author',
description: 'If you found this article helpful, consider a small donation.',
reference: 'https://example.com/posts/123',
beneficiaries: [
{
address: 'z2qa...gCLd', // Author's DID address
share: '100',
},See all 34 lines
The children function receives the following arguments:
openDonate: A function to manually trigger the donation dialog.totalAmount: A formatted string of the total amount donated (e.g.,"125.00 T").supporters: ADonateHistoryobject containing thesupportersarray and currency info.loading: A boolean indicating if the supporter data is being fetched.settings: The resolved donation settings, merged fromDonateProviderand props.