properly sending payments/charges
This commit is contained in:
@@ -1,33 +1,33 @@
|
|||||||
const db = require("../../models"),
|
const db = require('../../models'),
|
||||||
errorMaker = require("../helpers/error.maker");
|
errorMaker = require('../helpers/error.maker');
|
||||||
|
|
||||||
const { Charge, sequelize } = db;
|
const { Charge, sequelize } = db;
|
||||||
|
|
||||||
// Find grants with causes, regions, and organizations by donor_id
|
// Find grants with causes, regions, and organizations by donor_id
|
||||||
exports.getAllCharges = async (req, res, next) => {
|
exports.getAllChargesbyDonor = async (req, res, next) => {
|
||||||
const donor_id = req.user.id;
|
const donor_id = req.user.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const charges = await db.sequelize.query(
|
const charges = await db.sequelize.query(
|
||||||
`
|
`
|
||||||
SELECT
|
SELECT
|
||||||
g.name,
|
g.name,
|
||||||
g.monthly,
|
g.monthly,
|
||||||
g.num_causes,
|
c.description,
|
||||||
g.num_regions,
|
|
||||||
c.amount,
|
c.amount,
|
||||||
|
c.payment_status,
|
||||||
c.created_at
|
c.created_at
|
||||||
FROM grants as g
|
FROM grants as g
|
||||||
INNER JOIN charges as c on c.grant_id = g.id
|
INNER JOIN charges as c on c.grant_id = g.id
|
||||||
WHERE g.donor_id = :donor_id;
|
WHERE g.donor_id = :donor_id;
|
||||||
`,
|
`,
|
||||||
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
|
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
|
||||||
);
|
);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
charges
|
charges
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+127
-123
@@ -1,115 +1,117 @@
|
|||||||
const db = require("../../models"),
|
const db = require('../../models'),
|
||||||
errorMaker = require("../helpers/error.maker");
|
errorMaker = require('../helpers/error.maker');
|
||||||
|
|
||||||
const stripe = require("./stripe.controller");
|
const stripe = require('./stripe.controller');
|
||||||
const sendgrid = require("./sendgrid.controller");
|
const sendgrid = require('./sendgrid.controller');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
Grant,
|
Grant,
|
||||||
Charge,
|
Charge,
|
||||||
Cause,
|
Cause,
|
||||||
Region,
|
Region,
|
||||||
Organization,
|
Organization,
|
||||||
GrantOrganization,
|
GrantOrganization,
|
||||||
Project,
|
Project,
|
||||||
sequelize
|
sequelize
|
||||||
} = db;
|
} = db;
|
||||||
// Create a grant for certain donor
|
// Create a grant for certain donor
|
||||||
exports.createGrant = async (req, res, next) => {
|
exports.createGrant = async (req, res, next) => {
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
|
|
||||||
var { name, monthly, organizations, amount, stripeToken } = req.body;
|
var { name, monthly, organizations, amount, stripeToken } = req.body;
|
||||||
|
|
||||||
const causes = organizations.map(org => org.primary_cause);
|
const causes = organizations.map(org => org.primary_cause);
|
||||||
const regions = organizations.map(org => org.primary_region);
|
const regions = organizations.map(org => org.primary_region);
|
||||||
|
|
||||||
actual_total = Math.round(amount * 100) / 100;
|
actual_total = Math.round(amount * 100) / 100;
|
||||||
|
|
||||||
if (!stripeToken) {
|
if (!stripeToken) {
|
||||||
throw errorMaker(400, "No stripe token given");
|
throw errorMaker(400, 'No stripe token given');
|
||||||
}
|
}
|
||||||
|
|
||||||
let transaction;
|
let transaction;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// get transaction
|
// get transaction
|
||||||
transaction = await sequelize.transaction();
|
transaction = await sequelize.transaction();
|
||||||
|
|
||||||
const grant = await Grant.create(
|
const grant = await Grant.create(
|
||||||
{
|
{
|
||||||
donor_id: user.id,
|
donor_id: user.id,
|
||||||
name,
|
name,
|
||||||
amount: actual_total,
|
amount: actual_total,
|
||||||
monthly,
|
monthly,
|
||||||
num_causes: causes.length,
|
num_causes: causes.length,
|
||||||
num_regions: regions.length
|
num_regions: regions.length
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
transaction
|
transaction
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const grantsOrgs = organizations.map(org => {
|
const grantsOrgs = organizations.map(org => {
|
||||||
return {
|
return {
|
||||||
grant_id: grant.id,
|
grant_id: grant.id,
|
||||||
organization_id: org.id,
|
organization_id: org.id,
|
||||||
amount: org.amount
|
amount: org.amount
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// mapping between bundle and orgs
|
// mapping between bundle and orgs
|
||||||
await GrantOrganization.bulkCreate(grantsOrgs, {
|
await GrantOrganization.bulkCreate(grantsOrgs, {
|
||||||
transaction
|
transaction
|
||||||
});
|
});
|
||||||
|
|
||||||
// one time charge
|
// one time charge
|
||||||
const charge = await stripe.grantCharge({
|
const charge = await stripe.grantCharge({
|
||||||
grant,
|
grant,
|
||||||
stripeToken,
|
stripeToken,
|
||||||
stripeToken,
|
stripeToken,
|
||||||
organizations,
|
organizations,
|
||||||
monthly,
|
monthly,
|
||||||
amount,
|
amount,
|
||||||
user
|
user
|
||||||
});
|
});
|
||||||
|
|
||||||
await Charge.create(
|
await Charge.create(
|
||||||
{
|
{
|
||||||
id: charge.id,
|
id: charge.id,
|
||||||
amount,
|
amount,
|
||||||
description: "One time payment for grant",
|
description: 'One time payment for grant',
|
||||||
grant_id: grant.id
|
grant_id: grant.id,
|
||||||
},
|
payment_status: 'paid'
|
||||||
{ transaction }
|
},
|
||||||
);
|
{ transaction }
|
||||||
|
);
|
||||||
|
|
||||||
// send email
|
// send email
|
||||||
await sendgrid.paymentReceipt({
|
await sendgrid.paymentReceipt({
|
||||||
organizations,
|
organizations,
|
||||||
total_amount: actual_total,
|
total_amount: actual_total,
|
||||||
receiver: user.email
|
receiver: user.email
|
||||||
});
|
});
|
||||||
|
|
||||||
// commit
|
// commit
|
||||||
await transaction.commit();
|
await transaction.commit();
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
message: "Successfully charged or subscribed",
|
message: 'Successfully charged or subscribed',
|
||||||
grant
|
grant
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error) await transaction.rollback();
|
if (error) await transaction.rollback();
|
||||||
next(error);
|
console.log(error);
|
||||||
}
|
next(error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Find grants with causes, regions, and organizations by donor_id
|
// Find grants with causes, regions, and organizations by donor_id
|
||||||
exports.findGrantsByDonorId = async (req, res, next) => {
|
exports.getGrantsByDonorId = async (req, res, next) => {
|
||||||
const donor_id = req.user.id;
|
const donor_id = req.user.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const grants = await db.sequelize.query(
|
const grants = await db.sequelize.query(
|
||||||
`
|
`
|
||||||
Select
|
Select
|
||||||
g.id,
|
g.id,
|
||||||
g.name,
|
g.name,
|
||||||
@@ -117,7 +119,9 @@ exports.findGrantsByDonorId = async (req, res, next) => {
|
|||||||
g.monthly,
|
g.monthly,
|
||||||
g.num_causes,
|
g.num_causes,
|
||||||
g.num_regions,
|
g.num_regions,
|
||||||
g.donor_id,
|
g.donor_id,
|
||||||
|
g.created_at,
|
||||||
|
g.updated_at,
|
||||||
JSON_ARRAYAGG(o.id) AS organizations_ids,
|
JSON_ARRAYAGG(o.id) AS organizations_ids,
|
||||||
JSON_ARRAYAGG(o.name) AS organization_names,
|
JSON_ARRAYAGG(o.name) AS organization_names,
|
||||||
JSON_ARRAYAGG(go.amount) AS organization_amounts,
|
JSON_ARRAYAGG(go.amount) AS organization_amounts,
|
||||||
@@ -129,47 +133,47 @@ exports.findGrantsByDonorId = async (req, res, next) => {
|
|||||||
where donor_id = :donor_id
|
where donor_id = :donor_id
|
||||||
group by g.id, g.name, g.amount, g.monthly, g.num_causes, g.num_regions, g.donor_id;
|
group by g.id, g.name, g.amount, g.monthly, g.num_causes, g.num_regions, g.donor_id;
|
||||||
`,
|
`,
|
||||||
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
|
{ type: db.sequelize.QueryTypes.SELECT, replacements: { donor_id } }
|
||||||
);
|
);
|
||||||
|
|
||||||
// adding in the organizations, causes, and regions for each grant into arrays
|
// adding in the organizations, causes, and regions for each grant into arrays
|
||||||
grants.map(grant => {
|
grants.map(grant => {
|
||||||
grant.organizations = [];
|
grant.organizations = [];
|
||||||
|
|
||||||
for (var i = 0; i < grant.organization_names.length; i++) {
|
for (var i = 0; i < grant.organization_names.length; i++) {
|
||||||
grant.organizations.push({
|
grant.organizations.push({
|
||||||
id: grant.organizations_ids[i],
|
id: grant.organizations_ids[i],
|
||||||
name: grant.organization_names[i],
|
name: grant.organization_names[i],
|
||||||
amount: grant.organization_amounts[i]
|
amount: grant.organization_amounts[i]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
grant.monthly = grant.monthly == 1 ? true : false;
|
grant.monthly = grant.monthly == 1 ? true : false;
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
grants
|
grants
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.deleteGrant = (req, res, next) => {
|
exports.deleteGrant = (req, res, next) => {
|
||||||
const { grant_id } = req.body;
|
const { grant_id } = req.body;
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
|
|
||||||
Grant.destroy({ where: { id: grant_id, donor_id: user.id } })
|
Grant.destroy({ where: { id: grant_id, donor_id: user.id } })
|
||||||
.then(result => {
|
.then(result => {
|
||||||
var message = "Already Deleted";
|
var message = 'Already Deleted';
|
||||||
if (result) {
|
if (result) {
|
||||||
message = "Grant Deleted";
|
message = 'Grant Deleted';
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
result,
|
result,
|
||||||
message
|
message
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(error => next(error));
|
.catch(error => next(error));
|
||||||
};
|
};
|
||||||
|
|||||||
+26
-26
@@ -1,58 +1,58 @@
|
|||||||
const express = require("express"),
|
const express = require('express'),
|
||||||
router = express.Router(),
|
router = express.Router(),
|
||||||
checkAuth = require("../middleware/check-auth"),
|
checkAuth = require('../middleware/check-auth'),
|
||||||
roles = require("../helpers/roles");
|
roles = require('../helpers/roles');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
donor,
|
donor,
|
||||||
grant,
|
grant,
|
||||||
stripe,
|
stripe,
|
||||||
organization,
|
organization,
|
||||||
charge
|
charge
|
||||||
} = require("../controllers");
|
} = require('../controllers');
|
||||||
|
|
||||||
// POST donor signup
|
// POST donor signup
|
||||||
router.post("/", donor.createDonor);
|
router.post('/', donor.createDonor);
|
||||||
|
|
||||||
// GET all Donors
|
// GET all Donors
|
||||||
router.get("/", checkAuth(roles.ADMIN), donor.getAllDonors);
|
router.get('/', checkAuth(roles.ADMIN), donor.getAllDonors);
|
||||||
|
|
||||||
// GET grants with causes, regions, charities by donor_id
|
// GET grants with causes, regions, charities by donor_id
|
||||||
router.get("/grants/", checkAuth(roles.DONOR), grant.findGrantsByDonorId);
|
router.get('/grants/', checkAuth(roles.DONOR), grant.getGrantsByDonorId);
|
||||||
|
|
||||||
/** POST Create grants with following body:
|
/** POST Create grants with following body:
|
||||||
* - List of id's of selected causes, regions, charities
|
* - List of id's of selected causes, regions, charities
|
||||||
* - Monthly: true or false
|
* - Monthly: true or false
|
||||||
* - donor_id
|
* - donor_id
|
||||||
* */
|
* */
|
||||||
router.post("/grants/", checkAuth(roles.DONOR), grant.createGrant);
|
router.post('/grants/', checkAuth(roles.DONOR), grant.createGrant);
|
||||||
|
|
||||||
// DELETE a grant of a donor
|
// DELETE a grant of a donor
|
||||||
router.delete(
|
router.delete(
|
||||||
"/grants/",
|
'/grants/',
|
||||||
checkAuth(roles.DONOR),
|
checkAuth(roles.DONOR),
|
||||||
stripe.deleteGrant,
|
stripe.deleteGrant,
|
||||||
grant.deleteGrant
|
grant.deleteGrant
|
||||||
);
|
);
|
||||||
|
|
||||||
// POST to get suggested organizations to distribute to
|
// POST to get suggested organizations to distribute to
|
||||||
// running "the algorithm"
|
// running "the algorithm"
|
||||||
router.post(
|
router.post(
|
||||||
"/organizations/",
|
'/organizations/',
|
||||||
checkAuth(roles.DONOR),
|
checkAuth(roles.DONOR),
|
||||||
organization.findSuggestedCharities
|
organization.findSuggestedCharities
|
||||||
);
|
);
|
||||||
|
|
||||||
// GET min and optimal amount to choose
|
// GET min and optimal amount to choose
|
||||||
router.get(
|
router.get(
|
||||||
"/organizations/amounts",
|
'/organizations/amounts',
|
||||||
checkAuth(roles.DONOR),
|
checkAuth(roles.DONOR),
|
||||||
organization.findOptimalBundleAmounts
|
organization.findOptimalBundleAmounts
|
||||||
);
|
);
|
||||||
|
|
||||||
router.get("/dashboard", checkAuth(roles.DONOR), donor.getDashboardData);
|
router.get('/dashboard', checkAuth(roles.DONOR), donor.getDashboardData);
|
||||||
|
|
||||||
router.get("/charges", checkAuth(roles.DONOR), charge.getAllCharges);
|
router.get('/charges', checkAuth(roles.DONOR), charge.getAllChargesbyDonor);
|
||||||
|
|
||||||
// // Retrieve a single Donor by Id
|
// // Retrieve a single Donor by Id
|
||||||
// router.get('/:donor_id', donor.findById);
|
// router.get('/:donor_id', donor.findById);
|
||||||
|
|||||||
+16
-16
@@ -1,21 +1,21 @@
|
|||||||
"use strict";
|
'use strict';
|
||||||
const PaymentStatus = require("./PaymentStatus");
|
const PaymentStatus = require('./PaymentStatus');
|
||||||
|
|
||||||
module.exports = (sequelize, DataTypes) => {
|
module.exports = (sequelize, DataTypes) => {
|
||||||
const Charge = sequelize.define("Charge", {
|
const Charge = sequelize.define('Charge', {
|
||||||
id: { type: DataTypes.UUID, primaryKey: true, allowNull: false },
|
id: { type: DataTypes.UUID, primaryKey: true, allowNull: false },
|
||||||
description: DataTypes.STRING,
|
description: DataTypes.STRING,
|
||||||
amount: DataTypes.INTEGER,
|
amount: DataTypes.INTEGER,
|
||||||
|
|
||||||
payment_status: {
|
payment_status: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
references: {
|
references: {
|
||||||
model: PaymentStatus(sequelize, DataTypes),
|
model: PaymentStatus(sequelize, DataTypes),
|
||||||
key: "name"
|
key: 'name'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return Charge;
|
return Charge;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
up: (queryInterface, Sequelize) => {
|
||||||
|
return queryInterface.bulkInsert('paymentstatuses', [
|
||||||
|
{
|
||||||
|
name: 'pending',
|
||||||
|
created_at: new Date(),
|
||||||
|
updated_at: new Date()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'paid',
|
||||||
|
created_at: new Date(),
|
||||||
|
updated_at: new Date()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'failed',
|
||||||
|
created_at: new Date(),
|
||||||
|
updated_at: new Date()
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
},
|
||||||
|
|
||||||
|
down: (queryInterface, Sequelize) => {
|
||||||
|
return queryInterface.bulkDelete('paymentstatuses', [
|
||||||
|
{
|
||||||
|
name: 'pending'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'paid'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'failed'
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user