const mongoose = require('mongoose'); const paymentTransactionSchema = new mongoose.Schema( { organization: { type: mongoose.Schema.Types.ObjectId, ref: 'Organization', required: true, }, event: { type: mongoose.Schema.Types.ObjectId, ref: 'Event', default: null, }, participant: { type: mongoose.Schema.Types.ObjectId, ref: 'Participant', default: null, }, formId: { type: mongoose.Schema.Types.ObjectId, default: null }, checkoutSource: { type: String, enum: ['platform_razorpay', 'custom_gateway'], required: true, }, gatewayProvider: { type: String, required: true }, orgGatewayId: { type: mongoose.Schema.Types.ObjectId, default: null }, currency: { type: String, default: 'INR' }, /** Amounts in smallest currency unit (e.g. paise) */ amountSubtotal: { type: Number, required: true }, commissionAmount: { type: Number, default: 0 }, amountGross: { type: Number, required: true }, amountNet: { type: Number, required: true }, feeEstimate: { type: Number, default: null }, externalOrderId: { type: String, default: '' }, externalPaymentId: { type: String, default: '' }, status: { type: String, enum: ['pending', 'paid', 'failed', 'refunded'], default: 'pending', }, idempotencyKey: { type: String, default: '' }, /** Razorpay key_id used when order was created — must match on verify after resolve */ ledgerKeyId: { type: String, default: '' }, registrationContext: { type: mongoose.Schema.Types.Mixed, default: null }, /** `ticketing_manual_enablement` = one-time fee to unlock per-event manual UPI (billing). */ purpose: { type: String, enum: ['registration', 'saas_subscription', 'ticket_purchase', 'ticketing_manual_enablement'], default: 'registration', }, subscriptionPlanId: { type: String, default: '' }, purchasedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User', default: null }, }, { timestamps: true } ); paymentTransactionSchema.index({ organization: 1, createdAt: -1 }); paymentTransactionSchema.index({ organization: 1, idempotencyKey: 1 }, { unique: true, partialFilterExpression: { idempotencyKey: { $gt: '' } } }); paymentTransactionSchema.index({ event: 1 }); paymentTransactionSchema.index({ participant: 1 }); paymentTransactionSchema.index({ status: 1 }); paymentTransactionSchema.index({ externalPaymentId: 1 }); paymentTransactionSchema.index({ organization: 1, purpose: 1, createdAt: -1 }); module.exports = mongoose.model('PaymentTransaction', paymentTransactionSchema);