PayPal's Button Manager API with javascript -


what i'd achieve dynamically create hosted paypal buttons through button manager api offered paypal, preferrably nvp api. using client side javascript.

surprisingly, after extensive searching online i've failed find sample of code achieves goal. reading paypal documentation led me believe can use api xmlhttprequests. however, fail reply paypal. i've created string arbitrary parameters should correct, , come far as:

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 ie7+, firefox, chrome, opera, safari         xmlhttp=new xmlhttprequest();     }     else if (window.activexobject)     {         // code ie6, ie5         xmlhttp=new activexobject("microsoft.xmlhttp");     }     else     {         alert("your browser not support xmlhttp!");     }     xmlhttp.open("post",urlrequest,true);     xmlhttp.onreadystatechange= function(){         alert(xmlhttp.statustext) ;         if (xmlhttp.readystate==4)         {             alert(xmlhttp.responsetext) ;         }     };     xmlhttp.send();  } 

my paypal credentials listed *** privacy purposes, , in code proper. going right way? parameters incorrect, or issue xmlhttprequest? bear in mind new web programming, , detailed explanations appreciated. thank you.

after further reading, seems calling button manager api through client side javascript not correct approach requires cross domain request (which browsers not allow security reasons). possible bypass restriction cors mechanism, far can tell paypal not support it. other solutions exist 'dirty'. i've concluded better make request through server. requests can made own server, invoke button manager api , forward reply (your new button's html code) client. here nodejs program code create new hosted button, can implement in server:

    var querystring = require('querystring'); // url encode nvp      var https = require('https'); // use nvp api, request must https   // prototype create button request fields, can input database, client side user or hard code     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     }     // sample request     // replace **** api certificate specifics, find them in 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 options object after call querystring.stringify because  need     // return string '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...") ; 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -