【问题标题】:Add Discount to Braintree Rails Subscription为 Braintree Rails 订阅添加折扣
【发布时间】:2014-03-04 07:33:57
【问题描述】:

我正在尝试使用 braintree-rails gem 将折扣对象添加到订阅中,但未应用。我猜我的代码一定是错的,但我找不到一个可行的例子。

discount = BraintreeRails::Discount.find(params[:subscription_promo])
subscription = @plan.subscriptions.build permitted_params[:subscription]
subscription.discounts << discount
# ...
subscription.save

当我转储discount 时,它已正确加载。订阅创建得很好,但全价。折扣不存在。如何在订阅中添加折扣?

更新:我尝试修改直接查询,但没有帮助。

@subscription.raw_object.discounts = {add:[{inherited_from_id: discount.id}]}

更新 2:我还针对 API 运行了一个直接的 Braintree 请求,请求符合上述代码的预期,并且它有效。设置和保存之间发生了错误。

更新 3:可以通过提取 BraintreeRails::Subscription 对象的属性,使用 Braintree::Subscription 调用 API,并使用 BraintreeRails::Subscription.find 将其加载回对象来解决问题。不过,这绝对不是最佳选择,因为它不是很干净,并且需要额外的 API 调用。

【问题讨论】:

    标签: ruby-on-rails subscription recurring-billing braintree braintree-rails


    【解决方案1】:

    这里是宝石作者。

    不幸的是,BraintreeRails 和 Braintree ruby​​ gem 目前都不支持subscription.discounts &lt;&lt; discount 为订阅添加折扣的样式。

    正如您在braintree ruby doc 中看到的那样,添加/更新/覆盖插件/折扣 API 有点过于灵活,无法包装在单个 subscription.discounts &lt;&lt; discount 行中。

    如果您为订阅设置的插件/折扣很简单并且变化不大,您可以尝试为每个所需组合创建一个计划,然后使用正确的计划来创建订阅。

    如果您的设置非常动态(在价格、计费周期、数量等方面),直接使用 Braintree API 可能是您的最佳选择。例如:

    result = Braintree::Subscription.create(
      :payment_method_token => "the_payment_method_token",
      :plan_id => "the_plan_id",
      :add_ons => {
        :add => [
          {
            :inherited_from_id => "add_on_id_1",
            :amount => BigDecimal.new("20.00")
          }
        ],
        :update => [
          {
            :existing_id => "add_on_id_2",
            :quantity => 2
          }
        ],
        :remove => ["add_on_id_3"]
      },
      :discounts => {
        :add => [
          {
            :inherited_from_id => "discount_id_1",
            :amount => BigDecimal.new("15.00")
          }
        ],
        :update => [
          {
            :existing_id => "discount_id_2",
            :quantity => 3
          }
        ],
        :remove => ["discount_id_3"]
      }
    )
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2016-11-13
      • 1970-01-01
      • 2017-04-19
      • 2021-12-11
      • 1970-01-01
      • 2020-07-11
      • 2011-10-25
      • 2020-04-04
      相关资源
      最近更新 更多