Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created December 22, 2011 21:37
Show Gist options
  • Save joefiorini/1511971 to your computer and use it in GitHub Desktop.
Save joefiorini/1511971 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