const axios = require("axios");
const envCredential = require("../config");

const clientId = envCredential.CASHFREE_KEY_ID;
const secret = envCredential.CASHFREE_KEY_SECRET;
const baseUrl = envCredential.CASHFREE_BASE_URL;
const headers = {
  "x-api-version": "2023-08-01",
  "x-client-id": clientId,
  "x-client-secret": secret,
};

const createOrder = async (request) => {
  try {
    let data = await axios.post(`${baseUrl}/orders`, request, {
      headers: headers,
    });
    return data.data;
  } catch (err) {
    console.log(err);
    throw Error(err.message);
  }
};

const getPaymentDetailById = async (paymentId) => {
  try {
    let data = await axios.get(`${baseUrl}/orders/${paymentId}`, {
      headers: headers,
    });
    return data.data;
  } catch (err) {
    console.log(err);
    return null;
  }
};

module.exports = {
  createOrder,
  getPaymentDetailById,
};
