mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-27 09:35:39 -05:00
Set up stripe to get user emails so receipts can be emailed.
This commit is contained in:
parent
8cff42a843
commit
7210de02e9
6 changed files with 32 additions and 7 deletions
|
@ -3,7 +3,7 @@ winston = require 'winston'
|
||||||
mongoose = require 'mongoose'
|
mongoose = require 'mongoose'
|
||||||
Grid = require 'gridfs-stream'
|
Grid = require 'gridfs-stream'
|
||||||
|
|
||||||
testing = '--unittest' in process.argv
|
global.testing = testing = '--unittest' in process.argv
|
||||||
|
|
||||||
|
|
||||||
module.exports.connect = () ->
|
module.exports.connect = () ->
|
||||||
|
|
|
@ -163,7 +163,11 @@ PaymentHandler = class PaymentHandler extends Handler
|
||||||
if not req.user.get('stripe')?.customerID
|
if not req.user.get('stripe')?.customerID
|
||||||
stripe.customers.create({
|
stripe.customers.create({
|
||||||
card: token
|
card: token
|
||||||
description: req.user._id + ''
|
email: req.user.get('email')
|
||||||
|
metadata: {
|
||||||
|
id: req.user._id + ''
|
||||||
|
slug: req.user.get('slug')
|
||||||
|
}
|
||||||
}).then(((customer) =>
|
}).then(((customer) =>
|
||||||
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
|
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
|
||||||
stripeInfo.customerID = customer.id
|
stripeInfo.customerID = customer.id
|
||||||
|
|
|
@ -25,7 +25,11 @@ class SubscriptionHandler extends Handler
|
||||||
if stripeToken
|
if stripeToken
|
||||||
stripe.customers.create({
|
stripe.customers.create({
|
||||||
card: stripeToken
|
card: stripeToken
|
||||||
description: req.user._id + ''
|
email: req.user.get('email')
|
||||||
|
metadata: {
|
||||||
|
id: req.user._id + ''
|
||||||
|
slug: req.user.get('slug')
|
||||||
|
}
|
||||||
}).then(((customer) =>
|
}).then(((customer) =>
|
||||||
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
|
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
|
||||||
stripeInfo.customerID = customer.id
|
stripeInfo.customerID = customer.id
|
||||||
|
|
|
@ -15,7 +15,7 @@ module.exports.setup = (app) ->
|
||||||
stripe.customers.retrieve invoice.customer, (err, customer) =>
|
stripe.customers.retrieve invoice.customer, (err, customer) =>
|
||||||
return res.send(500, '') if err
|
return res.send(500, '') if err
|
||||||
|
|
||||||
userID = customer.description
|
userID = customer.metadata.id
|
||||||
User.findById userID, (err, user) =>
|
User.findById userID, (err, user) =>
|
||||||
return res.send(500, '') if err
|
return res.send(500, '') if err
|
||||||
return res.send(200) if not user # just for the sake of testing...
|
return res.send(200) if not user # just for the sake of testing...
|
||||||
|
|
|
@ -6,6 +6,9 @@ mail = require '../commons/mail'
|
||||||
log = require 'winston'
|
log = require 'winston'
|
||||||
plugins = require '../plugins/plugins'
|
plugins = require '../plugins/plugins'
|
||||||
|
|
||||||
|
config = require '../../server_config'
|
||||||
|
stripe = require('stripe')(config.stripe.secretKey)
|
||||||
|
|
||||||
sendwithus = require '../sendwithus'
|
sendwithus = require '../sendwithus'
|
||||||
delighted = require '../delighted'
|
delighted = require '../delighted'
|
||||||
|
|
||||||
|
@ -76,12 +79,17 @@ UserSchema.methods.isEmailSubscriptionEnabled = (newName) ->
|
||||||
_.defaults emails, _.cloneDeep(jsonschema.properties.emails.default)
|
_.defaults emails, _.cloneDeep(jsonschema.properties.emails.default)
|
||||||
return emails[newName]?.enabled
|
return emails[newName]?.enabled
|
||||||
|
|
||||||
UserSchema.statics.updateMailChimp = (doc, callback) ->
|
UserSchema.statics.updateServiceSettings = (doc, callback) ->
|
||||||
return callback?() unless isProduction or GLOBAL.testing
|
return callback?() unless isProduction or GLOBAL.testing
|
||||||
return callback?() if doc.updatedMailChimp
|
return callback?() if doc.updatedMailChimp
|
||||||
return callback?() unless doc.get('email')
|
return callback?() unless doc.get('email')
|
||||||
existingProps = doc.get('mailChimp')
|
existingProps = doc.get('mailChimp')
|
||||||
emailChanged = (not existingProps) or existingProps?.email isnt doc.get('email')
|
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
|
return callback?() unless emailChanged or doc.newsSubsChanged
|
||||||
|
|
||||||
newGroups = []
|
newGroups = []
|
||||||
|
@ -184,7 +192,7 @@ UserSchema.pre('save', (next) ->
|
||||||
|
|
||||||
UserSchema.post 'save', (doc) ->
|
UserSchema.post 'save', (doc) ->
|
||||||
doc.newsSubsChanged = not _.isEqual(_.pick(doc.get('emails'), mail.NEWS_GROUPS), _.pick(doc.startingEmails, mail.NEWS_GROUPS))
|
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) ->
|
UserSchema.post 'init', (doc) ->
|
||||||
doc.startingEmails = _.cloneDeep(doc.get('emails'))
|
doc.startingEmails = _.cloneDeep(doc.get('emails'))
|
||||||
|
|
|
@ -197,3 +197,12 @@ describe '/db/user, editing stripe property', ->
|
||||||
expect(user.get('stripe').subscriptionID).toBeUndefined()
|
expect(user.get('stripe').subscriptionID).toBeUndefined()
|
||||||
expect(user.get('stripe').planID).toBeUndefined()
|
expect(user.get('stripe').planID).toBeUndefined()
|
||||||
done()
|
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
|
||||||
|
|
Loading…
Reference in a new issue