Title: Rails Payola: add a coupon to an existing subscription
Tags: ruby-on-rails;stripe-payments;payola
Question: 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 Strip181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16Customer 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?


Here is the accepted 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 ```Payol181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16ChangeSubscriptionPlan.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 = Strip181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16Customer.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.

Comment for this answer: Thank you for the answer. For my purposes I was looking to automate adding a coupon because subsequent charges would be less than the first. But I realized that I could use the setup-fee feature by passing that when the subscription is created to increase the cost of the first subscription. Thank you for the investigation, I think it will help others.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:06/09/15', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<ruby-on-rails><stripe-payments><payola>', 'Title': 'Rails Payola: add a coupon to an existing subscription', 'ViewCount': 290, 'id': 30722437, 'user_AboutMe': '', 'user_AccountId': 3302642.0, 'user_DisplayName': 'mfhholmes', 'user_Reputation': 227.0, 'user_Views': 20.0, 'user_id': 2778384.0}}