M
M
Markiv072021-02-16 03:45:11
Qiwi
Markiv07, 2021-02-16 03:45:11

Hello, need help with Nodejs Express Qiwi invoice?

Hello, I have a problem that I have been struggling with for a few days now. When you click on a button via Fetch, you need to make a request to the kiwi server via PUT and receive a response in json format. That's the problem, from the client side, if I call a function in which there is Fetch and some local path to my server is specified there, then it catches and displays an error (that the connection was lost) and the following appears on the command line 602b14ac3963c539153353.png

And when I specify the path to some other site, then I get errors related to CORS, although all the headers seem to be indicated

Here is the client part of the code

document.querySelectorAll('.payments').forEach(function(element){
  element.onclick = createPayment;
});

function createPayment() {
  fetch('/cr',{
  method: 'PUT',
  headers: {
    'Authorization': 'Bearer wX1FRSbE6iyCj2gCRwwF3Dnh5XrasNTx3BGPiMsyXQFNKQhvukniQG8RTVhYm3iPwPhF1aV7hgCHTZbGec4giFGqmsEEVhPUjQ53RCTCTPZZSiJjWpK2yxxfQtUV8gg124j6t5xuC21LcuAvM25dLFy1x2cPKbA4QYTdqUfzK',
    'Accept': 'application/json',
    'Content-Type' : 'application/json',
  }
}).then(response => response.json()).then(response => console.log(response)).catch(err => {
        console.log("Error: " + err)
    })
}


Here is the server side of the code
app.put('/cr' ,async function (req,res,next) {
  res.header("Access-Control-Allow-Origin", "http://localhost:3000"); // restrict it to the required domain
  res.header("Access-Control-Allow-Methods", "PUT");
  res.header("Access-Control-Allow-Headers", "Content-type,Accept");
  if (req.method === "OPTIONS") {
        return res.status(200).end();
    }
  const data = await qiwi(); // получаем в data результат вызова qiwiApi.createBill
  console.log(data);
  let p = res.json(data);
  next(); // т.к. функция асинхронная, вызываем next, чтобы объявить о завершении выполнения этого middleware
});


async function qiwi() {
  const billId = qiwiApi.generateId();

  const fields = {
      amount: 1.00,
      currency: 'RUB',
      comment: 'test',
      expirationDateTime: '2022-03-02T08:44:07',
      successUrl: 'http://test.ru/'
  };

  const data = await qiwiApi.createBill( billId, fields );
  console.log(data);
  // тут делаем что-то еще, если нужно
  return data; // возвращаем результат вызова qiwiApi.createBill
}


Please help, how to fix and where to go?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question