PayPal的Button Manager API与javascript

时间:2016-11-10 13:43:32

标签: javascript paypal

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

令人惊讶的是,经过广泛的在线搜索后,我找不到实现该目标的代码示例。 阅读PayPal文档让我相信我可以将API与xmlhttprequests一起使用。但是,我没有收到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问题?请记住,我是网络编程的新手,详细解释将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:0)

在进一步阅读之后,似乎通过客户端javascript调用Button Manager 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...") ;