Skip to content

Instantly share code, notes, and snippets.

@dili91
Last active August 31, 2022 11:10
Show Gist options
  • Save dili91/549ade7566f10d9b6f7011e945498bc4 to your computer and use it in GitHub Desktop.
Save dili91/549ade7566f10d9b6f7011e945498bc4 to your computer and use it in GitHub Desktop.
Gist meant to test a payment mandate creation with truelayer-signing-java7 library
package com.truelayer.payments_jdk7.entities;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class AccessToken {
private final String accessToken;
private final int expiresIn;
private final String scope;
private final String tokenType;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public AccessToken(@JsonProperty("access_token") String accessToken, @JsonProperty("expires_in") int expiresIn, @JsonProperty("scopes") String scope, @JsonProperty("token_type") String tokenType) {
this.accessToken = accessToken;
this.expiresIn = expiresIn;
this.scope = scope;
this.tokenType = tokenType;
}
public String getAccessToken() {
return accessToken;
}
public int getExpiresIn() {
return expiresIn;
}
public String getScope() {
return scope;
}
public String getTokenType() {
return tokenType;
}
}
package com.truelayer.payments_jdk7.entities;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CreateMandateResponse {
private final String id;
private final String resourceToken;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public CreateMandateResponse(@JsonProperty("id") String id, @JsonProperty("resource_token") String resourceToken) {
this.id = id;
this.resourceToken = resourceToken;
}
public String getId() {
return id;
}
public String getResourceToken() {
return resourceToken;
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.truelayer.payments-jdk7</groupId>
<artifactId>payments-jdk7</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>payments-jdk7</name>
<url>https://maven.apache.org</url>
<properties>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
</properties>
<repositories>
<repository>
<id>maven-central</id>
<url> https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk7</artifactId>
<version>2.6.7</version>
</dependency>
<dependency>
<groupId>com.truelayer</groupId>
<artifactId>truelayer-signing-java7</artifactId>
<version>0.0.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
</project>
package com.truelayer.payments_jdk7;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.truelayer.payments_jdk7.entities.AccessToken;
import com.truelayer.payments_jdk7.entities.CreateMandateResponse;
import com.truelayer.signing.Signer;
import okhttp3.*;
import java.io.IOException;
import java.util.UUID;
public class TrueLayerClient {
public static final String HEADER_IDEMPOTENCY_KEY = "Idempotency-Key";
public static final String HEADER_TL_SIGNATURE = "Tl-Signature";
public static final String HEADER_AUTHORIZATION = "Authorization";
public static String AUTH_ENDPOINT = "https://auth.truelayer-sandbox.com";
public static String PAYMENTS_API_ENDPOINT = "https://api.truelayer-sandbox.com";
private static OkHttpClient httpClient;
private static ObjectMapper objectMapper;
static {
httpClient = new OkHttpClient();
objectMapper = new ObjectMapper();
// do not include null fields in JSON
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// do not fail in case of unknown properties returned JSON payloads
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static CreateMandateResponse createMandate() throws IOException {
RequestBody formData = new FormBody.Builder()
.add("client_id", System.getenv("TL_CLIENT_ID"))
.add("client_secret", System.getenv("TL_CLIENT_SECRET"))
.add("grant_type", "client_credentials")
.add("scopes", "payments")
.build();
Request getAccessTokenReq = new Request.Builder()
.url(AUTH_ENDPOINT + "/connect/token")
.post(formData)
.build();
ResponseBody getTokeResponseBody = httpClient.newCall(getAccessTokenReq).execute().body();
AccessToken token = objectMapper.readValue(getTokeResponseBody.string(), AccessToken.class);
if(token == null || token.getAccessToken().length()==0){
throw new RuntimeException("Unable to get an access token. Aborting");
}
String createMandateJsonReq = "{\"mandate\":" +
"{\"type\":\"sweeping\",\"provider_selection\":{\"type\":\"preselected\",\"provider_id\":\"ob-natwest-vrp-sandbox\"," +
"\"remitter\":{\"account_holder_name\":\"SydneyBeard\",\"account_identifier\":{\"type\":\"sort_code_account_number\",\"sort_code\":\"500000\",\"account_number\":\"12345601\"}}}," +
"\"beneficiary\":{\"type\":\"external_account\",\"account_holder_name\":\"Andrea\",\"account_identifier\":{\"type\":\"sort_code_account_number\",\"sort_code\":\"040662\",\"account_number\":\"00003924\"}}}," +
"\"currency\":\"GBP\",\"user\":{\"id\":\""+UUID.randomUUID()+"\",\"name\":\"Bob Marley\",\"email\":\"[email protected]\",\"phone\":\"+39333122334\"}," +
"\"constraints\":{\"valid_from\":\"2022-08-31T00:00:00.000Z\",\"valid_to\":\"2025-01-01T00:00:00.000Z\",\"maximum_individual_amount\":1000,\"periodic_limits\":{\"month\":{\"maximum_amount\":10000,\"period_alignment\":\"calendar\"}}}}";
String kid = System.getenv("TL_SIGNING_KEY_ID");
if (kid == null) throw new RuntimeException("Missing env var KID");
String privateKeyPem = System.getenv("TL_SIGNING_PRIVATE_KEY");
if (privateKeyPem == null) throw new RuntimeException("Missing env var PRIVATE_KEY");
String idempotencyKey = UUID.randomUUID().toString();
String tlSignature = Signer.from(kid, privateKeyPem)
.header(HEADER_IDEMPOTENCY_KEY, idempotencyKey)
.method("post")
.path("/mandates")
.body(createMandateJsonReq)
.sign();
RequestBody createMandateJsonBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
createMandateJsonReq);
Request createMandateReq = new Request.Builder()
.header(HEADER_AUTHORIZATION, "Bearer "+token.getAccessToken())
.header(HEADER_TL_SIGNATURE, tlSignature)
.header(HEADER_IDEMPOTENCY_KEY, idempotencyKey)
.url(PAYMENTS_API_ENDPOINT + "/mandates")
.post(createMandateJsonBody)
.build();
ResponseBody createMandateResponseBody = httpClient.newCall(createMandateReq).execute().body();
CreateMandateResponse createMandateResponse = objectMapper.readValue(createMandateResponseBody.string(), CreateMandateResponse.class);
return createMandateResponse;
}
}
package com.truelayer.payments_jdk7;
import com.truelayer.payments_jdk7.entities.CreateMandateResponse;
import org.junit.*;
import java.io.IOException;
public class TrueLayerClientTest
{
@Test
public void testTlClient() throws IOException {
CreateMandateResponse createMandateResponse = TrueLayerClient.createMandate();
Assert.assertNotNull(createMandateResponse.getId());
Assert.assertNotNull(createMandateResponse.getResourceToken());
System.out.println("created mandate with id "+createMandateResponse.getId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment