Back

Step by Step Guide to Building a GoCardless API Integration in Go

Sep 14, 20246 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to dive into the world of payment processing? Today, we're going to walk through building a GoCardless API integration using Go. We'll be using the gocardless-pro-go package, which makes our lives a whole lot easier. So, buckle up and let's get coding!

Prerequisites

Before we jump in, make sure you've got:

  • Go installed on your machine (I know, obvious, right?)
  • A GoCardless account with API keys (if you don't have one, go grab it!)
  • A basic understanding of Go and RESTful APIs (but don't worry, we'll keep it simple)

Setting up the project

Let's kick things off by creating a new Go project:

mkdir gocardless-integration cd gocardless-integration go mod init gocardless-integration

Now, let's install the star of our show, the gocardless-pro-go package:

go get github.com/gocardless/gocardless-pro-go/v2

Initializing the GoCardless client

Alright, time to get our hands dirty! First, let's import the package and create a client:

package main import ( "fmt" gocardless "github.com/gocardless/gocardless-pro-go/v2" ) func main() { client, err := gocardless.New("your_access_token", gocardless.Live) if err != nil { panic(err) } fmt.Println("GoCardless client initialized!") }

Replace "your_access_token" with your actual API key. And hey, in a real-world scenario, you'd want to use environment variables for that!

Implementing core functionalities

Now for the fun part - let's implement some core functionalities!

Creating customers

customer, err := client.Customers.Create(&gocardless.CustomerCreateParams{ Email: "[email protected]", GivenName: "John", FamilyName: "Doe", AddressLine1: "123 Main St", City: "London", PostalCode: "SW1A 1AA", CountryCode: "GB", }) if err != nil { panic(err) } fmt.Printf("Customer created: %s\n", customer.ID)

Setting up mandates

bankAccount, err := client.CustomerBankAccounts.Create(&gocardless.CustomerBankAccountCreateParams{ AccountNumber: "55779911", BranchCode: "200000", CountryCode: "GB", Currency: "GBP", Links: gocardless.CustomerBankAccountCreateParamsLinks{ Customer: customer.ID, }, }) if err != nil { panic(err) } mandate, err := client.Mandates.Create(&gocardless.MandateCreateParams{ Links: gocardless.MandateCreateParamsLinks{ CustomerBankAccount: bankAccount.ID, }, }) if err != nil { panic(err) } fmt.Printf("Mandate created: %s\n", mandate.ID)

Creating one-off payments

payment, err := client.Payments.Create(&gocardless.PaymentCreateParams{ Amount: 1000, // in cents Currency: "GBP", Links: gocardless.PaymentCreateParamsLinks{ Mandate: mandate.ID, }, }) if err != nil { panic(err) } fmt.Printf("Payment created: %s\n", payment.ID)

Error handling and best practices

Always check for errors after API calls. It's Go, after all - we love our explicit error handling!

For rate limiting, the gocardless-pro-go package handles it automatically, but it's good to be aware of the limits.

And please, for the love of all things secure, never hardcode your API keys. Use environment variables or a secure secret management system.

Testing the integration

GoCardless provides a sandbox environment for testing. Just use gocardless.Sandbox instead of gocardless.Live when initializing the client.

For unit tests, you can use Go's built-in testing package. Mock the API responses to test your integration thoroughly.

Deployment considerations

When deploying, remember to:

  • Use environment variables for API keys
  • Set up proper logging and monitoring
  • Consider using a tool like Prometheus for metrics

Conclusion

And there you have it! You've just built a GoCardless API integration in Go. Pretty cool, right? We've covered the basics, but there's always more to explore. Check out the GoCardless API docs for more advanced features like recurring payments, refunds, and handling large datasets.

Remember, the key to a great integration is attention to detail and robust error handling. Keep practicing, and soon you'll be a GoCardless Go guru!

Happy coding, and may your payments always process smoothly! 🚀💸