Class: Pay::Stripe::Subscription
- Inherits:
-
Object
- Object
- Pay::Stripe::Subscription
- Defined in:
- lib/pay/stripe/subscription.rb
Instance Attribute Summary collapse
-
#pay_subscription ⇒ Object
readonly
Returns the value of attribute pay_subscription.
Class Method Summary collapse
Instance Method Summary collapse
- #cancel ⇒ Object
- #cancel_now! ⇒ Object
- #change_quantity(quantity) ⇒ Object
-
#initialize(pay_subscription) ⇒ Subscription
constructor
A new instance of Subscription.
- #on_grace_period? ⇒ Boolean
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #resume ⇒ Object
- #subscription(**options) ⇒ Object
- #swap(plan) ⇒ Object
Constructor Details
#initialize(pay_subscription) ⇒ Subscription
Returns a new instance of Subscription.
52 53 54 |
# File 'lib/pay/stripe/subscription.rb', line 52 def initialize(pay_subscription) @pay_subscription = pay_subscription end |
Instance Attribute Details
#pay_subscription ⇒ Object (readonly)
Returns the value of attribute pay_subscription.
4 5 6 |
# File 'lib/pay/stripe/subscription.rb', line 4 def pay_subscription @pay_subscription end |
Class Method Details
.sync(subscription_id, object: nil, name: Pay.default_product_name) ⇒ Object
23 24 25 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 |
# File 'lib/pay/stripe/subscription.rb', line 23 def self.sync(subscription_id, object: nil, name: Pay.default_product_name) # Skip loading the latest subscription details from the API if we already have it object ||= ::Stripe::Subscription.retrieve(id: subscription_id, expand: ["pending_setup_intent", "latest_invoice.payment_intent"]) owner = Pay.find_billable(processor: :stripe, processor_id: object.customer) return unless owner attributes = { application_fee_percent: object.application_fee_percent, processor_plan: object.plan.id, quantity: object.quantity, name: name, status: object.status, stripe_account: owner.stripe_account, trial_ends_at: (object.trial_end ? Time.at(object.trial_end) : nil) } # Subscriptions cancelling in the future attributes[:ends_at] = Time.at(object.current_period_end) if object.cancel_at_period_end # Fully cancelled subscription attributes[:ends_at] = Time.at(object.ended_at) if object.ended_at # Update or create the subscription pay_subscription = owner.subscriptions.find_or_initialize_by(processor: :stripe, processor_id: object.id) pay_subscription.update(attributes) pay_subscription end |
Instance Method Details
#cancel ⇒ Object
60 61 62 63 64 65 |
# File 'lib/pay/stripe/subscription.rb', line 60 def cancel stripe_sub = ::Stripe::Subscription.update(processor_id, {cancel_at_period_end: true}, {stripe_account: stripe_account}) pay_subscription.update(ends_at: (on_trial? ? trial_ends_at : Time.at(stripe_sub.current_period_end))) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#cancel_now! ⇒ Object
67 68 69 70 71 72 |
# File 'lib/pay/stripe/subscription.rb', line 67 def cancel_now! ::Stripe::Subscription.delete(processor_id, {stripe_account: stripe_account}) pay_subscription.update(ends_at: Time.current, status: :canceled) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#change_quantity(quantity) ⇒ Object
74 75 76 77 78 |
# File 'lib/pay/stripe/subscription.rb', line 74 def change_quantity(quantity) ::Stripe::Subscription.update(processor_id, quantity: quantity) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#on_grace_period? ⇒ Boolean
80 81 82 |
# File 'lib/pay/stripe/subscription.rb', line 80 def on_grace_period? canceled? && Time.zone.now < ends_at end |
#pause ⇒ Object
88 89 90 |
# File 'lib/pay/stripe/subscription.rb', line 88 def pause raise NotImplementedError, "Stripe does not support pausing subscriptions" end |
#paused? ⇒ Boolean
84 85 86 |
# File 'lib/pay/stripe/subscription.rb', line 84 def paused? false end |
#resume ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/pay/stripe/subscription.rb', line 92 def resume unless on_grace_period? raise StandardError, "You can only resume subscriptions within their grace period." end ::Stripe::Subscription.update( processor_id, { plan: processor_plan, trial_end: (on_trial? ? trial_ends_at.to_i : "now"), cancel_at_period_end: false }, {stripe_account: stripe_account} ) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#subscription(**options) ⇒ Object
56 57 58 |
# File 'lib/pay/stripe/subscription.rb', line 56 def subscription(**) ::Stripe::Subscription.retrieve(.merge(id: processor_id)) end |
#swap(plan) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/pay/stripe/subscription.rb', line 110 def swap(plan) ::Stripe::Subscription.update( processor_id, { cancel_at_period_end: false, plan: plan, proration_behavior: (prorate ? "create_prorations" : "none"), trial_end: (on_trial? ? trial_ends_at.to_i : "now"), quantity: quantity }, {stripe_account: stripe_account} ) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |