Class: Pay::Braintree::Subscription

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/braintree/subscription.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pay_subscription) ⇒ Subscription

Returns a new instance of Subscription.



72
73
74
# File 'lib/pay/braintree/subscription.rb', line 72

def initialize(pay_subscription)
  @pay_subscription = pay_subscription
end

Instance Attribute Details

#pay_subscriptionObject (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

Class Method Details

.sync(subscription_id, object: nil, name: nil, try: 0, retries: 1) ⇒ 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pay/braintree/subscription.rb', line 23

def self.sync(subscription_id, object: nil, name: nil, try: 0, retries: 1)
  object ||= Pay.braintree_gateway.subscription.find(subscription_id)

  # Retrieve Pay::Customer
  payment_method = Pay.braintree_gateway.payment_method.find(object.payment_method_token)
  pay_customer = Pay::Customer.find_by(processor: :braintree, processor_id: payment_method.customer_id)
  return unless pay_customer

  # Sync the PaymentMethod since we've got it
  pay_customer.save_payment_method(payment_method, default: payment_method.default?)

  attributes = {
    created_at: object.created_at,
    current_period_end: object.billing_period_end_date,
    current_period_start: object.billing_period_start_date,
    payment_method_id: object.payment_method_token,
    processor_plan: object.plan_id,
    status: object.status.parameterize(separator: "_"),
    trial_ends_at: (object.created_at + object.trial_duration.send(object.trial_duration_unit) if object.trial_period)
  }

  # Canceled subscriptions should have access through the paid_through_date or updated_at
  if object.status == "Canceled"
    attributes[:ends_at] = object.updated_at

  # Set grace period for subscriptions that are marked to be canceled
  elsif object.status == "Active" && object.number_of_billing_cycles
    attributes[:ends_at] = object.paid_through_date.end_of_day
  end

  pay_subscription = pay_customer.subscriptions.find_by(processor_id: object.id)
  if pay_subscription
    pay_subscription.with_lock { pay_subscription.update!(attributes) }
  else
    name ||= Pay.default_product_name
    pay_subscription = pay_customer.subscriptions.create!(attributes.merge(name: name, processor_id: object.id))
  end

  pay_subscription
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  try += 1
  if try <= retries
    sleep 0.1
    retry
  else
    raise
  end
end

Instance Method Details

#cancel(**options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pay/braintree/subscription.rb', line 80

def cancel(**options)
  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(subscription.id, {
      number_of_billing_cycles: subscription.current_billing_cycle
    })
  end
  pay_subscription.sync!(object: result.subscription)
rescue ::Braintree::BraintreeError => e
  raise Pay::Braintree::Error, e
end

#cancel_now!(**options) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/pay/braintree/subscription.rb', line 96

def cancel_now!(**options)
  return if canceled?

  result = gateway.subscription.cancel(processor_id)
  pay_subscription.sync!(object: result.subscription)
rescue ::Braintree::BraintreeError => e
  raise Pay::Braintree::Error, e
end

#change_quantity(quantity, **options) ⇒ Object

Raises:

  • (NotImplementedError)


105
106
107
# File 'lib/pay/braintree/subscription.rb', line 105

def change_quantity(quantity, **options)
  raise NotImplementedError, "Braintree does not support setting quantity on subscriptions"
end

#on_grace_period?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/pay/braintree/subscription.rb', line 109

def on_grace_period?
  ends_at? && ends_at > Time.current
end

#pauseObject

Raises:

  • (NotImplementedError)


117
118
119
# File 'lib/pay/braintree/subscription.rb', line 117

def pause
  raise NotImplementedError, "Braintree does not support pausing subscriptions"
end

#paused?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/pay/braintree/subscription.rb', line 113

def paused?
  false
end

#resumable?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/pay/braintree/subscription.rb', line 121

def resumable?
  on_grace_period?
end

#resumeObject



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
151
152
# File 'lib/pay/braintree/subscription.rb', line 125

def resume
  unless resumable?
    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_paymentObject

Retries the latest invoice for a Past Due subscription



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/pay/braintree/subscription.rb', line 193

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



76
77
78
# File 'lib/pay/braintree/subscription.rb', line 76

def subscription(**options)
  gateway.subscription.find(processor_id)
end

#swap(plan, **options) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/pay/braintree/subscription.rb', line 154

def swap(plan, **options)
  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.message}" unless result.success?

  pay_subscription.update(processor_plan: plan, ends_at: nil, status: :active)
rescue ::Braintree::BraintreeError => e
  raise Pay::Braintree::Error, e
end