Class-Level vs Instance Methodsanchor

availability

This page is only relevant for Ruby, Python, and PHP integrations started before early 2018. You can skip it if:

  • You are using our .NET, Java, or Node SDKs, or
  • You are starting a new Ruby integration after January 4, 2018, or
  • You are starting a new Python or PHP integration after February 27, 2018, or
  • You are already using authentication credentials other than API keys in your integration (i.e. an OAuth access token or a client ID and client secret)

On January 4, 2018, we made some changes to our Ruby documentation to recommend a different way to pass your Braintree credentials and make requests to our API. No changes are required for existing integrations – your code can stay exactly the same, and we’ll continue to support both gateway configuration methods. Just be aware that the code snippets you see in our developer docs now may reflect a different gateway configuration method than the one you’re currently using.

Backgroundanchor

Our PHP, Python, and Ruby SDKs provide two different ways to interact with the Braintree API:

  • Class-level methods that use a single shared configuration object for all requests to Braintree
  • Instance methods that use a configurable Braintree gateway object

Using instance methods means you won’t have to manage authentication for your integration at a global level. This in turn makes it easier to add tools to your integration that use different credentials for authentication (like OAuth).

What changed in January 2018anchor

To shift toward this more flexible gateway configuration method, we updated the example code snippets throughout our Ruby developer docs to use instance methods.

For example, a Ruby gateway configuration that looked like this:

  1. ruby
Braintree::Configuration.environment = :sandbox
Braintree::Configuration.merchant_id = "use_your_merchant_id"
Braintree::Configuration.public_key = "use_your_public_key"
Braintree::Configuration.private_key = "use_your_private_key"

now looks like this:

  1. ruby
gateway = Braintree::Gateway.new(
  :environment => :sandbox,
  :merchant_id => "use_your_merchant_id",
  :public_key => "use_your_public_key",
  :private_key => "use_your_private_key"
)

And a Ruby transaction sale request that looked like this:

  1. ruby
result = Braintree::Transaction.sale(
  :amount => "10.00",
  :payment_method_nonce => nonce_from_the_client,
  :options => {
    :submit_for_settlement => true
  }
)

now looks like this:

  1. ruby
result = gateway.transaction.sale(
  :amount => "10.00",
  :payment_method_nonce => nonce_from_the_client,
  :options => {
    :submit_for_settlement => true
  }
)

Instance methods are already supported in all 6 of our official server SDKs and are already the default for our .NET, Java, and Node SDKs, so this change will bring all of our server SDKs into alignment. It also means that if you’re using .NET, Java, or Node, your existing integration already supports multiple types of authentication, and you won’t see any changes in the docs you’ve grown to know and love. (You do love them, right?)

What this means for youanchor

If you have already integrated or are planning to integrate with our .NET, Java, or Node SDKs, these changes do not impact you.