Skip to content

Instantly share code, notes, and snippets.

@ramses-lopez
Created August 21, 2017 02:21
Show Gist options
  • Save ramses-lopez/8bd07b32ce48fc65ef57c58d28637561 to your computer and use it in GitHub Desktop.
Save ramses-lopez/8bd07b32ce48fc65ef57c58d28637561 to your computer and use it in GitHub Desktop.
Node -Paypal Payment
const c = require('chalk')
const fetch = require('node-fetch')
const inspect = require('util').inspect
const l = str => console.log(inspect( str, { depth: 'Infinite', colors: true }))
/*
Client ID
AXVggvfsNB7SfW_gZNZ7TenVbkW5pylY9s9W3U6edvw5JTRK7TvazrFSUnFc4bzW4xUgihfZCaPHhctH
Secret
EAN4vZfE1UYAvc6Xne70swlhlH40EGx-n_ThjikMwZTfqqOQw9NdJDRvul1GJPw2FZhJ3HZRAO5LhNmH
*/
/*
- get access token
curl -v 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"
https://stackoverflow.com/questions/20803339/basic-authorization-command-for-curl
https://developer.paypal.com/docs/integration/direct/make-your-first-call/
https://developer.paypal.com/docs/integration/direct/payments/credit-card-payments/
*/
async function getAccessToken(){
const clientID = "AXVggvfsNB7SfW_gZNZ7TenVbkW5pylY9s9W3U6edvw5JTRK7TvazrFSUnFc4bzW4xUgihfZCaPHhctH"
const secret = "EAN4vZfE1UYAvc6Xne70swlhlH40EGx-n_ThjikMwZTfqqOQw9NdJDRvul1GJPw2FZhJ3HZRAO5LhNmH"
const authBase64 = new Buffer(`${clientID}:${secret}`).toString('base64')
const response = await fetch('https://api.sandbox.paypal.com/v1/oauth2/token?grant_type=client_credentials', {
method: "POST",
headers: {
"Accept": "application/json",
"Accept-Language": "en_US",
"Authorization": `Basic ${authBase64}`
},
body: {'grant_type':'client_credentials'}
})
if(response.ok){
console.log(c.clue('access_token received'));
const result = await response.json()
return result.access_token
}
else {
console.error('Auth error')
}
}
async function makePayment(token){
/*
// curl -v https://api.sandbox.paypal.com/v1/payments/payment \
// -H "Content-Type:application/json" \
// -H "Authorization: Bearer Access-Token" \
*/
const paymentObject = {
'intent': "sale",
"payer": {
"payment_method": "credit_card",
"funding_instruments": [{
"credit_card": {
"number": "4032032647673208",
"type": "visa",
"expire_month": 8,
"expire_year": 2022,
"cvv2": "874",
"first_name": "Ramses",
"last_name": "Lopez",
"billing_address": {
"line1": "111 First Street",
"city": "Saratoga",
"state": "CA",
"postal_code": "95070",
"country_code": "US"
}
}
}]
},
"transactions": [{
"amount": {
"total": "7.47",
"currency": "USD",
"details": {
"subtotal": "7.41",
"tax": "0.03",
"shipping": "0.03"
}
},
"description": "The payment transaction description."
}]
}
console.log(c.blue('sending payment rq'))
const response = await fetch('https://api.sandbox.paypal.com/v1/payments/payment', {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify(paymentObject)
})
if(response.ok){
l('payment response received')
const result = await response.json()
l(result)
}
else {
console.log(c.red('response not OK!'), response.status, response.statusText);
}
}
async function init(){
console.log(c.blue('============================================='));
try {
const token = await getAccessToken()
makePayment(token)
} catch (e) {
console.error(e);
}
}
init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment