1.0.1 • Published 8 years ago
paytrail v1.0.1
Paytrail
npm package for Paytrail payment API
Installation
npm i paytrailUsage
Payment creation
const { data } = await paytrail.createPayment(auth, paymentInfo)
// Save payment
res.redirect(data.url) // Redirect user to the payment pagepaymentInfo should be valid data (see. mockPaymentInfo.js and Paytrail docs for more info)
Error handling
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.
Payment confirmation
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)
})Examples
See tests/tests.js and tests/exampleServer.js