Paypal IPN验证每次都会返回无效

时间:2017-10-03 23:47:25

标签: javascript node.js express paypal paypal-ipn

基本上我得到了一个"无效"当我尝试验证使用Paypal和NodeJS的IPN听众进行的付款时,我将回复一个urlencoded的帖子回到paypal,让他们告诉我它是否有效,但是它总是返回Invalid。

我有一个节点应用,我使用expressJS来管理所有这些服务器调用。

我已经尝试了两种不同的方法:

app.post('/ipn', bodyParser.text({ type: 'urlencoded' }), function (req, res) {
    res.sendStatus(200);
    console.log('accedio al ipn');
    //console.log(req.body);
    var params = req.body;
    ipn.verify(string, {'allow_sandbox': true}, function callback(err, msg) {
        if (err) {
            console.log(string);
            console.error(err);
          } else {
            // Do stuff with original params here

            if (params.payment_status == 'Completed') {
              // Payment has been confirmed as completed
            }
          }
    });
});

app.post('/ipn', bodyParser.text({ type: 'urlencoded' }), function (req, res) {
    console.log('Received POST /');
    console.log(req.body);
    console.log('\n\n');

    // STEP 1: read POST data
    req.body = req.body || {};
    res.status(200).send('OK');
    res.end();

    // read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
    var postreq = 'cmd=_notify-validate';
    for (var key in req.body) {
        if (req.body.hasOwnProperty(key)) {
            var value = querystring.escape(req.body[key]);
            postreq = postreq + "&" + key + "=" + value;
        }
    }

    // Step 2: POST IPN data back to PayPal to validate
    console.log('Posting back to paypal');
    console.log(postreq);
    console.log('\n\n');
    var options = {
        url: 'https://www.sandbox.paypal.com/cgi-bin/webscr',
        method: 'POST',
        headers: {
            'Connection': 'close'
        },
        body: postreq,
        strictSSL: true,
        rejectUnauthorized: false,
        requestCert: true,
        agent: false
    };

    request(options, function callback(error, response, body) {
        if (!error && response.statusCode === 200) {

            // inspect IPN validation result and act accordingly
            console.log(body);
            if (body.substring(0, 8) === 'VERIFIED') {
                // The IPN is verified, process it
                console.log('Verified IPN!');
                console.log('\n\n');

                // assign posted variables to local variables
                var item_name = req.body['item_name'];
                var item_number = req.body['item_number'];
                var payment_status = req.body['payment_status'];
                var payment_amount = req.body['mc_gross'];
                var payment_currency = req.body['mc_currency'];
                var txn_id = req.body['txn_id'];
                var receiver_email = req.body['receiver_email'];
                var payer_email = req.body['payer_email'];

                //Lets check a variable
                console.log("Checking variable");
                console.log("payment_status:", payment_status)
                console.log('\n\n');

                // IPN message values depend upon the type of notification sent.
                // To loop through the &_POST array and print the NV pairs to the screen:
                console.log('Printing all key-value pairs...')
                for (var key in req.body) {
                    if (req.body.hasOwnProperty(key)) {
                        var value = req.body[key];
                        console.log(key + "=" + value);
                    }
                }

            } else if (body.substring(0, 7) === 'INVALID') {
                // IPN invalid, log for manual investigation
                console.log('Invalid IPN!');
                console.log('\n\n');
            }
        }
    });
});

它们都返回Invalid。

第一个例子来自: https://github.com/andzdroid/paypal-ipn

第二个来自: https://github.com/HenryGau/node-paypal-ipn

据我所知,我在服务器上收到使用IPN的付款通知,然后我将该请求重新发送给PayPal,以便我可以验证它是否有效,出于安全考虑,这一切都是在沙箱环境。 我也尝试发送我得到的请求,但是这次使用POSTMAN,它也返回Invalid,在这里停留了一段时间。

注意:我试图将此用于支付款项,我不知道这是否相关,以防万一。

0 个答案:

没有答案