Daniel Fischer
Daniel Fischer

Reputation: 3062

How to avoid a subscription from going out of sync with Stripe?

Given the following method

  def change_plan_to(plan_id)
    new_plan = Plan.find plan_id
    stripe_customer = Stripe::Customer.retrieve(stripe_customer_token)
    stripe_customer.update_subscription(plan: new_plan.slug)
    self.plan = new_plan
    self.active = true
    save
  rescue Stripe::InvalidRequestError => e
    logger.error "[STRIPE] #{ e }"
    errors.add :base, "Unable to change your plan!"
    false
  end

Specifically line #4-6. I want 4 and 5 to happen only if 4 is successful but Stripe doesn't return the ability to wrap that in a if. If it errors it just throws Stripe::InvalidRequestError.

What's the best way to handle this? Fire & forget and allow Stripe webhook callbacks to manage expiring active state as needed?

The other scenario is that all the code will halt after line 4 if it doesn't pass. Is this how rescue works?

Upvotes: 0

Views: 192

Answers (1)

Pandurang Waghulde
Pandurang Waghulde

Reputation: 1035

Yes thats the way rescue work, So better you execute these statements which is dependent on line 4 in webhook callbacks that stripe sends to you. Because that ensures subscription change.

Upvotes: 1

Related Questions