const envCredential = require("../config");
const RAZOR_PAY_KEY_ID = envCredential.RAZOR_PAY_KEY_ID;
const RAZOR_PAY_SECRET = envCredential.RAZOR_PAY_SECRET;
const Razorpay = require("razorpay");
var instance = new Razorpay({
  key_id: RAZOR_PAY_KEY_ID,
  key_secret: RAZOR_PAY_SECRET,
});

const createOrder = async (data) => {
  try {
    let order = await instance.orders.create(data);
    console.log("-=-=-=-=-err=-=-=-=-order-=-=-=-=-", order);
    return order;
  } catch (err) {
    console.log(err);
  }
};

const getPaymentDetailById = async (paymentId) => {
  try {
    let payment = await instance.payments.fetch(paymentId);
    return payment;
  } catch (err) {
    console.log(err);
  }
};

const capturePayment = async (paymentId, amount) => {
  try {
    let payment = await instance.payments.capture(paymentId, amount, "INR");
    console.log(payment);
    return payment;
  } catch (err) {
    console.log(err);
  }
};

module.exports = {
  createOrder,
  getPaymentDetailById,
};
