Skip to content

Instantly share code, notes, and snippets.

@brandonros
Last active July 6, 2019 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonros/6a958a933b91f8812481ff7099f734f3 to your computer and use it in GitHub Desktop.
Save brandonros/6a958a933b91f8812481ff7099f734f3 to your computer and use it in GitHub Desktop.
Easiest node.js Stripe API implementation I could come up with
const rp = require('request-promise-native')
const apiRequest = (uri, form) => {
return rp.post({
uri,
form,
json: true,
headers: {
authorization: `Basic ${Buffer.from(`${process.env.STRIPE_API_KEY}:`).toString('base64')}`
}
})
}
const createCustomerWithCard = async (email, number, expMonth, expYear, cvc) => {
const token = await apiRequest('https://api.stripe.com/v1/tokens', {
card: {
number,
exp_month: expMonth,
exp_year: expYear,
cvc
}
})
return apiRequest('https://api.stripe.com/v1/customers', {
email,
source: token.id
})
}
const createCharge = (amount, currency, customer, source) => {
return apiRequest('https://api.stripe.com/v1/charges', {
amount,
currency,
customer,
source
})
}
module.exports = {
createCustomerWithCard,
createCharge
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment