Created
July 29, 2020 23:09
-
-
Save romaad/cc588abf691ba1e29c4a853983de8eb1 to your computer and use it in GitHub Desktop.
Paypal gettoken with axios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks man. You saved me.! :) <3