Set up stripe to get user emails so receipts can be emailed.

This commit is contained in:
Scott Erickson 2014-12-03 16:35:53 -08:00
parent 8cff42a843
commit 7210de02e9
6 changed files with 32 additions and 7 deletions

View file

@ -3,7 +3,7 @@ winston = require 'winston'
mongoose = require 'mongoose'
Grid = require 'gridfs-stream'
testing = '--unittest' in process.argv
global.testing = testing = '--unittest' in process.argv
module.exports.connect = () ->

View file

@ -163,7 +163,11 @@ PaymentHandler = class PaymentHandler extends Handler
if not req.user.get('stripe')?.customerID
stripe.customers.create({
card: token
description: req.user._id + ''
email: req.user.get('email')
metadata: {
id: req.user._id + ''
slug: req.user.get('slug')
}
}).then(((customer) =>
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
stripeInfo.customerID = customer.id

View file

@ -25,7 +25,11 @@ class SubscriptionHandler extends Handler
if stripeToken
stripe.customers.create({
card: stripeToken
description: req.user._id + ''
email: req.user.get('email')
metadata: {
id: req.user._id + ''
slug: req.user.get('slug')
}
}).then(((customer) =>
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
stripeInfo.customerID = customer.id

View file

@ -15,7 +15,7 @@ module.exports.setup = (app) ->
stripe.customers.retrieve invoice.customer, (err, customer) =>
return res.send(500, '') if err
userID = customer.description
userID = customer.metadata.id
User.findById userID, (err, user) =>
return res.send(500, '') if err
return res.send(200) if not user # just for the sake of testing...

View file

@ -6,6 +6,9 @@ mail = require '../commons/mail'
log = require 'winston'
plugins = require '../plugins/plugins'
config = require '../../server_config'
stripe = require('stripe')(config.stripe.secretKey)
sendwithus = require '../sendwithus'
delighted = require '../delighted'
@ -76,12 +79,17 @@ UserSchema.methods.isEmailSubscriptionEnabled = (newName) ->
_.defaults emails, _.cloneDeep(jsonschema.properties.emails.default)
return emails[newName]?.enabled
UserSchema.statics.updateMailChimp = (doc, callback) ->
UserSchema.statics.updateServiceSettings = (doc, callback) ->
return callback?() unless isProduction or GLOBAL.testing
return callback?() if doc.updatedMailChimp
return callback?() unless doc.get('email')
existingProps = doc.get('mailChimp')
emailChanged = (not existingProps) or existingProps?.email isnt doc.get('email')
if emailChanged and customerID = doc.get('stripe')?.customerID
stripe.customers.update customerID, {email:doc.get('email')}, (err, customer) ->
console.error('Error updating stripe customer...', err) if err
return callback?() unless emailChanged or doc.newsSubsChanged
newGroups = []
@ -184,7 +192,7 @@ UserSchema.pre('save', (next) ->
UserSchema.post 'save', (doc) ->
doc.newsSubsChanged = not _.isEqual(_.pick(doc.get('emails'), mail.NEWS_GROUPS), _.pick(doc.startingEmails, mail.NEWS_GROUPS))
UserSchema.statics.updateMailChimp(doc)
UserSchema.statics.updateServiceSettings(doc)
UserSchema.post 'init', (doc) ->
doc.startingEmails = _.cloneDeep(doc.get('emails'))

View file

@ -197,3 +197,12 @@ describe '/db/user, editing stripe property', ->
expect(user.get('stripe').subscriptionID).toBeUndefined()
expect(user.get('stripe').planID).toBeUndefined()
done()
it "updates the customer's email when you change the user's email", (done) ->
joeData.email = 'newEmail@gmail.com'
request.put {uri: userURL, json: joeData }, (err, res, body) ->
f = -> stripe.customers.retrieve joeData.stripe.customerID, (err, customer) ->
console.log 'customer?', customer
expect(customer.email).toBe('newEmail@gmail.com')
done()
setTimeout(f, 500) # bit of a race condition here, response returns before stripe has been updated