carcass-stripe
Version:
(Node.js) A simple wrap of stripe APIs, in Carcass style.
102 lines (80 loc) • 2.21 kB
text/coffeescript
# debug = require('debug')('carcass:plugin:modella')
_ = require('lodash')
carcass = require('carcass')
httpError = carcass.httpError
highland = carcass.highland
###*
* Plugin for Modella.
*
* Add the ability of doing CRUD with CouchDB to a model.
###
module.exports = (Model) ->
###*
* Meant to be overridden.
*
* {Object} an instance of CouchDB.
###
Model.stripeClient = -> throw new Error('require a stripe client')
###*
* Accessor.
###
Model::stripeClient = carcass.helpers.accessor('_stripe_client', {
getDefault: -> return .stripeClient()
})
###*
* Create card
*
###
Model::createCard = (customerId, card, done = ->) ->
stripe =
stripe
.customers
.createCard(customerId, {card: card}, (err, card) ->
return done(httpError(err)) if err
done(null, card)
)
return @
###*
* Create customer
*
* {this}
###
Model::createCustomer = (options = {}, done = ->) ->
stripe =
stripe
.customers
.create(options, (err, customer) ->
return done(httpError(err)) if err
done(null, customer)
)
return @
###*
* Create subscription
*
* {this}
###
Model::createSubscription = (customerId, planId, options = {}, done = ->) ->
stripe =
options.plan = planId
stripe
.customers
.createSubscription(customerId, options, (err, subscription) ->
return done(httpError(err)) if err
done(null, subscription)
)
return @
###*
* Update subscription
*
* {this}
###
Model::updateSubscription = (customerId, subscriptionId, planId, options = {}, done = ->) ->
stripe =
options.plan = planId
stripe
.customers
.updateSubscription(customerId, subscriptionId, options, (err, newSub) ->
return done(httpError(err)) if err
done(null, newSub)
)
return @