NameError / / undefined局部变量或方法`params'for main:Object(使用Stripe.js创建条带费用)

时间:2017-01-15 00:48:43

标签: javascript ruby stripe-payments

根据以下代码收到此错误。我正在尝试使用Stripe.js创建一个带条纹的电荷。

这是我的web.rb文件:

require 'json'

require 'sinatra'
require 'sinatra/reloader'
require 'stripe'

get '/' do 
	erb :index
end

Stripe.api_key = "sk_test_EkZm6rgWtt3gndztGnlfm4Yy"
token = params[:stripeToken]

post '/your-charge-code' do
	
	charge = Stripe::Charge.create(
  		:amount => 1000,
  		:currency => "eur",
 		:description => "Example charge",
  		:customer => token,
)
end

这是带有付款表单和客户端Javascript的index.erb文件

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
  Stripe.setPublishableKey("pk_test_xnI3R1Zl3CbybIM3J83SNMr2");
</script>




<form action="/your-charge-code" method="POST" id="payment-form">
  <span class="payment-errors"></span>

  <div class="form-row">
    <label>
      <span>Card Number</span>
      <input type="text" size="20" data-stripe="number">
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>Expiration (MM/YY)</span>
      <input type="text" size="2" data-stripe="exp_month">
    </label>
    <span> / </span>
    <input type="text" size="2" data-stripe="exp_year">
  </div>

  <div class="form-row">
    <label>
      <span>CVC</span>
      <input type="text" size="4" data-stripe="cvc">
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>Billing Postal Code</span>
      <input type="text" size="6" data-stripe="address_zip">
    </label>
  </div>

  <input type="submit" class="submit" value="Submit Payment">
</form>

<script>
$(function() {
  var $form = $('#payment-form');
  $form.submit(function(event) {
    // Disable the submit button to prevent repeated clicks:
    $form.find('.submit').prop('disabled', true);

    // Request a token from Stripe:
    Stripe.card.createToken($form, stripeResponseHandler);

    // Prevent the form from being submitted:
    return false;
  });
});

function stripeResponseHandler(status, response) {
  // Grab the form:
  var $form = $('#payment-form');

  if (response.error) { // Problem!

    // Show the errors on the form:
    $form.find('.payment-errors').text(response.error.message);
    $form.find('.submit').prop('disabled', false); // Re-enable submission

  } else { // Token was created!

    // Get the token ID:
    var token = response.id;

    // Insert the token ID into the form so it gets submitted to the server:
    $form.append($('<input type="hidden" name="stripeToken">').val(token));

    // Submit the form:
    $form.get(0).submit();
  }
};
</script>

任何人都知道我错过了什么?

1 个答案:

答案 0 :(得分:0)

鉴于&#34;未定义的局部变量或方法参数&#34;您已经看到了,我这里的问题与您在token = params[:stripeToken]

web.rb的展示位置有关

您希望将其包含在您的收费路线post '/your-charge-code' do下,而不是在您的脚本顶部。

例如

require 'sinatra'
require 'stripe'

Stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"


get '/' do
    "Hello, world"
end

post '/charge' do
    token = params[:stripeToken]
    Stripe::Charge.create(
      :amount => 1000,
      :currency => "eur",
      :source => token, # obtained with Stripe.js or Checkout
      :description => "My test charge"
    )
end