简体   繁体   English

如何动态更改Rails中使用Stripe收取的价格?

[英]How to dynamically change price charged with Stripe in Rails?

I am trying to integrate Stripe into my Rails app. 我正在尝试将Stripe集成到我的Rails应用程序中。 I followed the tutorial on their website and have most of it working. 我在他们的网站上关注了该教程,并且大部分内容都可以正常工作。 My 1 question is how to dynamically change the price charged to customers. 我的第一个问题是如何动态更改向客户收取的价格。

Right now @amount is hardcoded at 500. How do I pass @price (from new.html.erb or the controller) to the 'create' action? 现在,@ amount的硬编码为500。如何将@price(来自new.html.erb或控制器)传递给'create'操作?

def new
    @project = Project.find(params[:project_id])
    number_of_testers = @project.testers
    @price = 30 * number_of_testers
end

def create
  # Amount in cents
  # @amount = 500



  customer = Stripe::Customer.create(
    :email => params[:stripeEmail],
    :source  => params[:stripeToken]
  )

  charge = Stripe::Charge.create(
    :customer    => customer.id,
    :amount      => @amount,
    :description => 'Rails Stripe customer',
    :currency    => 'usd'
  )

rescue Stripe::CardError => e
  flash[:error] = e.message
  redirect_to new_charge_path
end

new.html.erb new.html.erb

<center>
    <%= form_tag charges_path do %>

      <article>
        <% if flash[:error].present? %>
          <div id="error_explanation">
            <p><%= flash[:error] %></p>
          </div>
        <% end %>
        <label class="amount">
          <span>Amount: $<%= @price %></span>
        </label>
      </article>

        <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description="Buy usability testing credits"
          data-amount="<%= @price*100 %>"
          data-locale="auto"></script>
    <% end %>

</center>

Why not use a number_field_tag :amount to send your amount back to the controller create method in params? 为什么不使用number_field_tag :amount将您的number_field_tag :amount以参数形式发送回控制器create方法?

But from the js script below it seems that you actually need to send the total to stripe, which means if your price changes in the form, you will need to reload the value sent in js as well, so you might also need an onChange property for your number field. 但是从下面的js脚本看来,您实际上确实需要将总数发送到stripe,这意味着,如果您的价格在表单中发生变化,那么您还需要重新加载以js发送的值,因此您可能还需要onChange属性为您的数字字段。 It will call a js function that will grab the new value and send it to stripe. 它将调用一个js函数,该函数将获取新值并将其发送到stripe。

You can either pass @price via the params hash by adding a hidden form field or you can calculate it in your new action in the controller and then store it in a session. 您可以通过添加隐藏的表单字段通过params哈希传递@price,也可以在控制器的新操作中对其进行计算,然后将其存储在会话中。 Then access that value from the session. 然后从会话中访问该值。 For example: 例如:

def new
  @price = 30 * number_of_testers
  session[:price] = @price
end

def create
  @amount = session[:price] 
  ...rest of your code here...
  session.delete(:price)
end 

If you go the hidden form field route instead of using sessions you just have to whitelist the hidden field attribute in your controller and it will be passed as part of the params hash with the other form fields values. 如果您使用隐藏表单域路由而不是使用会话,则只需在控制器中将隐藏域属性列入白名单,该属性将作为参数散列的一部分与其他表单域值一起传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM