Skip to content

Instantly share code, notes, and snippets.

@crizstian
Created February 9, 2017 04:33
Show Gist options
  • Save crizstian/744f5e74172268b82caf67192bc611a7 to your computer and use it in GitHub Desktop.
Save crizstian/744f5e74172268b82caf67192bc611a7 to your computer and use it in GitHub Desktop.
Example of using stripe
// this the function that makes the charge, when it's done
// returns the charge object returned by stripe
const makePurchase = (payment) => {
return new Promise((resolve, reject) => {
// here we retrieve or stripe dependecy
const {stripe} = container.cradle
// we create the charge
stripe.charges.create({
amount: Math.ceil(payment.amount * 100),
currency: payment.currency,
source: {
number: payment.number,
cvc: payment.cvc,
exp_month: payment.exp_month,
exp_year: payment.exp_year
},
description: payment.description
}, (err, charge) => {
if (err && err.type === 'StripeCardError') {
reject(new Error('An error occuered procesing payment with stripe, err: ' + err))
} else {
const paid = Object.assign({}, {user: payment.userName, amount: payment.amount, charge})
resolve(paid)
}
})
})
}
// this the function that our API calls first
const registerPurchase = (payment) => {
return new Promise((resolve, reject) => {
// and here we call the function to execute stripe
makePurchase(payment)
.then(paid => {
// if every thing is succesfull, we make the registry at our db, for the record only
db.collection('payments').insertOne(paid, (err, result) => {
if (err) {
reject(new Error('an error occuered registring payment at db, err:' + err))
}
resolve(paid)
})
})
.catch(err => reject(err))
})
}
const getPurchaseById = (paymentId) => {
... more code, where we only query our database for the payment with the id
}
// more code... visit the repository to see the complete code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment