npm package for Paytrail payment API
npm i paytrail
const { data } = await paytrail.createPayment(auth, paymentInfo)
// Save payment
res.redirect(data.url) // Redirect user to the payment page
paymentInfo
should be valid data (see. mockPaymentInfo.js
and Paytrail docs for more info)
Use try...catch
statement to handle errors
try {
const { data } = await paytrail.createPayment(auth, paymentInfo)
res.redirect(data.url)
} catch (err) {
console.log(err)
res.send(err)
}
You can also use validateData
function to check if your data is valid before calling createPayment
. Beware, there still might be errors since validateData
isn't exhaustive and there might be other errors.
After successful payment user is redirected to url defined in paymentInfo (urlSet.success)
app.get('/success', (req, res) => {
const paymentIsValid = paytrail.confirmPayment(
req.query['ORDER_NUMBER'],
req.query['TIMESTAMP'],
req.query['PAID'],
req.query['METHOD'],
auth.merchantSecret,
req.query['RETURN_AUTHCODE']
)
const msg = paymentIsValid ? 'OK' : 'Failed'
// Do stuff (mark payment paid etc)
res.send(msg)
})
See tests/tests.js
and tests/exampleServer.js