I am building a payment gateway using paystack API and Axios for making the http request.
I used their API docs and checked their postman collection to get an idea of how to write the code.
The code works on postman because whenever I send a post request to initialize a transaction with the required parameters, I get a response object with the data I need (the url in this case)
Problem is, I can't get my app to redirect to the url contained in the response data object on the browser, I keep getting this error on the browser window
cannot GET /paystack/undefined
Already looked up the issue, tried some possible solutions, from refactoring the code to using async await but it doesn't work (except maybe bump the response speed a bit)
Here's the code
Controller
const { default: axios } = require("axios");
exports.postForm = async (req, res) => {
await axios({
method: 'post',
url: 'https://api.paystack.co/transaction/initialize',
headers: {
'Authorization': secretKey,
'Content-Type': 'application/json',
'cache-control': 'no-cache'
},
data: {
"email": req.body.email,
"amount": req.body.amount
}
})
.then((response) => {
res.status(302).redirect(response.data.authorizaton_url) // it is supposed to go to the url inside the data object
console.log(response.data)
})
.catch((error) => {
console.log(error);
});
};
Tried to stringify response.data but it doesn't change anything
Routes
const express = require('express');
const router = express.Router();
const pay = require('../controllers/paymentController');
router.get('/', pay.getForm);
router.post('/paystack/pay', pay.postForm);
Ps - a GET route wouldn't help unless I'm redirecting the user back to my app to confirm the transaction. Used another http library called Request, worked but it's deprecated.
You can setup a callback url on paystack to redirect user on successful payment, which you can check on your backend. webhook url
If that does not help, you can consider this instead res.status(301).redirect( your auth url
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.