From f865781c073581a0d471d3a6be5474b18ff40c68 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Sat, 11 May 2019 02:18:49 -0700 Subject: [PATCH] properly adding payment source with helper function --- app/controllers/grant.controller.js | 3 +- app/controllers/stripe.controller.js | 107 ++++++++++++--------------- 2 files changed, 48 insertions(+), 62 deletions(-) diff --git a/app/controllers/grant.controller.js b/app/controllers/grant.controller.js index 3ed10b3..ff23b8b 100644 --- a/app/controllers/grant.controller.js +++ b/app/controllers/grant.controller.js @@ -77,7 +77,8 @@ exports.createGrant = async (req, res, next) => { organizations, monthly, amount, - user + user, + transaction }); const grantsOrgs = organizations.map(org => { diff --git a/app/controllers/stripe.controller.js b/app/controllers/stripe.controller.js index 36168a6..2e8e55a 100644 --- a/app/controllers/stripe.controller.js +++ b/app/controllers/stripe.controller.js @@ -13,18 +13,22 @@ exports.grantCharge = async ({ organizations, monthly, amount, - user + user, + transaction }) => { return await new Promise(async (resolve, reject) => { try { const donors = await Donor.findAll({ where: { id: user.id } }); + await addCustomerPaymentSource(donors[0], stripeToken, transaction); + const stripeAmount = amount * 100; // charge if yes or no monthly let charge = await stripe.charges.create({ amount: stripeAmount, currency: 'usd', + customer: donors[0].stripe_id, source: stripeToken, description: `UCharify Bundle ${grant.id}`, statement_descriptor: `UCharify Bundle ${grant.id}`, @@ -50,38 +54,6 @@ exports.grantCharge = async ({ let finalAmountToOrg = 0; - // IMPLEMENTATION FOR CHARIFY CREDIT - // if (currOrg[0].charify_credit) { - // let updated_amt = currOrg[0].charify_credit; - - // if (currOrg[0].charify_credit < applicationAndStripeFee) { - // updated_amt = 0; - // // use up credit and take remaining as middle man - // finalAmountToOrg = - // selectedOrgAmount - - // applicationAndStripeFee + - // currOrg[0].charify_credit; - // } else { - // updated_amt = currOrg[0].charify_credit - applicationAndStripeFee; - // // take nothing as the middle man - // finalAmountToOrg = selectedOrgAmount; - // } - - // await sequelize.query( - // ` - // UPDATE organizations - // SET charify_credit = :updated_amt - // WHERE id = :org_id`, - // { - // type: db.Sequelize.QueryTypes.UPDATE, - // replacements: { - // org_id: org.id, - // updated_amt - // } - // } - // ); - // } else finalAmountToOrg = selectedOrgAmount - applicationAndStripeFee; - finalAmountToOrg = selectedOrgAmount - applicationAndStripeFee; // for record in grant org table @@ -112,33 +84,6 @@ exports.grantCharge = async ({ }); }; -// Delete grant's stripe plan under subscription -// exports.deleteGrant = async (req, res, next) => { -// const { grant_id } = req.body; - -// try { -// const paymentPlans = await PaymentPlan.findAll({ where: { grant_id } }); - -// // check if there are plans for that grant -// if (!paymentPlans.length) { -// return res.status(400).json({ -// message: 'Not a valid grant' -// }); -// } else { -// const plan = paymentPlans[0]; - -// //delete the plan/grant under the subscription for the user -// await stripe.subscriptionItems.del(plan.sub_item_id); -// //delete the plan from the main product 'Grants' -// await stripe.plans.del(plan.plan_id); -// } - -// next(); -// } catch (error) { -// next(error); -// } -// }; - // allows the org to see their stripe account with their balance and payout to their bank exports.getExpressUILink = async (req, res, next) => { // get connected stripe account id from db @@ -192,7 +137,10 @@ exports.activateStripeAccount = async (req, res, next) => { const updateRes = await db.sequelize.query( 'UPDATE organizations SET stripe_account_id = :stripe_user_id WHERE id = :org_id', - { replacements: { stripe_user_id, org_id } } + { + type: sequelize.QueryTypes.UPDATE, + replacements: { stripe_user_id, org_id } + } ); let redirectUrl = process.env.STRIPE_CONNECT_ACTIVATE_REDIRECT_LINK; @@ -201,3 +149,40 @@ exports.activateStripeAccount = async (req, res, next) => { next(error); } }; + +// Add a reusable source under a customer in stripe +const addCustomerPaymentSource = async (donor, source, transaction = null) => { + return await new Promise(async (resolve, reject) => { + try { + if (!donor.stripe_id) { + const stripeCustomer = await stripe.customers.create({ + email: donor.email, + name: `${donor.first_name} ${donor.last_name}`, + source + }); + + donor.stripe_id = stripeCustomer.id; + + await sequelize.query( + ` + UPDATE donors + SET stripe_id = :stripe_id + WHERE id = :donor_id`, + { + type: sequelize.QueryTypes.UPDATE, + replacements: { donor_id: donor.id, stripe_id: stripeCustomer.id }, + transaction + } + ); + } else { + await stripe.customers.createSource(donor.stripe_id, { + source + }); + } + + resolve(); + } catch (error) { + reject(error); + } + }); +};