首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“#<ActiveMerchant::Billing::CreditCard的未定义方法`has_key?‘...”

“#<ActiveMerchant::Billing::CreditCard的未定义方法`has_key?‘...”
EN

Stack Overflow用户
提问于 2011-05-27 10:24:17
回答 1查看 1.3K关注 0票数 1

我尝试在rails应用程序中启用信用卡账单,但在尝试创建订单时收到以下错误:undefined method 'has_key?' for #<ActiveMerchant::Billing::CreditCard:0x244b3d8>

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

代码语言:javascript
复制
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 Merchant库将purchase定义如下:

代码语言:javascript
复制
  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的最新版本。

有什么帮助吗?

EN

回答 1

Stack Overflow用户

发布于 2015-08-11 06:02:52

看起来有些网关有不同的购买方式。

TrustCommerceGateway (https://github.com/activemerchant/active_merchant上显示的代码)

代码语言:javascript
复制
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

代码语言:javascript
复制
def purchase(money, options = {})

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6147202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档