Back to: Blog Author: Piotr Pawłowski Published on: November 1, 2023

Integration with GoCardless.com System Using Java: A Technical Guide

Introduction:

Integrating payment gateways into applications is crucial for businesses looking to automate and streamline their payment processing systems. GoCardless.com is one such platform that specializes in Direct Debit services, providing an easy and efficient way for businesses to collect payments. This article will delve into the technical aspects of integrating with GoCardless.com using Java, providing step-by-step guidance and code examples.

Setting Up Your GoCardless Account:

Before diving into the technical integration, ensure you have a GoCardless account. Visit their official website, sign up, and follow the setup process. Once your account is ready, generate your API keys as they will be required for authentication.

Adding GoCardless Java SDK to Your Project:

GoCardless provides a Java SDK which makes the integration process smoother. You can add it to your project’s dependency manager. For Maven, add the following to your pom.xml file:
   com.gocardless
   gocardless-pro
   4.10.0  

Initializing GoCardless Client:

To interact with the GoCardless API, initialize the GoCardless client using your API key.
import com.gocardless.GoCardlessClient; public class GoCardlessIntegration { private static final String ACCESS_TOKEN = "your-access-token"; // Replace with your actual access token public static void main(String[] args) { GoCardlessClient client = GoCardlessClient.newBuilder(ACCESS_TOKEN) .withEnvironment(GoCardlessClient.Environment.SANDBOX) // Use SANDBOX for testing, LIVE for production .build(); } }

Creating a Customer:

To start collecting payments, you need to create a customer on GoCardless. Here’s an example of how to create a customer:
import com.gocardless.services.CustomerService.CustomerCreateRequest; import com.gocardless.services.CustomerService.Customer; public class GoCardlessIntegration { // ... Previous code ... public static void createCustomer(GoCardlessClient client) { Customer customer = client.customers().create() .withEmail("[email protected]") .withGivenName("John") .withFamilyName("Doe") .withAddressLine1("123 Main Street") .withCity("London") .withPostalCode("E1 6AN") .withCountryCode("GB") .execute(); System.out.println("Customer created: " + customer.getId()); } }

Setting Up Direct Debit Mandate:

After creating a customer, set up a Direct Debit mandate for them. import com.gocardless.services.MandateService.MandateCreateRequest; import com.gocardless.services.MandateService.Mandate; public class GoCardlessIntegration { // ... Previous code ... public static void createMandate(GoCardlessClient client, String customerId, String bankAccountId) { Mandate mandate = client.mandates().create() .withLinksCustomer(customerId) .withLinksBankAccount(bankAccountId) .execute(); System.out.println("Mandate created: " + mandate.getId()); } }

Processing a Payment:

Finally, create a payment for a customer with an active mandate. import com.gocardless.services.PaymentService.PaymentCreateRequest; import com.gocardless.services.PaymentService.Payment; import java.math.BigDecimal; public class GoCardlessIntegration { // ... Previous code ... public static void createPayment(GoCardlessClient client, String mandateId) { Payment payment = client.payments().create() .withAmount(new BigDecimal("10.00")) // Amount in GBP .withCurrency("GBP") .withLinksMandate(mandateId) .withDescription("Test payment") .execute(); System.out.println("Payment created: " + payment.getId()); } }

Error Handling and Further Steps:

Always implement proper error handling in your integration. GoCardless Java SDK throws exceptions for API errors, which you can catch and handle accordingly. Further steps would include setting up webhooks to receive updates on payment statuses and implementing additional features such as refunds, subscriptions, and integrations with your internal accounting or CRM systems.

Integrating with GoCardless using Java is straightforward thanks to their well-documented API and Java SDK. By following the steps outlined in this article and referring to the official GoCardless API documentation, you can successfully automate your payment collection process, saving time and reducing manual errors.

Work with the best experts in the fintech industry!

Do you have a project in mind?

Let's talk

Loading...