working async create payments/plans/subscrptions
- need to handle edge cases
This commit is contained in:
@@ -6,144 +6,210 @@ const stripe = require('stripe')('sk_test_n8NCvCFjD1xFhGiEq6SI8CXj');
|
|||||||
const { Donor, Organization, Charge, PaymentPlan } = 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 = async (req, res, next) => {
|
||||||
const { stripeToken, grant_id, amount, monthly } = req.body;
|
const { stripeToken, grant_id, amount, monthly } = req.body;
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
|
|
||||||
Donor.findAll({ where: { id: user.id } })
|
// Donor.findAll({ where: { id: user.id } })
|
||||||
.then(donors => {
|
// .then(donors => {
|
||||||
var { stripe_id, subscription_id } = donors[0];
|
// var { stripe_id, subscription_id } = donors[0];
|
||||||
req.subscription_id = subscription_id;
|
// req.subscription_id = subscription_id;
|
||||||
if (!stripe_id) {
|
// if (!stripe_id) {
|
||||||
stripe_id = stripe.customers
|
// stripe_id = stripe.customers
|
||||||
.create({
|
// .create({
|
||||||
source: stripeToken,
|
// source: stripeToken,
|
||||||
email: user.email
|
// email: user.email
|
||||||
})
|
// })
|
||||||
.then(customer => customer.id)
|
// .then(customer => customer.id)
|
||||||
.catch(error =>
|
// .catch(error =>
|
||||||
next(errorMaker(500, "Couldn't create customer with source"))
|
// next(errorMaker(500, "Couldn't create customer with source"))
|
||||||
);
|
// );
|
||||||
}
|
// }
|
||||||
|
|
||||||
return [stripe_id, subscription_id];
|
// return [stripe_id, subscription_id];
|
||||||
})
|
// })
|
||||||
.then(([stripe_id, subscription_id]) => {
|
// .then(([stripe_id, subscription_id]) => {
|
||||||
console.log(stripe_id, subscription_id);
|
// console.log(stripe_id, subscription_id);
|
||||||
if (!monthly) {
|
// if (!monthly) {
|
||||||
stripe.charges
|
// stripe.charges
|
||||||
.create({
|
// .create({
|
||||||
amount,
|
// amount,
|
||||||
currency: 'usd',
|
// currency: 'usd',
|
||||||
customer: stripe_id,
|
// customer: stripe_id,
|
||||||
description: 'One time payment for grant',
|
// description: 'One time payment for grant',
|
||||||
statement_descriptor: 'One time payment for grant'
|
// statement_descriptor: 'One time payment for grant'
|
||||||
})
|
// })
|
||||||
.then(chargeResponse => res.status(200).json(chargeResponse))
|
// .then(chargeResponse => res.status(200).json(chargeResponse))
|
||||||
.catch(error => next(error));
|
// .catch(error => next(error));
|
||||||
} else {
|
// } else {
|
||||||
stripe.plans
|
// stripe.plans
|
||||||
.create({
|
// .create({
|
||||||
id: grant_id,
|
// id: grant_id,
|
||||||
nickname: `Grant for user: ${user.id}`,
|
// nickname: `Grant for user: ${user.id}`,
|
||||||
product: 'prod_ER3jOog6QMX1GP',
|
// product: 'prod_ER3jOog6QMX1GP',
|
||||||
currency: 'usd',
|
// currency: 'usd',
|
||||||
interval: 'month',
|
// interval: 'month',
|
||||||
amount
|
// amount
|
||||||
})
|
// })
|
||||||
.then(plan => {
|
// .then(plan => {
|
||||||
if (!req.subscription_id) {
|
// if (!req.subscription_id) {
|
||||||
stripe.subscriptions
|
// stripe.subscriptions
|
||||||
.create({
|
// .create({
|
||||||
customer: stripe_id,
|
// customer: stripe_id,
|
||||||
items: [{ plan: plan.id }]
|
// items: [{ plan: plan.id }]
|
||||||
})
|
// })
|
||||||
.then(sub =>
|
// .then(sub =>
|
||||||
stripe.subscriptionItems
|
// stripe.subscriptionItems
|
||||||
.create({
|
// .create({
|
||||||
subscription: sub.id,
|
// subscription: sub.id,
|
||||||
plan: plan.id
|
// plan: plan.id
|
||||||
})
|
// })
|
||||||
.catch(error => next(error))
|
// .catch(error => next(error))
|
||||||
)
|
// )
|
||||||
.catch(error => next(error));
|
// .catch(error => next(error));
|
||||||
} else {
|
// } else {
|
||||||
stripe.subscriptionItems
|
// stripe.subscriptionItems
|
||||||
.create({
|
// .create({
|
||||||
subscription: req.subscription_id,
|
// subscription: req.subscription_id,
|
||||||
plan: plan.id
|
// plan: plan.id
|
||||||
})
|
// })
|
||||||
.catch(error => next(error));
|
// .catch(error => next(error));
|
||||||
}
|
// }
|
||||||
|
|
||||||
PaymentPlan.create({
|
// PaymentPlan.create({
|
||||||
plan_id: plan.id,
|
// plan_id: plan.id,
|
||||||
amount,
|
// amount,
|
||||||
grant_id: grant_id
|
// grant_id: grant_id
|
||||||
}).catch(error => next(error));
|
// }).catch(error => next(error));
|
||||||
});
|
// });
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
.catch(error => next(error));
|
// .catch(error => next(error));
|
||||||
// try {
|
try {
|
||||||
// const donors = await Donor.findAll({ where: { id: user.id } });
|
const donors = await Donor.findAll({ where: { id: user.id } });
|
||||||
|
|
||||||
// // check if already a stripe customer and have subscription
|
// check if already a stripe customer
|
||||||
// let customer_id = await (donors[0].stripe_id
|
let { stripe_id, subscription_id } = donors[0];
|
||||||
// ? donors[0].stripe_id
|
|
||||||
// : .id);
|
|
||||||
|
|
||||||
// let subscription_id = await (donors[0].subscription_id
|
if (!stripe_id) {
|
||||||
// ? donors[0].subscription_id
|
const customer = await stripe.customers.create({
|
||||||
// : await stripe.subscriptions.create({
|
source: stripeToken,
|
||||||
// customer: customer_id,
|
email: user.email
|
||||||
// items: [{ plan: 'Grant' }]
|
});
|
||||||
// }).id);
|
stripe_id = customer.id;
|
||||||
|
|
||||||
// console.log(await customer_id);
|
await Donor.update({ stripe_id }, { where: { id: user.id } });
|
||||||
// await Donor.update(
|
}
|
||||||
// { stripe_id: customer_id, subscription_id },
|
|
||||||
// { where: { id: user.id } }
|
|
||||||
// );
|
|
||||||
|
|
||||||
// // charge if not monthly, plan if it is
|
var result;
|
||||||
// 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({
|
// charge if not monthly, plan if it is
|
||||||
// message: 'Successfully charged or subscribed'
|
if (!monthly) {
|
||||||
// });
|
let charge = await stripe.charges.create({
|
||||||
// } catch (error) {
|
amount,
|
||||||
// next(error);
|
currency: 'usd',
|
||||||
// }
|
customer: stripe_id,
|
||||||
|
description: 'One time payment for grant',
|
||||||
|
statement_descriptor: 'one-time-grant'
|
||||||
|
});
|
||||||
|
|
||||||
|
await Charge.create({
|
||||||
|
id: charge.id,
|
||||||
|
amount,
|
||||||
|
description: 'One time payment for grant'
|
||||||
|
});
|
||||||
|
|
||||||
|
result = charge;
|
||||||
|
} else {
|
||||||
|
const plan = await stripe.plans.create({
|
||||||
|
id: grant_id,
|
||||||
|
nickname: `Grant for grant: ${grant_id}`,
|
||||||
|
product: 'prod_ER3jOog6QMX1GP',
|
||||||
|
currency: 'usd',
|
||||||
|
interval: 'month',
|
||||||
|
amount
|
||||||
|
});
|
||||||
|
|
||||||
|
// either create new sub with new plan, or append plan to existing sub
|
||||||
|
if (!subscription_id) {
|
||||||
|
let subscription = await stripe.subscriptions.create({
|
||||||
|
customer: stripe_id,
|
||||||
|
items: [{ plan: plan.id }]
|
||||||
|
});
|
||||||
|
|
||||||
|
await Donor.update(
|
||||||
|
{ subscription_id: subscription.id },
|
||||||
|
{ where: { id: user.id } }
|
||||||
|
);
|
||||||
|
} else
|
||||||
|
await stripe.subscriptionItems.create({
|
||||||
|
subscription: subscription_id,
|
||||||
|
plan: plan.id
|
||||||
|
});
|
||||||
|
|
||||||
|
result = await PaymentPlan.create({
|
||||||
|
plan_id: plan.id,
|
||||||
|
amount,
|
||||||
|
grant_id: grant_id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
autoIncrement: true
|
autoIncrement: true
|
||||||
},
|
},
|
||||||
plan_id: {
|
plan_id: {
|
||||||
|
unique: true,
|
||||||
type: DataTypes.UUID,
|
type: DataTypes.UUID,
|
||||||
allowNull: false
|
allowNull: false
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user