Implementing Braintree Using Coldfusion

The COBLOG

Implementing Braintree Using Coldfusion

6/1/2019 1:03:41 PM

One of my recent projects was to switch the way the company that I work for does business transactions. For the longest time, it was done via PayPal. Chargebacks, however, were not one of PayPal's best features. The company decided to go a different route with Braintree, which was owned by PayPal, but had lower chargebacks and more flexibility in payment options.

This project was assigned to me at the beginning of April. The company uses an Adobe Coldfusion server with a MySQL backend. To say the least, Braintree was not made to work specifically with Coldfusion. One thing I learned when talking to sales people for tech companies, they'll always say, "Sure, we can get our system to work with your system and we'll walk you through every step of the way." The real truth is, it may work with your system, but you will be doing most, if not all, of the work.

Since I took on the project, Braintree's technical support has been, to say the very least, disappointing. You have to be very specific about what you ask, and 99% of the response is a link to their rather vague documentation; versed only in Java, ASP.NET, Node.js, PHP, or Python.

You'll notice that Coldfusion isn't among those listed. My team and I had a very unpleasant time trying to get Node.js working on our Coldfusion server since the Braintree transaction drop-in in Node.js was considered the most ideal interface.

There were options online on getting Node.js to work in Coldfusion and we did find some old Coldfusion CFC's that work on multiple payment platforms including Braintree, but alas, to not much avail. The CFC used an outdated gateway with Braintree and trying to contact the author for an updated version yielded no response.

After all that, we decided that interfacing Coldfusion with their Java setup was the only option left. On a lighter note, Coldfusion relies a lot on Java libraries, but learning how to work with Java classes using Coldfusion Markup was not a walk in the park, and Braintree's Java documentation didn't make it much easier. After an exchange of not-so-friendly emails between us in programming, Braintree's support, and a few exchanges with our leaders, it was a few weeks until we found a solution.

Since there is little to no documentation on getting Coldfusion to work with Braintree's Java libraries, and getting a root canal is easier than getting good technical support from Braintree; I've put together some examples on how we got our Coldfusion server to successfully communicate with Braintree using their Java Library (JAR File).

Before I continue, please note that this may not be the best solution out there, and there's always room for improvement and code cleanup. That being said, you may look at these examples and come up with a better solution that is very possible; but this setup did work for us.

To start off, this post will cover the basics of setting up and initialize your Coldfusion server to work with Braintree via their Java Class Library JAR file.

Before you begin, make sure that Braintree has set up your Sandbox and Production Dashboards. You'll need to login to access your Merchant ID, Public Key, and Private Key on both dashboards.

Go to Braintree's Developer Documentation to get the latest Java Library JAR File. https://developers.braintreepayments.com/start/hello-server/java

This JAR file will need to be placed in your Coldfusion Library folder (e.g. D:\Coldfusion2016\cfusion\lib)

Restart the Coldfusion Application Server Service.

Now you need to set-up your transaction pages to declare and initialize the necessary Java classes.

(Note: The Braintree Java classes may be viewed non-compiled at https://github.com/braintree/braintree_java/tree/master/src/main/java/com/braintreegateway)

Each Java class needs to be declared on your transaction page or in a CFC if the processing occurs on more than one page.

<cfobject type="java" class="com.braintreegateway.Environment" name="environment" />
<cfobject type="java" class="com.braintreegateway.BraintreeGateway" name="gateway" />

The environment object is needed for the initialization. The gateway is needed for the initialization and for all Primary Braintree Java functions.

Following the declaration of objects, you'll be able to initialize the Braintree Gateway. You'll need three variables provided to you by your Braintree Control Panel/Dashboard; your Merchant ID, Public Key, and Private Key. It is recommended that these variables are declared in your Application.cfc file. For this example, we'll declare it on the page followed by the initialization.

<cfset variables.merchantID = "1z2y3x4w5v" />
<cfset variables.publicKey = "9a8b7c6d5e" />
<cfset variables.privateKey = "1q2w3e4r5t" />

<!--- SANDBOX INITIALIZATION FOR TEST TRANSACTIONS --->
<cfset j = gateway.init(Environment.SANDBOX, '#variables.MerchantID#', '#variables.PublicKey#', '#variables.PrivateKey#')>

When you're ready to go live, initialize the gateway with your Braintree Production Credentials. You can't pass 'Environment.PRODUCTION' in as a variable (at least I couldn't), so you'll need to hard-code it into the function.

<!--- PRODUCTION INITIALIZATION FOR LIVE TRANSACTIONS --->
<cfset j = gateway.init(Environment.PRODUCTION, '#variables.MerchantID#', '#variables.PublicKey#', '#variables.PrivateKey#')>

Here it is in it's entirety. This will get the Braintree Gateway setup for you for Sandbox testing.

<cfobject type="java" class="com.braintreegateway.Environment" name="environment" />
<cfobject type="java" class="com.braintreegateway.BraintreeGateway" name="gateway" />

<cfset variables.merchantID = "1z2y3x4w5v" />
<cfset variables.publicKey = "9a8b7c6d5e" />
<cfset variables.privateKey = "1q2w3e4r5t" />

<!--- SANDBOX INITIALIZATION FOR TEST TRANSACTIONS --->
<cfset j = gateway.init(Environment.SANDBOX, '#variables.MerchantID#', '#variables.PublicKey#', '#variables.PrivateKey#')>

Next post will go into how to process a simple credit card transaction. It will include the basic initialization code as well as additional 'cfobject' calls necessary for the transaction to run.


END OF LINE.
DISCLAIMER: The recommendations posted here may not be the best solutions. Please feel free to make your own adjustments which may be better than what I posted.
#45 - 2/27/2021 12:51:32 PM
Author: Anonymous
Looking into this in 2021, very helpful, thanks.
#40 - 9/27/2019 1:43:27 PM
Author: Cookie
Really appreciate you taking the time to document your experience and results. I am about to embark on the same journey you took. Is your solution working well ? Would you do anything different in hindsight ?
Reply from James O. - 9/27/2019 2:15:00 PM
Hi! Thanks for the feedback! So far, the solutions are working fine, but they can be fine tuned. I would like to find a better approach to submitting credit cards using an interface that returns a payment nonce just like the PayPal section does, but the only one Braintree offers uses node.js, which our server can't play nicely with in conjunction with Coldfusion. It's a lot of trial and error since Braintree Implementation Support has been, to say the least, disappointing.

Hope these posts will help you out! :)



© 2024 Openshaw Ltd. Productions