Class: Pay::Braintree::Subscription
- Inherits:
-
Object
- Object
- Pay::Braintree::Subscription
- Defined in:
- lib/pay/braintree/subscription.rb
Instance Attribute Summary collapse
-
#pay_subscription ⇒ Object
readonly
Returns the value of attribute pay_subscription.
Instance Method Summary collapse
- #cancel(**options) ⇒ Object
- #cancel_now!(**options) ⇒ Object
- #change_quantity(quantity, **options) ⇒ Object
-
#initialize(pay_subscription) ⇒ Subscription
constructor
A new instance of Subscription.
- #on_grace_period? ⇒ Boolean
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #resume ⇒ Object
-
#retry_failed_payment ⇒ Object
Retries the latest invoice for a Past Due subscription.
- #subscription(**options) ⇒ Object
- #swap(plan, **options) ⇒ Object
Constructor Details
#initialize(pay_subscription) ⇒ Subscription
Returns a new instance of Subscription.
23 24 25 |
# File 'lib/pay/braintree/subscription.rb', line 23 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/braintree/subscription.rb', line 4 def pay_subscription @pay_subscription end |
Instance Method Details
#cancel(**options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pay/braintree/subscription.rb', line 31 def cancel(**) subscription = processor_subscription if on_trial? gateway.subscription.cancel(processor_subscription.id) pay_subscription.update(status: :canceled, ends_at: trial_ends_at) else gateway.subscription.update(subscription.id, { number_of_billing_cycles: subscription.current_billing_cycle }) pay_subscription.update(status: :canceled, ends_at: subscription.billing_period_end_date.to_date) end rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |
#cancel_now!(**options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pay/braintree/subscription.rb', line 47 def cancel_now!(**) gateway.subscription.cancel(processor_subscription.id) ends_at = Time.current pay_subscription.update!( status: :canceled, trial_ends_at: (ends_at if pay_subscription.trial_ends_at?), ends_at: ends_at ) rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |
#change_quantity(quantity, **options) ⇒ Object
59 60 61 |
# File 'lib/pay/braintree/subscription.rb', line 59 def change_quantity(quantity, **) raise NotImplementedError, "Braintree does not support setting quantity on subscriptions" end |
#on_grace_period? ⇒ Boolean
63 64 65 |
# File 'lib/pay/braintree/subscription.rb', line 63 def on_grace_period? canceled? && Time.current < ends_at end |
#pause ⇒ Object
71 72 73 |
# File 'lib/pay/braintree/subscription.rb', line 71 def pause raise NotImplementedError, "Braintree does not support pausing subscriptions" end |
#paused? ⇒ Boolean
67 68 69 |
# File 'lib/pay/braintree/subscription.rb', line 67 def paused? false end |
#resume ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/pay/braintree/subscription.rb', line 75 def resume unless on_grace_period? raise StandardError, "You can only resume subscriptions within their grace period." end if canceled? && on_trial? duration = trial_ends_at.to_date - Date.today customer.subscribe( name: name, plan: processor_plan, trial_period: true, trial_duration: duration.to_i, trial_duration_unit: :day ) else subscription = processor_subscription gateway.subscription.update(subscription.id, { never_expires: true, number_of_billing_cycles: nil }) end pay_subscription.update(status: :active) rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |
#retry_failed_payment ⇒ Object
Retries the latest invoice for a Past Due subscription
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/pay/braintree/subscription.rb', line 143 def retry_failed_payment result = gateway.subscription.retry_charge( processor_id, nil, # amount if different true # submit for settlement ) if result.success? pay_subscription.update(status: :active) end end |
#subscription(**options) ⇒ Object
27 28 29 |
# File 'lib/pay/braintree/subscription.rb', line 27 def subscription(**) gateway.subscription.find(processor_id) end |
#swap(plan, **options) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/pay/braintree/subscription.rb', line 104 def swap(plan, **) raise ArgumentError, "plan must be a string" unless plan.is_a?(String) if on_grace_period? && processor_plan == plan resume return end unless active? customer.subscribe(name: name, plan: plan, trial_period: false) return end braintree_plan = find_braintree_plan(plan) if would_change_billing_frequency?(braintree_plan) && prorate? swap_across_frequencies(braintree_plan) return end subscription = processor_subscription result = gateway.subscription.update(subscription.id, { plan_id: braintree_plan.id, price: braintree_plan.price, never_expires: true, number_of_billing_cycles: nil, options: { prorate_charges: prorate? } }) raise Error, "Braintree failed to swap plans: #{result.}" unless result.success? pay_subscription.update(processor_plan: plan, ends_at: nil, status: :active) rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |