avinasha
8/24/2012 - 10:09 AM

Integrating Stripe

Integrating Stripe

def active?
   not (expired? or exceeds_ticket_volume?)
end

def expired?
  expiry < Time.now
end
module SupportBee
  class StripeWebhooks 

    attr_reader :params, :company

    # 'params' is the POST params which Stripe sends to your application 
    def initialize(params)
      @params = get_event_from_stripe(params['id'])
      @company = get_company
    end

    def consume
      case @params['type']
      when "invoice.payment_succeeded"
	send_invoice
	extend_expiry
        notify_campfire
      when "customer.deleted"
        downgrade_to_trial
      end
    end

    private

    def get_company
      customer_id = @params['data']['object']['customer'] if @params['type'] == "invoice.payment_succeeded"
      customer_id = @params['data']['object']['id'] if @params['type'] == "customer.deleted"
      Company.find_by_stripe_customer_id(customer_id)
    end

    def get_event_from_stripe(event_id)
      Stripe::Event.retrieve(event_id)
    end

    ...
    ...
    ...

  end
end