Class: Payola::ChangeSubscriptionPlan

Inherits:
Object
  • Object
show all
Defined in:
app/services/payola/change_subscription_plan.rb

Class Method Summary collapse

Class Method Details

.call(subscription, plan, quantity = 1, coupon_code = nil, trial_end = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/payola/change_subscription_plan.rb', line 3

def self.call(subscription, plan, quantity = 1, coupon_code = nil, trial_end = nil)
  secret_key = Payola.secret_key_for_sale(subscription)
  old_plan = subscription.plan

  begin
    sub = retrieve_subscription_for_customer(subscription, secret_key)
    sub.plan = plan.stripe_id
    sub.prorate = should_prorate?(subscription, plan, coupon_code)
    sub.coupon = coupon_code if coupon_code.present?
    sub.quantity = quantity
    sub.trial_end = trial_end if trial_end.present?
    sub.save

    subscription.cancel_at_period_end = false
    subscription.plan = plan
    subscription.quantity = quantity
    subscription.save!

    subscription.instrument_plan_changed(old_plan)

  rescue RuntimeError, Stripe::StripeError => e
    subscription.errors[:base] << e.message
  end

  subscription
end

.retrieve_subscription_for_customer(subscription, secret_key) ⇒ Object



30
31
32
33
# File 'app/services/payola/change_subscription_plan.rb', line 30

def self.retrieve_subscription_for_customer(subscription, secret_key)
  customer = Stripe::Customer.retrieve(subscription.stripe_customer_id, secret_key)
  customer.subscriptions.retrieve(subscription.stripe_id)
end

.should_prorate?(subscription, plan, coupon_code) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'app/services/payola/change_subscription_plan.rb', line 35

def self.should_prorate?(subscription, plan, coupon_code)
  prorate = plan.respond_to?(:should_prorate?) ? plan.should_prorate?(subscription) : true
  prorate = false if coupon_code.present?
  prorate
end