Module: AmdirentStripe::StripeMixin
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/amdirent_stripe/stripe_mixin.rb
Instance Method Summary collapse
- #cancel_subscription!(id) ⇒ Object
- #charges ⇒ Object
- #has_active_subs? ⇒ Boolean
- #plans ⇒ Object
- #save_card!(tok) ⇒ Object
- #stripe_customer ⇒ Object
- #subscribe_to!(plan) ⇒ Object
- #subscriptions ⇒ Object
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..match(/does not have a subscription/)) raise NoSuchSubscriptionError.new end raise e end end |
#charges ⇒ Object
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
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 |
#plans ⇒ Object
17 18 19 |
# File 'lib/amdirent_stripe/stripe_mixin.rb', line 17 def plans ::Stripe::Plan.list() end |
#save_card!(tok) ⇒ Object
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..match(/No such source/) or e..match(/No such token/) raise NoSuchCardError end raise e end end |
#stripe_customer ⇒ Object
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..match(/No such customer/)) raise NoSuchCustomerError.new end raise e end end end |
#subscribe_to!(plan) ⇒ Object
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 |
#subscriptions ⇒ Object
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 |