将 Stripe Checkout 与 Rails 应用集成
Integrate Stripe Checkout with Rails app
我有一个现有的 Rails 应用程序,用户可以在其中为船创建 classifieds/ads。目前没有集成支付,一旦用户提交表单,它就会被发布。现在我希望将 Spripe Checkout(因为它看起来最简单,如果我错了请纠正我)添加到我的应用程序。我不确定什么是解决这个问题的最佳方法。理想情况下,我希望用户提交表单然后被要求付款。
此外,我不确定我应该采用哪种方法:1) 首先在数据库中创建广告,然后更改布尔列值(例如 "payment")的付款或 2)广告的数据库记录只有在付款成功后才会创建。
我应该像 Stripe 教程建议的那样创建一个新的 Charges 控制器,还是应该将其合并到我现有的 AdsController 中?
ads_controller.rb
class AdsController < ApplicationController
before_action :logged_in_user, only: [:edit, :update, :new]
before_action :correct_user, only: [:edit, :update, :destroy]
def index
if params[:category].blank? && params[:state].blank?
@ads = Ad.all.order("created_at DESC")
elsif params[:category].blank?
@state_id = State.find_by(name: params[:state]).id
@ads = Ad.where(state_id: @state_id).order("created_at DESC")
else
@category_id = Category.find_by(name: params[:category]).id
@ads = Ad.where(category_id: @category_id).order("created_at DESC")
end
@ads = @ads.paginate(page: params[:page], :per_page => 21)
end
def new
@ad = Ad.new
end
def create
@ad = current_user.ads.build(ad_params)
if @ad.save
flash[:success] = "Ad successfully created"
redirect_to @ad
else
render 'new'
end
end
def show
@ad = Ad.find(params[:id])
end
def edit
@ad = Ad.find(params[:id])
end
def update
@ad = Ad.find(params[:id])
if @ad.update_attributes(ad_params)
flash[:success] = "Ad successfully updated"
redirect_to @ad
else
render 'edit'
end
end
def destroy
@ad.destroy
flash[:success] = "Ad deleted"
redirect_to request.referrer || root_url
end
private
def ad_params
params.require(:ad).permit(:title, :price, :year, :location, :description, :contact_name, :contact_number, :category_id, :picture, :picture2, :picture3, :state_id)
end
def correct_user
@ad = current_user.ads.find_by(id: params[:id])
redirect_to root_url if @ad.nil?
end
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in"
redirect_to login_path
end
end
我目前的表格:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@ad, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label "Category:" %>
<%= f.collection_select :category_id, Category.all, :id, :name, { prompt: "Select Category" }, class: 'form-control' %>
# Lots of other form fields
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
如有任何想法,我们将不胜感激。
创建一个单独的控制器比将其构建到广告控制器中更符合实际。您绝对应该将整个数据库进程包装在所谓的 'transaction' 中,并希望仅在成功付款时保存添加。如果在整个 'transaction' 过程中有任何部分失败,它将回滚您的数据库并保持一致性。
jstripe 教程非常深入,但如果您正在寻找更多信息和具体示例,我读了一本关于这个主题的好书,它带我绕过了很多陷阱和陷阱,您可能 运行 进入:
https://www.masteringmodernpayments.com/
我有一个现有的 Rails 应用程序,用户可以在其中为船创建 classifieds/ads。目前没有集成支付,一旦用户提交表单,它就会被发布。现在我希望将 Spripe Checkout(因为它看起来最简单,如果我错了请纠正我)添加到我的应用程序。我不确定什么是解决这个问题的最佳方法。理想情况下,我希望用户提交表单然后被要求付款。
此外,我不确定我应该采用哪种方法:1) 首先在数据库中创建广告,然后更改布尔列值(例如 "payment")的付款或 2)广告的数据库记录只有在付款成功后才会创建。
我应该像 Stripe 教程建议的那样创建一个新的 Charges 控制器,还是应该将其合并到我现有的 AdsController 中?
ads_controller.rb
class AdsController < ApplicationController
before_action :logged_in_user, only: [:edit, :update, :new]
before_action :correct_user, only: [:edit, :update, :destroy]
def index
if params[:category].blank? && params[:state].blank?
@ads = Ad.all.order("created_at DESC")
elsif params[:category].blank?
@state_id = State.find_by(name: params[:state]).id
@ads = Ad.where(state_id: @state_id).order("created_at DESC")
else
@category_id = Category.find_by(name: params[:category]).id
@ads = Ad.where(category_id: @category_id).order("created_at DESC")
end
@ads = @ads.paginate(page: params[:page], :per_page => 21)
end
def new
@ad = Ad.new
end
def create
@ad = current_user.ads.build(ad_params)
if @ad.save
flash[:success] = "Ad successfully created"
redirect_to @ad
else
render 'new'
end
end
def show
@ad = Ad.find(params[:id])
end
def edit
@ad = Ad.find(params[:id])
end
def update
@ad = Ad.find(params[:id])
if @ad.update_attributes(ad_params)
flash[:success] = "Ad successfully updated"
redirect_to @ad
else
render 'edit'
end
end
def destroy
@ad.destroy
flash[:success] = "Ad deleted"
redirect_to request.referrer || root_url
end
private
def ad_params
params.require(:ad).permit(:title, :price, :year, :location, :description, :contact_name, :contact_number, :category_id, :picture, :picture2, :picture3, :state_id)
end
def correct_user
@ad = current_user.ads.find_by(id: params[:id])
redirect_to root_url if @ad.nil?
end
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in"
redirect_to login_path
end
end
我目前的表格:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@ad, html: { multipart: true }) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label "Category:" %>
<%= f.collection_select :category_id, Category.all, :id, :name, { prompt: "Select Category" }, class: 'form-control' %>
# Lots of other form fields
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
如有任何想法,我们将不胜感激。
创建一个单独的控制器比将其构建到广告控制器中更符合实际。您绝对应该将整个数据库进程包装在所谓的 'transaction' 中,并希望仅在成功付款时保存添加。如果在整个 'transaction' 过程中有任何部分失败,它将回滚您的数据库并保持一致性。
jstripe 教程非常深入,但如果您正在寻找更多信息和具体示例,我读了一本关于这个主题的好书,它带我绕过了很多陷阱和陷阱,您可能 运行 进入: https://www.masteringmodernpayments.com/