Ruby:如何使用凭据初始化活动商家网关实例?

来自http://www.rubydoc.info/github/Shopify/active_merchant/ActiveMerchant%2FBilling%2FBase.gateway

我应该使用它来初始化active_merchant的一个实例

gateway = ActiveMerchant::Billing::Base.gateway( gateway_name ).new( :username => :some_credential :password => :some_other_credential ) 

但我不知道:username提前:username:password ,但是它们在fixtures文件https://github.com/activemerchant/active_merchant/blob/master/test/fixtures.yml这里。 那么如何正确地做到这一点?

举个例子,在fixtures.yml文件中我们可以看到这个..

adyen: username: '' password: '' merchant_account: ''

因此我们可以用这个初始化..

 gateway = ActiveMerchant::Billing::Base.gateway( 'adien' ).new( username: => :some_credential password: => :some_other_credential merchant_account: => some_more_credential ) 

我需要能够初始化网关实例而无需硬编码上面示例中的username:password:merchant_account:参数。

提前致谢!

你应该看看环境变量。 它们允许您在安全的位置定义变量,并在需要时引用它们。

您可以在某处定义PASSWORD=mysecretpassword ,然后在Rails代码PASSWORD=mysecretpassword其称为ENV["PASSWORD"]

有很多方法可以做到这一点。 看看这里:

http://railsapps.github.io/rails-environment-variables.html

用户名,密码和merchant_account是实例变量,因此您需要使用instance_variable_set更改它们

 gateway.instance_variable_set(:@username, :some_username) gateway.instance_variable_set(:@password, :some_password) gateway.instance_variable_set(:@merchant_account, :some_merchant_account)