Class: Payola::ChangeSubscriptionQuantity

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

Class Method Summary collapse

Class Method Details

.call(subscription, quantity) ⇒ Object



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

def self.call(subscription, quantity)
  secret_key = Payola.secret_key_for_sale(subscription)
  old_quantity = subscription.quantity

  begin
    customer = Stripe::Customer.retrieve(subscription.stripe_customer_id, secret_key)
    sub = customer.subscriptions.retrieve(subscription.stripe_id)
    sub.quantity = quantity
    sub.save

    subscription.quantity = quantity
    subscription.save!

    subscription.instrument_plan_changed(old_quantity)

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

  subscription
end