我正在尝试完成我在Ruby中设置的付款,但是正努力使gt订单确认屏幕打印出来。我已经设置了我的部分付款。
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-image="<%= asset_path(@product.image_url) %>"
data-name="<%= @product.name %>"
data-description="<%= @product.description %>"
data-amount="<%= @product.price*100.to_i %>">
</script>
我的付款控制者。
class PaymentsController < ApplicationController
before_action :authenticate_user!
def create
@product = Product.find(params[:product_id])
@user = current_user
token = params[:stripeToken]
# Create the charge on Stripe's servers - this will charge the user's card
begin
charge = Stripe::Charge.create(
amount: @product.price, # amount in cents, again
currency: "eur",
source: token,
description: params[:stripeEmail]
)
if charge.paid
UserMailer.order_confirmation(@user, @product).deliver_now
Order.create!(
:product_id => @product.id,
:user_id => @user.id,
:total => @product.price_show
)
flash[:success] = "Your payment was processed successfully"
end
rescue Stripe::CardError => e
body = e.json_body
err = body[:error]
flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]} Your card has not been charged. Please try again."
end
redirect_to product_path(@product), notice: "Thank you for your purchase."
end
end
和我的路线文件。
Rails.application.routes.draw do
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout' },
controllers: {registrations: "user_registrations"}
resources :products do
resources :comments
end
post 'payments/create'
resources :users
resources :orders, only: [:index, :show, :create, :destroy]
resources :users, except: [:index]
get 'simple_pages/about'
get 'simple_pages/contact'
root 'simple_pages#landing_page'
post 'simple_pages/thank_you'
mount ActionCable.server => '/cable'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
产品show.html.erb中的表格
<%= form_with(url: '/payments/create') do |form| %>
<%= render partial: "shared/stripe_checkout_button" %>
<%= hidden_field_tag(:product_id, @product.id) %>
<% end %>
但是,当我尝试完成测试付款时,会弹出动作控制器,显示“找不到没有ID的产品”。我以为这是在“创建”部分中定义的,但是我不确定该如何解决。任何建议将不胜感激。
答案 0 :(得分:0)
您的表单发布中未设置:product_id
,
1。快速解决方案:
您的表单网址可能是这样的:
<%= form_with(url: "/payments/create?#{product_id=@product.id}") do |form| %>
2。更好的方法:
配置您的付款路线已经具有产品ID:
# routes.rb
resources :products do
resources :comments
resources :payments
end
表单网址:
<%= form_with(url: product_payments_path(@product)) do |form| %>