无法使用 Active Merchant 验证信用卡
Cannot validate credit card using Active Merchant
我在使用 Active Merchant gem 验证信用卡详细信息时遇到问题。当我尝试插入新付款时,总是出现错误:The credit card you provided was declined. 2 Please double check your information and try again.
。这意味着,响应不是成功的。我不知道为什么。我使用教程中的代码:Active Merchant Tutorial
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. 2 Please double check your information and try again.") and return
false
end
end
end
end
initializers/activemerchant.rb
if Rails.env == "development"
#we set up a log file
ActiveMerchant::Billing::FirstdataE4Gateway.wiredump_device = File.open(Rails.root.join("log","active_merchant.log"), "a+")
# enable logging
ActiveMerchant::Billing::FirstdataE4Gateway.wiredump_device.sync = true
# we wish to use the test version of the gateway
ActiveMerchant::Billing::Base.mode = :test
login = ENV["GATEWAY_ID"]
password = ENV["GATEWAY_PASSWORD"]
elsif Rails.env == "production"
login = ENV["GATEWAY_ID"]
password = ENV["GATEWAY_PASSWORD"]
end
GATEWAY = ActiveMerchant::Billing::FirstdataE4Gateway.new({
login: login,
password: password
})
GATEWAY_ID 和 GATEWAY_PASSWORD 我存储在 .env 文件中的数据。
更新:
如果我使用 response.inspect,显示如下:
#<ActiveMerchant::Billing::Response:0x007f4d0bfc8d98 @params={"transaction_approved"=>"false", "error_number"=>"401", "error_description"=>"Unauthorized Request. Bad or missing credentials."}, @message="Unauthorized Request. Bad or missing credentials.", @success=false, @test=true, @authorization="", @fraud_review=nil, @error_code=nil, @avs_result={"code"=>nil, "message"=>nil, "street_match"=>nil, "postal_match"=>nil}, @cvv_result={"code"=>nil, "message"=>nil}>
Unauthorized Request. Bad or missing credentials.
这表明您的登录名或密码变量可能未正确设置?您是否确保在开发环境和生产环境中都正确设置了环境变量 GATEWAY_ID 和 GATEWAY_PASSWORD?
我在使用 Active Merchant gem 验证信用卡详细信息时遇到问题。当我尝试插入新付款时,总是出现错误:The credit card you provided was declined. 2 Please double check your information and try again.
。这意味着,响应不是成功的。我不知道为什么。我使用教程中的代码:Active Merchant Tutorial
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. 2 Please double check your information and try again.") and return
false
end
end
end
end
initializers/activemerchant.rb
if Rails.env == "development"
#we set up a log file
ActiveMerchant::Billing::FirstdataE4Gateway.wiredump_device = File.open(Rails.root.join("log","active_merchant.log"), "a+")
# enable logging
ActiveMerchant::Billing::FirstdataE4Gateway.wiredump_device.sync = true
# we wish to use the test version of the gateway
ActiveMerchant::Billing::Base.mode = :test
login = ENV["GATEWAY_ID"]
password = ENV["GATEWAY_PASSWORD"]
elsif Rails.env == "production"
login = ENV["GATEWAY_ID"]
password = ENV["GATEWAY_PASSWORD"]
end
GATEWAY = ActiveMerchant::Billing::FirstdataE4Gateway.new({
login: login,
password: password
})
GATEWAY_ID 和 GATEWAY_PASSWORD 我存储在 .env 文件中的数据。 更新: 如果我使用 response.inspect,显示如下:
#<ActiveMerchant::Billing::Response:0x007f4d0bfc8d98 @params={"transaction_approved"=>"false", "error_number"=>"401", "error_description"=>"Unauthorized Request. Bad or missing credentials."}, @message="Unauthorized Request. Bad or missing credentials.", @success=false, @test=true, @authorization="", @fraud_review=nil, @error_code=nil, @avs_result={"code"=>nil, "message"=>nil, "street_match"=>nil, "postal_match"=>nil}, @cvv_result={"code"=>nil, "message"=>nil}>
Unauthorized Request. Bad or missing credentials.
这表明您的登录名或密码变量可能未正确设置?您是否确保在开发环境和生产环境中都正确设置了环境变量 GATEWAY_ID 和 GATEWAY_PASSWORD?