Create a payment using Java

Learn how to make a payment initiation request using our Java SDK.

With our Java SDK, you can create a payment on your backend application. We recommend using the Java SDK over a custom integration with Java, as it will reduce the time required to make your first payment.

For installation and general information about our Java SDK, visit our GitHub.

📘

Check out the API reference for Payments API v3

Refer to the API reference for further information on creating a payment

Before you begin

  1. Set up your account on Console.
  2. Configure request signing.

Initialise the client

You now have all the information required to initialise the Java library. Make sure that you set the environment to sandbox when testing your integration as shown in the following code sample.
When you switch to live, you can simply remove the .environment(...) selector.

TrueLayerClient client = TrueLayerClient.New()
    .environment(Environment.sandbox())
    .clientCredentials(
            ClientCredentials.builder()
                    .clientId("client-id")
                    .clientSecret("client-secret")
                .build()
    )
    .signingOptions(SigningOptions.builder()
            .keyId("key-id")
            .privateKey(Files.readAllBytes(Path.of("ec512-private-key.pem")))
            .build())
    .build();

The client builder should give you enough flexibility to load your configuration from your filesystem or environment variables. See our GitHub repository for further examples.

Create a payment

Our Java library mimics the Payments V3 API. See our API reference to learn more about payment initiation. The following snippet can be used as reference:

CreatePaymentRequest createPaymentRequest = CreatePaymentRequest.builder()
    .amountInMinor(100)
    .currency(CurrencyCode.GBP)
    .paymentMethod(PaymentMethod.bankTransfer()
            .providerSelection(ProviderSelection.userSelected.build())
            .beneficiary(Beneficiary.merchantAccount()
                    .accountHolderName("Andrea")
                    .merchantAccountId("e83c4c20-b2ad-4b73-8a32-ee111112d72a")
                    .build())
            .build())
    .user(User.builder()
            .name("Andrea")
            .email("[email protected]")
            .build())
    .build();

ApiResponse<CreatePaymentResponse> createdPayment= tlClient.payments()
        .createPayment(createPaymentRequest).get();

if(createdPaymentResponse.isError()){
    //error scenario branch...
    ProblemDetails problemDetails = createdPaymentResponse.getError();

    // Inspect the Problem details error and return
}

// build a link to our Hosted Payment Page
URI url = client.hpp().getHostedPaymentPageLink(
    createdPayment.getId(),
    createdPayment.getResourceToken(),
    URI.create("https://yourdomain.com/redirect-page"));

return url;

Authorise a payment

With the successful result of a /payments request, you can complete a payment by:

Hosted Payment Page

The easiest option for web developers is to redirect to our Hosted Payment Page. The Java library provides a utility to easily create a link to that page:

CreatePaymentResponse createdPayment = createPaymentResponse.getData();

URI hppUrl = client.hpp().getHostedPaymentPageLink(
    createdPayment.getId(),
    createdPayment.getResourceToken(),
    URI.create("https://yourdomain.com/redirect-page"));

See our Hosted Payment Page documentation for further details.

Mobile SDKs

To move on with the payment authorization on TrueLayer mobile SDKs, you can return the id and resourceToken fields of a CreatePaymentResponse instance to your mobile clients.

See our iOS SDK or Android SDK documentation for further details.

Custom integration

If you want to have a fully customised journey, either on mobile or web applications, you can have your server side component taking over the authorisation steps. Refer to the startAuthorizationFlow and submitProviderSelection of the Java library for further details.

A sample implementation is as follows:

// start the auth flow
StartAuthorizationFlowRequest startAuthorizationFlowRequest = StartAuthorizationFlowRequest.builder()
    .redirect(Redirect.builder().returnUri(URI.create("https://yourdomain.com/redirect-page")).build())
    .withProviderSelection()
    .build();
ApiResponse<StartAuthorizationFlowResponse> startAuthorizationFlowResponse = tlClient.payments()
    .startAuthorizationFlow(createPaymentResponse.getData().getId(), startAuthorizationFlowRequest)
    .get();

if(startAuthorizationFlowResponse.isError()){
    //error scenario branch...
    ProblemDetails problemDetails = startAuthorizationFlowResponse.getError();
    
    //return the error details...
}    

// Submit the provider selection
ApiResponse<SubmitProviderSelectionResponse> submitProviderSelectionResponseResponse = tlClient.payments()
    .submitProviderSelection(
            createPaymentResponse.getData().getId(),
            SubmitProviderSelectionRequest.builder()
                    .providerId("provider-id")
                    .build())
    .get();

if(submitProviderSelectionResponse.isError()){
    //error scenario branch...
    ProblemDetails problemDetails = submitProviderSelectionResponse.getError();
    
    //return the error details...
}

// Get the redirect link to the bank and return it to your client
URI bankUrl = submitProviderSelectionResponseResponse.getData()
    .getAuthorizationFlow().getActions().getNext().asRedirect().getUri();

Did this page help you?