Module: Pay::Braintree::Subscription

Extended by:
ActiveSupport::Concern
Defined in:
lib/pay/braintree/subscription.rb

Instance Method Summary collapse

Instance Method Details

#braintree_cancelObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pay/braintree/subscription.rb', line 6

def braintree_cancel
  subscription = processor_subscription

  if on_trial?
    gateway.subscription.cancel(processor_subscription.id)
    update(status: :canceled, ends_at: trial_ends_at)
  else
    gateway.subscription.update(subscription.id, {
      number_of_billing_cycles: subscription.current_billing_cycle
    })
    update(status: :canceled, ends_at: subscription.billing_period_end_date.to_date)
  end
rescue ::Braintree::BraintreeError => e
  raise Pay::Braintree::Error, e
end

#braintree_cancel_now!Object



22
23
24
25
26
27
# File 'lib/pay/braintree/subscription.rb', line 22

def braintree_cancel_now!
  gateway.subscription.cancel(processor_subscription.id)
  update(status: :canceled, ends_at: Time.zone.now)
rescue ::Braintree::BraintreeError => e
  raise Pay::Braintree::Error, e
end

#braintree_on_grace_period?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/pay/braintree/subscription.rb', line 29

def braintree_on_grace_period?
  canceled? && Time.zone.now < ends_at
end

#braintree_pauseObject

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/pay/braintree/subscription.rb', line 37

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

#braintree_paused?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/pay/braintree/subscription.rb', line 33

def braintree_paused?
  false
end

#braintree_resumeObject



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

def braintree_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

    owner.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

  update(status: :active)
rescue ::Braintree::BraintreeError => e
  raise Pay::Braintree::Error, e
end

#braintree_swap(plan) ⇒ Object



71
72
73
74
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
103
104
105
106
107
108
# File 'lib/pay/braintree/subscription.rb', line 71

def braintree_swap(plan)
  if on_grace_period? && processor_plan == plan
    resume
    return
  end

  unless active?
    owner.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?
    }
  })

  if result.success?
    update(status: :active, processor_plan: braintree_plan.id, ends_at: nil)
  else
    raise Error, "Braintree failed to swap plans: #{result.message}"
  end
rescue ::Braintree::BraintreeError => e
  raise Pay::Braintree::Error, e
end