1

我的应用程序使用 activemerchant 来处理付款。我使用 Eway 作为我的支付网关。我正在使用 Eway 存储信用卡详细信息,以将它们排除在我的应用程序数据库之外。

我正在使用一种方法 store,它返回带有客户帐单 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 => 'mylogin@example.com',
  :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 => 'john.citizen@example.com',
    :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]

我怎样才能做到这一点?

4

0 回答 0