加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:23:38 所属栏目:百科 来源:网络整理
导读:我正在尝试使用 braintree-rails gem为订阅添加折扣对象,但它未应用.我猜我的代码一定是错的,但我找不到一个有效的例子. discount = BraintreeRails::Discount.find(params[:subscription_promo])subscription = @plan.subscriptions.build permitted_params
我正在尝试使用 braintree-rails gem为订阅添加折扣对象,但它未应用.我猜我的代码一定是错的,但我找不到一个有效的例子.

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

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

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

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

更新2:我还根据上述代码的请求运行了针对API的直接Braintree请求,并且它有效.在设置和保存之间发生了一些错误.

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

解决方法

宝石作者在这里.

不幸的是,BraintreeRails和Braintree ruby?? gem都不支持subscription.discounts<<目前为订阅添加折扣的折扣方式. 正如您在braintree ruby doc中所看到的,添加/更新/覆盖addon / discounts API有点过于灵活,无法包含在单个subscription.discounts<<折扣线. 如果您对订阅的插件/折扣的设置很简单并且变化不大,您可以尝试为每个所需组合创建一个计划,然后使用正确的计划来创建订阅. 如果您的设置非常动态(在价格,结算周期,数量等方面),直接使用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"]
  }
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读