/paytrail

Node module for processing payments through Paytrail API

Primary LanguageJavaScriptMIT LicenseMIT

Paytrail

npm package for Paytrail payment API

Installation

npm i paytrail

Usage

Payment creation

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)

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

Paytrail docs

http://docs.paytrail.com/en/index-all.html#payment-api.rest