有关在 Ryan bates Active_Merchant 集成视频上创建信用卡对象的问题

发布于 2024-08-10 16:48:29 字数 627 浏览 1 评论 0原文

我正在观看 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 方法实际上是如何调用的。

I was going through Ryan Bates' video on Active Merchant integration video Railscast #145 and my question was regarding the creation of the @credit_card method that he defines within the Order.rb method.

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
  )

end

What I don't follow is how does this method get called. The form_for in the new method creates an @order object while and there is no mention of the credit_card method. How does the credit_card method get called to initiate the creation of the @credit_card object.

I am aware of virtual attributes but I dont know how the credit_card method is actually called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森罗 2024-08-17 16:48:29

此处查看截屏视频代码。

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

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

<% form_for @order do |f| %>

我们可以看到,在提交时,表单使用了 oders_controller create 方法。

在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 的参数被调用

Looking at the screencast code here.

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

We can see the order form, and from the first line

<% form_for @order do |f| %>

We see that, on submission, the form use the oders_controller create method.

In 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

We can see that @order is Order instance built from the cart. Nothing special here
This order is now saved with @order.save and then the purchase method is called on @order

Let's take a look at this purchase method!

in 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

Second line response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
. The credit_card method is called there as an argument of GATEWAY.purchase

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文