AWS Lambda HTTPS发布到Paypal IPN错误

时间:2019-04-04 12:07:43

标签: post https aws-lambda paypal-ipn

我一直在尝试使用AWS Api Gateway来实现Paypal的IPN,以获取IPN处理程序URL。该api与Lambda函数集成为“接收器”。

我已经使用贝宝(Paypal)的IPN模拟器测试了api网关的网址。它适用于第一步,并且收到以下消息:“已发送IPN,并且握手已通过验证。”

现在我的问题是下一步,我必须使用HTTPS发布将收到的消息发送回Paypal。我已经尝试了很多次,并不断出现此错误:

{
"code": "ECONNREFUSED",
"errno": "ECONNREFUSED",
"syscall": "connect",
"address": "127.0.0.1",
"port": 443

}

我真的很感谢能帮助它正常工作。

我正在使用node.js 8.10,这是我的Lambda函数:

exports.handler = (event, context, callback) => {
console.log('Received event:', JSON.stringify(event, null, 2));

// Return 200 to caller
console.log('sending 200 back to paypal');
callback(null, {
    statusCode: '200'
});

// Read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
console.log('modifying return body...');
var body = 'cmd=_notify-validate&' + event.body;

callHttps(body, context);};

function callHttps(body, context) {
console.log('in callHttp()....');

var https = require('https');

var options = {
    url: 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr',
    method: 'POST',
    headers: {
        "user-agent": "Nodejs-IPN-VerificationScript"
    },
    body: body
};

const req = https.request(options, (res) => {
    res.on('data', (chunk) => {
        // code to execute
        console.log("on data - can execute code here....");
    });
    res.on('end', () => {
        // code to execute  
        console.log("on end - can execute code here....");
    });
});
req.on('error', (e) => {
    console.log("Error has occured: ", JSON.stringify(e, null, 2));
});
req.end();}

1 个答案:

答案 0 :(得分:0)

设法进行了排序。我使用的是url,而不是将其分解为主机和路径。以下是对我有用的完整代码:

exports.handler = (event, context, callback) => {

console.log('Received event:', JSON.stringify(event, null, 2));

// Return 200 to caller
console.log('sending 200 back to paypal');
callback(null, {
    statusCode: '200'
});

callHttps(event.body, context);};

function callHttps(body, context) {

console.log('in callHttp()....');

// Read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
console.log('modifying return body...');
var bodyModified = 'cmd=_notify-validate&' + body;

var https = require('https');

var options = {
    host: "ipnpb.sandbox.paypal.com",
    path: "/cgi-bin/webscr",
    method: 'POST',
    headers: {
        'user-agent': 'Nodejs-IPN-VerificationScript',
        'Content-Length': bodyModified.length,
    }
};

const req = https.request(options, (res) => {

    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);

    var result = '';
    res.on('data', (d) => {
        // get the result here
        result += d;
    });

    res.on('end', (end) => {
        // check the result

        if (result === 'VERIFIED') {
            // process the message

            // split the message
            var res = body.split("&");
            //   create an object
            var paypalMessageObject = new Object();
            // loop through split array
            res.forEach(element => {
                // split element
                var temp = (element.toString()).split("=");
                // add to the object
                paypalMessageObject[temp[0]] = temp[1];
            });
            console.log('paypalMessageObject: ' + JSON.stringify(paypalMessageObject, null, 2));

            var checkItems = {
                payment_status: paypalMessageObject.payment_status,
                mc_gross: paypalMessageObject.mc_gross,
                mc_currency: paypalMessageObject.mc_currency,
                txn_id: paypalMessageObject.txn_id,
                receiver_email: paypalMessageObject.receiver_email,
                item_number: paypalMessageObject.item_number,
                item_name: paypalMessageObject.item_name
            };

            console.log('checkItems: ', JSON.stringify(checkItems, null, 2));

        }
        else { console.log('not verified, now what?'); }
    });

});

req.on('error', (e) => {
    console.log("Error has occured: ", JSON.stringify(e, null, 2));
});

req.write(bodyModified);

req.end();}