Created
December 22, 2011 21:37
-
-
Save joefiorini/1511971 to your computer and use it in GitHub Desktop.
Backbone + Stripe = AWESOME
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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