Hey there, fellow developer! Ready to dive into the world of GoCardless API integration? You're in for a treat. GoCardless is a powerhouse for handling direct debit payments, and their API is a dream to work with. We'll be using the gocardless/gocardless-pro
package, which makes our lives so much easier. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get that package installed:
composer require gocardless/gocardless-pro
Easy peasy, right?
Now, let's initialize our GoCardless client:
<?php require_once 'vendor/autoload.php'; $client = new \GoCardlessPro\Client([ 'access_token' => 'your_access_token_here', 'environment' => \GoCardlessPro\Environment::SANDBOX ]);
Pro tip: Use the sandbox environment while testing. Switch to LIVE
when you're ready for the big leagues.
Time to create our first customer:
$customer = $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' ]);
Boom! Customer created. Easy, right?
Now for the fun part - setting up a mandate. This is where we get permission to charge the customer:
$redirectFlow = $client->redirectFlows()->create([ 'description' => 'Wine Club Membership', 'session_token' => 'unique_session_token', 'success_redirect_url' => 'https://example.com/success' ]); // Redirect your customer to $redirectFlow->redirect_url
When they come back:
$completedFlow = $client->redirectFlows()->complete( $redirect_flow_id, ['session_token' => 'unique_session_token'] ); $mandate = $completedFlow->links->mandate;
Let's charge that card!
$payment = $client->payments()->create([ 'amount' => 1000, // in cents 'currency' => 'GBP', 'links' => [ 'mandate' => $mandate ], 'metadata' => [ 'order_id' => '12345' ] ]);
For recurring payments, check out the subscriptions API. It's super slick!
Webhooks are your friends. They'll keep you updated on what's happening:
$webhook = new \GoCardlessPro\Webhook([ 'secret' => 'your_webhook_secret' ]); $events = $webhook->parse( file_get_contents('php://input'), $_SERVER['HTTP_SIGNATURE'] ); foreach ($events as $event) { switch ($event->resource_type) { case 'payments': // Handle payment event break; // Handle other event types } }
Always wrap your API calls in try-catch blocks. GoCardless throws specific exceptions that you can catch and handle:
try { // Your API call here } catch (\GoCardlessPro\Core\Exception\InvalidApiUsageException $e) { // Handle invalid API usage } catch (\GoCardlessPro\Core\Exception\GoCardlessProException $e) { // Handle any other GoCardless Pro exception }
Remember to respect rate limits and log everything. Your future self will thank you!
Use the sandbox environment to test everything thoroughly. Create test scenarios for successful payments, failed payments, and webhooks. It's better to catch issues now than when real money is involved!
Ready for prime time? Here's your checklist:
And there you have it! You're now a GoCardless integration ninja. Remember, the GoCardless API docs are your best friend if you get stuck.
Now go forth and process those payments like a boss! 💪💰