如何在Rails中使用Stripe自定义金额?

时间:2017-10-03 02:35:44

标签: ruby-on-rails stripe-payments

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

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 %>

查看for create.html.erb

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

    <%= @amount %>

1 个答案:

答案 0 :(得分:0)

在变量名称前使用@符号时,您可以将该变量提供给instance of that class。因此,您@amountclass可以使用view var。

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