# Setting up mandates with Components

Source: https://docs.gocardless.com/docs/collect-payments/integration-types/gocardless-components/billing-requests/mandates

# Setting up mandates with Components

Use the [Billing Request component](/docs/collect-payments/integration-types/gocardless-components/billing-requests) 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](/images/docs/collect-payments/integration-types/gocardless-components/image-4.gif)

## 1. Before you start

Complete the [general setup](/docs/collect-payments/integration-types/gocardless-components#1-setup): an access token with GoCardless Components enabled, and an endpoint that mints a `ui_components` session token. Then [add and configure the library](/docs/collect-payments/integration-types/gocardless-components#2-implementation).

## 2. Mount the component

```html
<div id="gc-billing"></div>
```

```javascript
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");
```

| Property          | Description                                                                                                                                                                                                                                                                                                         | Required |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| schemes           | The payment schemes to enable, as an array (e.g. `[GcComponents.Scheme.Bacs]`); currently only Bacs (`Scheme.Bacs`) and SEPA (`Scheme.SepaCore`) are supported                                                                                                                                                      | No       |
| creditorId        | ID of the creditor to create the billing request under                                                                                                                                                                                                                                                              | No       |
| prefilledCustomer | Prefills the customer/billing details form, see [Prefilled customer details](/docs/collect-payments/integration-types/gocardless-components/billing-requests#3-prefilled-customer-details)                                                                                                                          | No       |
| onReadyToFulfil   | **Deprecated 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](#4-fulfilment-is-automatic-from-version-2350-onwards)) | No       |
| onError           | `(error: GoCardlessError) => void` — called on error, in addition to the event bus                                                                                                                                                                                                                                  | No       |

See the [options shared by all Billing Request flows](/docs/collect-payments/integration-types/gocardless-components/billing-requests#2-options).

> **Info:**
> 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](/images/docs/collect-payments/integration-types/gocardless-components/image.png)

## 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](/docs/developer-resources/test-bank-details) to complete the flow.

![Successful DD](/images/docs/collect-payments/integration-types/gocardless-components/image-2.png)

## 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//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:

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

See the [API reference](/docs/api-reference/billing-request#fulfil-a-billing-request) for full details.

Example fulfilment endpoint in Ruby:

```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?

  
#### [Pay By Bank](/docs/collect-payments/integration-types/gocardless-components/billing-requests/pay-by-bank)

Collect one-off payments in GBP via Faster Payments.

  
#### [Remember Me Components](/docs/collect-payments/integration-types/gocardless-components/remember-me)

Recognise returning payers and save new payers' details for faster checkout.

  
#### [Components for Partners](/docs/collect-payments/integration-types/gocardless-components/partners)

Mint session tokens per merchant when you integrate Components on behalf of others.