“未定义的方法`has_key?”对于#
发布于 2024-11-10 04:08:42 字数 3672 浏览 0 评论 0 原文

我尝试在 Rails 应用程序中启用信用卡计费,但在尝试创建订单时收到以下错误:未定义方法“has_key?”对于 #

引用的代码是订单模型中的 purchase 方法:

class Order < ActiveRecord::Base
  ...
  include ActiveMerchant::Billing

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

  def purchase

    response = GATEWAY.authorize(price_in_cents, credit_card)
    if response.success?
      GATEWAY.capture(price_in_cents, response.authorization)
      cart.update_attribute(:purchased_at, Time.now)
    else
      raise StandardError, response.message
    end

  end

end

我使用的是 Paypal Express 网关,其 Active商家库定义 purchase 如下:

  def purchase(money, options = {})
    requires!(options, :token, :payer_id)

    commit 'DoExpressCheckoutPayment', build_sale_or_authorization_request('Sale', money, options)
  end

  private
  def build_get_details_request(token)
    xml = Builder::XmlMarkup.new :indent => 2
    xml.tag! 'GetExpressCheckoutDetailsReq', 'xmlns' => PAYPAL_NAMESPACE do
      xml.tag! 'GetExpressCheckoutDetailsRequest', 'xmlns:n2' => EBAY_NAMESPACE do
        xml.tag! 'n2:Version', API_VERSION
        xml.tag! 'Token', token
      end
    end

    xml.target!
  end

  def build_sale_or_authorization_request(action, money, options)
    currency_code = options[:currency] || currency(money)

    xml = Builder::XmlMarkup.new :indent => 2
    xml.tag! 'DoExpressCheckoutPaymentReq', 'xmlns' => PAYPAL_NAMESPACE do
      xml.tag! 'DoExpressCheckoutPaymentRequest', 'xmlns:n2' => EBAY_NAMESPACE do
        xml.tag! 'n2:Version', API_VERSION
        xml.tag! 'n2:DoExpressCheckoutPaymentRequestDetails' do
          xml.tag! 'n2:PaymentAction', action
          xml.tag! 'n2:Token', options[:token]
          xml.tag! 'n2:PayerID', options[:payer_id]
          xml.tag! 'n2:PaymentDetails' do
            xml.tag! 'n2:OrderTotal', localized_amount(money, currency_code), 'currencyID' => currency_code

            # All of the values must be included together and add up to the order total
            if [:subtotal, :shipping, :handling, :tax].all?{ |o| options.has_key?(o) }
              xml.tag! 'n2:ItemTotal', localized_amount(options[:subtotal], currency_code), 'currencyID' => currency_code
              xml.tag! 'n2:ShippingTotal', localized_amount(options[:shipping], currency_code),'currencyID' => currency_code
              xml.tag! 'n2:HandlingTotal', localized_amount(options[:handling], currency_code),'currencyID' => currency_code
              xml.tag! 'n2:TaxTotal', localized_amount(options[:tax], currency_code), 'currencyID' => currency_code
            end

            xml.tag! 'n2:NotifyURL', options[:notify_url]
            xml.tag! 'n2:ButtonSource', application_id.to_s.slice(0,32) unless application_id.blank?
            xml.tag! 'n2:InvoiceID', options[:order_id]
            xml.tag! 'n2:OrderDescription', options[:description]
          end
        end
      end
    end

    xml.target!
  end

看起来我的订单模型中的 credit_card 参数被视为在 Active Merchant 库中调用 has_key?options,其 CreditCard 代码未定义 has_key?。我正在使用最新版本的 Active Merchant。

有什么帮助吗?

I'm trying to enable credit card billing in a rails app, and I receive the following error when trying to create the order: undefined method 'has_key?' for #<ActiveMerchant::Billing::CreditCard:0x244b3d8>

The referenced code is the purchase method in the order model:

class Order < ActiveRecord::Base
  ...
  include ActiveMerchant::Billing

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

  def purchase

    response = GATEWAY.authorize(price_in_cents, credit_card)
    if response.success?
      GATEWAY.capture(price_in_cents, response.authorization)
      cart.update_attribute(:purchased_at, Time.now)
    else
      raise StandardError, response.message
    end

  end

end

I'm using a Paypal Express gateway, for which the Active Merchant library defines purchase as follows:

  def purchase(money, options = {})
    requires!(options, :token, :payer_id)

    commit 'DoExpressCheckoutPayment', build_sale_or_authorization_request('Sale', money, options)
  end

  private
  def build_get_details_request(token)
    xml = Builder::XmlMarkup.new :indent => 2
    xml.tag! 'GetExpressCheckoutDetailsReq', 'xmlns' => PAYPAL_NAMESPACE do
      xml.tag! 'GetExpressCheckoutDetailsRequest', 'xmlns:n2' => EBAY_NAMESPACE do
        xml.tag! 'n2:Version', API_VERSION
        xml.tag! 'Token', token
      end
    end

    xml.target!
  end

  def build_sale_or_authorization_request(action, money, options)
    currency_code = options[:currency] || currency(money)

    xml = Builder::XmlMarkup.new :indent => 2
    xml.tag! 'DoExpressCheckoutPaymentReq', 'xmlns' => PAYPAL_NAMESPACE do
      xml.tag! 'DoExpressCheckoutPaymentRequest', 'xmlns:n2' => EBAY_NAMESPACE do
        xml.tag! 'n2:Version', API_VERSION
        xml.tag! 'n2:DoExpressCheckoutPaymentRequestDetails' do
          xml.tag! 'n2:PaymentAction', action
          xml.tag! 'n2:Token', options[:token]
          xml.tag! 'n2:PayerID', options[:payer_id]
          xml.tag! 'n2:PaymentDetails' do
            xml.tag! 'n2:OrderTotal', localized_amount(money, currency_code), 'currencyID' => currency_code

            # All of the values must be included together and add up to the order total
            if [:subtotal, :shipping, :handling, :tax].all?{ |o| options.has_key?(o) }
              xml.tag! 'n2:ItemTotal', localized_amount(options[:subtotal], currency_code), 'currencyID' => currency_code
              xml.tag! 'n2:ShippingTotal', localized_amount(options[:shipping], currency_code),'currencyID' => currency_code
              xml.tag! 'n2:HandlingTotal', localized_amount(options[:handling], currency_code),'currencyID' => currency_code
              xml.tag! 'n2:TaxTotal', localized_amount(options[:tax], currency_code), 'currencyID' => currency_code
            end

            xml.tag! 'n2:NotifyURL', options[:notify_url]
            xml.tag! 'n2:ButtonSource', application_id.to_s.slice(0,32) unless application_id.blank?
            xml.tag! 'n2:InvoiceID', options[:order_id]
            xml.tag! 'n2:OrderDescription', options[:description]
          end
        end
      end
    end

    xml.target!
  end

It looks like the credit_card argument in my order model is being treated as the options on which has_key? is being called in the Active Merchant library, whose CreditCard code doesn't define has_key?. I'm using the latest version of Active Merchant.

Any help?

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

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

发布评论

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

评论(1

美人迟暮 2024-11-17 04:08:42

好像有些网关有不同的购买方式。

TrustCommerceGateway(代码显示在https://github.com/activemerchant/active_merchant

def purchase(money, creditcard_or_billing_id, options = {})

https://github.com/activemerchant/active_merchant/blob/b417c77d43 5b8d163b6e77588c80ac347cc1316a /lib/active_merchant/billing/gateways/trust_commerce.rb#L164

PayPalExpressCheckoutGateway

def purchase(money, options = {})

所以看起来如果你想使用 ActiveMerchant::Billing::CreditCard 与 Paypal,你必须使用 PaypalGateway (而不是 PaypalExpressCheckoutGateway)

Looks like some gateways have a different purchase method.

TrustCommerceGateway (code showed on https://github.com/activemerchant/active_merchant)

def purchase(money, creditcard_or_billing_id, options = {})

https://github.com/activemerchant/active_merchant/blob/b417c77d435b8d163b6e77588c80ac347cc1316a/lib/active_merchant/billing/gateways/trust_commerce.rb#L164

PayPalExpressCheckoutGateway

def purchase(money, options = {})

So looks like you have to use PaypalGateway (and not PaypalExpressCheckoutGateway) if you want to use ActiveMerchant::Billing::CreditCard with Paypal

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