adding integration with payment sources table and within stripe controller

This commit is contained in:
Arjun Patel
2019-05-11 03:27:30 -07:00
parent f865781c07
commit 3c55717dda
6 changed files with 187 additions and 7 deletions
+5 -3
View File
@@ -34,6 +34,7 @@ exports.createGrant = async (req, res, next) => {
// get transaction
transaction = await sequelize.transaction();
// creating grant in table
const grant = await Grant.create(
{
donor_id: user.id,
@@ -48,6 +49,7 @@ exports.createGrant = async (req, res, next) => {
}
);
// getting data for later use throughout
await Promise.all(
await organizations.map(async org => {
let dbOrgs = await sequelize.query(
@@ -70,7 +72,7 @@ exports.createGrant = async (req, res, next) => {
})
);
// one time charge
// one time charge with stripe helper
const charge = await stripe.grantCharge({
grant,
stripeToken,
@@ -81,6 +83,7 @@ exports.createGrant = async (req, res, next) => {
transaction
});
// mapping between bundle and orgs
const grantsOrgs = organizations.map(org => {
return {
grant_id: grant.id,
@@ -88,12 +91,11 @@ exports.createGrant = async (req, res, next) => {
amount: org.finalAmountToOrg
};
});
// mapping between bundle and orgs
await GrantOrganization.bulkCreate(grantsOrgs, {
transaction
});
// creating in charge table for reference
await Charge.create(
{
id: charge.id,
+57 -3
View File
@@ -1,6 +1,7 @@
const db = require('../../models'),
errorMaker = require('../helpers/error.maker'),
requestPromise = require('request-promise');
requestPromise = require('request-promise'),
uuidv4 = require('uuid/v4');
const stripe = require('stripe')(process.env.STRIPE_KEY);
@@ -20,7 +21,27 @@ exports.grantCharge = async ({
try {
const donors = await Donor.findAll({ where: { id: user.id } });
await addCustomerPaymentSource(donors[0], stripeToken, transaction);
const paymentSourceId = await addCustomerPaymentSource(
donors[0],
stripeToken,
transaction
);
// set the grant's payment source id to the newly created one
await sequelize.query(
`
UPDATE grants
SET payment_source_id = :payment_source_id
WHERE id = :grant_id`,
{
type: sequelize.QueryTypes.UPDATE,
replacements: {
payment_source_id: paymentSourceId,
grant_id: grant.id
},
transaction
}
);
const stripeAmount = amount * 100;
@@ -180,7 +201,40 @@ const addCustomerPaymentSource = async (donor, source, transaction = null) => {
});
}
resolve();
const date = new Date();
const paymentSourceId = uuidv4();
await sequelize.query(
`
INSERT INTO payment_sources
(id,
token,
provider,
donor_id,
createdAt,
updatedAt
)
VALUES
(:paymentSourceId,
:source,
:provider,
:donor_id,
:date,
:date
)`,
{
type: sequelize.QueryTypes.INSERT,
replacements: {
paymentSourceId,
source,
provider: 'stripe',
donor_id: donor.id,
date
},
transaction
}
);
resolve(paymentSourceId);
} catch (error) {
reject(error);
}