-
-
Save wordyallen/5182d1b92f422c2c66c1d9b5805fdf0a to your computer and use it in GitHub Desktop.
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 saveCard = ( {body:{source, amount}, user:{email, _id}}, res ) => | |
getBundle(amount) ? | |
customers.create({email, source, metadata: {_id:_id.toString()}}) | |
.then(writeCustomerId ) | |
.then(createCharge(amount,_id)) | |
.then(grantCredits) | |
.then(payload=>res.json(payload) ) | |
.catch(e=> res.status(400).send(e)) | |
: res.status(400).send('Product not found') | |
const bundles = [ | |
{category: 'Beginner', price: 1000, credits:10}, | |
{category: 'Reasonable', price: 2000, credits:20}, | |
{category: 'Beginner', price: 4000, credits:100}, | |
] | |
const getBundle = amt => bundles.find( ({price}) => price === +amt ) | |
const writeCustomerId = ({id, metadata:{_id}}) => | |
User.findOneAndUpdate({_id}, {customer:id}, {upsert:1, safe: 1, new: 1}) | |
const createCharge = (amount,_id) => ({customer}) => | |
charges.create({amount, customer, currency:'usd', metadata:{_id:_id.toString()}}) | |
const grantCredits = charge => User.findOneAndUpdate( | |
{_id: charge.metadata._id}, | |
{credits: getBundle(charge.amount).credits}, | |
{upsert:1, safe: 1,new:1}) | |
.then(mergePayloads(charge)) | |
const mergePayloads = charge => model => Object.assign( charge, model.toObject()) |
Author
wordyallen
commented
Feb 28, 2017
•
- check product
- create customer
- write customer id in the local db
- create charge
- grant credits to user
- merge stripe and db payload
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment