Class: Billy::Paddle::Subscription
- Inherits:
-
Object
- Object
- Billy::Paddle::Subscription
- Defined in:
- lib/billy/paddle/subscription.rb
Instance Attribute Summary collapse
-
#billy_subscription ⇒ Object
readonly
Returns the value of attribute billy_subscription.
Class Method Summary collapse
Instance Method Summary collapse
-
#cancel ⇒ Object
Cancels a subscription at the end of the subscription or right now.
-
#cancel_now! ⇒ Object
Cancels a subscription right now.
-
#initialize(billy_subscription) ⇒ Subscription
constructor
A new instance of Subscription.
-
#pause ⇒ Object
Pause a subscription.
-
#resume ⇒ Object
Resume a subscription.
-
#subscription(**options) ⇒ Object
Grab the Subscription info from the Paddle API.
Constructor Details
#initialize(billy_subscription) ⇒ Subscription
Returns a new instance of Subscription.
7 8 9 |
# File 'lib/billy/paddle/subscription.rb', line 7 def initialize(billy_subscription) @billy_subscription = billy_subscription end |
Instance Attribute Details
#billy_subscription ⇒ Object (readonly)
Returns the value of attribute billy_subscription.
5 6 7 |
# File 'lib/billy/paddle/subscription.rb', line 5 def billy_subscription @billy_subscription end |
Class Method Details
.sync(paddle_id:, object: nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/billy/paddle/subscription.rb', line 26 def self.sync(paddle_id:, object: nil) # Passthrough is not return from this API, so we can't use that object ||= Billy::Paddle.client.users.list(subscription_id: paddle_id).data.first billy_account = Billy::Account.find_by(processor: :paddle, processor_id: object.user_id) # If passthrough exists (only on webhooks) we can use it to create the Pay::Customer if billy_account.nil? && object.passthrough owner = Billy::Paddle.owner_from_passthrough(object.passthrough) return unless owner billy_account = owner.billy_accounts.find_or_create_by processor: :paddle, processor_id: object.user_id billy_account.make_default! end return unless billy_account plan = Billy::Plan.find_by(processor: "paddle", processor_id: object.plan_id || object.subscription_plan_id) attributes = { cancel_url: object.cancel_url, update_url: object.update_url, plan: plan, status: object.state || object.status } # If paused or delete while on trial, set ends_at to match case attributes[:status] when "trialing" attributes[:trial_ends_at] = Time.zone.parse(object.next_bill_date) attributes[:ends_at] = nil when "paused", "deleted" attributes[:trial_ends_at] = nil attributes[:ends_at] = Time.zone.parse(object.next_bill_date) end # Update or create the subscription if (billy_subscription = billy_account.subscriptions.find_by(processor: "paddle", processor_id: object.subscription_id)) billy_subscription.with_lock do billy_subscription.update!(attributes) end billy_subscription else sub = billy_account.subscriptions.create!(attributes.merge(processor: "paddle", processor_id: object.subscription_id)) sub.make_default! # Send Discord Event billy_account.discord(kind: :subscription_created, desc: "Plan: #{plan.name} (#{plan.interval}ly)") end end |
Instance Method Details
#cancel ⇒ Object
Cancels a subscription at the end of the subscription or right now
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/billy/paddle/subscription.rb', line 87 def cancel ends_at = if on_trial? trial_ends_at elsif paused? paused_from else subscription.next_payment.date || Time.current end Billy::Paddle.client.users.cancel(subscription_id: processor_id) billy_subscription.update(status: :cancelled, ends_at: ends_at) # Remove payment methods since customer cannot be reused after cancelling Billy::PaymentMethod.where(account_id: billy_subscription.account_id).destroy_all rescue ::Paddle::Error => e raise Billy::Errors::PaddleError, e end |
#cancel_now! ⇒ Object
Cancels a subscription right now
107 108 109 110 111 112 113 114 115 |
# File 'lib/billy/paddle/subscription.rb', line 107 def cancel_now! Billy::Paddle.client.users.cancel(subscription_id: processor_id) billy_subscription.update(status: :cancelled, ends_at: Time.current) # Remove payment methods since customer cannot be reused after cancelling Billy::PaymentMethod.where(account_id: billy_subscription.account_id).destroy_all rescue ::Paddle::Error => e raise Billy::Errors::PaddleError, e end |
#pause ⇒ Object
Pause a subscription
119 120 121 122 123 124 |
# File 'lib/billy/paddle/subscription.rb', line 119 def pause response = Billy::Paddle.client.users.update(subscription_id: processor_id, pause: true) billy_subscription.update(status: "paused", paused_from: response.next_payment.date) rescue ::Paddle::Error => e raise Billy::Errors::PaddleError, e end |
#resume ⇒ Object
Resume a subscription
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/billy/paddle/subscription.rb', line 128 def resume unless paused? raise StandardError, "You can only resume paused subscriptions." end Billy::Paddle.client.users.update(subscription_id: processor_id, pause: false) billy_subscription.update(status: :active, paused_from: nil) rescue ::Paddle::Error => e raise Billy::Errors::PaddleError, e end |
#subscription(**options) ⇒ Object
Grab the Subscription info from the Paddle API
79 80 81 82 83 |
# File 'lib/billy/paddle/subscription.rb', line 79 def subscription(**) Billy::Paddle.client.users.list(subscription_id: processor_id).data.first rescue ::Paddle::Error => e raise Billy::Errors::PaddleError, e end |