Rails Payola: add a coupon to an existing subscription

326 views Asked by At

We're working on a SaaS product that uses Payola to handle payment, and we'd like to add a referral promotion. Adding the coupon to the referee is simple enough (hidden field on the form with the coupon code), but there doesn't seem to be any obvious way of applying a coupon to an existing subscription.

I've checked the Payola source, and there doesn't seem to be any methods dealing with applying a coupon code to an existing subscription, just for a new one.

Can we just get the Stripe::Customer object and use this answer: How to Apply a Coupon to a Stripe Customer to apply the coupon? Will that mess up Payola at all?

1

There are 1 answers

1
Roope Hakulinen On BEST ANSWER

Since the Payola stores the subscription details in its own table, it isn't sufficient enough to just update subscription on Stripe. Now, if we take a look at the subscription_controller.rb:28 we see what Payola itself does when new coupon is applied (you can apply a new coupon with Payola when changing the plan as can be indirectly seen from before_filter find_plan_coupon_and_quantity called also on change_plan method. The find_plan_coupon_and_quantity method leads to the call @coupon = cookies[:cc] || params[:cc] || params[:coupon_code] || params[:coupon]). What it does is that it calls Payola::ChangeSubscriptionPlan.call(@subscription, @plan) which again is declared in change_subscription_plan.rb:3 and calls another method retrieve_subscription_for_customer on the same file. This method is the key here as it retrieves the actual subscription from the Stripe and returns it. Here is that method for reference

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

After fetching the subscription it updated according to the new plan details and stored in Payola's own data structures in database.

After this long and exhausting investigation we can see that it should be suitable to update the Stripe manually and then apply same kind of update on Payola subscription as described above. So to answer your original question about messing Payola: Yes, it will mess Payola, but you can fix it manually by copying the code used by Payola itself to keep in sync.

Hope this helps you to achieve the desired functionality and at least directs you to correct direction.