Class-Level vs Instance Methods
On February 27, 2018, we made some changes to our PHP 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.
Background
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 February 2018
To shift toward this more flexible gateway configuration method, we updated the example code snippets throughout our Python and PHP developer docs to use instance methods.
For example, a PHP gateway configuration that looked like this:
- php
BraintreeConfiguration::environment('sandbox');
BraintreeConfiguration::merchantId('use_your_merchant_id');
BraintreeConfiguration::publicKey('use_your_public_key');
BraintreeConfiguration::privateKey('use_your_private_key');
now looks like this:
- php
$gateway = new BraintreeGateway([
'environment' => 'sandbox',
'merchantId' => 'use_your_merchant_id',
'publicKey' => 'use_your_public_key',
'privateKey' => 'use_your_private_key'
]);
And a PHP transaction sale request that looked like this:
- php
$result = BraintreeTransaction::sale([
'amount' => '10.00',
'paymentMethodNonce' => $nonceFromTheClient,
'options' => [
'submitForSettlement' => True
]
]);
now looks like this:
- php
$result = $gateway->transaction()->sale([
'amount' => '10.00',
'paymentMethodNonce' => $nonceFromTheClient,
'options' => [
'submitForSettlement' => 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.
What this means for you
- If you have already integrated or are almost finished integrating with our PHP SDK:
- You can continue using class-level methods with no changes – we’ll continue to support both gateway configuration methods.
- From this point forward, if you return to our docs to make any changes to your integration, you’ll need to tweak the code snippets before using them in your integration. Refer to the table below to translate the instance methods in the docs to the class-level methods you’re using.
- If you are planning to integrate or have just started integrating with our PHP SDK:
- While not required, we recommend using instance methods. The docs are already updated to reflect instance methods, so you can use the code snippets as-is in your integration. If you've already started integrating, just be sure to update your configuration and server requests to use gateway instances.
- If you choose to use the older class-level methods, you’ll need to tweak the code snippets from the docs before using them in your integration. Refer to the table below to translate instance methods to class-level methods.
Class-to-instance method translation table
Class-level method | Instance method |
---|---|
Braintree\AddOn::<method> | $gateway->addOn()-><method> |
Braintree\Address::<method> | $gateway->address()-><method> |
Braintree\ClientToken::<method> | $gateway->clientToken()-><method> |
Braintree\CreditCard::<method> | $gateway->creditCard()-><method> |
Braintree\CreditCardVerification::<method> | $gateway->creditCardVerification()-><method> |
Braintree\Customer::<method> | $gateway->customer()-><method> |
Braintree\Discount::<method> | $gateway->discount()-><method> |
Braintree\Dispute::<method> | $gateway->dispute()-><method> |
Braintree\DocumentUpload::<method> | $gateway->documentUpload()-><method> |
Braintree\Merchant::<method> | $gateway->merchant()-><method> |
Braintree\MerchantAccount::<method> | $gateway->merchantAccount()-><method> |
Braintree\PaymentMethod::<method> | $gateway->paymentMethod()-><method> |
Braintree\PaymentMethodNonce::<method> | $gateway->paymentMethodNonce()-><method> |
Braintree\PaypalAccount::<method> | $gateway->payPalAccount()-><method> |
Braintree\Plan::<method> | $gateway->plan()-><method> |
Braintree\SettlementBatchSummary::<method> | $gateway->settlementBatchSummary()-><method> |
Braintree\Subscription::<method> | $gateway->subscription()-><method> |
Braintree\Testing::<method> | $gateway->testing()-><method> |
Braintree\Transaction::<method> | $gateway->transaction()-><method> |
Braintree\TransactionLineItem::<method> | $gateway->transactionLineItem()-><method> |
Braintree\WebhookNotification::<method> | $gateway->webhookNotification()-><method> |
Common errors
There are a couple of common errors you may see if you make a server request that doesn’t match your gateway configuration method.
For authentication, authorization, or configuration errors related to your authentication credentials themselves, see the Exceptions reference page.
Undefined variable or null object
You’ll see an error like this directly from PHP if you attempt to use an instance method for a server request, but you’re using a single shared configuration object.
To fix this:
- If you are using class-level methods everywhere else, switch the offending instance method to a class-level method instead. Refer to the translation table above for help forming the corresponding class-level method.
- If you are using instance methods everywhere else, make sure that you have a properly-configured gateway in the current scope (e.g. pass the gateway as an argument to the method). [Refer to the section above on what changed](#what-changed-in-february-2018) to see an example of a correctly-configured gateway instance.
Configuration exception
In PHP, you’ll receive this exception if you attempt to use a class-level method for a server request, but your gateway doesn’t have a single shared configuration object.
To fix this:
- If you are using instance methods everywhere else, switch the offending class-level method to an instance method instead. Refer to the translation table above for help forming the corresponding instance method.
- If you are using class-level methods everywhere else, make sure your integration is configured with a shared
<LanguageMatcher lang="ruby">Braintree::Configuration</LanguageMatcher><LanguageMatcher lang="php">Braintree\Configuration</LanguageMatcher>
object. [Refer to the section above on what changed](#what-changed-in-february-2018) to see an example of a correctly-formed shared configuration object.