Back

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

Sep 14, 20246 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of GoCardless API integration? You're in for a treat. We'll be using the gocardless-nodejs package to make our lives easier. Let's get cracking!

Prerequisites

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

  • Node.js and npm installed (I know you probably do, but just checking!)
  • A GoCardless account with API keys at the ready

Setting up the project

First things first, let's get our project set up:

mkdir gocardless-integration cd gocardless-integration npm init -y npm install gocardless-nodejs

Configuring the GoCardless client

Now, let's get that GoCardless client up and running:

const gocardless = require('gocardless-nodejs'); const client = gocardless( process.env.GOCARDLESS_ACCESS_TOKEN, gocardless.environments.sandbox // Switch to .live when you're ready! );

Implementing core functionalities

Creating a customer

Let's start by creating a customer:

async function createCustomer() { const customer = await client.customers.create({ email: '[email protected]', given_name: 'John', family_name: 'Doe', address_line1: '123 Main St', city: 'London', postal_code: 'SW1A 1AA', country_code: 'GB' }); console.log('Customer created:', customer.id); return customer; }

Setting up a mandate

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

async function setupMandate(customer) { const mandate = await client.mandates.create({ scheme: 'bacs', links: { customer: customer.id } }); console.log('Mandate created:', mandate.id); return mandate; }

Creating a payment

Time to create a payment:

async function createPayment(mandate) { const payment = await client.payments.create({ amount: 1000, // Amount in pence currency: 'GBP', links: { mandate: mandate.id }, metadata: { order_id: '12345' } }); console.log('Payment created:', payment.id); return payment; }

Handling webhooks

Webhooks are crucial for staying in sync with GoCardless. Here's a quick setup:

const express = require('express'); const app = express(); app.post('/webhooks', express.raw({type: 'application/json'}), (req, res) => { const signature = req.headers['webhook-signature']; try { const events = client.webhooks.parse(req.body, signature); events.forEach(handleEvent); res.sendStatus(200); } catch (err) { console.error('Invalid webhook signature', err); res.sendStatus(498); } }); function handleEvent(event) { console.log('Received event:', event.resource_type, event.action); // Handle the event based on its type and action }

Error handling and best practices

Always wrap your API calls in try/catch blocks:

try { const customer = await createCustomer(); // More operations... } catch (error) { console.error('Error:', error.message); // Handle the error appropriately }

And don't forget about rate limiting! The gocardless-nodejs package handles this for you, but it's good to be aware of it.

Testing the integration

Use the sandbox environment for testing. It's your playground to break things without consequences!

const client = gocardless( process.env.GOCARDLESS_ACCESS_TOKEN, gocardless.environments.sandbox );

Run through various scenarios: create customers, mandates, payments, and simulate webhook events.

Going live

When you're ready to go live:

  1. Switch to the live environment:
    gocardless.environments.live
  2. Use your live API keys
  3. Update webhook endpoints
  4. Double-check error handling
  5. Monitor your first few live transactions closely

Conclusion

And there you have it! You've just built a solid GoCardless API integration. Remember, this is just the beginning. There's so much more you can do with GoCardless.

For more in-depth info, check out the GoCardless API docs and the gocardless-nodejs package docs.

Now go forth and process those payments like a pro! 💪💻