PayPal's按钮管理器API与javascript

PayPal's Button Manager API with javascript

本文关键字:管理器 API javascript 按钮 PayPal      更新时间:2023-09-26

我想要实现的是通过PayPal提供的按钮管理器API动态创建托管的 PayPal按钮,最好是NVP API。我想这样做,使用客户端Javascript。

令人惊讶的是,在网上进行了广泛的搜索之后,我没有找到一个实现这个目标的代码示例。阅读PayPal文档让我相信我可以使用xmlhttprequest的API。但是我没有得到PayPal的回复。我已经创建了一个字符串与一些任意参数应该是正确的,并到目前为止:

var xmlhttp;
function generateButton()
{
    console.log("Function begun") ;
    var strUrl= "https://api.paypal.com/nvp";
    var strParameters="?METHOD=BMCreateButton&VERSION=204.0&BUTTONCODE=HOSTED&BUTTONTYPE=BUYNOW&BUTTONSUBTYPE=PRODUCTS&L_BUTTONVAR0=amount=15.00&L_BUTTONVAR1=item_name=test_item2USER=***&PWD=***&SIGNATURE=***";
    var urlRequest= strUrl+encodeURI(strParameters);
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        alert("Your browser does not support XMLHTTP!");
    }
    xmlhttp.open("POST",urlRequest,true);
    xmlhttp.onreadystatechange= function(){
        alert(xmlhttp.statusText) ;
        if (xmlhttp.readyState==4)
        {
            alert(xmlhttp.responseText) ;
        }
    };
    xmlhttp.send();
}

出于隐私目的,我的paypal凭据被列为***,在我的代码中它们是正确的。我这样做对吗?是我的参数不正确,还是我的xmlhttprequest有问题?请记住,我是web编程的新手,详细的解释将是感激的。谢谢你!

进一步阅读后,似乎通过客户端javascript调用按钮管理器API不是一种正确的方法,因为它需要跨域请求(浏览器不允许出于安全原因)。有可能通过CORS机制绕过这个限制,但据我所知,PayPal不支持它。其他解决方案是存在的,但都是"肮脏的"。我的结论是,最好通过服务器发出请求。可以向您自己的服务器发出请求,服务器将调用按钮管理器API并将应答(新按钮的HTML代码)转发回客户机。下面是一个NodeJS程序代码来创建一个新的托管按钮,你可以在你的服务器中实现:

    var querystring = require('querystring'); // to URL encode for the NVP 
    var https = require('https'); // to use the NVP API, request must be https
 // Prototype for a create button request fields, you can get your input from database, client side user or just hard code it
    function CreateButtonRequest(user, pwd, signature, buttonPrice, buttonName){ 
            this.USER =user,
            this.PWD  = pwd,
            this.SIGNATURE = signature,
            this.METHOD = "BMCreateButton",
            this.VERSION = "204.0",
            this.BUTTONCODE = "HOSTED",
            this. BUTTONTYPE = "BUYNOW",
            this.BUTTONSUBTYPE = "PRODUCTS",
            this.L_BUTTONVAR0  = "amount="+buttonPrice,
            this.L_BUTTONVAR1 = "item_name="+buttonName
    }
    // A sample request
    // Replace **** with your API certificate specifics, find them in your paypal account
    sampleRequestData = {   
        USER : "****",
        PWD : "****",
        SIGNATURE : "****",
        METHOD : "BMCreateButton",
        VERSION : "204.0",
        BUTTONCODE : "HOSTED",
        BUTTONTYPE : "BUYNOW",
        BUTTONSUBTYPE : "PRODUCTS",
        L_BUTTONVAR0 : "amount=10.00",
        L_BUTTONVAR1 : "item_name=test item2"
    };
    sampleRequestData = querystring.stringify(postData);
    //init your options object after you call querystring.stringify because you  need
    // the return string for the 'content length' header
    console.log(sampleRequestData) ;
    var options = {
        host: 'api-3t.paypal.com',
        port: 443,
        method: 'POST',
        path: '/nvp',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': postBody.length
        }
    };

    var postreq = https.request(options, function (res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('Response: ' + chunk);
        });
    });
    postreq.write(sampleRequestData);
    postreq.end();
    console.log("Message sent...") ;