paynl / nodejs-sdk

Pay.nl nodejs sdk
6 stars 7 forks source link

get transactionId for exchange #1

Closed thisismichael23 closed 7 years ago

thisismichael23 commented 7 years ago

Hello Andy I want to use this node sdk but i confused about fetching a transaction. In your example Paynl.Transaction.get('715844054X85729e') but how exactly we get the transaction id 715844054X85729e? Like in php version $paynl->getId(); Should we do it manually by listening the params query for order_id? Or you have more convenient way to get it?

I can get the transaction start running, only this webhook part need more info.

Thank you

Michael

andypieters commented 7 years ago

Michael,

The transactionId is returned when you start the transaction. The result of the transaction start has a transactionId inside it. If you want to implement an exchange script, you'll need a webserver like express.

See my example below.

Best regards,

Andy Pieters

"use strict";
var Paynl = require('paynl-sdk');
var express = require('express');
var formData = require('express-form-data');
var bodyParser = require('body-parser');

Paynl.Config.setApiToken('your-api-token');
Paynl.Config.setServiceId('SL-1234-4567');

/* Note that the Pay.nl exchange will only call port 80 or 443 any other ports will be ignored */
var PORT = 80;
var app = express();

app.use(formData.parse());
app.use(bodyParser.urlencoded({ extended: true }));

/* Start a transaction */
app.get('/', function (req, res) {
    var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
    Paynl.Transaction.start({
        amount: 1,
        returnUrl: fullUrl + 'return',
        ipAddress: req.ip,
        testMode: true,
        exchangeUrl: fullUrl + 'exchange'
    }).subscribe(function (result) {
        /* Here you can get the transactionId: result.transactionId */
        res.redirect(result.paymentURL);
    }, function (error) {
        console.log(error);
        res.end('Error: ' + error);
    });
});

/* Return page */
app.get('/return', function (req, res) {
    var transactionId = req.query.orderId;
    Paynl.Transaction.get(transactionId).subscribe(function (transaction) {
        var result = 'Welcome back, the transactionStatus is: ' + transaction.paymentDetails.stateName;
        result += '<br /><a href="/">New payment</a>';
        res.send(result);
    });
});

/* Exchange, notice that it can be get and post depending on the settings in your pay.nl account */
app.get('/exchange', function (req, res) {
    var transactionId = req.query.order_id;
    Paynl.Transaction.get(transactionId).subscribe(function (transaction) {
        res.end('TRUE| Status is: ' + transaction.paymentDetails.stateName);
    });
});
app.post('/exchange', function (req, res) {
    var transactionId = req.body.order_id;
    Paynl.Transaction.get(transactionId).subscribe(function (transaction) {
        res.end('TRUE| Status is: ' + transaction.paymentDetails.stateName);
    });
});

app.listen(PORT, function () {
    console.log('listening on port: ' + PORT);
});
thisismichael23 commented 7 years ago

Thank you for your response. I have the express server running already and I did exactly like your example. Worked as expected.

Thanks :)