【问题标题】:How to use Stripe for custom amounts in Rails?如何在 Rails 中使用 Stripe 进行自定义金额?
【发布时间】:2017-10-03 02:35:43
【问题描述】:

我正在使用 Stripe Checkout,我希望用户设置他/她想要支付的金额。我想将变量从“新”传递到“创建”控制器,特别是捐赠金额。如何使用 Stripe 模板将 @amount 变量从“new”传递到“create”。在我的控制器中,我有:

def new
    @amount = 10
    @amount_in_cents = @amount * 100
end

def create
  # Amount in cents...HOW DO I GET THIS TO BE PULLED FROM 'NEW'
  @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 的看法:

    <%= 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: $<%= @amount %></span>
        </label>
      </article>

      <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
              data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
              data-description="Your donation of $<%= @amount %>"
              data-amount="<%= @amount_in_cents %>"
              data-email = "test@gmail.com"
              data-locale="auto"></script>
    <% end %>

create.html.erb 的视图

    <h2>Thanks, you paid <strong>$5.00</strong>!</h2>

    <%= @amount %>

【问题讨论】:

  • 有什么问题?您已经将它从 #new 传递到您的视图和 #create。

标签: ruby-on-rails stripe-payments


【解决方案1】:

当您在变量名前使用@ 符号时,您使该变量可用于instance of that class。所以你的@amount var 将在你的class 和你的view 中可用。

class Transaction 

 def new 
  @amount=12
 end

 def create

  puts @amount+13
  @amount=@amount+12

 end

end

me = Transaction.new 
puts me.new 
puts me.create

ruby variable scope

instance variables in ruby

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-29
    • 2021-05-29
    • 2020-05-27
    • 2022-10-24
    • 2015-06-28
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    相关资源
    最近更新 更多