【发布时间】:2017-02-07 17:18:39
【问题描述】:
我是 Stripe connect 的新手,我使用独立的 Stripe Connect 构建了一个市场应用程序,并要求用户输入自定义金额以向其他用户付款。我使用的旧表单运行良好,但是一旦我更改为新表单,我的 :source => params[:stripeToken] 和 :stripeEmail 都不再生成。这是什么原因造成的?
无效的源对象:必须是字典或非空字符串
我原来的charges_controller.rb
class ChargesController < ApplicationController
def new
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 => 'Wage Payment',
:currency => 'cad'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
然后我添加了自定义数量的条带配方中的方法:
def create
@amount = params[:amount]
@amount = @amount.gsub('$', '').gsub(',', '')
begin
@amount = Float(@amount).round(2)
rescue
flash[:error] = 'Charge not completed. Please enter a valid amount in CAD ($).'
redirect_to new_charge_path
return
end
@amount = (@amount * 100).to_i # Must be an integer!
if @amount < 500
flash[:error] = 'Charge not completed. Payment amount must be at least $5.'
redirect_to new_charge_path
return
end
charge = Stripe::Charge.create(
:amount => @amount,
:customer => customer.id,
:currency => 'cad',
:source => params[:stripeToken],
:description => ‘Payment'
)
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
我还修改了表单,在charges/new.html.erb中添加了javascript:
旧形式:
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_test_my_key');
</script>
<%= 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: $5.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"
data-locale="auto"></script>
<% end %>
新形式:
<%= form_tag charges_path, id: 'form' do %>
<div id="error_explanation">
<% if flash[:error].present? %>
<p><%= flash[:error] %></p>
<% end %>
</div>
<article>
<%= label_tag(:amount, 'Payment Amount:') %>
<%= text_field_tag(:amount) %>
</article>
<article>
<%= hidden_field_tag(:stripeToken) %>
<%= hidden_field_tag(:stripeEmail) %>
</article>
<button id='donateButton'>Pay Now</button>
<% end %>
<script src="https://checkout.stripe.com/checkout.js"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_test_CcUZ1IJxEqLR5RvVE3p5tx3U');
</script>
<script>
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
locale: 'auto',
name: 'Payments',
description: 'Wage',
token: function(token) {
$('input#stripeToken').val(token.id);
$('input#stripeEmail').val(token.id);
$('form').submit();
}
});
$('#donateButton').on('click', function(e) {
e.preventDefault();
$('#error_explanation').html('');
var amount = $('input#amount').val();
amount = amount.replace(/\$/g, '').replace(/\,/g, '')
amount = parseFloat(amount);
if (isNaN(amount)) {
$('#error_explanation').html('<p>Please enter a valid amount in CAD ($).</p>');
}
else if (amount < 5.00) {
$('#error_explanation').html('<p>Wage amount must be at least $5.</p>');
}
else {
amount = amount * 100; // Needs to be an integer!
handler.open({
amount: Math.round(amount)
})
}
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>
现在当我提交自定义金额时,我收到以下错误:
您为“源”传递了一个空白字符串。你应该删除 来自您的请求的 'source' 参数或提供非空值
提取的源代码(第 27 行附近):Stripe::Charge.create(
不确定如何继续。此错误似乎与创建费用有关,因为该错误位于费用控制器中。或者在新自定义表单的javascript中。
我错过了什么?
【问题讨论】:
-
请检查 params[:stripeToken] 值,可能是您的条带令牌没有正确生成。
-
您是否检查了控制器中的 params[:stripeToken] 值?
-
你是对的,它没有正确生成: "authenticity_token"=>"kdL18CbkOWWJMnytdU/IQx4WD+0IgfTzbLDEs1rV31O6dgLTV/MgA3HWfjBo5FgJ1mxrXxw9ZU8+BVO7LayxQg==", "amount"=>"10" =>“”}。我该如何解决这个问题?
-
我已经从这个答案编辑了我的表单js:stackoverflow.com/questions/20049865/…,但我仍然遇到同样的问题
-
请在 config/initializers Rails.configuration.stripe = { :publishable_key => 'PUBLISHABLE_KEY', :secret_key => 'SECRET_KEY' } Stripe.api_key = Rails.configuration.stripe 中创建 stripe.rb 文件[:secret_key]
标签: ruby-on-rails ruby stripe-connect