Skip to content
Go to GoCardless homepage
LoginSign up

GoCardless PHP library

By GoCardlessFeb 20122 min read

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!

GoCardless is used by over 55,000 businesses around the world. Learn more about how you can improve payment processing at your business today.

Learn moreSign Up

Interested in automating the way you get paid? GoCardless can help

Contact sales

Contact Us


Contact sales

+44 20 8338 9539


Request support

+44 20 8338 9540

Seen 'GoCardless Ltd' on your bank statement? Learn more

GoCardless Ltd., Sutton Yard, 65 Goswell Road, London, EC1V 7EN, United Kingdom

GoCardless (company registration number 07495895) is authorised by the Financial Conduct Authority under the Payment Services Regulations 2017, registration number 597190, for the provision of payment services. GoCardless SAS (23-25 Avenue Mac-Mahon, Paris, 75017, France), an affiliate of GoCardless Ltd (company registration number 834 422 180, R.C.S. PARIS), is authorised by the ACPR (French Prudential Supervision and Resolution Authority), Bank Code (CIB) 17118, for the provision of payment services.