我正在将Stripe支付集成到我的应用程序中。我更换了Stripe默认的“使用信用卡付款”按钮,并替换为我定制的一个按钮。在这样做之后,测试模式成功弹出,我可以输入测试凭据。然而,当我提交测试付款并且它被批准时,它仍然停留在相同的pin页面上,而不是转到我为其创建的charges页面。
注意:在我替换默认的Stripe付款按钮为我的新定制按钮之前,应用程序从Stripe付款过渡到charges create.html.erb页面都很好。
此外,查看开发人员工具时,我确实注意到它在我提交测试付款时读取:
注意:在我替换默认的Stripe付款按钮为我的新定制按钮之前,应用程序从Stripe付款过渡到charges create.html.erb页面都很好。
此外,查看开发人员工具时,我确实注意到它在我提交测试付款时读取:
Uncaught TypeError: Cannot read property 'submit' of null
我认为这是指我下面的代码,当我提交chargeForm时。
有人遇到过这个问题或者知道我该如何解决吗?非常感谢!
app/views/pins/_details.html.erb
<%= form_tag charges_path, id: 'chargesForm' do %>
<script src="https://checkout.stripe.com/checkout.js"></script>
<%= hidden_field_tag 'stripeToken' %>
<%= hidden_field_tag 'stripeEmail' %>
<button id="btn-buy" type="button" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-heart"></span> I want this!</button>
<script>
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
token: function(token, arg) {
document.getElementById("stripeToken").value = token.id;
document.getElementById("stripeEmail").value = token.email;
document.getElementById("chargeForm").submit();
}
});
document.getElementById('btn-buy').addEventListener('click', function(e) {
handler.open({
name: 'OMG! <%= @pin.manufacturer %>',
description: '<%= @pin.description %>',
amount: 1000
});
e.preventDefault();
})
</script>
<% end %>
app/controllers/charges_controller.rb
class ChargesController < ApplicationController
def create
# Amount in cents
@amount = 500
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => 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 charges_path
end
end
app/views/charges/create.html.erb
You bought some Awesome stuff!
app/config/routes.rb
Rails.application.routes.draw do
resources :pins
resources :charges
devise_for :users
root "pins#index"
get "about" => "pages#about" #creates about_path
get "contact" => "pages#contact" #creates contact_path
get "auction" => "pages#auction" #creates auction_path
get "terms" => "pages#terms" #creates terms_path
post 'send_mail', to: 'contact#send_mail'
get 'contact', to: 'contact#show'
scope 'pins', controller: :pins do
scope '/:id' do
post 'bid', to: :bid
end
end
scope 'admin', controller: :admin do
scope 'pins' do
get '/:pin_id', to: :pin
end
end
end