attempt to do controller function

This commit is contained in:
Arjun Patel
2019-01-29 23:47:06 -08:00
parent d6643c32f9
commit 876e77e11a
4 changed files with 133 additions and 57 deletions
+128 -52
View File
@@ -3,71 +3,147 @@ const db = require('../config/db.config.js'),
const stripe = require('stripe')('sk_test_n8NCvCFjD1xFhGiEq6SI8CXj'); const stripe = require('stripe')('sk_test_n8NCvCFjD1xFhGiEq6SI8CXj');
const { Donor, Organization } = db; const { Donor, Organization, Charge, PaymentPlan } = db;
// Subscribe user to plan or one time charge // Subscribe user to plan or one time charge
exports.grantCharge = (req, res, next) => { exports.grantCharge = (req, res, next) => {
const { stripeToken, amount, monthly } = req.body; const { stripeToken, grant_id, amount, monthly } = req.body;
const user = req.user; const user = req.user;
findStripeId(user.id) Donor.findAll({ where: { id: user.id } })
.then(stripeId => { .then(donors => {
if (!stripeId) { var { stripe_id, subscription_id } = donors[0];
return stripe.customers req.subscription_id = subscription_id;
if (!stripe_id) {
stripe_id = stripe.customers
.create({ .create({
source: stripeToken, source: stripeToken,
email: user.email email: user.email
}) })
.then(customer => { .then(customer => customer.id)
Donor.update( .catch(error =>
{ stripe_id: customer.id }, next(errorMaker(500, "Couldn't create customer with source"))
{ where: { id: user.id } } );
); }
return customer.id;
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));
}); });
} }
return stripeId;
})
.then(stripeId => {
// return stripe.charges.create({
// amount,
// currency: 'usd',
// customer: stripeId
// });s
stripe.plans
.create({
product: 'prod_CbvTFuXWh7BPJH',
currency: 'usd',
interval: 'month',
amount: 10000
})
.then();
})
.then(chargeResponse => {
res.status(200).json(chargeResponse);
}) })
.catch(error => next(error)); .catch(error => next(error));
// if (!monthly) { // try {
// const charge = stripe.charges.create({ // const donors = await Donor.findAll({ where: { id: user.id } });
// amount,
// currency: 'usd', // // check if already a stripe customer and have subscription
// description: 'One time payment for grant', // let customer_id = await (donors[0].stripe_id
// source: stripeToken, // ? donors[0].stripe_id
// statement_descriptor: 'One time payment for grant' // : .id);
// 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);
// } // }
}; };
const findStripeId = userId => {
return new Promise((resolve, reject) => {
Donor.findAll({ where: { id: userId } })
.then(donors => {
// add stripe id for
// if (donors[0]) {
// return next(errorMaker(409, `Email Exists: ${req.body.email}`));
// }
resolve(donors[0].stripe_id);
})
.catch(error => reject(error));
});
};
+4
View File
@@ -9,6 +9,10 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.UUID, type: DataTypes.UUID,
defaultValue: null defaultValue: null
}, },
subscription_id: {
type: DataTypes.UUID,
defaultValue: null
},
first_name: { first_name: {
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false allowNull: false
-4
View File
@@ -10,10 +10,6 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.UUID, type: DataTypes.UUID,
allowNull: false allowNull: false
}, },
subscription_id: {
type: DataTypes.UUID,
allowNull: false
},
amount: { amount: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false allowNull: false
+1 -1
View File
@@ -37,7 +37,7 @@ db.sequelize
}); });
//force: true will drop the table if it already exists //force: true will drop the table if it already exists
const drop_tables = true; const drop_tables = false;
db.sequelize.sync({ force: drop_tables }).then(() => { db.sequelize.sync({ force: drop_tables }).then(() => {
console.log(`Drop and Resync with { force: ${drop_tables} }`); console.log(`Drop and Resync with { force: ${drop_tables} }`);
}); });