got delete and create grants all in two routes
This commit is contained in:
@@ -35,8 +35,8 @@ exports.create = (req, res, next) => {
|
|||||||
// message: 'Grant Created'
|
// message: 'Grant Created'
|
||||||
// });
|
// });
|
||||||
|
|
||||||
req.grant = grants.dataValues;
|
next(grants.dataValues);
|
||||||
next();
|
return null;
|
||||||
})
|
})
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
// transaction rollback
|
// transaction rollback
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ const stripe = require('stripe')('sk_test_n8NCvCFjD1xFhGiEq6SI8CXj');
|
|||||||
const { Donor, Charge, PaymentPlan } = db;
|
const { Donor, Charge, PaymentPlan } = db;
|
||||||
|
|
||||||
// Subscribe user to plan or one time charge
|
// Subscribe user to plan or one time charge
|
||||||
exports.grantCharge = async (req, res, next) => {
|
exports.grantCharge = async (grant, req, res, next) => {
|
||||||
const { stripeToken, monthly } = req.body;
|
const { stripeToken, monthly } = req.body;
|
||||||
const { grant_id } = req.grant;
|
const grant_id = await grant.id;
|
||||||
|
|
||||||
const amount = req.body.amount * 100; //stripe standards
|
const amount = req.body.amount * 100; //stripe standards
|
||||||
|
|
||||||
@@ -54,7 +54,6 @@ exports.grantCharge = async (req, res, next) => {
|
|||||||
result = charge;
|
result = charge;
|
||||||
} else {
|
} else {
|
||||||
const plan = await stripe.plans.create({
|
const plan = await stripe.plans.create({
|
||||||
id: grant_id,
|
|
||||||
nickname: `Plan for grant: ${grant_id}`,
|
nickname: `Plan for grant: ${grant_id}`,
|
||||||
product: product_id,
|
product: product_id,
|
||||||
currency: 'usd',
|
currency: 'usd',
|
||||||
@@ -109,19 +108,19 @@ exports.deleteGrant = async (req, res, next) => {
|
|||||||
|
|
||||||
// check if there are plans for that grant
|
// check if there are plans for that grant
|
||||||
if (!paymentPlans.length) {
|
if (!paymentPlans.length) {
|
||||||
res.status(400).json({
|
return res.status(400).json({
|
||||||
message: 'Not a valid grant'
|
message: 'Not a valid grant'
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const plan = paymentPlans[0];
|
const plan = paymentPlans[0];
|
||||||
|
|
||||||
|
//delete the plan/grant under the subscription for the user
|
||||||
await stripe.subscriptionItems.del(plan.sub_item_id);
|
await stripe.subscriptionItems.del(plan.sub_item_id);
|
||||||
|
//delete the plan from the main product 'Grants'
|
||||||
res.status(201).json({
|
await stripe.plans.del(plan.plan_id);
|
||||||
message: 'Removed monthly subscription plan',
|
|
||||||
plan
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ router.post(
|
|||||||
controllers.stripe.grantCharge
|
controllers.stripe.grantCharge
|
||||||
);
|
);
|
||||||
|
|
||||||
// DELECT a grant of a donor
|
// DELETE a grant of a donor
|
||||||
router.delete(
|
router.delete(
|
||||||
'/grants/',
|
'/grants/',
|
||||||
checkAuth(roles.DONOR),
|
checkAuth(roles.DONOR),
|
||||||
|
|||||||
Reference in New Issue
Block a user