working charnges from the frontend
This commit is contained in:
@@ -8,10 +8,19 @@ const { Grant, Cause, Region, Organization, sequelize } = db;
|
|||||||
// Create a grant for certain donor
|
// Create a grant for certain donor
|
||||||
exports.create = (req, res, next) => {
|
exports.create = (req, res, next) => {
|
||||||
const donor_id = req.user.id;
|
const donor_id = req.user.id;
|
||||||
const { name, monthly, causes, regions, organizations } = req.body;
|
const {
|
||||||
|
name,
|
||||||
|
monthly,
|
||||||
|
causes,
|
||||||
|
regions,
|
||||||
|
organizations,
|
||||||
|
amount,
|
||||||
|
stripeToken
|
||||||
|
} = req.body;
|
||||||
|
|
||||||
var total_amount = organizations.map((org) => org.amount).reduce((partial_sum, a) => partial_sum + a);
|
if (!stripeToken) {
|
||||||
var total_amount = total_amount * 100;
|
throw errorMaker(400, 'No stripe token given');
|
||||||
|
}
|
||||||
|
|
||||||
return sequelize
|
return sequelize
|
||||||
.transaction(function(t) {
|
.transaction(function(t) {
|
||||||
@@ -19,7 +28,7 @@ exports.create = (req, res, next) => {
|
|||||||
{
|
{
|
||||||
donor_id,
|
donor_id,
|
||||||
name,
|
name,
|
||||||
amount: total_amount,
|
amount,
|
||||||
monthly,
|
monthly,
|
||||||
num_causes: causes.length,
|
num_causes: causes.length,
|
||||||
num_regions: regions.length
|
num_regions: regions.length
|
||||||
@@ -42,10 +51,11 @@ exports.create = (req, res, next) => {
|
|||||||
})
|
})
|
||||||
.then(function(grants) {
|
.then(function(grants) {
|
||||||
// transaction committed
|
// transaction committed
|
||||||
stripe.grantCharge({grant: grants.dataValues, total_amount}, req, res);
|
stripe.grantCharge({ grant: grants.dataValues }, req, res);
|
||||||
return null;
|
return null;
|
||||||
})
|
})
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
|
console.log(error);
|
||||||
// transaction rollback
|
// transaction rollback
|
||||||
next(error);
|
next(error);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ const { Donor, Charge, PaymentPlan } = db;
|
|||||||
|
|
||||||
// Subscribe user to plan or one time charge
|
// Subscribe user to plan or one time charge
|
||||||
exports.grantCharge = async (data, req, res) => {
|
exports.grantCharge = async (data, req, res) => {
|
||||||
const { stripeToken, organizations, monthly } = req.body;
|
const { stripeToken, organizations, monthly, amount } = req.body;
|
||||||
const { total_amount, grant } = await data; // passed from grant controller
|
const { grant } = await data; // passed from grant controller
|
||||||
const grant_id = await grant.id;
|
const grant_id = await grant.id;
|
||||||
|
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
@@ -34,84 +34,35 @@ exports.grantCharge = async (data, req, res) => {
|
|||||||
|
|
||||||
var result;
|
var result;
|
||||||
|
|
||||||
// charge if not monthly, plan if it is
|
// charge if yes or no monthly
|
||||||
if (!monthly) {
|
let charge = await stripe.charges.create({
|
||||||
let charge = await stripe.charges.create({
|
amount,
|
||||||
amount: total_amount,
|
currency: 'usd',
|
||||||
|
source: 'tok_visa',
|
||||||
|
description: 'One time payment for grant',
|
||||||
|
statement_descriptor: 'one-time-grant'
|
||||||
|
});
|
||||||
|
|
||||||
|
let transfers = await organizations.map(async org => {
|
||||||
|
org.amount = org.amount * 100;
|
||||||
|
const applicationStripeFee = org.amount * 0.05;
|
||||||
|
|
||||||
|
let t = await stripe.transfers.create({
|
||||||
|
amount: org.amount - applicationStripeFee,
|
||||||
currency: 'usd',
|
currency: 'usd',
|
||||||
source: 'tok_visa',
|
source_transaction: charge.id,
|
||||||
description: 'One time payment for grant',
|
destination: 'acct_1EAxzUIVW1uo07uH'
|
||||||
statement_descriptor: 'one-time-grant'
|
|
||||||
});
|
});
|
||||||
|
return t;
|
||||||
|
});
|
||||||
|
|
||||||
let transfers = await organizations.map(async (org) => {
|
await Charge.create({
|
||||||
org.amount = org.amount * 100;
|
id: charge.id,
|
||||||
const applicationStripeFee = org.amount * 0.05;
|
amount,
|
||||||
|
description: 'One time payment for grant'
|
||||||
|
});
|
||||||
|
|
||||||
let t = await stripe.transfers.create({
|
result = charge;
|
||||||
amount: org.amount - applicationStripeFee,
|
|
||||||
currency: 'usd',
|
|
||||||
source_transaction: charge.id,
|
|
||||||
destination: 'acct_1EAxzUIVW1uo07uH'
|
|
||||||
});
|
|
||||||
return t;
|
|
||||||
});
|
|
||||||
|
|
||||||
await Charge.create({
|
|
||||||
id: charge.id,
|
|
||||||
amount: total_amount,
|
|
||||||
description: 'One time payment for grant'
|
|
||||||
});
|
|
||||||
|
|
||||||
result = charge;
|
|
||||||
} else {
|
|
||||||
const plan = await stripe.plans.create({
|
|
||||||
nickname: `Plan for grant: ${grant_id}`,
|
|
||||||
product: product_id,
|
|
||||||
currency: 'usd',
|
|
||||||
interval: 'month',
|
|
||||||
amount
|
|
||||||
});
|
|
||||||
|
|
||||||
// either create new sub with new plan, or append plan to existing sub
|
|
||||||
if (!subscription_id) {
|
|
||||||
const currDate = new Date();
|
|
||||||
const unixFirstNextMonth = Math.round(
|
|
||||||
new Date(
|
|
||||||
currDate.getFullYear(),
|
|
||||||
currDate.getMonth() + 1,
|
|
||||||
1
|
|
||||||
).getTime() / 1000
|
|
||||||
);
|
|
||||||
|
|
||||||
let subscription = await stripe.subscriptions.create({
|
|
||||||
customer: stripe_id,
|
|
||||||
items: [{ plan: default_plan_id }],
|
|
||||||
billing_cycle_anchor: unixFirstNextMonth,
|
|
||||||
trial_end: unixFirstNextMonth
|
|
||||||
});
|
|
||||||
|
|
||||||
subscription_id = subscription.id;
|
|
||||||
|
|
||||||
await Donor.update(
|
|
||||||
{ subscription_id: subscription.id },
|
|
||||||
{ where: { id: user.id } }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const sub_item = await stripe.subscriptionItems.create({
|
|
||||||
subscription: subscription_id,
|
|
||||||
plan: plan.id
|
|
||||||
});
|
|
||||||
|
|
||||||
result = await PaymentPlan.create({
|
|
||||||
plan_id: plan.id,
|
|
||||||
amount,
|
|
||||||
grant_id,
|
|
||||||
subscription_id,
|
|
||||||
sub_item_id: sub_item.id
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
message: 'Successfully charged or subscribed',
|
message: 'Successfully charged or subscribed',
|
||||||
|
|||||||
Reference in New Issue
Block a user