Class: Pay::Braintree::Subscription
- Inherits:
-
Subscription
- Object
- Subscription
- Pay::Braintree::Subscription
- Extended by:
- Sync
- Defined in:
- app/models/pay/braintree/subscription.rb
Class Method Summary collapse
Instance Method Summary collapse
- #api_record(**options) ⇒ Object
- #cancel(**options) ⇒ Object
- #cancel_now!(**options) ⇒ Object
- #change_quantity(quantity, **options) ⇒ Object
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #resumable? ⇒ Boolean
- #resume ⇒ Object
-
#retry_failed_payment ⇒ Object
Retries the latest invoice for a Past Due subscription.
- #swap(plan, **options) ⇒ Object
Methods included from Sync
find_pay_customer, pay_processor, sync_with_retries
Class Method Details
.sync(subscription_id, object: nil, name: nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/pay/braintree/subscription.rb', line 6 def self.sync(subscription_id, object: nil, name: nil) sync_with_retries do subscription = object || Pay.braintree_gateway.subscription.find(subscription_id) # The customer is only reachable through the subscription's payment method payment_method = Pay.braintree_gateway.payment_method.find(subscription.payment_method_token) return unless (pay_customer = find_pay_customer(payment_method.customer_id)) # Sync the PaymentMethod since we've got it pay_customer.save_payment_method(payment_method, default: payment_method.default?) attributes = { created_at: subscription.created_at, current_period_end: subscription.billing_period_end_date, current_period_start: subscription.billing_period_start_date, payment_method_id: subscription.payment_method_token, processor_plan: subscription.plan_id, status: subscription.status.parameterize(separator: "_"), trial_ends_at: (subscription.created_at + subscription.trial_duration.send(subscription.trial_duration_unit) if subscription.trial_period) } # Canceled subscriptions should have access through the paid_through_date or updated_at if subscription.status == "Canceled" attributes[:ends_at] = subscription.updated_at # Set grace period for subscriptions that are marked to be canceled elsif subscription.status == "Active" && subscription.number_of_billing_cycles attributes[:ends_at] = subscription.paid_through_date.end_of_day end if (pay_subscription = find_by(customer: pay_customer, processor_id: subscription.id)) pay_subscription.with_lock { pay_subscription.update!(attributes) } else name ||= Pay.default_product_name create!(attributes.merge(customer: pay_customer, name: name, processor_id: subscription.id)) end end end |
Instance Method Details
#api_record(**options) ⇒ Object
45 46 47 |
# File 'app/models/pay/braintree/subscription.rb', line 45 def api_record(**) gateway.subscription.find(processor_id) end |
#cancel(**options) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/pay/braintree/subscription.rb', line 49 def cancel(**) return if canceled? # Braintree doesn't allow canceling at period end while on trial, so trials are canceled immediately result = if on_trial? gateway.subscription.cancel(processor_id) else gateway.subscription.update(processor_id, {number_of_billing_cycles: api_record.current_billing_cycle}) end sync!(object: result.subscription) rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |
#cancel_now!(**options) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'app/models/pay/braintree/subscription.rb', line 63 def cancel_now!(**) return if canceled? result = gateway.subscription.cancel(processor_id) sync!(object: result.subscription) rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |
#change_quantity(quantity, **options) ⇒ Object
72 73 74 |
# File 'app/models/pay/braintree/subscription.rb', line 72 def change_quantity(quantity, **) raise Pay::NotSupportedError, "Braintree does not support setting quantity on subscriptions" end |
#pause ⇒ Object
80 81 82 |
# File 'app/models/pay/braintree/subscription.rb', line 80 def pause raise Pay::NotSupportedError, "Braintree does not support pausing subscriptions" end |
#paused? ⇒ Boolean
76 77 78 |
# File 'app/models/pay/braintree/subscription.rb', line 76 def paused? false end |
#resumable? ⇒ Boolean
84 85 86 |
# File 'app/models/pay/braintree/subscription.rb', line 84 def resumable? on_grace_period? end |
#resume ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/models/pay/braintree/subscription.rb', line 88 def resume unless resumable? raise Error, "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 gateway.subscription.update(processor_id, { never_expires: true, number_of_billing_cycles: nil }) end update(ends_at: nil, 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
153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/models/pay/braintree/subscription.rb', line 153 def retry_failed_payment result = gateway.subscription.retry_charge( processor_id, nil, # amount if different true # submit for settlement ) if result.success? update(status: :active) end end |
#swap(plan, **options) ⇒ Object
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 141 142 143 144 145 146 147 148 149 150 |
# File 'app/models/pay/braintree/subscription.rb', line 115 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) prorate = .fetch(:prorate) { true } if would_change_billing_frequency?(braintree_plan) && prorate swap_across_frequencies(braintree_plan) return end result = gateway.subscription.update(processor_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? update(processor_plan: plan, ends_at: nil, status: :active) rescue ::Braintree::BraintreeError => e raise Pay::Braintree::Error, e end |