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

问题描述 投票:0回答:3

从这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:password,但他们在这里的夹具文件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:参数进行硬编码的情况下初始化网关实例。

提前致谢!

ruby-on-rails activemerchant
3个回答
1
投票

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

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

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

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


0
投票

用户名,密码和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)

0
投票

我想我可以回答我自己的问题。一个人应该用自己的哈希传递每个不同的初始化。

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

请注意,我可以传递不同的选项,而无需对任何键进行硬编码,这正是我所希望的。

© www.soinside.com 2019 - 2024. All rights reserved.