UNPKG

@chainpay/component

Version:

A React-based payment component library that supports multiple payment channels and internationalization.

232 lines (177 loc) • 5.87 kB
# ChainPay Component A React-based payment component library that supports multiple payment channels and internationalization. ## Features - šŸš€ 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"; ``` ### 2. Basic Usage ```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; ``` ## API Documentation ### useChainPay(options) Returns an object containing the `showPayModal` and `closePayModal` methods. #### Parameters (ChainPayProps) | 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 | #### Supported Languages ```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 } ``` #### Function Signatures ```typescript // Function to get order information getSignInfoFn?: (data: object) => Promise<any>; ``` ### showPayModal(props) Displays the payment modal. Parameters are the same as `useChainPay`, but parameters in `showPayModal` have higher priority. ### closePayModal() Manually closes the payment modal. ## Advanced Usage ### Custom Theme ```tsx const { showPayModal } = useChainPay({ dark: true, // Enable dark theme // Other configurations... }); ``` ### Dynamic Language Switching ```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 }); ``` ### Error Handling ```tsx const { showPayModal } = useChainPay({ onError: (error) => { // Handle payment errors console.error('Payment failed:', error); // Can display error message to the user } }); ``` ## Important Notes āš ļø **React 19 Compatibility**: If you encounter `ReactCurrentDispatcher` errors in React 19 projects, please refer to the [React 19 Compatibility Guide](./REACT19_COMPATIBILITY.md). ### Quick Solutions for React 19 Errors 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'] } ``` ## React 19 Compatibility 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). ## Notes 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 ## License MIT License ## Support If you encounter any issues or have feature suggestions, please submit an issue on GitHub.