Note: Currently creating Direct Debit mandate for AUTOGIRO scheme is not supported
Depending on the verify setting, payers may be asked to verify themselves if GoCardless deems them a risk or scheme compliance requires it. Read more about using verified mandates here.
This guide shows how to use the Javascript Drop-in to allow customers to complete the Billing Request Flow without leaving the integrator’s site.
Use the Drop-in when you already have a frontend website, and want to benefit from having customers stay on your site, and avoiding site-to-site redirects.
You are discouraged from bundling the initialisation script or vendoring it, as GoCardless reserves the right to make non-breaking changes to the underlying implementation, which you will want your sites to pickup automatically.
We guarantee backward compatibility between major versions of the initialise script, as denoted by the script source URL.
GoCardless offer a react-dropin library that provides React bindings and TypeScript definitions for the Drop-in: check the README for full details, and example implementations.
You can install the package using either npm or yarn.
As Bacs supports verification, you’ll see we have a bank_authorisation action in the Billing Request response. Note the action is required, which means we cannot fulfil this request without first completing the bank authorisation.
We can use Billing Request Flows to generate a checkout flow that guides the payer through these actions, including the verification via bank authorisation.
Goal - Create a Billing Request Flow that can be used for your customer to authorise payments
Engineering complexity - Easy
Time taken - 15 minutes
Billing Request Flows can be created against Billing Requests, and provide an entry into a hosted GoCardless flow that completes whatever actions remain against the request.
Create a Billing Request Flow to retrieve a link that can be provided to your customer to complete the request:
You will need to store a reference to the Billing Request and Billing Request Flow ID, often against an internal customer record.
1// Handler is an instance of the Dropin, with methods2// open, exit, etc.3//4// You must call open() before the Dropin will activate.5//6// You can pass one of either billingRequestFlowID or billingRequestTemplateID.7const handler = GoCardlessDropin.create({8billingRequestFlowID:"<generated-on-backend>",9// billingRequestTemplateID: "BRT123",10environment:"live",// either live or sandbox11onSuccess:(billingRequest, billingRequestFlow)=>{},12onExit:(error, metadata)=>{},13});
When using Javascript, GoCardlessDropin.create returns an Object with two functions, open and exit.
With React, the useGoCardlessDropin hook returns an Object with four values, open, exit, ready and error. open and exit behave just as above.
The ready value will be true once the Drop-in has been loaded and the open function is ready to be called. You should watch error for any issues that occurred that prevented the Drop-in from properly loading.
Calling open will attach the Drop-in iframe to the DOM, and open the modal ready for a customer to complete the flow. Follow the progress of the customer via the onSuccess and onExit handlers.
1const handler = GoCardlessDropin.create({2billingRequestFlowID:"<generated-on-backend>",3environment:"live",// either live or sandbox4onSuccess:(billingRequest, billingRequestFlow)=>{},5onExit:(error, metadata)=>{},6});78// Create handler as above9const handler = GoCardlessDropin.create({/* ... */});1011// Open the Dropin modal, making it visible to the customer12handler.open();
Calling exit will close the Drop-in, removing the modal from the DOM.
If the customer had successfully completed the flow, this will trigger the onSuccess callback. Otherwise, the onExit callback is triggered with a null error.
onSuccess receives the Billing Request that has been worked.
This is called when the flow exits successfully. Depending on how the flow was configured, it may have completed but not fulfilled the Billing Request - check the status of the request before assuming it has been fulfilled.
onExit is called with two arguments: an errorObject and a metadataObject.
The onExit callback is called when the customer leaves the flow before completion. This can happen when they voluntarily leave the flow, or due to an unexpected error within it.
The error object is null if no error was encountered, such as when a customer clicks the exit button. Otherwise it is set to the error that caused the flow to fail.
The metadata object is populated with contextual information that can help understand the source or cause of the error.
1import React,{ useCallback, useState }from"react";2import{3 useGoCardlessDropin,4 GoCardlessDropinOptions,5 GoCardlessDropinOnSuccess,6}from"@gocardless/react-dropin";78// Display a button that opens the Dropin on click, starting a checkout9// flow for the specified Billing Request Flow.10constDropinButton=(options: GoCardlessDropinOptions)=>{11const{ open }=useGoCardlessDropin({...options });1213return(14<buttontype="button"onClick={()=>open()}>15 Start Dropin for <code>{options.billingRequestFlowID}</code> in{" "}16<code>{options.environment}</code>17</button>18);19};2021// Example checkout flow, where we create a Billing Request Flow ID by talking22// with our backend API.23const App:FunctionComponent=()=>{24const[flowID, setFlowID]=useState<string|null>(null);2526// Build your backend with an API endpoint that:27//28// 1. Creates a Billing Request for the resources you wish to create29// 2. Create a Billing Request Flow against (1)30// 3. Return the ID from (2)31//32// See an example of this at Taking an Instant Bank Payment:33// https://developer.gocardless.com/getting-started/billing-requests/taking-an-instant-bank-payment/34 React.useEffect(()=>{35asyncfunctioncreateFlow(){36// Expecting a JSON body like:37// {38// "flow_id": "BRF123456"39// }40let response =awaitfetch("/api/flows",{41 method:"POST",42});43const{ flow_id }=await response.json();44setFlowID(flow_id);45}46createFlow();47},[]);4849// Only show the button once we have a Billing Request Flow ID50return flowID ===null?(51<divclassName="loader"></div>52):(53<DropinButtonbillingRequestFlowID={flowID}environment={"live"}/>54);55};