# Billing Request (PayTo feature)

## Billing Request actions

> In order to access the following endpoints and complete the required actions integrators will need the **custom payment pages upgrade **enabled on their account.

### What are Billing Request Actions?

### Billing Request Actions

Whenever you create a Billing Request, it will have a list of `actions` that require completion before it’s possible to fulfil the request.

The possible actions are:

- `collect_customer_details`, collect customer details required for the schemes
- `collect_bank_account`, create the bank account for the mandate/payment
- `bank_authorisation`, have the payer authorise the billing request via their bank if required
- `confirm_payer_details`, confirm customer and bank account details provided by the payer

Once all required actions have been completed, the Billing Request will `fulfil `automatically.

### Which actions should I implement?

Even if integrators complete a selection of actions themselves, Billing Request Flows can be used to complete the remaining actions.

Our recommendations for whether integrators should implement each action are:

- `collect_customer_details`, implement this if you want a white label flow to collect customer details or have already collected the details (prefilling, etc)
- `collect_bank_account`, as with customer details, but be aware that Billing Request Flows can avoid this step for certain schemes which can improve conversion by reducing the number of details a payer has to manually input
- `confirm_payer_details`, this action is not required for PayTo.
- `bank_authorisation`, implement this as the final step

### Ordering and control flow

The actions presented on a Billing Request are unordered, but each action may have dependencies, or have other actions depending on it.

These are specified in the fields `completes_actions` and `requires_actions`.

Taking a `bank_authorisation` as an example:

```json
{
  "type": "bank_authorisation",
  "status": "completed",
  "required": true,
  "completes_actions": ["collect_customer_details"],
  "requires_actions": ["collect_bank_account"]
}
```

**Action \*\***`status`\*\* is either `pending` or `completed`.

`**requires_actions: **`Actions can only be executed if the actions listed in `requires_action` have been completed- in this example, we can’t complete the bank authorisation until we’ve collected the bank.

**`completes_actions: `**Actions can complete other actions, which can help streamline checkout flows. The example shows how a bank authorisation might collect customer details, completing that action as a side effect.

For most integrators, we suggest building flows that process actions in the order of:

1. `collect_customer_details`, so customer contact details are prioritised
2. `collect_bank_account`, if it is required and another action won’t complete it
3. `bank_authorisation`, as the penultimate step before completing the checkout flow
4. `confirm_payer_details`, only applicable if it is a `mandate_request`

> The hosted checkout flow provided by Billing Request Flows is built to optimise conversion, and will follow the minimum number of steps to fulfil the Billing Request. Most integrators should lean on the flow to complete anything beyond collection of customer details.

### Action: collect_customer_details

### Action: `collect_customer_details`

Billing Requests aim to create billing resources against a customer, either a mandate (PayTo or Direct Debit), an Instant Bank Payment, or both.

Payment schemes vary in what customer details you are required to collect. The `collect_customer_details` action is about collecting all the details required by either the mandate or the payment scheme, to ensure we meet regulatory needs.

As an example, we can create a Billing Request for a AUD mandate:

```http
POST /billing_requests
{
  "billing_requests": {
    "mandate_request": {
       "currency": "AUD"
    }
  }
}
```

This returns a Billing Request that looks like this:

```json
{
  "billing_requests": {
    "id": "BRQ000010NMDMH2",
    "status": "pending",
    "mandate_request": {
      "scheme": "pay_to",
      "currency": "AUD"
    },
    "links": {
      "customer": "CU00016WDAM7BS",
      "customer_billing_detail": "CBD000010P52VRF",
      "organisation": "OR123"
    },
    "actions": [
      {
        "type": "collect_customer_details",
        "required": true,
        "completes_actions": [],
        "requires_actions": [
          "choose_currency"
        ],
        "status": "pending",
        "collect_customer_details": {
          "incomplete_fields": {
            "customer": [
              "email",
              "given_name",
              "family_name"
            ],
            "customer_billing_detail": [
              "address_line1",
              "city",
              "postal_code",
              "country_code"
            ]
          }
        }
      },
      ...,
    ],
    "resources": {
      "customer": {
        "id": "CU00016WDAM7BS",
        "created_at": "2021-04-08T14:06:30.977Z",
        "email": null,
        "given_name": null,
        "family_name": null,
        "company_name": null,
        "language": "en",
        "phone_number": null,
        "metadata": {}
      },
      "customer_billing_detail": {
        "id": "CBD000010P52VRF",
        "created_at": "2021-04-08T14:06:30.997Z",
        "address_line1": null,
        "address_line2": null,
        "address_line3": null,
        "city": null,
        "region": null,
        "postal_code": null,
        "country_code": null,
        "swedish_identity_number": null,
        "danish_identity_number": null
      }
    }
  }
}
```

Note that:

- There is a `collect_customer_details `action, which is `pending`
- A new customer has been created for us, as the Billing Request wasn’t attached to an existing customer
- The `customer `resource is presented back to us, and they have no filled fields
- The `collect_customer_details` action is designed to help us collect the required information from our customers.

Taking a closer look at the action:

```json
{
  "type": "collect_customer_details",
  "required": true,
  "completes_actions": [],
  "requires_actions": ["choose_currency"],
  "status": "pending",
  "collect_customer_details": {
    "incomplete_fields": {
      "customer": ["email", "given_name", "family_name"],
      "customer_billing_detail": [
        "address_line1",
        "city",
        "postal_code",
        "country_code"
      ]
    }
  }
}
```

The `collect_customer_details.incomplete_fields` object tells us what fields we need to collect, for both resources. Which fields are required changes depending on the schemes of the mandate (or payment).

We can complete this action by POST’ing to the [Collect customer details for the billing request](https://developer.gocardless.com/api-reference/#billing-requests-collect-customer-details-for-the-billing-request) endpoint:

```PHP
$client = new \GoCardlessPro\Client(array(
  'access_token' => 'your_access_token_here',
  'environment'  => \GoCardlessPro\Environment::SANDBOX
));

$client->billingRequests()->collectCustomerDetails("BR123", [
  "params" => [
    "customer" => [
      "email" => "paddington@bearthings.com"
      "given_name" => "Paddington",
      "family_name" => "Bear"
    ],
    "customer_billing_detail" => [
      "address_line1" => "32 Windsor Gardens",
      "city" => "London",
      "postal_code" => "W9 3RG",
      "country_code" => "GB"
    ]
  ]
]);
```

```python
import gocardless_pro
client = gocardless_pro.Client(access_token="your_access_token_here", environment='sandbox')

client.billing_requests.collect_customer_details("BR123", params={
  customer: {
    email: "paddington@bearthings.com"
    given_name: "Paddington",
    family_name: "Bear",
  },
  customer_billing_detail: {
    address_line1: "32 Windsor Gardens",
    city: "London",
    postal_code: "W9 3RG",
    country_code: "GB",
  }
})
```

```ruby
@client = GoCardlessPro::Client.new(
  access_token: "your_access_token",
  environment: :sandbox
)

@client.billing_requests.collect_customer_details("BR123", {
  params: {
    customer: {
      email: "paddington@bearthings.com"
      given_name: "Paddington",
      family_name: "Bear",
    },
    customer_billing_detail: {
      address_line1: "32 Windsor Gardens",
      city: "London",
      postal_code: "W9 3RG",
      country_code: "GB".
    }
  }
})
```

```java
@client = GoCardlessPro::Client.new(
  access_token: "your_access_token",
  environment: :sandbox
)

@client.billing_requests.collect_customer_details("BR123", {
  params: {
    customer: {
      email: "paddington@bearthings.com"
      given_name: "Paddington",
      family_name: "Bear",
    },
    customer_billing_detail: {
      address_line1: "32 Windsor Gardens",
      city: "London",
      postal_code: "W9 3RG",
      country_code: "GB"
    }
  }
})
```

```javascript
const constants = require('gocardless-nodejs/constants');
const gocardless = require('gocardless-nodejs');
const client = gocardless('your_access_token_here', constants.Environments.Sandbox);

const resp = await client.billingRequests.collectCustomerDetails("BR123", {
  customer: {
    email: "paddington@bearthings.com"
    given_name: "Paddington",
    family_name: "Bear",
  },
  customer_billing_detail: {
    address_line1: "32 Windsor Gardens",
    city: "London",
    postal_code: "W9 3RG",
    country_code: "GB"
  }
});
```

```.net
String accessToken = "your_access_token";
GoCardlessClient gocardless = GoCardlessClient.Create(accessToken, Environment.SANDBOX);

var customer = new GoCardless.Services.BillingRequestCollectCustomerDetailsRequest.BillingRequestCustomer
{
  Email = "paddington@bearthings.com",
  GivenName = "Paddington",
  FamilyName = "Bear",
};

var customerBillingDetail =new GoCardless.Services.BillingRequestCollectCustomerDetailsRequest.BillingRequestCustomerBillingDetail
{
  AddressLine1 = "32 Windsor Gardens",
  City = "London",
  PostalCode = "W9 3RG",
  CountryCode = "GB",
};

var resp = await gocardless.BillingRequests.CollectCustomerDetails("BR123",
  new GoCardless.Services.BillingRequestCollectCustomerDetailsRequest
  {
    Customer = customer,
    CustomerBillingDetail = customerBillingDetail,
  });
```

```HTTP
POST /billing_requests/BRQ000010NMDMH2/actions/collect_customer_details HTTP/1.1
{
  "data": {
    "customer": {
      "email": "paddington@bearthings.com",
      "given_name": "Paddington",
      "family_name": "Bear"
    },
    "customer_billing_detail": {
      "address_line1": "32 Windsor Gardens",
      "city": "London",
      "postal_code": "W9 3RG",
      "country_code": "GB"
    }
  }
}
```

```go
package main

import (
	gocardless "github.com/gocardless/gocardless-pro-go/v2"
)

accessToken := "your_access_token_here"
config, err := gocardless.NewConfig(accessToken, gocardless.WithEndpoint(gocardless.SandboxEndpoint))
if err != nil {
	fmt.Printf("got err in initialising config: %s", err.Error())
	return
}
client, err := gocardless.New(config)
if err != nil {
	fmt.Println("error in initialisating client: %s", err.Error())
	return
}

billingRequestCollectCustomerDetailsParams := gocardless.BillingRequestCollectCustomerDetailsParams{
  Customer: &gocardless.BillingRequestCollectCustomerDetailsParamsCustomer{
    GivenName:  "Paddington",
    FamilyName: "Bear",
    Email:      "paddington@bearthings.com",
  },
  CustomerBillingDetail: &gocardless.BillingRequestCollectCustomerDetailsParamsCustomerBillingDetail{
    AddressLine1: "32 Windsor Gardens",
    City: "London",
    PostalCode: "W9 3RG",
    CountryCode: "GB",
  },
}

billingRequest, err := client.BillingRequests.CollectCustomerDetails(context, "BR123", billingRequestCollectCustomerDetailsParams)
```

As with all action endpoints, the response is the Billing Request. What we get back is:

```json
{
  "billing_requests": {
    "id": "BRQ000010NMDMH2",
    "status": "pending",
    "mandate_request": {
      "scheme": "pay_to",
      "currency": "AUD",
    },
    "links": {
      "customer": "CU00016WDAM7BS",
      "customer_billing_detail": "CBD000010P52VRF",
      "organisation": "OR123"
    },
    "actions": [
      {
        "type": "collect_customer_details",
        "required": true,
        "completes_actions": [],
        "requires_actions": [
          "choose_currency"
        ],
        "status": "completed",
        "collect_customer_details": {
          "incomplete_fields": {
            "customer": [],
            "customer_billing_detail": []
          }
        }
      },
      ...,
    ],
    "resources": {
      "customer": {
        "id": "CU00016WDAM7BS",
        "created_at": "2021-04-08T14:06:30.977Z",
        "email": "paddington@bearthings.com",
        "given_name": "Paddington",
        "family_name": "Bear",
        "company_name": null,
        "language": "en",
        "phone_number": null,
        "metadata": {}
      },
      "customer_billing_detail": {
        "id": "CBD000010P52VRF",
        "created_at": "2021-04-08T14:06:30.997Z",
        "address_line1": "32 Windsor Gardens",
        "address_line2": null,
        "address_line3": null,
        "city": "London",
        "region": null,
        "postal_code": "W9 3RG",
        "country_code": "GB",
        "swedish_identity_number": null,
        "danish_identity_number": null
      }
    }
  }
}
```

Note that:

- The `collect_customer_details `action is now `completed`, meaning we can move on to other actions
- As a result of us collecting the details, our `customer `and `customer_billing_detail `has been populated with the details we collected

Most integrators will collect these details via web forms, filled by their payers. Integrators are expected to build forms that can collect all possible `customer` and `customer_billing_detail` fields (see the [Collect customer details](https://developer.gocardless.com/api-reference/#billing-requests-collect-customer-details-for-the-billing-request) schema) but only display the inputs required, as per `incomplete_fields`.

### Action: collect_bank_account

Depending on the scheme, we might need to collect bank account details before fulfilling the Billing Request. An example is a PayTo mandate, where we need to capture the payer’s bank in order to create payments against them.

As an example, we can create a Billing Request for a PayTo mandate:

```http
POST /billing_requests
{
  "billing_requests": {
    "mandate_request": {
       "currency": "AUD"
    }
  }
}
```

This returns a Billing Request that looks like this:

```json
{
  "billing_requests": {
    "id": "BRQ000010NMDMH2",
    "status": "pending",
    "mandate_request": {
      "currency": "AUD",
      "scheme": "pay_to",
    },
    "links": {
      "customer": "CU00016WDAM7BS",
      "customer_billing_detail": "CBD000010PAJ810",
      "organisation": "OR123"
    },
    "actions": [
      {
        "type": "collect_bank_account",
        "required": true,
        "completes_actions": [
          "choose_currency"
        ],
        "requires_actions": [],
        "status": "pending"
      },
      ...,
    ],
  }
}
```

Note that:

- There is a `collect_bank_account `action that is `pending`
- We have no `links.customer_bank_account`, confirming no bank account is attached

We can complete this action by POST’ing to the [Collect bank account for the billing request](https://developer.gocardless.com/api-reference/#billing-requests-collect-bank-account-for-the-billing-request) endpoint:

```HTTP
POST /billing_requests/BRQ000010NMDMH2/actions/collect_bank_account HTTP/1.1
{
  "data": {
    "pay_id": "12345678900",
    "country_code": "AU"
  }
}
```

As with all action endpoints, the response is the Billing Request. What we get back is:

```json
{
  "billing_requests": {
    "id": "BRQ000010NMDMH2",
    "status": "ready_to_fulfil",
    "mandate_request": {
       "scheme": "pay_to",
      "currency": "AUD"
    },
    "links": {
      "customer": "CU00016WDAM7BS",
      "customer_billing_detail": "CBD000010P52VRF",
      "customer_bank_account": "BA0000QDWEEAFB",
      "organisation": "OR123"
    },
    "actions": [
      {
        "type": "collect_bank_account",
        "required": true,
        "completes_actions": [
          "choose_currency"
        ],
        "requires_actions": [],
        "status": "completed"
      },
      ...,
    ],
    "resources": {
      "customer_bank_account": {
        "id": "BA0000QDWEEAFB",
        "created_at": "2021-04-08T15:30:36.019Z",
        "account_number_ending": "70",
        "account_holder_name": "PADDINGTON BEAR",
        "account_type": null,
        "bank_name": "NATIONAL AUSTRALIA BANK",
        "currency": "AUD",
        "country_code": "AU",
        "metadata": {},
        "enabled": true,
        "links": {
          "customer": "CU00016WDAM7BS"
        }
      }
    }
  }
}
```

Note that:

- The `collect_bank_account `action is now `completed`, meaning we can move on to other actions
- As a result of us collecting the bank account, we have created a `customer_bank_account `resource and you can see the `links.customer_bank_account `ID has been set

We advise most integrators use Billing Request Flows to complete this action, as the hosted flows will try skipping this action if it can be fulfilled by another (such as bank authorisation).

### Action: bank_authorisation

To authorise the PayTo mandate, the payer needs to log in to their bank. This is initiated by the bank_authorisation.

In other words, hitting the bank_authorisation endpoint will trigger the creation of the PayTo mandate which is the final action required. The rest is up to the payer.

As an example, we can create a Billing Request for PayTo:

```http
POST /billing_requests
{
  "billing_requests": {
    "mandate_request": {
       "currency": "AUD"
    }
  }
}
```

This returns a Billing Request that looks like this:

```json
{
  "billing_requests": {
    "id": "BRQ000010NVSP5H",
    "status": "pending",
    "mandate_request": {
      "currency": "AUD",
      "scheme": "pay_to",
    },
    "actions": [
      {
        "type": "bank_authorisation",
        "required": true,
        "completes_actions": [],
        "requires_actions": [
          "collect_bank_account"
        ],
        "status": "pending"
      },
      ...,
    ]
  }
}
```

### Preventing Breaking Changes

#### Preventing Breaking changes

In order to automatically receive new features and always remain scheme compliant without having to change your integration, GoCardless will need to adjust actions by adding new actions or making optional actions required. This means it will break your integration if you don’t fall back to Billing Request Flow. Therefore if you are building custom payment pages you needs to Implement the actions you are aware of and want to process.

> If you reach the last action and the Billing Request is still `pending `(as opposed to `ready_to_fulfil `or `fulfilled`), then you need to create a [Billing Request Flow](https://developer.gocardless.com/api-reference/#billing-request-flows-create-a-billing-request-flow) and send the payers through that flow.

### What’s next?

### Get started
