Skip to content

Instantly share code, notes, and snippets.

@mattsgarrison
Created August 16, 2012 13:34
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 mattsgarrison/3370109 to your computer and use it in GitHub Desktop.
Save mattsgarrison/3370109 to your computer and use it in GitHub Desktop.
Stripe Credit Card Processing
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
subscription.setupForm()
subscription =
setupForm: ->
$('#new_subscription input[type=submit]').click ->
$(this).attr('disabled', true) #disable to prevent doubleclicks·
subscription.processCard()
processCard: ->
Stripe.createToken
number: $("#card_number").val()
cvc: $("#card_code").val()
exp_month: $("#card_month").val()
exp_year: $("#card_year").val()
, subscription.handleStripeResponse
handleStripeResponse: (status, response) ->
if status == 200
console.log(response.id)
$("#subscription_stripe_card_token").val(response.id)
$("#new_subscription").submit()
else
alert(response.error.message)
$('#new_subscription input[type=submit]').attr('disabled', false)
class StripeWebhooksController < ApplicationController
require 'json'
def receiver
data_json = JSON.parse request.body.read
customer = data_json['data']['object']['customer']
case data_json['type']
when "charge.succeeded"
Rails.logger.info "Stripe Charge Succeeded"
when "charge.failed"
Rails.logger.info "Stripe Charge Failed"
when "charge.refunded"
Rails.logger.info "Stripe Charge Refunded"
when "charge.disputed"
Rails.logger.info "Stripe Charge Disputed"
when "customer.created"
Rails.logger.info "Stripe Customer Created"
when "customer.updated"
Rails.logger.info "Stripe Customer Created"
when "customer.deleted"
Rails.logger.info "Stripe Customer Deleted"
when "customer.subscription.created"
Rails.logger.info "Stripe Subscription Created"
when "customer.subscription.updated"
Rails.logger.info "Stripe Subscription Updated"
when "customer.subscription.deleted"
Rails.logger.info "Stripe Subscription Deleted"
when "customer.subscription.trial_will_end"
Rails.logger.info "Stripe Subscription Trial Will End"
when "customer.discount.created"
Rails.logger.info "Stripe Discount Created"
when "customer.discount.updated"
Rails.logger.info "Stripe Discount Updated"
when "customer.discount.deleted"
Rails.logger.info "Stripe Discount Deleted"
when "invoice.created"
Rails.logger.info "Stripe Invoice Created"
when "invoice.updated"
Rails.logger.info "Stripe Invoice Updated"
when "invoice.payment_succeeded"
Rails.logger.info "Stripe Payment Succeeded"
when "invoice.payment_failed"
Rails.logger.info "Stripe Payment Failed"
when "invoiceitem.created"
Rails.logger.info "Stripe Invoice Item Created"
when "invoiceitem.updated"
Rails.logger.info "Stripe Invoice Item Updated"
when "invoiceitem.deleted"
Rails.logger.info "Stripe Invoice Item Deleted"
when "plan.created"
Rails.logger.info "Stripe Plan Created"
when "plan.updated"
Rails.logger.info "Stripe Plan Updated"
when "plan.deleted"
Rails.logger.info "Stripe Plan Deleted"
when "coupon.created"
Rails.logger.info "Stripe Coupon Created"
when "coupon.updated"
Rails.logger.info "Stripe Coupon Updated"
when "transfer.created"
Rails.logger.info "Stripe Transfer Created"
when "transfer.failed"
Rails.logger.info "Stripe Transfer Failed"
when "ping"
Rails.logger.info "Stripe Pinged the Webhook"
else
Rails.logger.info "Received unknown Stripe webhook call: #{data_json.to_yaml}"
end #end case
render :nothing => true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment