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.
74 75 76 |
# File 'lib/pay/stripe/subscription.rb', line 74 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, stripe_account: nil, try: 0, retries: 1) ⇒ Object
22 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pay/stripe/subscription.rb', line 22 def self.sync(subscription_id, object: nil, name: Pay.default_product_name, stripe_account: nil, try: 0, retries: 1) # 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", "latest_invoice.charge.invoice"]}, {stripe_account: stripe_account}.compact) pay_customer = Pay::Customer.find_by(processor: :stripe, processor_id: object.customer) return unless pay_customer attributes = { application_fee_percent: object.application_fee_percent, processor_plan: object.plan.id, quantity: object.quantity, status: object.status, stripe_account: pay_customer.stripe_account, trial_ends_at: (object.trial_end ? Time.at(object.trial_end) : nil), metadata: object. } attributes[:ends_at] = if object.ended_at # Fully cancelled subscription Time.at(object.ended_at) elsif object.cancel_at # subscription cancelling in the future Time.at(object.cancel_at) elsif object.cancel_at_period_end # Subscriptions cancelling in the future Time.at(object.current_period_end) end # Update or create the subscription pay_subscription = pay_customer.subscriptions.find_by(processor_id: object.id) if pay_subscription pay_subscription.with_lock { pay_subscription.update!(attributes) } else pay_subscription = pay_customer.subscriptions.create!(attributes.merge(name: name, processor_id: object.id)) end # Sync the latest charge if we already have it loaded (like during subscrbe), otherwise, let webhooks take care of creating it if (charge = object.try(:latest_invoice).try(:charge)) && charge.try(:status) == "succeeded" Pay::Stripe::Charge.sync(charge.id, object: charge) end pay_subscription rescue ActiveRecord::RecordInvalid try += 1 if try <= retries sleep 0.1 retry else raise end end |
Instance Method Details
#cancel ⇒ Object
84 85 86 87 88 89 |
# File 'lib/pay/stripe/subscription.rb', line 84 def cancel stripe_sub = ::Stripe::Subscription.update(processor_id, {cancel_at_period_end: true}, ) 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
91 92 93 94 95 96 |
# File 'lib/pay/stripe/subscription.rb', line 91 def cancel_now! ::Stripe::Subscription.delete(processor_id, {}, ) pay_subscription.update(ends_at: Time.current, status: :canceled) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#change_quantity(quantity) ⇒ Object
98 99 100 101 102 |
# File 'lib/pay/stripe/subscription.rb', line 98 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
104 105 106 |
# File 'lib/pay/stripe/subscription.rb', line 104 def on_grace_period? canceled? && Time.current < ends_at end |
#pause ⇒ Object
112 113 114 |
# File 'lib/pay/stripe/subscription.rb', line 112 def pause raise NotImplementedError, "Stripe does not support pausing subscriptions" end |
#paused? ⇒ Boolean
108 109 110 |
# File 'lib/pay/stripe/subscription.rb', line 108 def paused? false end |
#resume ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/pay/stripe/subscription.rb', line 116 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 }, ) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#subscription(**options) ⇒ Object
78 79 80 81 82 |
# File 'lib/pay/stripe/subscription.rb', line 78 def subscription(**) [:id] = processor_id [:expand] ||= ["pending_setup_intent", "latest_invoice.payment_intent", "latest_invoice.charge.invoice"] ::Stripe::Subscription.retrieve(, {stripe_account: stripe_account}.compact) end |
#swap(plan) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/pay/stripe/subscription.rb', line 134 def swap(plan) raise ArgumentError, "plan must be a string" unless plan.is_a?(String) ::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 }, ) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |