gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:42:44 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com