Class: Pay::PaddleClassic::Subscription

Inherits:
Subscription
  • Object
show all
Extended by:
Sync
Defined in:
app/models/pay/paddle_classic/subscription.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sync

find_pay_customer, pay_processor, sync_with_retries

Class Method Details

.sync(subscription_id, object: nil, name: Pay.default_product_name) ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
# File 'app/models/pay/paddle_classic/subscription.rb', line 9

def self.sync(subscription_id, object: nil, name: Pay.default_product_name)
  sync_with_retries do
    subscription = object || PaddleClassic.client.users.list(subscription_id: subscription_id).data.first
    pay_customer = find_pay_customer(subscription.user_id)

    # If passthrough exists (only on webhooks) we can use it to create the Pay::Customer
    if pay_customer.nil? && subscription.passthrough
      owner = Pay::PaddleClassic.owner_from_passthrough(subscription.passthrough)
      pay_customer = owner&.set_payment_processor(:paddle_classic, processor_id: subscription.user_id)
    end

    return unless pay_customer

    attributes = {
      paddle_cancel_url: subscription.cancel_url,
      paddle_update_url: subscription.update_url,
      processor_plan: subscription.plan_id || subscription.subscription_plan_id,
      quantity: subscription.quantity || 1,
      status: subscription.state || subscription.status
    }

    case attributes[:status]
    when "trialing"
      attributes[:trial_ends_at] = Time.zone.parse(subscription.next_bill_date)
      attributes[:ends_at] = nil
    when "active", "past_due"
      attributes[:trial_ends_at] = nil
      attributes[:ends_at] = nil
    when "paused", "deleted"
      # If paused or delete while on trial, set ends_at to match
      attributes[:trial_ends_at] = nil
      attributes[:ends_at] = Time.zone.parse(subscription.next_bill_date)
    end

    # Update or create the subscription
    if (pay_subscription = pay_customer.subscriptions.find_by(processor_id: subscription.subscription_id))
      pay_subscription.with_lock do
        pay_subscription.update!(attributes)
      end
      pay_subscription
    else
      pay_customer.subscriptions.create!(attributes.merge(name: name, processor_id: subscription.subscription_id))
    end
  end
end

Instance Method Details

#api_record(**options) ⇒ Object



55
56
57
58
59
# File 'app/models/pay/paddle_classic/subscription.rb', line 55

def api_record(**options)
  PaddleClassic.client.users.list(subscription_id: processor_id).data.try(:first)
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end

#cancel(**options) ⇒ Object

Paddle subscriptions are canceled immediately, however we still want to give the user access to the end of the period they paid for



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/pay/paddle_classic/subscription.rb', line 62

def cancel(**options)
  return if canceled?

  ends_at = if on_trial?
    trial_ends_at
  elsif paused?
    pause_starts_at
  else
    Time.parse(api_record.next_payment.date)
  end

  PaddleClassic.client.users.cancel(subscription_id: processor_id)
  update(
    status: (ends_at.future? ? :active : :canceled),
    ends_at: ends_at
  )

  # Remove payment methods since customer cannot be reused after cancelling
  Pay::PaymentMethod.where(customer_id: customer_id).destroy_all
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end

#cancel_now!(**options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/pay/paddle_classic/subscription.rb', line 85

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

  PaddleClassic.client.users.cancel(subscription_id: processor_id)
  update(status: :canceled, ends_at: Time.current)

  # Remove payment methods since customer cannot be reused after cancelling
  Pay::PaymentMethod.where(customer_id: customer_id).destroy_all
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end

#change_quantity(quantity, **options) ⇒ Object



97
98
99
# File 'app/models/pay/paddle_classic/subscription.rb', line 97

def change_quantity(quantity, **options)
  raise Pay::NotSupportedError, "Paddle does not support setting quantity on subscriptions"
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)


103
104
105
# File 'app/models/pay/paddle_classic/subscription.rb', line 103

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

#pauseObject



111
112
113
114
115
116
# File 'app/models/pay/paddle_classic/subscription.rb', line 111

def pause
  response = PaddleClassic.client.users.pause(subscription_id: processor_id)
  update(status: :paused, pause_starts_at: Time.zone.parse(response.dig(:next_payment, :date)))
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end

#paused?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/models/pay/paddle_classic/subscription.rb', line 107

def paused?
  status == "paused"
end

#resumable?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/models/pay/paddle_classic/subscription.rb', line 118

def resumable?
  paused?
end

#resumeObject



122
123
124
125
126
127
128
129
130
131
# File 'app/models/pay/paddle_classic/subscription.rb', line 122

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

  PaddleClassic.client.users.unpause(subscription_id: processor_id)
  update(ends_at: nil, status: :active, pause_starts_at: nil)
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end

#swap(plan, **options) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/pay/paddle_classic/subscription.rb', line 133

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

  attributes = {plan_id: plan, prorate: options.fetch(:prorate) { true }}
  attributes[:quantity] = quantity if quantity?
  PaddleClassic.client.users.update(subscription_id: processor_id, **attributes)

  update(processor_plan: plan, ends_at: nil, status: :active)
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end