From 53c0c9500660b124f5d484f6490d2685533919e2 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Wed, 30 Jan 2019 12:50:05 -0800 Subject: [PATCH] cleaning out the controller --- app/controllers/stripe.controller.js | 131 +-------------------------- 1 file changed, 3 insertions(+), 128 deletions(-) diff --git a/app/controllers/stripe.controller.js b/app/controllers/stripe.controller.js index 886fea1..1c91d5f 100644 --- a/app/controllers/stripe.controller.js +++ b/app/controllers/stripe.controller.js @@ -3,100 +3,26 @@ const db = require('../config/db.config.js'), const stripe = require('stripe')('sk_test_n8NCvCFjD1xFhGiEq6SI8CXj'); -const { Donor, Organization, Charge, PaymentPlan } = db; +const { Donor, Charge, PaymentPlan } = db; // Subscribe user to plan or one time charge exports.grantCharge = async (req, res, next) => { const { stripeToken, grant_id, amount, monthly } = req.body; const user = req.user; - // Donor.findAll({ where: { id: user.id } }) - // .then(donors => { - // var { stripe_id, subscription_id } = donors[0]; - // req.subscription_id = subscription_id; - // if (!stripe_id) { - // stripe_id = stripe.customers - // .create({ - // source: stripeToken, - // email: user.email - // }) - // .then(customer => customer.id) - // .catch(error => - // next(errorMaker(500, "Couldn't create customer with source")) - // ); - // } - - // return [stripe_id, subscription_id]; - // }) - // .then(([stripe_id, subscription_id]) => { - // console.log(stripe_id, subscription_id); - // if (!monthly) { - // stripe.charges - // .create({ - // amount, - // currency: 'usd', - // customer: stripe_id, - // description: 'One time payment for grant', - // statement_descriptor: 'One time payment for grant' - // }) - // .then(chargeResponse => res.status(200).json(chargeResponse)) - // .catch(error => next(error)); - // } else { - // stripe.plans - // .create({ - // id: grant_id, - // nickname: `Grant for user: ${user.id}`, - // product: 'prod_ER3jOog6QMX1GP', - // currency: 'usd', - // interval: 'month', - // amount - // }) - // .then(plan => { - // if (!req.subscription_id) { - // stripe.subscriptions - // .create({ - // customer: stripe_id, - // items: [{ plan: plan.id }] - // }) - // .then(sub => - // stripe.subscriptionItems - // .create({ - // subscription: sub.id, - // plan: plan.id - // }) - // .catch(error => next(error)) - // ) - // .catch(error => next(error)); - // } else { - // stripe.subscriptionItems - // .create({ - // subscription: req.subscription_id, - // plan: plan.id - // }) - // .catch(error => next(error)); - // } - - // PaymentPlan.create({ - // plan_id: plan.id, - // amount, - // grant_id: grant_id - // }).catch(error => next(error)); - // }); - // } - // }) - // .catch(error => next(error)); try { const donors = await Donor.findAll({ where: { id: user.id } }); // check if already a stripe customer let { stripe_id, subscription_id } = donors[0]; + console.log(stripeToken); if (!stripe_id) { const customer = await stripe.customers.create({ source: stripeToken, email: user.email }); - stripe_id = customer.id; + stripe_id = await customer.id; await Donor.update({ stripe_id }, { where: { id: user.id } }); } @@ -158,57 +84,6 @@ exports.grantCharge = async (req, res, next) => { message: 'Successfully charged or subscribed', result }); - - // let subscription_id = await (donors[0].subscription_id - // ? donors[0].subscription_id - // : await stripe.subscriptions.create({ - // customer: customer_id, - // items: [{ plan: 'Grant' }] - // }).id); - - // console.log(await customer_id); - // await Donor.update( - // { stripe_id: customer_id, subscription_id }, - // { where: { id: user.id } } - // ); - - // // charge if not monthly, plan if it is - // if (!monthly) { - // let charge = await stripe.charges.create({ - // amount, - // currency: 'usd', - // customer: customer_id, - // description: 'One time payment for grant', - // statement_descriptor: 'One time payment for grant' - // }); - // await Charge.create({ - // id: charge.id, - // amount, - // description: 'One time payment for grant' - // }); - // } else { - // const plan = await stripe.plans.create({ - // id: grant_id, - // nickname: `Grant for user: ${user.id}`, - // product: 'prod_ER3jOog6QMX1GP', - // currency: 'usd', - // interval: 'month', - // amount - // }); - // await PaymentPlan.create({ - // plan_id: plan.id, - // amount, - // grant_id: grant_id - // }); - // await stripe.subscriptionItems.create({ - // subscription: subscription_id, - // plan: plan.id - // }); - // } - - // res.status(200).json({ - // message: 'Successfully charged or subscribed' - // }); } catch (error) { next(error); }