GoCardlessClient OAuth2 partner integration.
Partner platforms use OAuth to act on behalf of multiple merchant accounts.
Flow
- Build an authorisation URL and redirect the merchant.
- GoCardlessClient redirects back with
?code=... - Exchange the code for an access token.
- Use the token to make API calls on behalf of the merchant.
Example
config = %{
client_id: System.get_env("GC_CLIENT_ID"),
client_secret: System.get_env("GC_CLIENT_SECRET"),
redirect_uri: "https://yourapp.com/oauth/callback",
environment: :sandbox
}
# Step 1 — redirect merchant
auth_url = GoCardlessClient.OAuth.authorise_url(config,
scope: "read_write",
state: csrf_token
)
redirect(conn, external: auth_url)
# Step 2 — on callback
{:ok, token} = GoCardlessClient.OAuth.exchange_code(config, params["code"])
# Step 3 — use token
client = GoCardlessClient.Client.new!(access_token: token["access_token"])
Summary
Functions
Builds the GoCardlessClient OAuth authorisation URL.
Revokes an access token, disconnecting the merchant from your app.
Exchanges an authorisation code for an access token.
Looks up which organisation an access token belongs to.
Types
Functions
Builds the GoCardlessClient OAuth authorisation URL.
Options
:scope—"read_write"(default) or"read_only":state— CSRF protection token (recommended):initial_view—"signup"or"login":prefill_email— pre-fill the merchant's email
@spec disconnect(config(), String.t()) :: :ok | {:error, GoCardlessClient.Error.t() | %{status: non_neg_integer(), body: term()}}
Revokes an access token, disconnecting the merchant from your app.
@spec exchange_code(config(), String.t()) :: {:ok, map()} | {:error, GoCardlessClient.Error.t() | %{status: non_neg_integer(), body: term()}}
Exchanges an authorisation code for an access token.
Returns {:ok, token_response} where the response contains:
"access_token"— use withGoCardlessClient.Client.new!/1"token_type"—"Bearer""scope"— granted scope"organisation_id"— the merchant's GoCardlessClient organisation ID
@spec lookup_token(config(), String.t()) :: {:ok, map()} | {:error, GoCardlessClient.Error.t() | %{status: non_neg_integer(), body: term()}}
Looks up which organisation an access token belongs to.
Returns {:ok, %{"organisation_id" => ..., "links" => ...}}.