Skip to content

Instantly share code, notes, and snippets.

@thogg4
Created February 27, 2013 16:04
Show Gist options
  • Select an option

  • Save thogg4/5049028 to your computer and use it in GitHub Desktop.

Select an option

Save thogg4/5049028 to your computer and use it in GitHub Desktop.
Striper, for stripe
$ ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').prop('content'))
Striper.init
Striper =
init: (options) ->
defaults =
form: 'form.billing'
formButton: '.billing_button'
errorContainer: '.stripe_error'
tokenInput: '.stripe_card_token'
card:
numberInput: '.card_number'
monthInput: '.card_month'
yearInput: '.card_year'
cvcInput: '.card_cvc'
this.opts = $.extend(true, defaults, options)
setupForm: ->
$(this.opts.form).on 'submit', ->
$(this.opts.errorContainer).hide()
$(this.opts.formButton).prop('disabled', true)
if $(this.opts.card.numberInput).val().indexOf("****") == 0
true
else
this.processCard()
false
processCard: ->
card =
number: $(this.opts.card.numberInput).val()
cvc: $(this.opts.card.cvcInput).val()
expMonth: $(this.opts.card.monthInput).val()
expYear: $(this.opts.card.yearInput).val()
Stripe.createToken(card, subscription.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 200
$(this.opts.tokenInput).val(response.id)
$(this.opts.form).submit()
else
$(this.opts.errorContainer).text(response.error.message)
$(this.opts.errorContainer).show()
$(this.opts.formButton).attr('disabled', false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment