我正在从ChargesContoller的create方法创建一个新的Purchase。 我可以成功创建新的购买并传递与收费相关的信息。
我的问题是:如何获取购买的订单ID?
下面'结帐流程如何运作: 活动>选项>预订>订单"有很多预订" > 在订单显示视图中,我有条带按钮创建新费用>重定向到购买。
购买基本上是订单的收据。这就是为什么我要显示订单ID和与订单相关的预订。我知道没有传递订单ID,因为我可以在rails控制台中看到它
我应该使用像has_many_and_belongs_to_many这样的其他类型的关联吗? 也许是一个joinTable?
我正在阅读导轨指南的这一部分,但我不确定我是否正在寻找合适的地方:http://guides.rubyonrails.org/association_basics.html
或者也许是我尝试传递订单ID的方式"在费用创建"购买:
purchase = Purchase.create(customer_email: params[:stripeEmail], amount: params[:amount],
customer_card: params[:stripeToken], order_id: params[:order_id], customer_id: customer.id)
订单型号:
class Order < ActiveRecord::Base
belongs_to :order_status
belongs_to :purchase
has_many :reservations
before_create :set_order_status
before_create :create_unique_identifier
before_save :total_for_no_price
before_save :update_subtotal
def to_param
uuid
end
def subtotal
reservations.collect{ |r| r.price }.sum
end
def total_for_no_price
if self.subtotal.nil?
self[:subtotal] = 0
end
end
def create_unique_identifier
self.uuid = SecureRandom.uuid
end
private
def set_order_status
self.order_status_id = 1
end
def update_subtotal
self[:subtotal] = subtotal
end
end
购买型号:
class Purchase < ActiveRecord::Base
has_many :orders
has_many :reservations, through: :orders
end
充电控制器:
class ChargesController < ApplicationController
def new
end
def create
# Amount in cents
@order = Order.find_by_uuid(session[:order_id])
@reservations = @order.reservations
@amount = @order.subtotal.to_i * 100
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => @order.id,
:currency => 'usd'
)
purchase = Purchase.create(customer_email: params[:stripeEmail], amount: params[:amount],
customer_card: params[:stripeToken], order_id: params[:order_id], customer_id: customer.id)
if charge.save
@order_id = @order.update_attributes(order_status_id: 2)
#redirect_to @order
redirect_to purchase
reset_session
end
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
订单控制器:
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
# GET /orders
# GET /orders.json
def index
@orders = Order.all
end
# GET /orders/1
# GET /orders/1.json
def show
@order = Order.find_by_uuid(params[:id])
@reservations = @order.reservations
end
# GET /orders/new
def new
@order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
@order = Order.new(order_params)
respond_to do |format|
if @order.save
format.html { redirect_to @order, notice: 'Order was successfully created.' }
format.json { render :show, status: :created, location: @order }
else
format.html { render :new }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /orders/1
# PATCH/PUT /orders/1.json
def update
respond_to do |format|
if @order.update(order_params)
format.html { redirect_to @order, notice: 'Order was successfully updated.' }
format.json { render :show, status: :ok, location: @order }
else
format.html { render :edit }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
@order.destroy
respond_to do |format|
format.html { redirect_to orders_url, notice: 'Order was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
@order = Order.find_by_uuid(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:subtotal, :tax, :total, :order_status_id, :uuid)
end
end
订单显示页面带有充电按钮:
<div class="container-fluid events-container">
<div class="row">
<div class="col-sm-12">
<h4>Your Registrations:</h4>
<% @order.reservations.each do |reservation| %>
<h4><%= reservation.name %> <%= reservation.lastname %> | <%= reservation.email %> | <%= reservation.phone %></h4>
<h4><%= reservation.gender %> <%= reservation.shirt %> </h4>
<% unless reservation.team === 'N/A' %>
<h4>Team: <%= reservation.team %></h4>
<% end %>
<% unless reservation.redeemcode === 'N/A' %>
<h4>Redeem Code: <%= reservation.redeemcode %></h4>
<% end %>
<hr>
<% end %>
<h1>Order Total: <%= number_to_currency(@order.subtotal)%></h1>
<% if @order.order_status_id === 1 %>
<%= form_tag charges_path(@order) do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description= "Event Registration(s)"
data-amount="<%= @order.subtotal.to_i * 100%>"
data-locale="auto">
</script>
<% end %>
<% end %>
<h3>Need to Modify you order? <%= link_to 'Back to Cart', cart_path %> </h3>
</div>
</div>
</div>
答案 0 :(得分:0)
我通过改变来解决这个问题: order_id:params [:order_id] 到 order_id:(@ order.id) 在收费控制器中
更新了费用控制器:
class ChargesController < ApplicationController
def new
end
def create
# Amount in cents
@order = Order.find_by_uuid(session[:order_id])
@reservations = @order.reservations
@amount = @order.subtotal.to_i * 100
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => @order.id,
:currency => 'usd'
)
purchase = Purchase.create(customer_email: params[:stripeEmail], amount: params[:amount],
customer_card: params[:stripeToken], order_id: (@order.id), customer_id: customer.id)
if charge.save
@order_id = @order.update_attributes(order_status_id: 2)
#redirect_to @order
redirect_to purchase
reset_session
end
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end