ruby-on-rails - 关于在 Ryan bates Active_Merchant 集成视频中创建信用卡对象的问题

标签 ruby-on-rails paypal activemerchant

我正在观看 Ryan Bates 关于 Active Merchant 集成视频 Railscast #145 的视频,我的问题是关于他在 Order.rb 方法中定义的 @credit_card 方法的创建。

def credit_card
  @credit_card||=ActiveMerchant::Billing::CreditCard.new(
    :type=>card_type,
    :number=>card_number,
    :verification_value=>card_verification,
    :month=>card_expires_on.month,
    :year=>card_expires_on.year,
    :first_name=>first_name,
    :last_name=>last_name
  )

结束

我不了解的是如何调用此方法。新方法中的 form_for 创建了一个 @order 对象,而没有提及 credit_card 方法。如何调用 credit_card 方法来启动 @credit_card 对象的创建。

我知道虚拟属性,但我不知道 credit_card 方法实际上是如何调用的。

最佳答案

查看截屏代码 here .

在 app/views/orders/new.html.erb

我们可以看到订单,从第一行开始

<% form_for @order do |f| %>

我们看到,在提交时,表单使用 oders_controller 创建方法。

在 app/controller/orders_controller.rb

    def create
      @order = current_cart.build_order(params[:order])
      @order.ip_address = request.remote_ip

      if @order.save
        if @order.purchase
          render :action => "success"
        else
          render :action => "failure"
        end
      else
        render :action => 'new'
      end
    end

我们可以看到 @order 是从购物车构建的 Order 实例。这里没什么特别的 此订单现在使用 @order.save 保存,然后在 @order 上调用购买方法

一起来看看这个购买方式吧!

在 app/model/order.rb 中

    def purchase
      response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
      transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
      cart.update_attribute(:purchased_at, Time.now) if response.success?
      response.success?
    end

第二行 response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options) 。 credit_card 方法作为 GATEWAY.purchase 的参数被调用

关于ruby-on-rails - 关于在 Ryan bates Active_Merchant 集成视频中创建信用卡对象的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1697500/

相关文章:

php - 与支付网关集成

ruby-on-rails - Ruby on Rails Activemerchant 集成 - 传递变量

ruby-on-rails-3 - ActionController::TestCase 模拟 PayPal 购买

ruby-on-rails - rails 4 中的 options_from_collection_for_select

ruby-on-rails - Rails ActionCable 中出现 "Unable to find subscription with identifier"的原因是什么?

ruby-on-rails - Date.today 与实际日期不对应

Android Autofill 仅处理来自 onFillRequest 的响应,而不处理来自 Authentication Activity 的响应

android - paypal initWithAppID() 应该是什么?

sql - 压缩数据库中的列数是否有益?

ruby-on-rails - 使用 ActiveMerchant 在 Paypal setExpressCheckout 中预填名字和姓氏