Custom HTTP Client Braintree Go-lang Google App Engine

Tajhul Faijin Aliyudin
Google Cloud - Community
2 min readMar 17, 2018
Image source https://blog.charmes.net/images/gopherswrench.jpg

Overview

Braintree is one of the biggest payment gateway provider that providing tons of capabilities to perform payment operations.

Google App Engine is a fully managed platform that completely abstracts away infrastructure so developer only focus on code

Braintree is not providing (yet) official SDK for Go-lang. Gratefully someone has made it perfectly — available for us.

Braintree Go SDK : https://github.com/lionelbarrow/braintree-go

Problem

In past few months ago I was struggled when running it on Google App Engine machine, either in local GAE (dev_appserver) or when it was published to the GAE cloud.

The SDK throwing this error message :

“Unable to generate client token : Post https://api.sandbox.braintreegateway.com:443/merchants/xxxxx/client_token: http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://cloud.google.com/appengine/docs/go/urlfetch/"

Someone has similiar issue with me :

https://github.com/lionelbarrow/braintree-go/issues/49

This issue appears because App Engine doesn’t allow us to perform outbound request directly using native Go-lang http client, everything should be passed over urlfetch.

Solution

So we need to attach custom http client (GAE url fetch client) when we creating Braintree client.

Here’s two example that covered those issue :

  • Create Braintree client ( had a problem here ) :
Iin the code above, app engine will block the outbound requests from the SDK
  • Solution, create Braintree client with custom http client :
create custom httpclient (line 2) then pass it on the braintree client creation (line 8)

Now, it’s working perfectly on GAE machine.

That’s all :)

--

--