Module: AmdirentStripe::StripeMixin

Extended by:
ActiveSupport::Concern
Defined in:
lib/amdirent_stripe/stripe_mixin.rb

Instance Method Summary collapse

Instance Method Details

#cancel_subscription!(id) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# 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

#chargesObject

Raises:



12
13
14
15
# 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?Boolean

Returns:

  • (Boolean)


67
68
69
# 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

#plansObject



17
18
19
# File 'lib/amdirent_stripe/stripe_mixin.rb', line 17

def plans
  ::Stripe::Plan.list()
end

#save_card!(tok) ⇒ Object

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
# 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_customerObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
# 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

#subscriptionsObject

Raises:



21
22
23
24
# File 'lib/amdirent_stripe/stripe_mixin.rb', line 21

def subscriptions
  raise NoCustomerError.new unless stripe_customer
  ::Stripe::Subscription.list(customer: stripe_key)
end