我试图弄清楚如何将条带支付构建到我的事件控制器中,但我正在努力弄清楚一切是如何工作的,而不是说'/registration/new'并将用户保持在同一页面上,即/技术。
注意 - 我使用的是 mongoid 而不是 activerecord
到目前为止,这是我的代码:
模型
class Registration
include Mongoid::Document
field :address
field :email
field :first_name
field :last_name
field :postcode
field :stripe_card_token
field :stripe_customer_token
def save_with_payment event
if valid?
charge = Stripe::Charge.create({
amount: total_price(event),
currency: "gbp",
card: stripe_card_token,
description: email
})
save
end
rescue
false
end
def total_price attribute
price = attribute.price
price
end
end
形式
<%= simple_form_for @registration, :html => { :class => 'custom medium' },
:method => :post, :url => registration_path do |f| %>
<h2>Contact Information</h2>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :address %>
<%= f.input :postcode %>
<!-- Add stripe card details -->
<h2>Payment Information</h2>
<%= f.input :stripe_card_token, as: :hidden %>
<div class="input card_number">
<label for="card_number">Card number</label>
<%= text_field_tag :card_number %>
</div>
<div class="input card_cvc">
<label for="card_code">Security code (CVC)</label>
<%= text_field_tag :card_cvc %>
</div>
<div class="input card_dates">
<label>Card expiration date</label>
<%= select_month nil, { add_month_number: true }, { id: "card_month"} %>
<%= select_year nil,
{ add_year: Date.today.year, end_year: Date.today.year + 15 },
{ id: "card_year"} %>
</div>
<%= f.button :submit, "Register" %>
<% end %>
控制器
class EventsController < ApplicationController
def show
@event = Event.where(:slug => params[:slug]).first
@registration = Registration.new
end
def payment
@registration = Registration.new(params[:registration])
if @registration.save_with_payment(@event)
redirect_to event_path, :notice => "Thank you for registering"
else
redirect_to event_path, :notice => "We're sorry, something went wrong"
end
end
end
路线
Disrupt::Application.routes.draw do
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
root to: "pages#home"
match '/home' => "pages#home"
match '/:slug' => "events#show"
end
目前页面正在重定向到主页,而不是返回事件页面显示通知,我只使用 rails 几个月,所以我还在学习。
任何帮助,将不胜感激。