in Engineering

GoCardless PHP library

This post relates to the Legacy GoCardless API. If you're starting a new integration, you'll need to use the new GoCardless API - for help getting started, check out our guide.

PHP is the most requested API client library here at GoCardless HQ. Today we’re pleased to announce that we have released our PHP library on Github. This post will give you a walkthrough of how to use it to collect a regular subscription payment. Once you've read it through then it might be useful to check out the examples included within the library.

First, check out our general overview of signing up to GoCardless. This walks you through signing up for a sandbox account. Once you have logged into the sandbox, click the 'Developer' tab to find your API keys - you'll need these shortly.

'git clone' or download the latest version of the PHP library from Github and copy it into a subdirectory. The essential files that you need are in the /lib folder. Then initialize the library within your code like this:

// Include library
include_once 'lib/gocardless.php';

// Config vars
$account_details = array(
 'app_id' => XXXXXXX,
 'app_secret' => XXXXXXX,
 'merchant_id' => XXXXXXX,
 'access_token' => XXXXXXX

// Initialize GoCardless

Next generate a URL to send users to to subscribe to your service:

// The parameters for the payment
$subscription_details = array(
 'amount' => '10.00',
 'interval_length' => 1,
 'interval_unit' => 'month'

// Generate the url
$subscription_url = GoCardless::new_subscription_url($subscription_details);

// Display the link
echo '<a href="'.$subscription_url.'">New subscription</a>';

When the user clicks this link they will be redirected to GoCardless to enter their bank details and create a new subscription. After this is complete, they will be redirected to the path you’ve set as the 'Redirect URI' in the Developer Panel.

The next step is to confirm the payment. You'll need the following code on the page that you've specified as the 'Redirect URI' in Developer settings:

// Default confirm variables
$confirm_params = array(
 'resource_id' => $_GET['resource_id'],
 'resource_type' => $_GET['resource_type'],
 'resource_uri' => $_GET['resource_uri'],
 'signature' => $_GET['signature']

// State is optional
if (isset($_GET['state'])) {
 $confirm_params['state'] = $_GET['state'];

$confirmed_resource = GoCardless::confirm_resource($confirm_params);

GoCardless will now generate a new payment for £10 and debit it directly from the user’s bank account every month. The first payment will be taken immediately.

You can now fetch information about all of your subscriptions from the API like this:


And you're done!

We hope this post has been useful. If you want to dive deeper into the GoCardless API then check out our documentation and our follow-up post on the PHP library.

Oh and we're hiring too!

By using this site you agree to the use of cookies for analytics, personalised content and ads. Learn more