@chainpay/component
Version:
A React-based payment component library that supports multiple payment channels and internationalization.
232 lines (177 loc) ⢠5.87 kB
Markdown
A React-based payment component library that supports multiple payment channels and internationalization.
- š Support for React 18 and React 19
- š Multi-language support (Chinese, English, Japanese, Korean, and 8 languages in total)
- šØ Support for dark/light themes
- š± Responsive design
- ā” TypeScript support
- š Automatic polling of payment status
- š« Elegant loading and error handling
## Installation
```bash
npm install @chainpay/component
# or
yarn add @chainpay/component
# or
pnpm add @chainpay/component
```
## Dependencies
Ensure your project has the following dependencies installed:
```bash
npm install react react-dom antd i18next react-i18next tailwindcss
```
## Quick Start
### 1. Import Component and Styles
```tsx
import { useChainPay, ChainPayLng } from "@chainpay/component";
import "@chainpay/component/dist/style.css";
```
```tsx
import React from 'react';
import { useChainPay, ChainPayLng } from "@chainpay/component";
import "@chainpay/component/dist/style.css";
function PaymentExample() {
const { showPayModal } = useChainPay({
// dark: true,
language: "en",
currency: 'Ā„',
appId: "app-77cccd53-b091-4dee-98f9-a44d641ac128",
getSignInfoFn: async (params) => {
return await getSign(params) // Return signature information
},
onError(error) {
console.log("error", error);
},
onSuccess() {
console.log("success");
},
onClose() {
console.log("close");
},
onCountDownTimeout() {
console.log("countDownTimeout");
}
});
return (
<div>
<button onClick={() => {
showPayModal({
amount: 100,
})
}}>
Initiate Payment
</button>
</div>
);
}
export default PaymentExample;
```
Returns an object containing the `showPayModal` and `closePayModal` methods.
| Parameter | Type | Default | Required | Description |
|------|------|--------|------|------|
| `appId` | `string` | - | ā
| Application ID |
| `amount` | `number` | - | ā | Payment amount |
| `currency` | `string` | - | ā | Currency symbol |
| `language` | `LngType` | `'zh-CN'` | ā | Interface language |
| `dark` | `boolean` | `false` | ā | Enable dark theme |
| `getSignInfoFn` | `function` | - | ā | Function to get signature information |
| `onSuccess` | `function` | - | ā | Payment success callback |
| `onError` | `function` | - | ā | Payment error callback |
| `onClose` | `function` | - | ā | Modal close callback |
| `onBack` | `function` | - | ā | Back button callback |
| `onCountDownTimeout` | `function` | - | ā | Countdown timeout callback |
```typescript
enum ChainPayLng {
"en" = "en", // English
"zh-CN" = "zh-CN", // Simplified Chinese
"zh-TW" = "zh-TW", // Traditional Chinese
"ko" = "ko", // Korean
"ja" = "ja", // Japanese
"ms" = "ms", // Malay
"vi" = "vi", // Vietnamese
"th" = "th" // Thai
}
```
```typescript
// Function to get order information
getSignInfoFn?: (data: object) => Promise<any>;
```
Displays the payment modal. Parameters are the same as `useChainPay`, but parameters in `showPayModal` have higher priority.
Manually closes the payment modal.
```tsx
const { showPayModal } = useChainPay({
dark: true, // Enable dark theme
// Other configurations...
});
```
```tsx
const { showPayModal } = useChainPay({
language: ChainPayLng['zh-CN'], // Use Chinese
// Other configurations...
});
// Switch language when displaying the modal
showPayModal({
language: ChainPayLng.en, // Switch to English
amount: 100
});
```
```tsx
const { showPayModal } = useChainPay({
onError: (error) => {
// Handle payment errors
console.error('Payment failed:', error);
// Can display error message to the user
}
});
```
ā ļø **React 19 Compatibility**: If you encounter `ReactCurrentDispatcher` errors in React 19 projects, please refer to the [React 19 Compatibility Guide](./REACT19_COMPATIBILITY.md).
If you encounter the error `TypeError: Cannot read properties of undefined (reading 'ReactCurrentDispatcher')`:
1. **Temporary Solution**: Install a compatible version in your project
```bash
npm install @chainpay/component@latest
# If problems persist, clear cache
rm -rf node_modules package-lock.json
npm install
```
2. **Ensure JSX Transform is Configured Correctly** (Vite projects):
```javascript
// vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()]
})
```
3. **Additional Configuration for Next.js Projects**:
```javascript
// next.config.js
module.exports = {
transpilePackages: ['@chainpay/component']
}
```
This component library fully supports React 19. If you encounter `ReactCurrentDispatcher` related errors during use, please refer to the [React 19 Compatibility Guide](./REACT19_COMPATIBILITY.md).
1. **Must Import Style File**: Ensure you import the CSS file before using the component
2. **peerDependencies**: Make sure all required dependencies are installed
3. **API Functions**: `getSignInfoFn` should return a Promise
4. **Error Handling**: It is recommended to implement complete error handling logic in production environments
MIT License
If you encounter any issues or have feature suggestions, please submit an issue on GitHub.