GoCardlessDeveloper Docs
Create a sandbox account

Setting up mandates with Components#

View as Markdown

Use the Billing Request component to set up a Direct Debit mandate for Bacs (UK) and Sepa (EUR). Payers fill in the form and authorise the mandate without leaving your page.

Entire Flow

1. Before you start#

Complete the general setup: an access token with GoCardless Components enabled, and an endpoint that mints a ui_components session token. Then add and configure the library.

2. Mount the component#

<div id="gc-billing"></div>
const gc = GcComponents.init({
  environment: "sandbox",
  sessionToken: () => fetch("/your-backend/session-token").then((r) => r.text()),
  onEvent: (event) => logEvent(event),
  onError: (error) => logError(error),
});
 
const brComponent = gc.createComponent("billing-request", {
  schemes: [GcComponents.Scheme.Bacs],
  onError: (error) => console.error("Error: ", error),
});
 
brComponent.mount("#gc-billing");
PropertyDescriptionRequired
schemesThe payment schemes to enable, as an array (e.g. [GcComponents.Scheme.Bacs]); currently only Bacs (Scheme.Bacs) and SEPA (Scheme.SepaCore) are supportedNo
creditorIdID of the creditor to create the billing request underNo
prefilledCustomerPrefills the customer/billing details form, see Prefilled customer detailsNo
onReadyToFulfilDeprecated since 2.35.0: the component now fulfils the billing request itself, so this callback is no longer called. Before 2.35.0, it received the billing request ID once the payer had submitted everything needed to fulfil it (see Fulfilment)No
onError(error: GoCardlessError) => void — called on error, in addition to the event busNo

See the options shared by all Billing Request flows.

Configured schemes must be valid for the merchant. If multiple schemes are configured or none are provided, the billing request component automatically displays a currency selector so the payer can choose their preferred currency for setting up the mandate

The currency selector is available when no scheme is enforced.

Billing Component Form

3. Testing#

We recommend testing your checkout end to end before going live. Check that you reach the confirmation screen without errors, then confirm the mandate was created in the GoCardless dashboard or through the API.

In sandbox, you can use our test bank details to complete the flow.

Successful DD

4. Fulfilment is automatic from version 2.35.0 onwards#

From version 2.35.0 onwards: fulfilment is automatic, so you don't need a fulfilment endpoint and you can skip this section. onReadyToFulfil is never called, so remove it, along with your fulfilment endpoint, if you still have them.

On a version before 2.35.0: you must fulfil the billing request from your server. Follow the steps below, and plan to upgrade to the latest version so you can remove them.

In versions before 2.35.0, when a payer completes the checkout flow, a callback signals that the billing request is ready to fulfil. You are responsible for calling POST /billing_requests/{billing_request_id}/actions/fulfil from your server at that point. The component waits up to 10 seconds for the billing request to be fulfilled before timing out and cancelling it.

To do this, pass onReadyToFulfil when creating the component. It receives the billing request ID once the payer has submitted everything needed to fulfil it, and is where you call your fulfilment endpoint:

const brComponent = gc.createComponent("billing-request", {
  schemes: [GcComponents.Scheme.Bacs],
  onReadyToFulfil: (billingRequestId) => fulfil(billingRequestId),
  onError: (error) => console.error("Error: ", error),
});

See the API reference for full details.

Example fulfilment endpoint in Ruby:

require 'sinatra'
require 'gocardless_pro'
 
client = GoCardlessPro::Client.new(
  access_token: '<YOUR_ACCESS_TOKEN>',
  environment: :sandbox
)
 
def validate_session_token(session_token, billing_request_id)
  session_token_client = GoCardlessPro::Client.new(
    access_token: session_token,
    environment: :sandbox
  )
  session_token_client.billing_requests.get(billing_request_id)
end
 
def fulfil_billing_request(billing_request_id)
  client.billing_requests.fulfil(billing_request_id)
end
 
post '/fulfil-billing-request/:billing_request_id' do
  billing_request_id = params[:billing_request_id]
  session_token = request.env['HTTP_SESSION_TOKEN']
 
  begin
    validate_session_token(session_token, billing_request_id)
    fulfil_billing_request(billing_request_id)
  rescue GoCardlessPro::Error => e
    status e.code
    content_type :json
    { success: false, error: e.message }.to_json
  end
end

What's next?#