"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PayUGateway = void 0;
const crypto_1 = __importDefault(require("crypto"));
const logger_1 = require("../../../config/logger");
class PayUGateway {
    key;
    salt;
    constructor(config) {
        this.key = config?.keyId || process.env.PAYU_KEY || 'MOCK_PAYU_KEY';
        this.salt = config?.saltKey || process.env.PAYU_SALT || 'mock_payu_salt';
    }
    async createOrder(order) {
        const txnId = `payu_txn_${Date.now()}`;
        return {
            id: order.id,
            gatewayOrderId: txnId,
            amount: Number(order.grandTotal),
            currency: 'INR',
            status: 'CREATED',
        };
    }
    async createCheckout(checkout) {
        // PayU requires a SHA512 hash: key|txnid|amount|productinfo|firstname|email|||||||||||salt
        const hashString = `${this.key}|${checkout.gatewayOrderId}|${checkout.amount}|BookPurchase|Customer|customer@example.com|||||||||||${this.salt}`;
        const hash = crypto_1.default.createHash('sha512').update(hashString).digest('hex');
        logger_1.logger.info(`[PayUGateway] Generated SHA512 hash: ${hash}`);
        return {
            checkoutUrl: `https://test.payu.in/_payment?key=${this.key}&txnid=${checkout.gatewayOrderId}&amount=${checkout.amount}&productinfo=BookPurchase&firstname=Customer&email=customer@example.com&hash=${hash}`,
            gatewayTransactionId: checkout.gatewayOrderId,
        };
    }
    async verifyPayment(payload) {
        // Reverse Hash Validation: salt|status|||||||||||email|firstname|productinfo|amount|txnid|key
        const { status, txnid, amount, email, firstname, productinfo, key, hash } = payload;
        const reverseHashString = `${this.salt}|${status}|||||||||||${email}|${firstname}|${productinfo}|${amount}|${txnid}|${key}`;
        const calculatedHash = crypto_1.default.createHash('sha512').update(reverseHashString).digest('hex');
        const isValid = hash === calculatedHash || process.env.NODE_ENV !== 'production';
        const isSuccess = status === 'success';
        return {
            success: isValid && isSuccess,
            status: isSuccess ? 'SUCCESS' : 'FAILED',
            gatewayPaymentId: payload.mihpayid || `payu_mih_${Date.now()}`,
            gatewayTransactionId: txnid,
            message: `PayU payment status verified: ${status}`,
            rawResponse: payload,
        };
    }
    async capturePayment(paymentId, amount) {
        return {
            success: true,
            gatewayPaymentId: paymentId,
            amount,
            status: 'SUCCESS',
        };
    }
    async refundPayment(transactionId, amount, reason) {
        const refundId = `payu_ref_${Date.now()}`;
        return {
            success: true,
            gatewayRefundId: refundId,
            amount,
            status: 'SUCCESS',
        };
    }
    async paymentStatus(transactionId) {
        return {
            status: 'SUCCESS',
            gatewayTransactionId: transactionId,
        };
    }
    async handleCallback(payload) {
        return this.verifyPayment(payload);
    }
    async handleWebhook(payload, signature) {
        return {
            event: 'payment.success',
            orderId: payload.txnid,
            paymentId: payload.mihpayid,
            status: 'SUCCESS',
        };
    }
    async generateReceipt(paymentId) {
        return { receiptId: `payu_rcpt_${paymentId}` };
    }
    async cancelPayment(paymentId) {
        return { success: true };
    }
}
exports.PayUGateway = PayUGateway;
//# sourceMappingURL=payu.gateway.js.map