Last active
July 6, 2019 21:25
-
-
Save brandonros/6a958a933b91f8812481ff7099f734f3 to your computer and use it in GitHub Desktop.
Easiest node.js Stripe API implementation I could come up with
This file contains hidden or 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 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