"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PayPalGateway = void 0;
const payment_config_1 = require("../../../config/payment.config");
class PayPalGateway {
    clientId;
    clientSecret;
    constructor(config) {
        this.clientId = config?.clientId || payment_config_1.paymentConfig.paypal.clientId;
        this.clientSecret = config?.clientSecret || payment_config_1.paymentConfig.paypal.clientSecret;
    }
    async createOrder(order) {
        const paypalOrderId = `pp_ord_${Date.now()}`;
        return {
            id: order.id,
            gatewayOrderId: paypalOrderId,
            amount: Number(order.grandTotal),
            currency: 'USD', // PayPal standard conversion
            status: 'CREATED',
        };
    }
    async createCheckout(checkout) {
        return {
            checkoutUrl: `https://www.sandbox.paypal.com/checkoutnow?token=${checkout.gatewayOrderId}`,
            gatewayTransactionId: checkout.gatewayOrderId,
        };
    }
    async verifyPayment(payload) {
        const isSuccess = payload.status === 'COMPLETED' || payload.status === 'success';
        return {
            success: isSuccess,
            status: isSuccess ? 'SUCCESS' : 'FAILED',
            gatewayPaymentId: payload.payerId || `pp_pay_${Date.now()}`,
            gatewayTransactionId: payload.orderId || payload.paypalOrderId,
            message: 'PayPal payment status verified',
            rawResponse: payload,
        };
    }
    async capturePayment(paymentId, amount) {
        return {
            success: true,
            gatewayPaymentId: paymentId,
            amount,
            status: 'SUCCESS',
        };
    }
    async refundPayment(transactionId, amount, reason) {
        const refundId = `pp_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) {
        const event = payload.event_type;
        const resource = payload.resource;
        let status = 'PENDING';
        if (event === 'PAYMENT.CAPTURE.COMPLETED')
            status = 'SUCCESS';
        else if (event === 'PAYMENT.CAPTURE.DENIED')
            status = 'FAILED';
        else if (event === 'PAYMENT.CAPTURE.REFUNDED')
            status = 'REFUNDED';
        return {
            event,
            orderId: resource?.supplementary_data?.related_ids?.order_id || resource?.id,
            paymentId: resource?.id,
            status,
        };
    }
    async generateReceipt(paymentId) {
        return { receiptId: `pp_rcpt_${paymentId}` };
    }
    async cancelPayment(paymentId) {
        return { success: true };
    }
}
exports.PayPalGateway = PayPalGateway;
//# sourceMappingURL=paypal.gateway.js.map