codecombat/server/lib/stripe_utils.coffee
Matt Lott 5095eac4ac Purchase prepaid on course instance creation
Updating purchase prepaid API to support courses.
Refactoring the prepaid server code.

Completes #54270567235517
2015-10-01 15:23:20 -07:00

81 lines
3 KiB
CoffeeScript

log = require 'winston'
Payment = require '../payments/Payment'
PaymentHandler = require '../payments/payment_handler'
module.exports =
logError: (user, msg) ->
log.error "Stripe Utils Error: #{user.get('slug')} (#{user._id}): '#{msg}'"
createCharge: (user, amount, metadata, done) ->
# TODO: create Stripe customer if necessary
options =
amount: amount
currency: 'usd'
customer: user.get('stripe')?.customerID
metadata: metadata
receipt_email: user.get('email')
statement_descriptor: 'CODECOMBAT.COM'
stripe.charges.create options, (err, charge) =>
if err
@logError(user, "Charge create error: #{JSON.stringify(err)}")
return done(err)
done(err, charge)
createPayment: (user, stripeCharge, done) ->
payment = new Payment
purchaser: user._id
recipient: user._id
created: new Date().toISOString()
service: 'stripe'
amount: parseInt(stripeCharge.amount)
payment.set 'description', stripeCharge.metadata.description if stripeCharge.metadata.description
payment.set 'gems', parseInt(stripeCharge.metadata.gems) if stripeCharge.metadata.gems
payment.set 'stripe',
customerID: stripeCharge.customer
timestamp: parseInt(stripeCharge.metadata.timestamp)
chargeID: stripeCharge.id
validation = PaymentHandler.validateDocumentInput(payment.toObject())
if validation.valid is false
@logError(user, 'Invalid stripe payment object.')
return done(validation.errors)
payment.save (err) =>
if err
@logError(user, "Payment save error: #{JSON.stringify(err)}")
return done(err)
done(err, payment)
getCustomer: (user, token, done) ->
# If necessary, creates new Stripe customer and saves to user
customerID = user.get('stripe')?.customerID
if customerID
if token
# old customer, new token. Save it.
stripe.customers.update customerID, { card: token }, (err, customer) =>
if err
@logError(user, "Customer update error: #{JSON.stringify(err)}")
return done(err)
done(err, customer)
else
stripe.customers.retrieve customerID, (err, customer) =>
if err
@logError(user, "Customer retrieve error: #{JSON.stringify(err)}")
return done(err)
done(err, customer)
else
newCustomer = {
card: token
email: user.get('email')
metadata: { id: user._id + '', slug: user.get('slug') }
}
stripe.customers.create newCustomer, (err, customer) =>
if err
@logError(user, "Customer creation error: #{JSON.stringify(err)}")
return done(err)
stripeInfo = _.cloneDeep(user.get('stripe') ? {})
stripeInfo.customerID = customer.id
user.set('stripe', stripeInfo)
user.save (err) =>
if err
@logError(user, 'Stripe customer id save db error. '+err)
return done(err)
done(err, customer)