Add prepaids to payments

Closes #3402
This commit is contained in:
Matt Lott 2016-02-17 05:34:12 -08:00
parent 37fbd8ca21
commit cb29d7068b
6 changed files with 50 additions and 43 deletions

View file

@ -10,6 +10,7 @@ PaymentSchema = c.object({title: 'Payment', required: []}, {
gems: { type: 'integer', description: 'The number of gems acquired.' }
productID: { enum: ['gems_5', 'gems_10', 'gems_20', 'custom']}
description: { type: 'string' }
prepaidID: c.objectId()
ios: c.object({title: 'iOS IAP Data'}, {
transactionID: { type: 'string' }

View file

@ -37,7 +37,7 @@ module.exports = class PrepaidView extends RootView
@codes = new CocoCollection([], { url: '/db/user/'+me.id+'/prepaid_codes', model: Prepaid })
@codes.on 'sync', (code) => @render?()
@supermodel.loadCollection(@codes, 'prepaid', {cache: false})
@supermodel.loadCollection(@codes, {cache: false})
@ppc = utils.getQueryVariable('_ppc') ? ''
unless _.isEmpty(@ppc)
@ -45,7 +45,7 @@ module.exports = class PrepaidView extends RootView
@loadPrepaid(@ppc)
@products = new Products()
@supermodel.loadCollection(@products, 'products')
@supermodel.loadCollection(@products)
onLoaded: ->
@prepaidProduct = @products.findWhere { name: 'prepaid_subscription' }
@ -190,7 +190,7 @@ module.exports = class PrepaidView extends RootView
@renderSelectors('#codes-panel')
@statusMessage "Finalizing purchase...", "information"
@supermodel.addRequestResource('purchase_prepaid', options, 0).load()
@supermodel.addRequestResource(options, 0).load()
loadPrepaid: (ppc) ->
return unless ppc

View file

@ -21,7 +21,7 @@ module.exports =
return done(err)
done(err, charge)
createPayment: (user, stripeCharge, done) ->
createPayment: (user, stripeCharge, extraProps, done) ->
payment = new Payment
purchaser: user._id
recipient: user._id
@ -34,6 +34,7 @@ module.exports =
customerID: stripeCharge.customer
timestamp: parseInt(stripeCharge.metadata.timestamp)
chargeID: stripeCharge.id
payment.set(prop, val) for prop, val of extraProps
validation = PaymentHandler.validateDocumentInput(payment.toObject())
if validation.valid is false
@logError(user, 'Invalid stripe payment object.')

View file

@ -156,7 +156,7 @@ class SubscriptionHandler extends Handler
@logSubscriptionError(req.user, "Purchase year sale create charge: #{JSON.stringify(err)}")
return @sendDatabaseError(res, err)
StripeUtils.createPayment req.user, charge, (err, payment) =>
StripeUtils.createPayment req.user, charge, {}, (err, payment) =>
if err
@logSubscriptionError(req.user, "Purchase year sale create payment: #{JSON.stringify(err)}")
return @sendDatabaseError(res, err)
@ -200,7 +200,7 @@ class SubscriptionHandler extends Handler
@logSubscriptionError(req.user, "Redeem Prepaid Code find: #{JSON.stringify(err)}")
return @sendDatabaseError(res, err)
unless prepaid
@logSubscriptionError(req.user, "Could not find prepaid code #{req.body.ppc}")
@logSubscriptionError(req.user, "Could not find prepaid code #{req.body.ppc?.toString()}")
return @sendNotFoundError(res, "Prepaid not found")
oldRedeemers = prepaid.get('redeemers') ? []

View file

@ -202,13 +202,18 @@ PrepaidHandler = class PrepaidHandler extends Handler
@logError(user, "createCharge error: #{JSON.stringify(err)}")
return done(err)
StripeUtils.createPayment user, charge, (err, payment) =>
@createPrepaid user, type, maxRedeemers, {}, (err, prepaid) =>
if err
@logError(user, "createPrepaid error: #{JSON.stringify(err)}")
return done(err)
StripeUtils.createPayment user, charge, {prepaidID: prepaid._id}, (err, payment) =>
if err
@logError(user, "createPayment error: #{JSON.stringify(err)}")
return done(err)
msg = "Prepaid code purchased: #{type} seats=#{maxRedeemers} #{user.get('email')}"
hipchat.sendHipChatMessage msg, ['tower']
@createPrepaid(user, type, maxRedeemers, {}, done)
done(null, prepaid)
purchasePrepaidTerminalSubscription: (user, description, maxRedeemers, months, timestamp, token, product, done) ->
type = 'terminal_subscription'
@ -234,27 +239,18 @@ PrepaidHandler = class PrepaidHandler extends Handler
@logError(user, "createCharge error: #{JSON.stringify(err)}")
return done(err)
StripeUtils.createPayment user, charge, (err, payment) =>
@createPrepaid user, type, maxRedeemers, {months: months}, (err, prepaid) =>
if err
@logError(user, "createPrepaid error: #{JSON.stringify(err)}")
return done(err)
StripeUtils.createPayment user, charge, {prepaidID: prepaid._id}, (err, payment) =>
if err
@logError(user, "createPayment error: #{JSON.stringify(err)}")
return done(err)
Prepaid.generateNewCode (code) =>
return done('Database error.') unless code
prepaid = new Prepaid
creator: user._id
type: type
code: code
maxRedeemers: parseInt(maxRedeemers)
redeemers: []
properties:
months: months
prepaid.save (err) =>
return done(err) if err
msg = "Prepaid code purchased: #{type} users=#{maxRedeemers} months=#{months} #{user.get('email')}"
hipchat.sendHipChatMessage msg, ['tower']
return done(null, prepaid)
done(null, prepaid)
get: (req, res) ->
if creator = req.query.creator

View file

@ -20,6 +20,11 @@ describe '/db/prepaid', ->
expect(prepaid.type).toEqual('course')
expect(prepaid.maxRedeemers).toBeGreaterThan(0)
expect(prepaid.code).toMatch(/^\w{8}$/)
return done() if user.isAdmin()
Payment.findOne {prepaidID: new ObjectId(prepaid._id)}, (err, payment) ->
expect(err).toBeNull()
expect(payment).not.toBeNull()
expect(payment?.get('purchaser')).toEqual(user._id)
done()
verifySubscriptionPrepaid = (user, prepaid, done) ->
@ -28,6 +33,11 @@ describe '/db/prepaid', ->
expect(prepaid.maxRedeemers).toBeGreaterThan(0)
expect(prepaid.code).toMatch(/^\w{8}$/)
expect(prepaid.properties?.couponID).toEqual('free')
return done() if user.isAdmin()
Payment.findOne {prepaidID: new ObjectId(prepaid._id)}, (err, payment) ->
expect(err).toBeNull()
expect(payment).not.toBeNull()
expect(payment?.get('purchaser')).toEqual(user._id)
done()
it 'Clear database', (done) ->
@ -690,7 +700,6 @@ describe '/db/prepaid', ->
expect(res.statusCode).toEqual(403)
done()
it 'Can fetch a list of purchased and redeemed prepaid codes', (done) ->
nockUtils.setupNock 'db-sub-redeem-test-2.json', (err, nockDone) ->
stripe.tokens.create {