我的Rails4应用程序中有订单和 OrderTransactions 模型。他们之间有一个基本的 has_one 和 belongs_to 关系。
我将来自 / orders / new 页面的请求发布到银行的网址,如下所示:
<%= simple_form_for(@order, :url => "https://testsanalpos.est.com.tr/servlet/est3Dgate", :method => :post) do |f| %>
<% @hashing.each do |k, v| %>
<%= f.input k, input_html: {name: k, value: v}, as: :hidden %>
<% end %>
<%= f.input :participation_id, ... %>
<%= f.button :submit, "Ödeme Yap" %>
<% end %>
@hashing,哈希来自我的控制器=&gt;
class OrdersController < ApplicationController
before_filter :authenticate_user!
before_action :set_order, only: [:show, :edit, :update, :destroy]
skip_before_action :verify_authenticity_token
def new
@order = Order.new
@hashing = {
clientid: POS['clientid'],
oid: Time.now.to_i.to_s,
amount: POS['amount'],
okUrl: POS['okUrl'],
failUrl: POS['failUrl'],
rnd: Time.now.to_i.to_s,
}
end
def create
@order = Order.new(order_params)
respond_to do |format|
@order.purchase
end
end
def success
end
def fail
end
private
def set_order
@order = Order.find(params[:id])
end
def order_params
params.require(:order).permit(:id, :ip_address, :first_name, :last_name, :card_brand, :card_number, :card_verification, :card_expires_on, :user_id, :participation_id)
end
end
Order.rb =&gt;
class Order < ActiveRecord::Base
belongs_to :user
belongs_to :participation
has_one :transaction, :class_name => "OrderTransaction"
def purchase
participation.update_attribute(:payment_status, true)
create_transaction(:action => "purchase", :response => response)
end
end
银行的页面从用户那里获得所有必要的信息,如信用卡号,卡有效期等。我的申请没有做任何有关购买过程的事情,所有这些都发生在银行方面。
然后银行正在给我一些关于付款流程的参数。如果付款成功已满,银行会将参数发布到我的/orders/success.html.erb,如果失败则会发布到/order/fail.html.erb。
我有2个问题=&gt;
1)我希望无论响应成功还是失败,都会创建Order模型实例。似乎应该通过控制器中的create方法进行,但它不起作用:/
2)如何获取银行发送到我/失败或/成功URL的参数?我需要将它们放入我的应用程序并将它们保存为我的数据库中的OrderTransaction实例。我可以看到我的日志中的参数,例如this =&gt;
Started POST "/orders/fail" for 127.0.0.1 at 2014-06-01 13:40:28 +0300
Processing by OrdersController#fail as HTML
Parameters:
{
"TRANID"=>"",
"Faturafirma"=>"OMÜ Uzaktan Eğitim Merkezi",
"islemtipi"=>"Auth",
"refreshtime"=>"5",
"lang"=>"tr",
"amount"=>"30",
"ACQBIN"=>"490740",
"clientIp"=>"193.140.28.145",
"name"=>"AKBANK",
"cardHolderName"=>"dsadas dasdsa",
"okUrl"=>"http://localhost:3000/orders/success",
"storetype"=>"3d_pay_hosting",
"Response"=>"Declined"
....
}
数据库架构=&gt;
create_table "orders", force: true do |t|
t.integer "participation_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "order_transactions", force: true do |t|
t.integer "order_id"
t.string "clientip"
t.string "cardholdername"
t.string "response"
t.string "errmsg"
...
t.datetime "created_at"
t.datetime "updated_at"
end
Routes.rb =&gt;
...
post 'orders/success' => 'orders#success'
post 'orders/fail' => 'orders#fail'
resources :orders, only: [:index, :new, :create]
...
答案 0 :(得分:0)
推荐: 将您的创建成功失败移动到模型中并传递它。
def success
Order.create_from_params(order_params)
end
def fail
Order.create_from_params(order_params)
end
然后使用响应Decline等来处理params的成功失败。
class Order<ActivewRecord::Base
def self.create_from_params(order_params)
self.create(inflatewithfields) && self.purchase if params[response'] == 'success'
self.create(inflatewithfields) if params[response'] == 'Decline'
end
end