Class: Payola::ChangeSubscriptionPlan
- Inherits:
-
Object
- Object
- Payola::ChangeSubscriptionPlan
- Defined in:
- app/services/payola/change_subscription_plan.rb
Class Method Summary collapse
- .call(subscription, plan, quantity = 1, coupon_code = nil) ⇒ Object
- .retrieve_subscription_for_customer(subscription, secret_key) ⇒ Object
- .should_prorate?(subscription, plan, coupon_code) ⇒ Boolean
Class Method Details
.call(subscription, plan, quantity = 1, coupon_code = 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 |
# File 'app/services/payola/change_subscription_plan.rb', line 3 def self.call(subscription, plan, quantity = 1, coupon_code = 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.save subscription.plan = plan subscription.quantity = quantity subscription.save! subscription.instrument_plan_changed(old_plan) rescue RuntimeError, Stripe::StripeError => e subscription.errors[:base] << e. end subscription end |
.retrieve_subscription_for_customer(subscription, secret_key) ⇒ Object
28 29 30 31 |
# File 'app/services/payola/change_subscription_plan.rb', line 28 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
33 34 35 36 37 |
# File 'app/services/payola/change_subscription_plan.rb', line 33 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 |