Direct Debit: Taking a One off payment
Using one-off payments
Creating a payment
Let’s start by collecting a payment of £10 from the customer we just created:
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$payment = $client->payments()->create([
10 "params" => [
11 "amount" => 1000, // 10 GBP in pence
12 "currency" => "GBP",
13 "links" => [
14 "mandate" => "MD0000XH9A3T4C"
15 // The mandate ID from last section
16 ],
17 // Almost all resources in the API let you store custom metadata,
18 // which you can retrieve later
19 "metadata" => [
20 "invoice_number" => "001"
21 ]
22 ],
23 "headers" => [
24 "Idempotency-Key" => "random_payment_specific_string"
25 ]
26]);
27
28// Keep hold of this payment ID - we'll use it in a minute
29// It should look like "PM000260X9VKF4"
30print("ID: " . $payment->id);
You’ll need to use the currency
appropriate for the mandate you set up. This depends on what Direct Debit scheme you are using - the scheme of your mandate set up through the redirect flow will depend on your location (though you can set it manually by specifying a scheme
):
UK 🇬🇧 | GBP |
Sweden 🇸🇪 | SEK |
Denmark 🇩🇰 | DKK |
Australia 🇦🇺 | AUD |
New Zealand 🇳🇿 | NZD |
Canada 🇨🇦 | CAD |
United States of America 🇺🇸 | USD |
Everywhere else 🇪🇺 | EUR |
In the live environment, £10 would be collected from the customer’s bank account and then paid out to you. Since we’re in the sandbox, this won’t happen, but the payment will progress with realistic timings as though it was really being processed.
If you head to your dashboard, you’ll be able to see the payment you’ve just created.
Listing and updating payments
Much like listing customers with list()
as we did earlier, we can fetch an individual resource from the API at any time using its ID.
Once you have the ID, you can also perform useful functions on the payment, like cancelling it or retrying it if it has failed.
Let’s try grabbing our payment, and then cancelling it, taking a look at its status
before and after:
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$payment = $client->payments()->get("PM000260X9VKF4");
10 // Payment ID from above
11
12print("Status: " . $payment->status . "<br />");
13print("Cancelling...<br />");
14
15$payment = $client->payments()->cancel("PM000260X9VKF4");
16print("Status: " . $payment->status);
What's Next?
Taking subscription payments against a mandate