const express = require('express')

const app = express()

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.post('/initphonepe', async (req, res) => {
  const xVerify = req.body.xVerify;
  const requestBody = JSON.stringify(req.body.request)
  console.log(xVerify)
  console.log(requestBody)
  try {
    console.log("Started call..");
    const response = await fetch('https://api.phonepe.com/apis/hermes/pg/v1/pay', {
      method: 'POST',
      body: requestBody,
      headers: {
        'Content-Type': 'application/json',
        'accept': 'application/json',
        'X-Verify': xVerify
      }
    });

    if (response.ok) {
      const myJson = await response.json();
      console.log(myJson);
      res.send(myJson);
    } else {
      const errorBody = await response.text()
      console.error('Request failed');
      res.status(response.status).send(errorBody);
    }
  } catch (error) {
    console.error(error);
    res.status(500).send('Internal Server Error');
  }
});


app.listen(3000)

