ibc-payment-gateway
Version:
A modular payment gateway for Node.js applications with PostgreSQL and Sequelize
142 lines (126 loc) • 4.09 kB
JavaScript
const express = require('express');
const bodyParser = require('body-parser');
class PaymentRoutes {
constructor(paymentService) {
this.paymentService = paymentService;
this.router = express.Router();
this.setupRoutes();
}
setupRoutes() {
// Middleware for parsing JSON
// this.router.use(bodyParser.json());S
// Create payment provider
this.router.post('/providers', async (req, res) => {
try {
const provider = await this.paymentService.createPaymentProvider(req.body);
res.status(201).json({
success: true,
data: provider
});
} catch (error) {
res.status(400).json({
success: false,
error: error.message
});
}
});
// Update payment provider by name
this.router.put('/providers/:name', async (req, res) => {
try {
const updatedProvider = await this.paymentService.updatePaymentProvider(req.params.name, req.body);
res.json({ success: true, data: updatedProvider });
} catch (error) {
res.status(400).json({ success: false, error: error.message });
}
});
// Delete payment provider by name
this.router.delete('/providers/:name', async (req, res) => {
try {
const result = await this.paymentService.deletePaymentProvider(req.params.name);
res.json({ success: true, data: result });
} catch (error) {
res.status(400).json({ success: false, error: error.message });
}
});
// List all providers
this.router.get('/providers', async (req, res) => {
try {
const providers = await this.paymentService.listPaymentProviders();
res.json({ success: true, data: providers });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
// Initiate payment
this.router.post('/initiate', async (req, res) => {
try {
const result = await this.paymentService.initiatePayment(req.body);
res.status(200).json(result);
} catch (error) {
res.status(400).json({
success: false,
error: error.message
});
}
});
// Webhook endpoint
this.router.post('/webhook/:providerName', async (req, res) => {
try {
const { providerName } = req.params;
const signature = req.headers['x-razorpay-signature'];
await this.paymentService.handleWebhook(
providerName,
signature,
req.body
);
res.status(200).json({ success: true });
} catch (error) {
console.error('Webhook error:', error);
res.status(400).json({
success: false,
error: error.message
});
}
});
// Get transaction status
this.router.get('/transactions/:order_id', async (req, res) => {
try {
const transactions = await this.paymentService.getTransactions(req.params.order_id);
if (!transactions) {
return res.status(404).json({
success: false,
error: 'Transaction not found'
});
}
res.status(200).json({
success: true,
data: transactions
});
} catch (error) {
res.status(400).json({
success: false,
error: error.message
});
}
});
// Get transactions by status
this.router.get('/transactions/status/:status', async (req, res) => {
try {
const transactions = await this.paymentService.getTransactionsByStatus(req.params.status);
res.status(200).json({
success: true,
data: transactions
});
} catch (error) {
res.status(400).json({
success: false,
error: error.message
});
}
});
}
getRouter() {
return this.router;
}
}
module.exports = PaymentRoutes;