Ruby on rails Ruby:如何使用凭据初始化活动商户网关实例?
由此 我应该用这个来初始化一个活动的实例Ruby on rails Ruby:如何使用凭据初始化活动商户网关实例?,ruby-on-rails,activemerchant,Ruby On Rails,Activemerchant,由此 我应该用这个来初始化一个活动的实例 gateway = ActiveMerchant::Billing::Base.gateway( gateway_name ).new( :username => :some_credential :password => :some_other_credential ) 但是我事先不知道:username或:password,但是它们在这里的fixtures文件中。那么如何正确地做到这一点呢 例如,在fixtures.yml文件中,我们
gateway = ActiveMerchant::Billing::Base.gateway( gateway_name ).new(
:username => :some_credential
:password => :some_other_credential
)
但是我事先不知道:username
或:password
,但是它们在这里的fixtures文件中。那么如何正确地做到这一点呢
例如,在fixtures.yml文件中,我们可以看到以下内容
adyen:
用户名:“”
密码:“”
商户账户:''
因此,我们可以用这个来初始化
gateway = ActiveMerchant::Billing::Base.gateway( 'adien' ).new(
username: => :some_credential
password: => :some_other_credential
merchant_account: => some_more_credential
)
我需要能够在不硬编码上述示例中的用户名:
、密码:
和商户账户:
参数的情况下初始化网关实例
提前谢谢 您应该看看环境变量。它们允许您在安全的地方定义变量,并在需要时引用它们 您可以在某个地方定义
PASSWORD=mysecretpassword
,然后在Rails代码中将其称为ENV[“PASSWORD”]
有很多方法可以做到这一点。请看这里:
用户名、密码和商户账户都是实例变量,因此您需要使用
instance\u variable\u set
gateway.instance_variable_set(:@username, :some_username)
gateway.instance_variable_set(:@password, :some_password)
gateway.instance_variable_set(:@merchant_account, :some_merchant_account)
我想我可以回答我自己的问题。应该使用自己的哈希通过每个不同的初始化
require 'active_merchant'
settings = {name: 'braintree_blue', merchant_id: 'x', public_key: 'y', private_key: 'z'}
another_settings = {name: 'trust_commerce', login: 'TestMerchant', password: 'password'}
Gateway = ActiveMerchant::Billing::Base::gateway(settings[:name]).new(settings)
Another_Gateway = ActiveMerchant::Billing::Base::gateway(another_settings[:name]).new(another_settings)
puts Gateway.options
puts Another_Gateway.options
请注意,我可以传递不同的选项,而不需要硬编码任何密钥,这正是我所需要的。我想知道一种方法,而不需要将“用户名:”和“密码:”以及“商户帐户:”硬编码到示例代码中,而不是:some_凭证”,“some_其他_凭证”部分(您的解决方案显然可以解决)。我想我选错了术语“凭证参数”,所以现在编辑…但这只能适用于adien示例。。如果网关只需要两个凭据,如@username和@password,该怎么办?如果只需要一个像@apikey这样的凭证呢?