paypal / PayPal-node-SDK

node.js SDK for PayPal RESTful APIs
Other
1.28k stars 454 forks source link

payout_batch_id status needs a token #143

Closed benzmuircroft closed 8 years ago

benzmuircroft commented 8 years ago

How do I check payout_batch_id status that my app created?

{ batch_header: 
   { payout_batch_id: 'Z86R46Q86677Y',
     batch_status: 'PENDING',
     sender_batch_header: 
      { email_subject: 'You have a payment',
        sender_batch_id: 'plw0cnmi' } },
  links: 
   [ { href: 'https://api.sandbox.paypal.com/v1/payments/payouts/Z86R46Q86677Y',
       rel: 'self',
       method: 'GET' } ],
  httpStatusCode: 201 }

when I GET response.links[0].href

then I recieve undefined data and 401 code as a reply.

I saw this in paypals docs:

curl -v -X GET https://api.sandbox.paypal.com/v1/payments/payouts-item/452345 \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <Access-Token>"

I am changing my GET request to

{url:r.links[0].href,headers:{Content-Type:application/json,Authorization:``}

but I'm unsure how

I saw this but not sure how to put it as an url string

curl https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice \
  -u "Client-Id:Secret" \
  -d grant_type=authorization_code \
  -d code=Authorization-Code"

-u -d ?? what do they mean how can I write this request as an url in node?? or Is it possible to get the token with this module?

bluk commented 8 years ago

To get a Bearer Access token, you may want to read the documentation here under the OAuth Request / Response. Once you have the bearer access token, you replace <Access-Token> in your

curl -v -X GET https://api.sandbox.paypal.com/v1/payments/payouts-item/452345 \
-H "Content-Type:application/json" \
-H "Authorization: Bearer <Access-Token>"

with the access token and get the data you want.

If you're using this PayPal Node SDK, you may want to look at the get payout sample.

Be sure to look at the configuration.js for the samples for some initial code.

The https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice is for a different use case.

bluk commented 8 years ago

Just to be clear the request to get the access token is the section with the example curl request:

curl https://api.sandbox.paypal.com/v1/oauth2/token \
  -H "Accept: application/json" \
  -H "Accept-Language: en_US" \
  -u "<Client-Id>:<Secret>" \
  -d "grant_type=client_credentials"

You should have needed to create a REST app in your developer dashboard which is where you got your client ID and secret.

If you have any other questions, please leave a comment.

benzmuircroft commented 8 years ago
    {
    url:`https://api.sandbox.paypal.com/v1/oauth2/token?`+`xxxxxx_client_id_xxxxxx`+`=`+`xxxxxx_client_secret_xxxxxx
,   headers:{'Content-Type':`application/json`,'Accept-Language':`en_US`}
    }

gives me

data undefined code 200

as a response with GET

bluk commented 8 years ago

@benzmuircroft What code/client are you using for your requests? I would recommend trying the curl command in a terminal/command prompt to make sure it works with your credentials first.

The -u "<Client-iD>:<Secret>" command argument to curl actually adds an Authorization HTTP header (technically a basic auth like Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l ). Passing your client ID and secret via query parameters won't work. The -d "grant_type=client_credentials" is also a form encoded body so curl would add a header Content-Type: application/x-www-form-urlencoded and grant_type=client_credentials as the request body.

The SDKs (whether it be node.js, PHP, Ruby, Python, Java, etc.) can help you so you don't have to worry about constructing request/responses.

benzmuircroft commented 8 years ago

thanks @bluk I'm using this to make the requests:

var JSON=module.exports={
    https:require('https')
,   parse:require('try-json-parse')
,   tryparse:function(str){return this.parse(str)||str;}
,   stringify:require('json-stringify-safe')
,   queryify:function(obj,sep,eq,name){var q=function(v){switch(typeof v){case 'string':return v;case 'boolean':return v?'true':'false';case 'number':return isFinite(v)?v:'';default:return '';}};sep=sep||'&';eq=eq||'=';if(obj===null){obj=undefined;}if(typeof obj==='object'){return Object.keys(obj).map(function(k){var ks=encodeURIComponent(q(k))+eq;if(Array.isArray(obj[k])){return obj[k].map(function(v){return ks+encodeURIComponent(q(v));}).join(sep);}else{return ks+encodeURIComponent(q(obj[k]));}}).join(sep);}if(!name)return '';return encodeURIComponent(q(name))+eq+encodeURIComponent(q(obj));}
,   clone:function(o){return JSON.parse(JSON.stringify(o));}
,   get:function(url_objORstr,callback){process.env.NODE_TLS_REJECT_UNAUTHORIZED="0";var req=JSON.https.request(url_objORstr,function(res){var buffer='';res.setEncoding('utf8');res.on('data',function(chunk){buffer+=chunk;});res.on('end',function(){try{var data=JSON.parse(buffer);callback(data,res.statusCode);}catch(e){console.log(e);}});});req.end();}
,   post:function(url,path,data,type,callback){if(!callback){callback=type;type=undefined;}data=JSON.stringify(data);var options={hostname:url,path:path,method:'POST',headers:{'Content-Type':type||'application/json','Content-Length':data.length}};console.dir([options,data]);var req=JSON.https.request(options,function(res){var buffer='';res.setEncoding('utf8');res.on('data',function(chunk){buffer+=chunk;});res.on('end',function(){try{var data=JSON.parse(buffer);callback(data,res.statusCode);}catch(e){console.log(e);}});});req.write(data);req.end();}
    };

(The get function)

benzmuircroft commented 8 years ago
    JSON.get({
        url:`https://api.sandbox.paypal.com/v1/oauth2/token?grant_type=client_credentials`
    ,   headers:{
            'Content-Type':`application/x-www-form-urlencoded`
        ,   'Accept-Language':`en_US`
        ,   'Authorization':`Basic `+`xxxxxx_client_id_xxxxxx`+`:`+`xxxxxx_client_secret_xxxxxx`
            }},function(e,a){
        console.log(e,a);

data undefined code 200

benzmuircroft commented 8 years ago

sandbox environment btw lol

benzmuircroft commented 8 years ago

I don't see the relation with cUrl and node.js. not only do I not get cUrl (I thought it was a php thing) but, also I think this looks like a pretty 'funky'-non-standard cUrl request

benzmuircroft commented 8 years ago

I mean if you go to wikipedia and look at cUrl it says

"The libcurl library is portable. It builds and works identically on many platforms, including Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, HP-UX, IRIX, AIX, Tru64, Linux, UnixWare, HURD, Windows, Symbian, AmigaOS, OS/2, BeOS, OS X, iOS, Android, Ultrix, QNX Neutrino, BlackBerry Tablet OS and BlackBerry 10,[5] OpenVMS, RISC OS, NetWare and DOS"

with no mention of node.js

but npm has some modules..

Still I havn't cule of the syntax paypal is looking for here and its just a GET request! I just need to know the correct formatting and irregardless of wether I am using cUrl or simple node.js flavored http.request it will work!

benzmuircroft commented 8 years ago

dude its here: http://stackoverflow.com/questions/28361205/how-to-retrieve-paypal-rest-api-access-token-using-node I will test this tomorrow before closing this