【问题标题】:AWS Lambda HTTPS post to Paypal IPN errorAWS Lambda HTTPS 发布到 Paypal IPN 错误
【发布时间】:2019-08-26 04:37:03
【问题描述】:

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

我已经使用 Paypal 的 IPN 模拟器测试了 api 网关 url。它适用于第一步,我收到此消息“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();}

【问题讨论】:

    标签: post https aws-lambda paypal-ipn


    【解决方案1】:

    设法对其进行了整理。我使用的是 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();}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-20
      • 2016-06-16
      • 2021-03-18
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 2011-11-11
      • 2012-08-02
      相关资源
      最近更新 更多