1

I'm using nodejs and express. This is my code that is run on the return from Paypal. I only get a 302 errors in response from Paypal. I saw a couple examples that use ssl:// instead of https:// but nodejs yells saying that its not a valid protocol for the https module. Does anyone have a working nodejs script for PDT and IPN?

var purchaseID = req.query.tx;
var atoken = MYAuthToken;
var postDataArray = {'cmd':'_notify-synch','tx': purchaseID, 'at': atoken}
var postData = JSON.stringify(postDataArray);
console.log(postData);
var options = {
    hostname: 'www.sandbox.paypal.com',
    port: 443,
    path: '/cgi-bin/webscr',
method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': postData.length
        }
    };

    var req = https.request(options, function(res) {
        console.log('STATUS: '+ res.statusCode);
        console.log('HEADERS: '+ JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            console.log('BODY: '+chunk);
        });
        res.on('end', function() {
            console.log('No more data in response.')
        });
    });
    req.on('error', function(e) {
        console.log('problem with request: '+e.message);
    });
    req.write(postData);
    req.end();
});

This

Blalan
  • 57
  • 4

2 Answers2

0

You're missing Accept: */* header. Also, JSON.stringify is not application/x-www-form-urlencoded. Here is some working code for you to build based on:

var request = require('request');
var endpoint = 'www.sandbox.paypal.com';
var options = {
  form: {
    cmd: '_notify-synch',
    tx: tx,
    at: auth
  },
  headers: {
    Accept: '*/*'
  }
};
request.post('https://' + endpoint + '/cgi-bin/webscr', options, function(e, r, body) {
  return console.log(body);
});
Fluffy
  • 27,504
  • 41
  • 151
  • 234
  • Is it possible for pure client JavaScript? I only have Google Blogger, I have no web server for the `require('request')` to work. – Antonio Ooi Feb 25 '20 at 14:24
-1

Try just posting without the JSON

var postData = "cmd=_notify-synch,at=" + at + ",tx=" + tx;

I've edited a couple of times as i ran into issues. I'm new to node so just hacking out a solution with trial and error. Your post moved me towards the solution. So here is postData that works with your code. It's nice to see the FAIL ans SUCCESS messages come through. Note .. need the &'s

var postData = "cmd=_notify-synch&at=" + at + "&tx=" + tx;

Robert
  • 1
  • 1