UNPKG

ibc-payment-gateway

Version:

A modular payment gateway for Node.js applications with PostgreSQL and Sequelize

175 lines (166 loc) 4.42 kB
const { DataTypes } = require('sequelize'); function initModels(sequelize) { // Payment Providers Model const PaymentProvider = sequelize.define('PaymentProvider', { name: { type: DataTypes.STRING, primaryKey: true, // now primary key allowNull: false }, display_name: { type: DataTypes.STRING, allowNull: false }, provider: { type: DataTypes.ENUM('razorpay'), defaultValue: 'razorpay', allowNull: false, }, is_active: { type: DataTypes.BOOLEAN, defaultValue: true }, config: { type: DataTypes.JSONB, allowNull: false, comment: 'Provider-specific configuration like API keys, secrets etc.' }, }, { tableName: 'payment_providers', timestamps: true }); // Payment Transactions Model const PaymentTransaction = sequelize.define('PaymentTransaction', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, order_id: { type: DataTypes.STRING, allowNull: false, comment: 'Internal order ID' }, provider_order_id: { type: DataTypes.STRING, allowNull: true, comment: 'Provider-specific order ID (e.g., Razorpay order_id)' }, provider_payment_id: { type: DataTypes.STRING, allowNull: true, comment: 'Provider-specific payment ID' }, provider_name: { type: DataTypes.STRING, allowNull: false, references: { model: PaymentProvider, key: 'name' } }, amount: { type: DataTypes.DECIMAL(10, 2), allowNull: false }, currency: { type: DataTypes.STRING(3), defaultValue: 'INR' }, status: { type: DataTypes.ENUM('pending', 'processing', 'success', 'failed', 'cancelled', 'refunded'), defaultValue: 'pending' }, payment_method: { type: DataTypes.STRING, allowNull: true }, customer_info: { type: DataTypes.JSONB, comment: 'Customer details like email, phone, name' }, metadata: { type: DataTypes.JSONB, comment: 'Additional data from the application' }, provider_response: { type: DataTypes.JSONB, comment: 'Raw response from payment provider' }, webhook_received: { type: DataTypes.BOOLEAN, defaultValue: false }, webhook_data: { type: DataTypes.JSONB, comment: 'Webhook payload from provider' }, failure_reason: { type: DataTypes.TEXT, allowNull: true } }, { tableName: 'payment_transactions', timestamps: true }); // Payment Webhooks Log Model const PaymentWebhookLog = sequelize.define('PaymentWebhookLog', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, provider_name: { type: DataTypes.STRING, allowNull: false, references: { model: PaymentProvider, key: 'name' } }, event_type: { type: DataTypes.STRING, allowNull: false }, payload: { type: DataTypes.JSONB, allowNull: false }, signature: { type: DataTypes.STRING, allowNull: true }, processed: { type: DataTypes.BOOLEAN, defaultValue: false }, transaction_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: PaymentTransaction, key: 'id' } }, processing_error: { type: DataTypes.TEXT, allowNull: true } }, { tableName: 'payment_webhook_logs', timestamps: true }); // Define associations PaymentProvider.hasMany(PaymentTransaction, { foreignKey: 'provider_name' }); PaymentTransaction.belongsTo(PaymentProvider, { foreignKey: 'provider_name' }); PaymentProvider.hasMany(PaymentWebhookLog, { foreignKey: 'provider_name' }); PaymentWebhookLog.belongsTo(PaymentProvider, { foreignKey: 'provider_name' }); PaymentTransaction.hasMany(PaymentWebhookLog, { foreignKey: 'transaction_id' }); PaymentWebhookLog.belongsTo(PaymentTransaction, { foreignKey: 'transaction_id' }); return { PaymentProvider, PaymentTransaction, PaymentWebhookLog }; } module.exports = { initModels };