mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-03-14 07:00:01 -04:00
Refactored stripeCustomerID to stripe object in the user object so we can put more stuff in there.
This commit is contained in:
parent
6cfb45e7a6
commit
5dcdabfd62
4 changed files with 17 additions and 9 deletions
|
@ -271,7 +271,13 @@ _.extend UserSchema.properties,
|
|||
earned: c.RewardSchema 'earned by achievements'
|
||||
purchased: c.RewardSchema 'purchased with gems or money'
|
||||
spent: {type: 'number'}
|
||||
stripeCustomerID: { type: 'string' }
|
||||
stripeCustomerID: { type: 'string' } # TODO: Migrate away from this property
|
||||
|
||||
stripe: c.object {}, {
|
||||
customerID: { type: 'string' }
|
||||
subscription: { enum: ['basic'] }
|
||||
token: { type: 'string' }
|
||||
}
|
||||
|
||||
c.extendBasicProperties UserSchema, 'user'
|
||||
|
||||
|
|
|
@ -71,7 +71,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
|||
@logPaymentError(req, 'Missing stripe productID')
|
||||
return @sendBadInputError(res, 'Need productID if paying with Stripe.')
|
||||
|
||||
if stripeTimestamp and (not stripeToken) and (not user.get('stripeCustomerID'))
|
||||
if stripeTimestamp and (not stripeToken) and (not req.user.get('stripe')?.customerID)
|
||||
@logPaymentError(req, 'Missing stripe token')
|
||||
return @sendBadInputError(res, 'Need stripe.token if new customer.')
|
||||
|
||||
|
@ -160,12 +160,14 @@ PaymentHandler = class PaymentHandler extends Handler
|
|||
handleStripePaymentPost: (req, res, timestamp, productID, token) ->
|
||||
|
||||
# First, make sure we save the payment info as a Customer object, if we haven't already.
|
||||
if not req.user.get('stripeCustomerID')
|
||||
if not req.user.get('stripe')?.customerID
|
||||
stripe.customers.create({
|
||||
card: token
|
||||
description: req.user._id + ''
|
||||
}).then(((customer) =>
|
||||
req.user.set('stripeCustomerID', customer.id)
|
||||
stripeInfo = _.cloneDeep(req.user.get('stripe') ? {})
|
||||
stripeInfo.customerID = customer.id
|
||||
req.user.set('stripe', stripeInfo)
|
||||
req.user.save((err) =>
|
||||
if err
|
||||
@logPaymentError(req, 'Stripe customer id save db error. '+err)
|
||||
|
@ -193,7 +195,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
|||
)
|
||||
),
|
||||
((callback) ->
|
||||
stripe.charges.list({customer: req.user.get('stripeCustomerID')}, (err, recentCharges) =>
|
||||
stripe.charges.list({customer: req.user.get('stripe')?.customerID}, (err, recentCharges) =>
|
||||
return callback(err) if err
|
||||
charge = _.find recentCharges.data, (c) -> c.metadata.timestamp is timestamp
|
||||
callback(null, charge)
|
||||
|
@ -232,7 +234,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
|||
stripe.charges.create({
|
||||
amount: product.amount
|
||||
currency: 'usd'
|
||||
customer: req.user.get('stripeCustomerID')
|
||||
customer: req.user.get('stripe')?.customerID
|
||||
metadata: {
|
||||
productID: product.id
|
||||
userID: req.user._id + ''
|
||||
|
@ -262,7 +264,7 @@ PaymentHandler = class PaymentHandler extends Handler
|
|||
payment.set 'amount', product.amount
|
||||
payment.set 'gems', product.gems
|
||||
payment.set 'stripe', {
|
||||
customerID: req.user.get('stripeCustomerID')
|
||||
customerID: req.user.get('stripe')?.customerID
|
||||
timestamp: parseInt(req.body.stripe.timestamp)
|
||||
chargeID: charge.id
|
||||
}
|
||||
|
|
|
@ -192,7 +192,7 @@ UserSchema.statics.hashPassword = (password) ->
|
|||
UserSchema.statics.privateProperties = [
|
||||
'permissions', 'email', 'mailChimp', 'firstName', 'lastName', 'gender', 'facebookID',
|
||||
'gplusID', 'music', 'volume', 'aceConfig', 'employerAt', 'signedEmployerAgreement',
|
||||
'emailSubscriptions', 'emails', 'activity', 'stripeCustomerID'
|
||||
'emailSubscriptions', 'emails', 'activity', 'stripe', 'stripeCustomerID'
|
||||
]
|
||||
UserSchema.statics.jsonSchema = jsonschema
|
||||
UserSchema.statics.editableProperties = [
|
||||
|
|
|
@ -109,7 +109,7 @@ describe '/db/payment', ->
|
|||
expect(body.purchaser).toBe(joeID)
|
||||
User.findById(joe.get('_id'), (err, user) ->
|
||||
expect(user.get('purchased').gems).toBe(5000)
|
||||
expect(user.get('stripeCustomerID')).toBe(body.stripe.customerID)
|
||||
expect(user.get('stripe').customerID).toBe(body.stripe.customerID)
|
||||
done()
|
||||
)
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue