Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/http/routes/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const logger = require('util/logger').child({ module: 'OrganizationRouter' })
const stripe = require('util/stripe')
const util = require('util/index')
const bigPoppa = require('util/big-poppa')
const DiscountService = require('services/discount-service')
const _ = require('lodash')

const BaseRouter = require('http/routes/base')
Expand Down Expand Up @@ -213,19 +214,29 @@ class OrganizationRouter extends BaseRouter {
const log = logger.child({ validatedReq: validatedReq })
log.info('postPaymentMethod called')
return bigPoppa.getOrganization(validatedReq.params.id)
.then(function updatePlanForOrg (org) {
.tap(function updatePlanForOrg (org) {
log.trace({ org: org }, 'Organization fetched')
let user = org.users.find(user => user.id === validatedReq.body.user.id)
if (!user) {
throw new UserNotPartOfOrganizationError('Organization does not have user with provided user id')
}
return stripe.updatePaymentMethodForOrganization(org, validatedReq.body.stripeToken, user)
})
.then(function updateHasPaymentMethod () {
.tap(function updateHasPaymentMethod () {
return bigPoppa.updateOrganization(validatedReq.params.id, {
hasPaymentMethod: true
})
})
.tap(function addDiscountToOrganization (org) {
/**
* These is currently a very simple implementation for discounts. In
* the future this should be much more robust but this will do for now
*/
const discount = DiscountService.getDiscountAtPaymentMethodTime(org)
if (discount) {
return stripe.updateDiscountForCustomer(org.stripeCustomerId, discount)
}
})
.then(() => {
return res.status(201).send('Successfully updated')
})
Expand Down
35 changes: 35 additions & 0 deletions lib/services/discount-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const moment = require('moment')

const PREVIEW_DISCOUNT_SIGNUP_DEADLINE = moment('2016-08-24T07:00:00.000Z') // Wednesday, August 24th 2016, 12:00:00 am PST
const PREVIEW_DISCOUNT_PAYMENT_METHOD_DEADLINE = moment('2016-09-10T07:00:00.000Z') // Saturday, September 10th 2016, 12:00:00 am PST
const BETA_DISCOUNT_SIGNUP_DEADLINE = moment('2016-09-21T07:00:00.000Z') // Wednesday, September 21st 2016, 12:00:00 am PST

const PREVIEW_PLAN_ID = 'Preview'
const BETA_PLAN_ID = 'Beta'

module.exports = class DiscountService {

static getDiscountAtPaymentMethodTime (org) {
const orgCreatedTime = moment(org.created)
const paymentMethodCreatedTime = moment()
const trialEndTime = moment(org.trialEnd)
// Preview
if (
orgCreatedTime.isBefore(PREVIEW_DISCOUNT_SIGNUP_DEADLINE) &&
paymentMethodCreatedTime.isBefore(PREVIEW_DISCOUNT_PAYMENT_METHOD_DEADLINE)
) {
return PREVIEW_PLAN_ID
}
// Beta
if (
orgCreatedTime.isBefore(BETA_DISCOUNT_SIGNUP_DEADLINE) &&
paymentMethodCreatedTime.isBefore(trialEndTime) // Trial should not be over
) {
return BETA_PLAN_ID
}
return null
}

}
20 changes: 20 additions & 0 deletions lib/util/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,26 @@ module.exports = class Stripe {
})
}

/**
* Update discount for organiation
*
* @param {String} stripeCustomerId - Stripe Organization ID
* @param {String} discountId - Stripe Discount ID
* @returns {Promise}
*/
static updateDiscountForCustomer (orgStripeCustomerId, discountId) {
const log = logger.child({ orgStripeCustomerId: orgStripeCustomerId, discountId: discountId })
log.info('Stripe.updateDiscountForCustomer called')
let updates = {
coupon: discountId,
metadata: {
discountAdded: (new Date()).toISOString()
}
}
log.trace({ updates: updates }, 'Update Stripe customer')
return stripeClient.customers.update(orgStripeCustomerId, updates)
}

/**
* Add payment-method owner to the invoice. This facilitates knowing what
* user actually paid the invoice
Expand Down
3 changes: 3 additions & 0 deletions test/unit/http/routes/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ describe('HTTP /organization', () => {
let getOrganizationStub
let updatePaymentMethodForOrganizationStub
let updateOrganizationStub
let updateDiscountForCustomerStub
let org = Object.assign({}, OrganizationWithStripeCustomerIdFixture)
let orgId = org.id
let user = org.users[0]
Expand All @@ -416,11 +417,13 @@ describe('HTTP /organization', () => {
getOrganizationStub = sinon.stub(bigPoppa, 'getOrganization').resolves(org)
updateOrganizationStub = sinon.stub(bigPoppa, 'updateOrganization').resolves(org)
updatePaymentMethodForOrganizationStub = sinon.stub(stripe, 'updatePaymentMethodForOrganization').resolves()
updateDiscountForCustomerStub = sinon.stub(stripe, 'updateDiscountForCustomer').resolves()
})
afterEach('Restore stub', () => {
getOrganizationStub.restore()
updateOrganizationStub.restore()
updatePaymentMethodForOrganizationStub.restore()
updateDiscountForCustomerStub.restore()
})

it('should call `getOrganization`', () => {
Expand Down