我是Stripe連接的新手,使用條帶連接獨立構建市場應用程序,並要求用戶輸入自定義金額以支付其他用戶。我使用的舊窗體運行良好,但是一旦我更改爲新窗體,我的:source => params [:stripeToken]不再生成以及:stripeEmail。這是什麼造成的?Stripe standalone:source => params [:stripeToken]錯誤
Invalid source object: must be a dictionary or a non-empty string
我原來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
我還修改了形式,而在添加了JavaScript的收費/ new.html.erb:
舊形式:
<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>
現在,當我提出的定製量,我提示以下錯誤:
You have passed a blank string for 'source'. You should remove the 'source' parameter from your request or supply a non-blank value
Extracted source (around line #27): Stripe::Charge.create(
不知道如何着手。這個錯誤似乎與創建收費相關聯,因爲錯誤在收費控制器中。或者它是在新的自定義窗體的JavaScript中。
我錯過了什麼?
請檢查params [:stripeToken]的值,可能是您的條形標記沒有正確生成。 –
你在控制器中檢查了params [:stripeToken]的值嗎? –
你是對的,它沒有正確生成:「authenticity_token」=>「kdL18CbkOWWJMnytdU/IQx4WD + 0IgfTzbLDEs1rV31O6dgLTV/MgA3HWfjBo5FgJ1mxrXxw9ZU8 + BVO7LayxQg ==」,「amount」=>「10」,「stripeToken」=>「」}。我該如何解決? – ChrisJC2017