2015-09-23 31 views
1

當我使用表格向信用卡收費時,它會拋出驗證錯誤「您提供的信用卡信息無效,請仔細檢查您提供的信息,然後重試。無法使用活動商家驗證信用卡?

任何解決方案?

這是我payment.rb

class Payment < ActiveRecord::Base 
    require "active_merchant/billing/rails" 

    attr_accessor :card_security_code 
    attr_accessor :credit_card_number 
    attr_accessor :expiration_month 
    attr_accessor :expiration_year 

    validates :first_name, presence: true 
    validates :last_name, presence: true 
    validates :card_security_code, presence: true 
    validates :credit_card_number, presence: true 
    validates :expiration_month, presence: true, numericality: {   greater_than_or_equal_to: 1, less_than_or_equal_to: 12 } 
    validates :expiration_year, presence: true 
    validates :amount, presence: true, numericality: { greater_than: 0 } 

    validate :valid_card 

    def credit_card 
    ActiveMerchant::Billing::CreditCard.new(
     number:    credit_card_number, 
     verification_value: card_security_code, 
     month:    expiration_month, 
     year:    expiration_year, 
     first_name:   first_name, 
     last_name:   last_name 
) 
    end 

    def valid_card 
    if !credit_card.valid? 
     errors.add(:base, "The credit card information you provided is not valid. Please double check the information you provided and then try again.") 
    false 
    else 
    true 
    end 
    end 

    def process 
    if valid_card 
     response = GATEWAY.authorize(amount * 100, credit_card) 
     if response.success? 
     transaction = GATEWAY.capture(amount * 100, response.authorization) 
     if !transaction.success? 
      errors.add(:base, "The credit card you provided was declined. Please double check your information and try again.") and return 
      false 
     end 
     update_columns({authorization_code: transaction.authorization, success: true}) 
     true 
     else 
     errors.add(:base, "The credit card you provided was declined. Please double check your information and try again.") and return 
     false 
     end 
    end 
    end 
end 

這是我activemerchant.rb

if Rails.env == 'development' 
    ActiveMerchant::Billing::FirstdataE4Gateway.wiredump_device = File.open(Rails.root.join('log','active_merchant.log'), 'a+') 
    ActiveMerchant::Billing::FirstdataE4Gateway.wiredump_device.sync = true 
    ActiveMerchant::Billing::Base.mode = :test 

    login = 'mylogin' 
    password='mypassword' 
elsif Rails.env == 'production' 
    login = 'mylogin' 
    password='mypassword' 
end 
GATEWAY = ActiveMerchant::Billing::FirstdataE4Gateway.new({ 
                  login: login, 
                  password: password 
                 }) 

檢查here

+0

jus t作爲重構你的過程'方法'的建議http://pastebin.com/7qEkNUau – IS04

回答

0

您還需要提供信用卡品牌名稱

def credit_card 
    ActiveMerchant::Billing::CreditCard.new(
     number:    credit_card_number, # 4111111111111111 
     verification_value: card_security_code, # 123 
     month:    expiration_month, # 1 
     year:    expiration_year, # 2020 
     first_name:   first_name, # Bibek 
     last_name:   last_name, # Sharma 
     brand:    credit_card_brand # visa 
    ) 
    end 
+0

你也可以在這裏找到http://www.rubydoc.info/github/Shopify/active_merchant/ActiveMerchant/Billing/CreditCard –

+0

它不工作@bipashant –

+0

你還會得到同樣的錯誤嗎? –