ruby-on-rails - 为Braintree Rails订阅增加折扣

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

我正在尝试将折扣对象添加到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调用。

最佳答案

gem 作家在这里。

不幸的是,BraintreeRails和Braintree ruby 都不支持现在为订阅提供折扣的subscription.discounts << discount样式。

如您在braintree ruby doc中所见,添加/更新/覆盖插件/折扣API有点太灵活以至于不能包装在单个subscription.discounts << 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"]
  }
)

关于ruby-on-rails - 为Braintree Rails订阅增加折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21618437/

相关文章:

ruby-on-rails - Rails中如何判断日志设备的类型

express - Paypal Express Checkout 使用信用卡/借记卡定期付款的后续步骤

使用组织 ID(来自同步 Azure AD)登录 Azure

ios - Never Get RENEWAL Notification Type on Apple status 更新通知

ios - 应用程序收据是否会在交易完成前反射(reflect)有效订阅?

e-commerce - 推荐一个允许定期/增量付款的支付网关?

magento - magento 中使用 EBS 支付网关进行定期付款(EMI)

ruby-on-rails - 将昂贵的查询放在后台线程上会有助于网站性能吗?

ruby-on-rails - Rails 不适用于新项目。显示错误 "superclass mismatch for class Cipher (TypeError)"

javascript - 使用 AJAX 将参数传递给 Rails Controller 时出现问题