Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge d6fbb9d into 3d1107a
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Lott authored Oct 10, 2017
2 parents 3d1107a + d6fbb9d commit 352e9dd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
19 changes: 17 additions & 2 deletions bin/create-debits.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
const logger = require('../lib/logger');

const StorjMailer = require('storj-service-mailer');
const defaults = require('../config.js').DEFAULTS;
const mailer = new StorjMailer(defaults.mailer);
const moment = require('moment');
const program = require('commander');
const rl = require('readline');
Expand Down Expand Up @@ -64,7 +66,6 @@ const BILLING_URL = process.env.BILLING_URL || 'http://localhost:3000';
const PRIVKEY = process.env.PRIVKEY ||
'd6b0e5ac88be1f9c3749548de7b6148f14c2ca8ccdf5295369476567e8c8d218';


function start() {
const billingClient = new BillingClient(BILLING_URL, PRIVKEY);
const storage = new Storage(process.env.MONGO_URL || 'mongodb://127.0.0.1:27017/bridge', mongoOptions);
Expand Down Expand Up @@ -154,6 +155,20 @@ function confirm(question, callback) {
// return storage.models.Debit.remove({});
// }

function sendInvoice (user, amount, storage, bandwidth) {
const self = this;

mailer.dispatch(user, 'invoice', {
amount: amount,
storage: storage,
bandwidth: bandwidth
}, function (err) {
if (err) {
logger.('Error sending user invoice: ', err);
}
});
}

// Confirm with user that date range is as expected before moving on
console.log("We will generate debits starting on %s and ending on %s", generationBeginDate, generationEndDate);
if (program.remove) {
Expand Down
20 changes: 14 additions & 6 deletions lib/server/routes/credits.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const stripe = require('../vendor/stripe');
const errors = require('storj-service-error-types');
const Promise = require('bluebird');
const helperFactory = require('./helpers/credits-helper');
const StorjMailer = require('storj-service-mailer');
const defaults = require('../../config.js').DEFAULTS;
const mailer = new StorjMailer(defaults.mailer);

// TODO: Refactor all stripe-related endpoints into a single endpoint
// to remain payment processor agnostic.
Expand All @@ -40,10 +43,8 @@ CreditsRouter.prototype.handleSignups = function (req, res) {
const Marketing = this.models.Marketing;
Marketing.create(req.body.email, function(err, marketing) {
if (err) {
console.log('Error creating signup marketing: ', err);
return res.status(500).send(err);
}
console.log('CREATED MARKETING DOC: ', marketing);
if (req.body.referralLink) {
return self.handleReferralSignup(req, res);
}
Expand Down Expand Up @@ -140,6 +141,7 @@ CreditsRouter.prototype._noDuplicateCredit = function(data) {
.catch((err) => reject(new errors.InternalError(err.message)));
});
};

/**
* Issues a referral recipient sign up credit
* @param {Object} data - req.body with email and referralLink props
Expand All @@ -149,8 +151,6 @@ CreditsRouter.prototype._issueReferralSignupCredit = function(data, referral) {
const Credit = this.models.Credit;

return new Promise((resolve, reject) => {
console.log('_issueReferralSignupCredit', data, referral.id);

const newCredit = new Credit({
user: data.email,
type: CREDIT_TYPES.AUTO,
Expand Down Expand Up @@ -221,7 +221,8 @@ CreditsRouter.prototype.checkType = function(type) {
};

CreditsRouter.prototype.handleCreateCredit = function(req, res) {
// TODO: refactor this to be payment processor agnostic (or mount at /stripe)
const self = this;

try {
if (/invoice.payment_failure/.test(res.locals.event.type)) {
handlePaymentFailure(res);
Expand Down Expand Up @@ -270,6 +271,14 @@ CreditsRouter.prototype.handleCreateCredit = function(req, res) {
return res.sendStatus(500);
}

mailer.dispatch(req.user._id, 'payment-confirmation', {
amount: invoice.total
}, function (err) {
if (err) {
console.error('Error sending payment confirmation email: ', err);
}
});

return res.sendStatus(201);
};

Expand Down Expand Up @@ -332,7 +341,6 @@ CreditsRouter.prototype.handleConfirmCredit = function(req, res) {
};

CreditsRouter.prototype.getCredits = function(req, res, next) {
console.log('body', req.body, 'params', req.params, 'query', req.query);
const Credit = this.models.Credit;

if (!req.query.startDate && !req.query.endDate) {
Expand Down
11 changes: 0 additions & 11 deletions lib/server/routes/marketing.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ const log = require('../../logger');
const errors = require('storj-service-error-types');
const Promise = require('bluebird');

// TODO: Refactor all stripe-related endpoints into a single endpoint
// to remain payment processor agnostic.

/**
* Handles endpoints for all user related operations
*/
function MarketingRouter(options) {
if (!(this instanceof MarketingRouter)) {
return new MarketingRouter(options);
Expand All @@ -37,7 +31,6 @@ MarketingRouter.prototype.get = function (req, res) {
if (doc) {
return res.status(200).send(doc);
}
console.log('no user');

Marketing.create(user, function (err, marketing) {
if (err) {
Expand All @@ -52,10 +45,6 @@ MarketingRouter.prototype.create = function (req, res) {

}

/**
* Export definitions
* @private
*/
MarketingRouter.prototype._definitions = function() {
return [
['GET', '/marketing',
Expand Down
32 changes: 13 additions & 19 deletions lib/server/routes/payment-processors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ const Router = require('./index');
const middleware = require('storj-service-middleware');
const errors = require('storj-service-error-types');
const Promise = require('bluebird');
const StorjMailer = require('storj-service-mailer');
const defaults = require('../../config.js').DEFAULTS;
const mailer = new StorjMailer(defaults.mailer);
const log = require('../../logger');

function PaymentProcessorsRouter(options) {
if (!(this instanceof PaymentProcessorsRouter)) {
return new PaymentProcessorsRouter(options);
}

Router.apply(this, arguments);

this.models = options.storage.models;
this.authenticate = middleware.authenticate(options.storage);
}
Expand All @@ -37,7 +40,9 @@ PaymentProcessorsRouter.prototype._addPaymentProcessor = function(req) {
.then((pp) => resolve(pp))
.catch((err) => reject(err));
})
.catch((err) => reject(err));
.catch((err) => {
reject(err)
});
});
};

Expand All @@ -54,35 +59,26 @@ PaymentProcessorsRouter.prototype.addPaymentMethod = function(req, res, next) {
const PaymentProcessor = this.models.PaymentProcessor;
const User = this.models.User;

console.log('req.user: ', req.user);

return PaymentProcessor
.findOne({
user: req.user.id,
name: req.body.processor.name
})
.then((pp) => {
console.log('PP: ', pp);

if (pp) {
pp.addPaymentMethod(req.body.data);
console.log('added payment method with', req.body.data);
return pp;
}

self._addPaymentProcessor(req);
console.log('added payment processor');

return pp;
})
.then((pp) => {

// set user to free tier
self._setUserFreeTier(req, false);

console.log('set to free tier');

// respond
mailer.dispatch(req.user._id, 'add-card', {}, (err) => {
log.info('error sending add-card email', req.user._id, err)
});
return res.status(200).send({
pp,
user: req.user
Expand All @@ -99,19 +95,18 @@ PaymentProcessorsRouter.prototype.removePaymentMethod = function(req, res, next)
PaymentProcessor.findOne({ _id: ppId })
.then((pp) => {
if (pp.paymentMethods.length <= 0) {
console.error('No payment methods to remove.');
return res.status(200).send(`No payment processor id ${ppId}`);
}

console.log('removing method', methodId);
pp.adapter.removePaymentMethod(methodId);
mailer.dispatch(req.user._id, 'remove-card', {}, (err) => {
if (err) log.error('Error sending remove-card email', err);
});

return pp;
})
.then(pp => {
const user = self._setUserFreeTier(req, true);
console.log('user: ', user);
console.log('pp: ', pp);

return res.status(200).json({
user,
Expand All @@ -132,7 +127,6 @@ PaymentProcessorsRouter.prototype.getDefaultPP = function(req, res, next) {
return res.status(200).send({ pp: null });
})
.catch((err) => {
console.error('Error #getDefaultPP', err);
res.status(500).send(err);
})
};
Expand Down
6 changes: 0 additions & 6 deletions lib/server/routes/referrals.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ ReferralsRouter.prototype.sendReferralEmail = function(req, res) {
emailList.forEach((email) => {
const emailPromise = function() {
return new Promise((resolve, reject) => {
console.log('sending from: ', marketing.user);
console.log('sending to: ', email);
console.log('sending marketing: ', marketing);
const user = marketing.user;
self._isNotCurrentUser(email)
.then(() => self._sendEmail(user, email, marketing))
Expand Down Expand Up @@ -103,16 +100,13 @@ ReferralsRouter.prototype._isNotCurrentUser = function(recipientEmail) {

ReferralsRouter.prototype._sendEmail = function(senderEmail, recipientEmail, marketing) {
return new Promise((resolve, reject) => {
console.log('GOT INTO PROMISE')
mailer.dispatch(recipientEmail, 'referral', {
url: 'https://app.storj.io/#/signup?referralLink=' + marketing.referralLink,
senderEmail: senderEmail
}, function(err) {
if (err) {
console.error('Error sendReferralEmail: ', err);
return reject(new errors.InternalError('Internal mailer error'));
}
console.log('GOT PASS ERROR');
return resolve(true);
});
});
Expand Down

0 comments on commit 352e9dd

Please sign in to comment.