Rails Payola:向现有订阅添加优惠券

Rails Payola: add a coupon to an existing subscription

我们正在开发使用 Payola 处理付款的 SaaS 产品,我们想添加推荐促销。将优惠券添加到 refereer 很简单(表单上带有优惠券代码的隐藏字段),但似乎没有任何明显的方法可以将优惠券应用于现有订阅。

我检查了 Payola 来源,似乎没有任何方法可以处理将优惠券代码应用于现有订阅,仅适用于新订阅。

我们可以只获取 Stripe::Customer 对象并使用此答案:How to Apply a Coupon to a Stripe Customer 来应用优惠券吗?这会把 Payola 搞砸吗?

因为 Payola 将订阅详细信息存储在自己的 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 中,并在同一文件上调用另一个方法 retrieve_subscription_for_customer。此方法是这里的关键,因为它从 Stripe 检索实际订阅并 returns 它。下面是参考方法

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

获取订阅后,它会根据新的计划详细信息进行更新,并存储在数据库中 Payola 自己的数据结构中。

经过漫长而艰苦的调查,我们可以看到手动更新 Stripe 应该是合适的,然后如上所述对 Payola 订阅应用相同类型的更新。所以回答你最初关于弄乱 Payola 的问题:是的,它会弄乱 Payola,但你可以通过复制 Payola 本身使用的代码来保持同步来手动修复它.

希望这可以帮助您实现所需的功能,并至少指导您正确的方向。