Back

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

Sep 14, 2024 • 5 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of GoCardless API integration? You're in the right place. We'll be using the gocardless_pro package to make our lives easier. Let's get cracking!

Prerequisites

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

  • A Python environment set up (I know you've got this!)
  • A GoCardless account with API keys (if you don't have one, go grab it real quick)

Installation

First things first, let's get that gocardless_pro package installed:

pip install gocardless_pro

Easy peasy, right?

Setting up the client

Now, let's import the package and set up our client:

import gocardless_pro client = gocardless_pro.Client( access_token='your_access_token_here', environment='live' # Use 'sandbox' for testing )

Basic API operations

Creating a customer

Let's create a customer:

customer = client.customers.create( params={ "email": "[email protected]", "given_name": "Jane", "family_name": "Doe", "address_line1": "123 Main St", "city": "London", "postal_code": "SW1A 1AA", "country_code": "GB" } )

Setting up a mandate

Now, let's set up a mandate for our customer:

mandate = client.mandates.create( params={ "scheme": "bacs", "links": { "customer_bank_account": "BA123456" } } )

Creating a payment

Time to create a payment:

payment = client.payments.create( params={ "amount": 1000, # Amount in pence "currency": "GBP", "links": { "mandate": mandate.id }, "metadata": { "order_id": "ORDER-123" } } )

Handling webhooks

Webhooks are crucial for staying updated. Here's a quick example using Flask:

from flask import Flask, request import gocardless_pro app = Flask(__name__) @app.route('/webhooks', methods=['POST']) def handle_webhook(): webhook_secret = 'your_webhook_secret' signature = request.headers.get('Webhook-Signature') try: events = client.events.parse(request.data, signature, webhook_secret) for event in events: # Process the event print(f"Received event: {event.action}") return "", 200 except gocardless_pro.errors.InvalidSignatureError: return "Invalid signature", 498

Error handling and best practices

Always wrap your API calls in try-except blocks:

try: payment = client.payments.create(...) except gocardless_pro.errors.ValidationFailedError as e: print(f"Validation failed: {e.error}") except gocardless_pro.errors.GoCardlessProError as e: print(f"API error: {e}")

Remember to respect rate limits and implement proper logging!

Advanced features

Want to set up recurring payments? It's a breeze:

subscription = client.subscriptions.create( params={ "amount": 1500, "currency": "GBP", "interval_unit": "monthly", "day_of_month": "1", "links": { "mandate": mandate.id } } )

Testing

Always use the sandbox environment for testing:

client = gocardless_pro.Client( access_token='your_sandbox_access_token', environment='sandbox' )

And don't forget to write those unit tests!

Conclusion

And there you have it! You're now equipped to integrate GoCardless into your Python projects like a pro. Remember, practice makes perfect, so keep coding and exploring. If you need more info, check out the GoCardless API docs. Happy coding!