Introduction
Stablecoins are revolutionizing how businesses handle cross-border payments, offering the speed of blockchain technology with the stability of fiat currencies. In this tutorial, you'll learn how to build a payment processing system that supports both stablecoin and fiat settlements using the Stablecoin Payment API. This system will allow businesses to accept payments in multiple currencies, process them through smart contracts, and settle in either stablecoins or traditional fiat.
Prerequisites
- Intermediate understanding of JavaScript and Node.js
- Basic knowledge of blockchain concepts and Ethereum smart contracts
- Access to a test blockchain network (like Goerli or Sepolia)
- Basic understanding of REST APIs and HTTP requests
- Node.js installed on your machine
- npm or yarn package manager
Step-by-Step Instructions
1. Set up your development environment
First, create a new project directory and initialize your Node.js application:
mkdir stablecoin-payment-system
cd stablecoin-payment-system
npm init -y
This creates a basic project structure for our payment system.
2. Install required dependencies
Install the necessary libraries for blockchain interaction and API handling:
npm install ethers express cors dotenv
Why: ethers provides Ethereum wallet and contract interaction, express builds our API server, cors handles cross-origin requests, and dotenv manages environment variables.
3. Create environment configuration
Create a .env file to store your private keys and API endpoints:
PRIVATE_KEY=your_private_key_here
RPC_URL=https://sepolia.infura.io/v3/YOUR_PROJECT_ID
STABLECOIN_ADDRESS=0x1234567890123456789012345678901234567890
PAYMENT_CONTRACT_ADDRESS=0x9876543210987654321098765432109876543210
Why: Keeping sensitive data in environment variables prevents exposure in version control systems.
4. Initialize the Ethereum provider and wallet
Create a file ethereum.js to handle blockchain interactions:
const { ethers } = require('ethers');
require('dotenv').config();
const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
module.exports = { provider, wallet };
Why: This setup allows us to interact with the Ethereum network using our wallet credentials.
5. Create a payment contract interface
Create paymentContract.js to interact with the smart contract:
const { wallet } = require('./ethereum');
const { ethers } = require('ethers');
require('dotenv').config();
const paymentABI = [
"function processPayment(address token, uint256 amount, address recipient) external returns (bool)"
];
const contract = new ethers.Contract(
process.env.PAYMENT_CONTRACT_ADDRESS,
paymentABI,
wallet
);
async function processPayment(tokenAddress, amount, recipient) {
try {
const tx = await contract.processPayment(tokenAddress, amount, recipient);
await tx.wait();
return tx;
} catch (error) {
console.error('Payment processing error:', error);
throw error;
}
}
module.exports = { processPayment };
Why: This interface allows us to call smart contract functions from our Node.js application.
6. Build the payment processing API
Create server.js to handle incoming payment requests:
const express = require('express');
const cors = require('cors');
const { processPayment } = require('./paymentContract');
const { wallet } = require('./ethereum');
const app = express();
app.use(cors());
app.use(express.json());
app.post('/process-payment', async (req, res) => {
try {
const { tokenAddress, amount, recipient } = req.body;
const tx = await processPayment(tokenAddress, amount, recipient);
res.json({
success: true,
transactionHash: tx.hash,
receipt: tx
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
const PORT = process.env.PORT || 3000;
apprun(() => {
console.log(`Payment server running on port ${PORT}`);
});
Why: This API endpoint allows external systems to submit payment requests that get processed through our smart contract.
7. Test your payment system
Use curl to test your API endpoint:
curl -X POST http://localhost:3000/process-payment \
-H "Content-Type: application/json" \
-d '{"tokenAddress":"0x1234567890123456789012345678901234567890","amount":"1000000000000000000","recipient":"0x9876543210987654321098765432109876543210"}'
Why: Testing with curl verifies that your API can correctly process payment requests and interact with the blockchain.
8. Add reporting and compliance features
Create reporting.js to track transaction history:
const { provider } = require('./ethereum');
async function getTransactionHistory(address) {
const filter = {
address: process.env.PAYMENT_CONTRACT_ADDRESS,
fromBlock: '0x0',
toBlock: 'latest'
};
const logs = await provider.getLogs(filter);
return logs.map(log => ({
transactionHash: log.transactionHash,
blockNumber: log.blockNumber,
amount: log.data
}));
}
module.exports = { getTransactionHistory };
Why: This feature allows businesses to maintain compliance by tracking all transactions and generating reports.
Summary
You've now built a foundational stablecoin payment solution that can process payments in both stablecoins and fiat currencies. This system supports key features like multi-currency settlement, transaction reporting, and compliance tracking. The architecture allows for easy expansion to include additional payment methods and business-specific features like affiliate payouts or payroll processing. This payment system provides the foundation for businesses looking to leverage blockchain technology for global commerce while maintaining the stability and regulatory compliance required for enterprise adoption.



