私はストライプ接続を初めて使用しました。スタンドアロン接続のマーケットプレイスアプリケーションを構築し、別のユーザーに支払うためにカスタム金額を入力する必要があります。私が使用していた古いフォームは正常に動作しましたが、新しいフォームに変更すると、my:source => params [:stripeToken]は、もはやstripeEmailを生成しなくなりました。これを引き起こしているのは何ですか?ストライプスタンドアロン: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 %>
新しいフォーム:
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(
続行するかどうかはわからない:私はカスタムの量を提出する際
<%= 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>
は今、私は次のエラーを与えています。このエラーは、料金コントローラにエラーがあるため、料金を作成することに関連しているようです。または、新しいカスタムフォームのjavascriptにあります。
私には何が欠けていますか?
params [:stripeToken]の値が正しく生成されていない可能性があります。 –
コントローラのparams [:stripeToken]の値をチェックしましたか? –
"authenticity_token" => "kdL18CbkOWWJMnytdU/IQx4WD + 0IgfTzbLDEs1rV31O6dgLTV/MgA3HWfjBo5FgJ1mxrXxw9ZU8 + BVO7LayxQg =="、 "amount" => "10"、 "stripeToken" => ""}正しくありません。どうすればこの問題を解決できますか? – ChrisJC2017