Class: Pay::PaddleBilling::Subscription

Inherits:
Subscription show all
Defined in:
app/models/pay/paddle_billing/subscription.rb

Constant Summary

Constants inherited from Subscription

Subscription::STATUSES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Subscription

#active?, #canceled?, #cancelled?, #ended?, find_by_processor_and_id, #generic_trial?, #has_incomplete_payment?, #has_trial?, #incomplete?, #on_trial?, #past_due?, #skip_trial, #swap_and_invoice, #sync!, #trial_ended?, #unpaid?

Class Method Details

.sync(subscription_id, object: nil, name: Pay.default_product_name, try: 0, retries: 1) ⇒ Object



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
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
73
74
# File 'app/models/pay/paddle_billing/subscription.rb', line 12

def self.sync(subscription_id, object: nil, name: Pay.default_product_name, try: 0, retries: 1)
  # Passthrough is not return from this API, so we can't use that
  object ||= ::Paddle::Subscription.retrieve(id: subscription_id)

  pay_customer = Pay::Customer.find_by(processor: :paddle_billing, processor_id: object.customer_id)
  return unless pay_customer

  attributes = {
    current_period_end: object.current_billing_period&.ends_at,
    current_period_start: object.current_billing_period&.starts_at,
    ends_at: (object.canceled_at ? Time.parse(object.canceled_at) : nil),
    metadata: object.custom_data,
    paddle_cancel_url: object.management_urls&.cancel,
    paddle_update_url: object.management_urls&.update_payment_method,
    pause_starts_at: (object.paused_at ? Time.parse(object.paused_at) : nil),
    status: object.status
  }

  if object.items&.first
    item = object.items.first
    attributes[:processor_plan] = item.price.id
    attributes[:quantity] = item.quantity
  end

  case attributes[:status]
  when "canceled"
    # Remove payment methods since customer cannot be reused after cancelling
    Pay::PaymentMethod.where(customer_id: object.customer_id).destroy_all
  when "trialing"
    attributes[:trial_ends_at] = Time.parse(object.next_billed_at) if object.next_billed_at
  when "paused"
    attributes[:pause_starts_at] = Time.parse(object.paused_at) if object.paused_at
  when "active", "past_due"
    attributes[:trial_ends_at] = nil
    attributes[:pause_starts_at] = nil
    attributes[:ends_at] = nil
  end

  case object.scheduled_change&.action
  when "cancel"
    attributes[:ends_at] = Time.parse(object.scheduled_change.effective_at)
  when "pause"
    attributes[:pause_starts_at] = Time.parse(object.scheduled_change.effective_at)
  when "resume"
    attributes[:pause_resumes_at] = Time.parse(object.scheduled_change.effective_at)
  end

  # Update or create the subscription
  if (pay_subscription = find_by(customer: pay_customer, processor_id: subscription_id))
    pay_subscription.with_lock { pay_subscription.update!(attributes) }
    pay_subscription
  else
    create!(attributes.merge(customer: pay_customer, name: name, processor_id: subscription_id))
  end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  try += 1
  if try <= retries
    sleep 0.1
    retry
  else
    raise
  end
end

.sync_from_transaction(transaction_id) ⇒ Object



7
8
9
10
# File 'app/models/pay/paddle_billing/subscription.rb', line 7

def self.sync_from_transaction(transaction_id)
  transaction = ::Paddle::Transaction.retrieve(id: transaction_id)
  sync(transaction.subscription_id) if transaction.subscription_id
end

Instance Method Details

#api_record(**options) ⇒ Object



76
77
78
# File 'app/models/pay/paddle_billing/subscription.rb', line 76

def api_record(**options)
  @api_record ||= ::Paddle::Subscription.retrieve(id: processor_id, **options)
end

#cancel(**options) ⇒ Object

If a subscription is paused, cancel immediately Otherwise, cancel at period end



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/pay/paddle_billing/subscription.rb', line 87

def cancel(**options)
  return if canceled?

  response = ::Paddle::Subscription.cancel(
    id: processor_id,
    effective_from: options.fetch(:effective_from, paused? ? "immediately" : "next_billing_period")
  )
  update(
    status: response.status,
    ends_at: response.scheduled_change&.effective_at || Time.current
  )
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

#cancel_now!(**options) ⇒ Object



102
103
104
105
106
# File 'app/models/pay/paddle_billing/subscription.rb', line 102

def cancel_now!(**options)
  cancel(**options.merge(effective_from: "immediately"))
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

#change_quantity(quantity, **options) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/pay/paddle_billing/subscription.rb', line 108

def change_quantity(quantity, **options)
  items = [{
    price_id: processor_plan,
    quantity: quantity
  }]

  ::Paddle::Subscription.update(
    id: processor_id,
    items: items,
    proration_billing_mode: options.delete(:proration_billing_mode) || "prorated_immediately"
  )
  update(quantity: quantity)
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

#on_grace_period?Boolean

A subscription could be set to cancel or pause in the future It is considered on grace period until the cancel or pause time begins

Returns:

  • (Boolean)


126
127
128
# File 'app/models/pay/paddle_billing/subscription.rb', line 126

def on_grace_period?
  (canceled? && Time.current < ends_at) || (paused? && pause_starts_at? && Time.current < pause_starts_at)
end

#pauseObject



134
135
136
137
138
139
# File 'app/models/pay/paddle_billing/subscription.rb', line 134

def pause
  response = ::Paddle::Subscription.pause(id: processor_id)
  update!(status: :paused, pause_starts_at: response.scheduled_change.effective_at)
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

#paused?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'app/models/pay/paddle_billing/subscription.rb', line 130

def paused?
  status == "paused"
end

#payment_method_transactionObject

Get a transaction to update payment method



81
82
83
# File 'app/models/pay/paddle_billing/subscription.rb', line 81

def payment_method_transaction
  ::Paddle::Subscription.get_transaction(id: processor_id)
end

#resumable?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/models/pay/paddle_billing/subscription.rb', line 141

def resumable?
  paused?
end

#resumeObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/pay/paddle_billing/subscription.rb', line 145

def resume
  unless resumable?
    raise Error, "You can only resume paused subscriptions."
  end

  # Paddle Billing API only allows "resuming" subscriptions when they are paused
  # So cancel the scheduled change if it is in the future
  if paused? && pause_starts_at? && Time.current < pause_starts_at
    ::Paddle::Subscription.update(id: processor_id, scheduled_change: nil)
  else
    ::Paddle::Subscription.resume(id: processor_id, effective_from: "immediately")
  end

  update(ends_at: nil, status: :active, pause_starts_at: nil)
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

#retry_failed_paymentObject

Retries the latest invoice for a Past Due subscription



180
181
# File 'app/models/pay/paddle_billing/subscription.rb', line 180

def retry_failed_payment
end

#swap(plan, **options) ⇒ Object

Raises:

  • (ArgumentError)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/models/pay/paddle_billing/subscription.rb', line 163

def swap(plan, **options)
  raise ArgumentError, "plan must be a string" unless plan.is_a?(String)

  items = [{
    price_id: plan,
    quantity: quantity || 1
  }]

  ::Paddle::Subscription.update(
    id: processor_id,
    items: items,
    proration_billing_mode: options.delete(:proration_billing_mode) || "prorated_immediately"
  )
  update(processor_plan: plan, ends_at: nil, status: :active)
end