如何将网关响应从模型传递到控制器-Ruby on Rails

问题描述 投票:1回答:0

我的应用程序使用现役商人来处理付款。 我使用Eway作为付款网关。 我正在用Eway存储信用卡详细信息,以使它们不进入我的应用程序数据库。

我正在使用一个方法库,该方法库返回一个带有客户帐单ID的响应,我可以在以后使用它来处理订单。

http://rdoc.info/github/Shopify/active_merchant/master/ActiveMerchant/Billing/EwayManagedGateway

我的主要问题是如何将响应值输入到控制器中,以便将其保存到成员模型中。

我创建了一个简单的ruby文件来测试所有工作,并且确实如此。 我只需要转换此代码即可在我的rails应用程序中工作。

require "rubygems"
gem 'activemerchant', '1.15.0'
require 'activemerchant'

ActiveMerchant::Billing::Base.mode = :production

gateway = ActiveMerchant::Billing::EwayManagedGateway.new(
  :login => '12345678',
  :username => '[email protected]',
  :password => 'mypassword'
) 

credit_card = ActiveMerchant::Billing::CreditCard.new(
    :type               =>  "visa",
    :number             =>  "4444333322221111",
    :verification_value =>  "123",
    :month              =>  "11",
    :year               =>  "2011",
    :first_name         =>  "John",
    :last_name          =>  "Citizen"
)

options = {
    :order_id => '1230123',
    :ip => "127.0.0.1",
    :email => '[email protected]',
    :billing_address => { :title => "Mr.",
                      :address1 => '123 Sample Street',
                      :city => 'Sampleville',
                      :state => 'NSW',
                      :country => 'AU',
                      :zip => '2000'
                      },
    :description => 'purchased items'
}



if credit_card.valid?
  response = gateway.store(credit_card, options)
  if response.success?
    puts "Credit Card Stored. #{response.message}"
    customer = response.params['CreateCustomerResult']
    puts "Customer Id: #{customer}"
  else
    puts "Error: #{response.message}"
  end
else
  puts "Error, credit card is not valid. #{credit_card.errors.full_messages.join('. ')}"
end

这是我的订单模型中的相关代码。

  serialize       :params
  cattr_accessor  :gateway

  def response=(response)
    self.success        = response.success?
    self.message        = response.message
    self.params         = response.params
    self.billing_id     = response.params['CreateCustomerResult']
  rescue ActiveMerchant::ActiveMerchantError => e
    self.success        = false
    self.message        = e.message
    self.params         = {}
    self.billing_id     = nil
 end

 def store
   response = Order.gateway.store(credit_card, options)
 end

这是我的订单控制器创建代码。

  def create
    @member  = current_member
    @order_deal = Deal.find(params[:deal_id])
    @order = @order_deal.orders.build(params[:order])
    @order.member_id = current_member.id
    @order.ip_address = request.remote_ip
    @deal = @order_deal
    if @order.save
      if @order.store
        render :action => "success"
        flash[:notice] = "Successfully processed your order."
      else 
        render :action => "new"
      end
    else
      render :action  => 'new'
    end
  end

所以本质上我想得到

response.params['CreateCustomerResult'] 

并将其添加到我的成员模型下

member.billing_id = response.params['CreateCustomerResult]

我怎样才能做到这一点?

ruby-on-rails ruby-on-rails-3 activemerchant
© www.soinside.com 2019 - 2024. All rights reserved.