This project demonstrates how to integrate the X-Pay cryptocurrency payment gateway SDK into a React application.
- Initialize the X-Pay SDK with your API credentials
- Create cryptocurrency collection orders
- Create cryptocurrency payout orders
- Display payment QR codes for customers
- Check payment status
- Get supported cryptocurrencies and chains
- Verify webhook signatures
- Node.js 14.x or higher
- npm or yarn
- Clone this repository
- Install dependencies:
npm install- Install the official X-Pay SDK:
npm install x-pay-sdk-officialTo use this example with your X-Pay account, you'll need to:
-
Sign up for an X-Pay account at x-pay.fun
-
Obtain your API key and secret from the dashboard
-
Configure your credentials by creating a
.envfile from the provided.env.example:# Copy the example file cp .env.example .env # Edit the .env file with your actual API credentials # REACT_APP_XPAY_API_KEY=your-actual-api-key # REACT_APP_XPAY_API_SECRET=your-actual-api-secret
IMPORTANT: Never commit your
.envfile to version control!
Start the development server:
npm startThen open your browser to http://localhost:3001
src/views/Home.js- Landing page with SDK informationsrc/views/PaymentDemo.js- Main demo page with SDK integration examplessrc/components/PaymentQRCode.js- Component for displaying payment QR codessrc/components/WebhookHandler.js- Component for webhook verification demosrc/services/XPayService.js- Service for interacting with the X-Pay API
// src/services/XPayService.js
import { XPaySDK } from 'x-pay-sdk-official';
class XPayService {
constructor(config) {
this.sdk = new XPaySDK({
apiKey: config.apiKey,
apiSecret: config.apiSecret,
baseUrl: config.baseUrl || 'https://api.x-pay.fun'
});
}
// API methods
async createCollection(params) {
return this.sdk.createCollection(params);
}
async createPayout(params) {
return this.sdk.createPayout(params);
}
async getOrderStatus(orderId) {
return this.sdk.getOrderStatus(orderId);
}
// More methods...
}import XPayService from '../services/XPayService';
// Initialize the service
const xpay = new XPayService({
apiKey: process.env.REACT_APP_XPAY_API_KEY,
apiSecret: process.env.REACT_APP_XPAY_API_SECRET,
baseUrl: process.env.REACT_APP_XPAY_BASE_URL || 'https://api.x-pay.fun'
});const response = await xpay.createCollection({
amount: 100,
symbol: 'USDT',
chain: 'TRON',
uid: 'user123',
orderId: 'order-123456'
});
// The response contains the payment address and other details
console.log(response.data.address);const status = await xpay.getOrderStatus('order-123456');
console.log(status);// In your webhook handler
const isValid = xpay.verifyWebhook(
JSON.stringify(webhookBody),
signature,
timestamp
);
if (isValid) {
// Process the webhook
const event = xpay.parseWebhook(JSON.stringify(webhookBody));
console.log(event.notifyType, event.data);
}This example uses environment variables to securely store API credentials. All configuration parameters are stored in a .env file that should never be committed to version control.
-
Copy the example environment file to create your own:
For Windows:
copy .env.example .envFor Linux/macOS:
cp .env.example .env
-
Edit the
.envfile with your actual X-Pay API credentials:REACT_APP_XPAY_API_KEY=your-actual-api-key REACT_APP_XPAY_API_SECRET=your-actual-api-secret REACT_APP_XPAY_BASE_URL=https://api.x-pay.fun -
Make sure
.envis in your.gitignorefile to prevent accidentally committing your credentials.
- NEVER commit your
.envfile to version control - NEVER store API keys or secrets in public files
- IMPORTANT: For production applications, you should store your API secret on the backend server and implement a server-side API to handle the communication with the X-Pay API. The frontend should only communicate with your backend API, not directly with the X-Pay API.
- This example is for demonstration purposes only. In a real-world application, you should never expose your API secret in client-side code.
This project uses dotenv to load environment variables from the .env file. The webpack configuration includes a DefinePlugin that makes these variables available to the React application through process.env.
In the React application, environment variables are accessed using process.env.VARIABLE_NAME:
const apiKey = process.env.REACT_APP_XPAY_API_KEY;
const apiSecret = process.env.REACT_APP_XPAY_API_SECRET;
const baseUrl = process.env.REACT_APP_XPAY_BASE_URL || 'https://api.x-pay.fun';When deploying to production:
-
Never expose your API secret in client-side code
- For production applications, handle sensitive operations like signature generation on the server side
- Use environment variables for configuration, but be aware that client-side environment variables are still visible in the browser
-
Recommended Production Architecture:
- Frontend (React): Handles UI and user interactions
- Backend (Node.js, Python, etc.): Stores API credentials securely and communicates with X-Pay API
- API Flow: Frontend → Your Backend → X-Pay API
-
Implement proper error handling and retry mechanisms
-
Set up webhook verification to securely receive payment notifications
-
Consider implementing rate limiting and request validation
This example project is MIT licensed.