Skip to content

Instantly share code, notes, and snippets.

Created December 22, 2011 21:37
What would you like to do?
Backbone + Stripe = AWESOME
class Stripe.TokenRequest extends Backbone.Model
validate: (attrs)->
error =
if !
attribute: "name"
message: "Please enter your name"
else if !
attribute: "email"
message: "Invalid email"
else if !Stripe.validateCardNumber(attrs.number)
attribute: "number"
message: "Invalid card number"
else if(!Stripe.validateExpiry attrs.exp_month, attrs.exp_year)
attribute: "exp_year"
message: "Invalid expiration date"
else if(!Stripe.validateCVC attrs.cvc)
attribute: "cvc"
message: "Invalid CVC code"
sync: (method, model, options)->
fields = model.attributes
amount = model.attributes.amount
delete model.attributes.amount
callback = (status, response)->
if response.error
options.error(response.error, model, options)
token = new Stripe.Token response
model.token = token
options.success(model, status, response)
Stripe.createToken fields, amount, callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment