Skip to content

Instantly share code, notes, and snippets.

@dingyi
Forked from joefiorini/gist:1511971
Created January 9, 2012 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingyi/1584007 to your computer and use it in GitHub Desktop.
Save dingyi/1584007 to your computer and use it in GitHub Desktop.
Backbone + Stripe = AWESOME
class Stripe.TokenRequest extends Backbone.Model
validate: (attrs)->
error =
if !attrs.name
attribute: "name"
message: "Please enter your name"
else if !attrs.email
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)
else
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