Skip to content

Instantly share code, notes, and snippets.

@romaad
Created July 29, 2020 23:09
Show Gist options
  • Save romaad/cc588abf691ba1e29c4a853983de8eb1 to your computer and use it in GitHub Desktop.
Save romaad/cc588abf691ba1e29c4a853983de8eb1 to your computer and use it in GitHub Desktop.
Paypal gettoken with axios
const SANDBOX_URL = "https://api.sandbox.paypal.com";
export class PayPalInterface {
async getToken() {
if (!this._token ||
this._token.expires_in + this._token.created >= new Date().getTime()) {
const url = SANDBOX_URL + '/v1/oauth2/token'
const headers = {
'Accept': 'application/json',
'Accept-Language': 'en_US',
'content-type': 'application/x-www-form-urlencoded',
}
const auth = {
'username': process.env.PAYPAL_CLIENT_ID,
'password': process.env.PAYPAL_SECRET
}
try {
const resp = await Axios.post(url,
//you need this line to format the body in the expected way
// for 'application/x-www-form-urlencoded'
QS.stringify({'grant_type': 'client_credentials'}),
{ headers, auth }
);
this._token = resp.data;
console.log(this._token);
this._token.created = new Date().getTime();
} catch (e) {
console.log(e)
}
}
return this._token;
}
}
@sahilrajput03
Copy link

Thanks man. You saved me.! :) <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment