module AmdirentStripe::StripeMixin

Public Instance Methods

cancel_subscription!(id) click to toggle source
# File lib/amdirent_stripe/stripe_mixin.rb, line 56
def cancel_subscription!(id)
  begin
    stripe_customer.subscriptions.retrieve(id).delete
  rescue  ::Stripe::InvalidRequestError => e
    if(e.message.match(/does not have a subscription/))
      raise NoSuchSubscriptionError.new
    end
    raise e
  end
end
charges() click to toggle source
# File lib/amdirent_stripe/stripe_mixin.rb, line 12
def charges
  raise NoCustomerError.new unless stripe_customer
  ::Stripe::Charge.list(customer: stripe_key)
end
has_active_subs?() click to toggle source
# File lib/amdirent_stripe/stripe_mixin.rb, line 67
def has_active_subs?
  subscriptions.data.any? { |sub| !sub.ended_at and !sub.canceled_at }
end
plans() click to toggle source
# File lib/amdirent_stripe/stripe_mixin.rb, line 17
def plans
  ::Stripe::Plan.list()
end
save_card!(tok) click to toggle source
# File lib/amdirent_stripe/stripe_mixin.rb, line 71
def save_card!(tok)
  raise NoCustomerError.new unless stripe_customer
  begin
    stripe_customer.source = tok
    stripe_customer.save
  rescue  ::Stripe::InvalidRequestError => e
    if e.message.match(/No such source/) or e.message.match(/No such token/)
      raise NoSuchCardError
    end
    raise e
  end
end
stripe_customer() click to toggle source
# File lib/amdirent_stripe/stripe_mixin.rb, line 37
def stripe_customer
  if stripe_key
    begin
      @stripe_customer ||= ::Stripe::Customer.retrieve(stripe_key)

      if(@stripe_customer.deleted?)
        raise NoSuchCustomerError.new
      end

      @stripe_customer
    rescue ::Stripe::InvalidRequestError => e
      if(e.message.match(/No such customer/))
        raise NoSuchCustomerError.new
      end
      raise e
    end
  end
end
subscribe_to!(plan) click to toggle source
# File lib/amdirent_stripe/stripe_mixin.rb, line 27
def subscribe_to!(plan)
  raise NoCustomerError.new unless stripe_customer
  raise NoCardError.new unless stripe_customer.default_source

  ::Stripe::Subscription.create(
    :customer => stripe_key,
    :plan => plan.is_a?(String) ? plan : plan.id
  )
end
subscriptions() click to toggle source
# File lib/amdirent_stripe/stripe_mixin.rb, line 21
def subscriptions
  raise NoCustomerError.new unless stripe_customer
  ::Stripe::Subscription.list(customer: stripe_key)
end

Private Instance Methods

charge!(amt, desc) click to toggle source
# File lib/amdirent_stripe/stripe_mixin.rb, line 85
def charge!(amt, desc)
  raise NoCustomerError.new unless stripe_customer
  raise NoCardError.new unless stripe_customer.default_source

  ::Stripe::Charge.create(
    :amount => amt,
    :currency => "usd",
    :customer => stripe_key,
    :description => "Charge for #{email} for #{desc}"
  )
end