Class: Payflow::SubscriptionService
- Inherits:
-
Object
- Object
- Payflow::SubscriptionService
- Defined in:
- lib/payflow/subscription_service.rb
Instance Method Summary collapse
- #cancel! ⇒ Object
-
#initialize(billable:) ⇒ SubscriptionService
constructor
A new instance of SubscriptionService.
- #subscribe!(plan:, provider: Payflow.config.provider) ⇒ Object
Constructor Details
#initialize(billable:) ⇒ SubscriptionService
Returns a new instance of SubscriptionService.
5 6 7 |
# File 'lib/payflow/subscription_service.rb', line 5 def initialize(billable:) @billable = billable end |
Instance Method Details
#cancel! ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/payflow/subscription_service.rb', line 28 def cancel! subscription = @billable.subscription raise SubscriptionError, "No active subscription" unless subscription client = Payflow.provider(subscription.provider.to_sym) client.cancel_subscription(subscription.external_id) subscription.update!(status: :cancelled, cancelled_at: Time.current) subscription end |
#subscribe!(plan:, provider: Payflow.config.provider) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/payflow/subscription_service.rb', line 9 def subscribe!(plan:, provider: Payflow.config.provider) current = @billable.subscription raise SubscriptionError, "Already subscribed" if current&.active? client = Payflow.provider(provider) remote = client.create_subscription( customer_id: customer_id_for(@billable), plan_id: plan ) Subscription.create!( billable: @billable, plan: plan, provider: provider.to_s, external_id: remote[:id], status: :active ) end |