Direct Debit: Taking Subscription Payments
Using subscriptions
Now, let’s create a subscription. Subscriptions collect a fixed, regular amount from a customer.
Let’s try collecting £15 per month on the 5th of each month from our customer:
1<?php
2require 'vendor/autoload.php';
3
4$client = new \GoCardlessPro\Client([
5 'access_token' => getenv('GC_ACCESS_TOKEN'),
6 'environment' => \GoCardlessPro\Environment::SANDBOX
7]);
8
9$subscription = $client->subscriptions()->create([
10 "params" => [
11 "amount" => 1500, // 15 GBP in pence
12 "currency" => "GBP",
13 "interval_unit" => "monthly",
14 "day_of_month" => "5",
15 "links" => [
16 "mandate" => "MD0000XH9A3T4C"
17 // Mandate ID from the last section
18 ],
19 "metadata" => [
20 "subscription_number" => "ABC1234"
21 ]
22 ],
23 "headers" => [
24 "Idempotency-Key" => "random_subscription_specific_string"
25 ]
26]);
27
28// Keep hold of this subscription ID - we'll use it in a minute.
29// It should look a bit like "SB00003GKMHFFY"
30print("ID: " . $subscription->id);
Each month, on the 5th of the month, the subscription will generate a new payment for £15 to be collected from the customer.
With the subscription ID, we can grab the subscription from the API. Let’s then try cancelling it so that no further payments are taken:
1<?php
2require 'vendor/autoload.php';
3
4$client = new \GoCardlessPro\Client([
5 'access_token' => getenv('GC_ACCESS_TOKEN'),
6 'environment' => \GoCardlessPro\Environment::SANDBOX
7]);
8
9$subscription = $client->subscriptions()->get("SB00003GKMHFFY");
10 // Subscription ID from above.
11
12print("Status: " . $subscription->status . "<br />");
13print("Cancelling...<br />");
14
15$subscription = $client->subscriptions()->cancel("SB00003GKMHFFY");
16
17print("Status: " . $subscription->status . "<br />");
What's Next?
Taking instalment payments against a mandate