const axios = require("axios");

const AUTOGOPAY_BASE =
  process.env.AUTOGOPAY_BASE ||
  "https://v1-gateway.autogopay.site";

const AUTOGOPAY_API_KEY =
  process.env.AUTOGOPAY_API_KEY;

function headers() {
  return {
    "Content-Type": "application/json",
    "Authorization": `Bearer ${AUTOGOPAY_API_KEY}`
  };
}

async function createQris(amount) {

  const res = await axios.post(
    `${AUTOGOPAY_BASE}/qris/generate`,
    {
      amount: Number(amount)
    },
    {
      headers: headers(),
      timeout: 20000
    }
  );

  return res.data;
}

async function checkQrisStatus(transactionId) {

  const res = await axios.post(
    `${AUTOGOPAY_BASE}/qris/status`,
    {
      transaction_id: transactionId
    },
    {
      headers: headers(),
      timeout: 20000
    }
  );

  return res.data;
}

async function cancelQris(transactionId) {

  const res = await axios.post(
    `${AUTOGOPAY_BASE}/qris/cancel`,
    {
      transaction_id: transactionId
    },
    {
      headers: headers(),
      timeout: 20000
    }
  );

  return res.data;
}

module.exports = {
  createQris,
  checkQrisStatus,
  cancelQris
};