PayPal表达Node.js结账(错误10410)

时间:2016-06-08 16:27:36

标签: node.js paypal token paypal-sandbox

我无法成功解析返回的令牌以验证最终的Paypal沙盒付款流程。任何帮助或输入将不胜感激:)

REF:

https://github.com/petersirka/node-paypal-express-checkout https://developer.paypal.com/docs/classic/express-checkout/in-context/integration/

以下是我返回的错误:

ACK: 'Failure',
VERSION: '52.0',
BUILD: '22708556'
L_ERRORCODE0: '10410',
L_SHORTMESSAGE0: 'Invalid token',
L_LONGMESSAGE0: 'Invalid token.',
L_SEVERITYCODE0: 'Error'

客户方:

<form id="myContainer" method="post" action="/api/payment/payone"></form>
    <script>
        window.paypalCheckoutReady = function () {
           paypal.checkout.setup('XXX', {
           environment: 'sandbox',
           container: 'myContainer'
           });
        };
     </script>
     <script src="//www.paypalobjects.com/api/checkout.js" async></script>

API:

payone: function(req, res) {
    var paypal = require('paypal-express-checkout').init('username', 'password', 'signature', 'return url', 'cancel url', 'debug');



    paypal.pay('20130001', 10.00, 'XX', 'USD', false, function(err, url) {
        if (err) {
            console.log(err);
            return;
        }
        res.redirect(url);
    });


    paypal.detail( "token", "PayerID", function(err, data, invoiceNumber, price) {

        if (err) {
            console.log(err);
            return;
        }
    });

}

最后,paypal-express doc:

var parser = require('url');
var https = require('https');
var qs = require('querystring');

function Paypal(username, password, signature, returnUrl, cancelUrl, debug) {
this.username = "XXX";
this.password = "XXX";
this.solutiontype = 'Mark';
this.signature = "XXX";
this.debug = debug || false;
this.returnUrl = 'XXX';
this.cancelUrl = 'XXX';
this.url = 'https://' + 'api-3t.sandbox.paypal.com' + '/nvp';   //'https://' +        (debug ? 'api-3t.sandbox.paypal.com' : 'api-3t.paypal.com') + '/nvp';
this.redirect = 'https://' + 'www.sandbox.paypal.com/cgi-bin/webscr';      //https://' + (debug ? 'www.sandbox.paypal.com/cgi-bin/webscr' : 'www.paypal.com/cgi-bin/webscr');
};

Paypal.prototype.params = function() {
var self = this;
return {
    USER: self.username,
    PWD: self.password,
    SIGNATURE: self.signature,
    SOLUTIONTYPE: self.solutiontype,
    VERSION: '52.0'
};
console.log(self);
};


Paypal.prototype.detail = function(token, payer, callback) {

if (token.get !== undefined && typeof(payer) === 'function') {
    callback = payer;
    payer = token.get.PayerID;
    token = token.get.token;
}

console.log(token);
var self = this;
var params = self.params();




params.TOKEN = token;
params.METHOD = 'GetExpressCheckoutDetails';

self.request(self.url, 'POST', params, function(err, data) {



    if (err) {
        callback(err, data);
        return;
    }

    if (typeof(data.CUSTOM) === 'undefined') {
        callback(data, null);
        return;
    }

    console.log('3.3');
    var custom = data.CUSTOM.split('|');
    var params = self.params();

    params.PAYMENTACTION = 'Sale';
    params.PAYERID = payer;
    params.TOKEN = token;
    params.AMT = custom[1];
    params.CURRENCYCODE = custom[2];
    params.METHOD = 'DoExpressCheckoutPayment';


    self.request(self.url, 'POST', params, function(err, data) {

        if (err) {
            callback(err, data);
            return;
        }
        console.log('3.4');
        callback(null, data, custom[0], custom[1]);
    });
});

return self;
};




 Paypal.prototype.request = function(url, method, data, callback) {




var self = this;
var params = qs.stringify(data);

if (method === 'GET')
    url += '?' + params;

var uri = parser.parse(url);
var headers = {};


headers['Content-Type'] = method === 'POST' ? 'application/x-www-form-urlencoded' : 'text/plain';
headers['Content-Length'] = params.length;

var location = '';
var options = { protocol: uri.protocol, auth: uri.auth, method: method || 'GET', hostname: uri.hostname, port: uri.port, path: uri.path, agent: false, headers: headers };

var response = function (res) {
    var buffer = '';

    res.on('data', function(chunk) {
        buffer += chunk.toString('utf8');
    })

    req.setTimeout(exports.timeout, function() {
        callback(new Error('timeout'), null);
    });

    res.on('end', function() {

        var error = null;
        var data = '';

        if (res.statusCode > 200) {
            error = new Error(res.statusCode);
            data = buffer;
        } else
            data = qs.parse(buffer);

        callback(error, data);
    });
};

var req = https.request(options, response);

req.on('error', function(err) {
    callback(err, null);
});

if (method === 'POST')
    req.end(params);
else
    req.end();

return self;
};

Paypal.prototype.pay = function(invoiceNumber, amount, description, currency,      requireAddress, callback) {

// Backward compatibility
if (typeof(requireAddress) === 'function') {
    callback = requireAddress;
    requireAddress = false;
}

var self = this;
var params = self.params();


params.PAYMENTACTION = 'Sale';
params.AMT = prepareNumber(amount);
params.RETURNURL = self.returnUrl;
params.CANCELURL = self.cancelUrl;
params.DESC = description;
params.NOSHIPPING = requireAddress ? 0 : 1;
params.ALLOWNOTE = 1;
params.CURRENCYCODE = currency;
params.METHOD = 'SetExpressCheckout';
params.INVNUM = invoiceNumber;
params.CUSTOM = invoiceNumber + '|' + params.AMT + '|' + currency;

self.request(self.url, 'POST', params, function(err, data) {

    if (err) {
        callback(err, null);
        return;
    }

    if (data.ACK === 'Success') {
        callback(null, self.redirect + '?cmd=_express-    checkout&useraction=commit&token=' + data.TOKEN);
        return;
    }

    callback(new Error('ACK ' + data.ACK + ': ' + data.L_LONGMESSAGE0), null);
});

return self;
console.log(self);
 };


 function prepareNumber(num, doubleZero) {
var str = num.toString().replace(',', '.');

var index = str.indexOf('.');
if (index > -1) {
    var len = str.substring(index + 1).length;
    if (len === 1)
        str += '0';
    if (len > 2)
        str = str.substring(0, index + 3);
} else {
    if (doubleZero || true)
        str += '.00';
}
return str;
};

exports.timeout = 10000;
 exports.Paypal = Paypal;

exports.init = function(username, password, signature, returnUrl, cancelUrl,    debug) {
return new Paypal(username, password, signature, returnUrl, cancelUrl, debug);
 };

 exports.create = function(username, password, signature, returnUrl, cancelUrl, debug) {
return exports.init(username, password, signature, returnUrl, cancelUrl,   debug);
 };

1 个答案:

答案 0 :(得分:0)

如果有不正确的令牌或没有传递令牌,我们通常会遇到错误10410。 请确保您传递了正确的PayPal Express令牌。

有关您可能会引用的错误代码的信息:https://developer.paypal.com/docs/classic/api/errorcodes/