Skip to content

Commit 3ff2d33

Browse files
committed
[CC-1388] Add Jwt creation:
- Add request for auth token creation. - Add support for doing requests to different APIs. - Add config class for token service api. - Disabled deprecated/unstable tests of secured payment methods.
1 parent 6a3dfd8 commit 3ff2d33

File tree

14 files changed

+282
-14
lines changed

14 files changed

+282
-14
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.unzer.payment;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class AuthToken implements Resource {
7+
@Override
8+
public String getId() {
9+
return null;
10+
}
11+
12+
@Override
13+
public String getUrl() {
14+
return "/v1/auth/token";
15+
}
16+
17+
private String accessToken;
18+
}

src/main/java/com/unzer/payment/Unzer.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
import com.unzer.payment.models.paylater.InstallmentPlansRequest;
1414
import com.unzer.payment.paymenttypes.PaylaterInstallment;
1515
import com.unzer.payment.paymenttypes.PaymentType;
16-
import com.unzer.payment.service.LinkpayService;
17-
import com.unzer.payment.service.PaymentService;
18-
import com.unzer.payment.service.PaypageService;
19-
import com.unzer.payment.service.WebhookService;
16+
import com.unzer.payment.service.*;
2017
import com.unzer.payment.service.marketplace.MarketplacePaymentService;
18+
import com.unzer.payment.util.JwtHelper;
2119
import com.unzer.payment.webhook.Webhook;
2220
import com.unzer.payment.webhook.WebhookList;
2321
import lombok.Getter;
@@ -38,11 +36,15 @@
3836
public class Unzer {
3937
@Getter
4038
private final String privateKey;
39+
@Getter
40+
private String jwtToken;
41+
4142
private final transient PaymentService paymentService;
4243
private final transient MarketplacePaymentService marketplacePaymentService;
4344
private final transient PaypageService paypageService;
4445
private final transient LinkpayService linkpayService;
4546
private final transient WebhookService webhookService;
47+
private final transient TokenService tokenService;
4648

4749
public Unzer(String privateKey) {
4850
this(privateKey, null, null);
@@ -82,6 +84,7 @@ public Unzer(UnzerRestCommunication restCommunication, String privateKey, String
8284
this.paypageService = new PaypageService(this, restCommunication);
8385
this.linkpayService = new LinkpayService(this, restCommunication);
8486
this.webhookService = new WebhookService(this, restCommunication);
87+
this.tokenService = new TokenService(this, restCommunication);
8588
}
8689

8790
public Unzer(String privateKey, Locale locale) {
@@ -1522,4 +1525,15 @@ public Webhook updateSingleWebhook(String updateId, Webhook updateWebhook)
15221525
public WebhookList getWebhooks() throws HttpCommunicationException {
15231526
return webhookService.getWebhooks();
15241527
}
1528+
1529+
public AuthToken createAuthToken() throws HttpCommunicationException {
1530+
return tokenService.create();
1531+
}
1532+
1533+
public void prepareJwtToken() throws HttpCommunicationException {
1534+
if (this.jwtToken != null && JwtHelper.validateExpiryDate(this.jwtToken)) {
1535+
return;
1536+
}
1537+
this.jwtToken = tokenService.create().getAccessToken();
1538+
}
15251539
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.unzer.payment.communication.api;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class ApiConfig {
7+
enum AUTH_METOD {
8+
BASIC,
9+
BEARER;
10+
}
11+
12+
private final String baseUrl;
13+
private final String testBaseUrl;
14+
private final AUTH_METOD authMethod;
15+
16+
public ApiConfig(String baseUrl, String testBaseUrl) {
17+
this(baseUrl, testBaseUrl, AUTH_METOD.BASIC);
18+
}
19+
20+
public ApiConfig(String baseUrl, String testBaseUrl, AUTH_METOD authMethod) {
21+
this.baseUrl = baseUrl;
22+
this.testBaseUrl = testBaseUrl;
23+
this.authMethod = authMethod;
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.unzer.payment.communication.api;
2+
3+
public class ApiConfigs {
4+
5+
public static final ApiConfig PAYMENT_API = new ApiConfig("https://api.unzer.com", "https://sbx-api.unzer.com");
6+
public static final ApiConfig TOKEN_SERVICE_API = new ApiConfig("https://token.upcgw.com", "https://token.test.upcgw.com");
7+
public static final ApiConfig PAYPAGE_API = new ApiConfig("https://paypage.unzer.com", "https://paypage.test.unzer.io");
8+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.unzer.payment.service;
2+
3+
import com.unzer.payment.AuthToken;
4+
import com.unzer.payment.Unzer;
5+
import com.unzer.payment.communication.HttpCommunicationException;
6+
import com.unzer.payment.communication.JsonParser;
7+
import com.unzer.payment.communication.UnzerRestCommunication;
8+
import com.unzer.payment.communication.api.ApiConfig;
9+
import com.unzer.payment.communication.api.ApiConfigs;
10+
11+
public class TokenService {
12+
private final UnzerRestCommunication restCommunication;
13+
14+
private final UrlUtil urlUtil;
15+
private final Unzer unzer;
16+
17+
protected JsonParser jsonParser;
18+
19+
private static final ApiConfig API_CONFIG = ApiConfigs.TOKEN_SERVICE_API;
20+
21+
public TokenService(Unzer unzer, UnzerRestCommunication restCommunication) {
22+
this(unzer, restCommunication, new JsonParser());
23+
}
24+
25+
public TokenService(Unzer unzer, UnzerRestCommunication restCommunication, JsonParser jsonParser) {
26+
super();
27+
this.unzer = unzer;
28+
this.urlUtil = new UrlUtil(unzer.getPrivateKey(), API_CONFIG);
29+
this.restCommunication = restCommunication;
30+
this.jsonParser = jsonParser;
31+
}
32+
33+
public AuthToken create() throws HttpCommunicationException {
34+
AuthToken authToken = new AuthToken();
35+
String response = restCommunication.httpPost(
36+
urlUtil.getUrl(authToken),
37+
unzer.getPrivateKey(),
38+
authToken
39+
);
40+
41+
return jsonParser.fromJson(response, AuthToken.class);
42+
}
43+
}

src/main/java/com/unzer/payment/service/UrlUtil.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.unzer.payment.service;
22

33
import com.unzer.payment.Resource;
4+
import com.unzer.payment.communication.api.ApiConfig;
5+
import com.unzer.payment.communication.api.ApiConfigs;
46
import com.unzer.payment.models.PaylaterInvoiceConfigRequest;
57
import com.unzer.payment.models.paylater.InstallmentPlansRequest;
68

@@ -18,11 +20,19 @@ public UrlUtil(String privateKey) {
1820
this.apiEndpoint = resolveApiEndpoint(privateKey);
1921
}
2022

21-
private static String resolveApiEndpoint(String privateKey) {
23+
public UrlUtil(String privateKey, ApiConfig apiConfig) {
24+
this.apiEndpoint = resolveApiEndpoint(privateKey, apiConfig);
25+
}
26+
27+
private static String resolveApiEndpoint(String privateKey, ApiConfig apiConfig) {
2228
// 'p-' for production, 's-' for sandbox
2329
return privateKey.charAt(0) == 'p'
24-
? "https://api.unzer.com"
25-
: "https://sbx-api.unzer.com";
30+
? apiConfig.getBaseUrl()
31+
: apiConfig.getTestBaseUrl();
32+
}
33+
34+
private static String resolveApiEndpoint(String privateKey) {
35+
return resolveApiEndpoint(privateKey, ApiConfigs.PAYMENT_API);
2636
}
2737

2838
public String getRestUrl() {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.unzer.payment.util;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
import java.util.Base64;
8+
import java.util.Date;
9+
10+
public class JwtHelper {
11+
12+
public static boolean validateExpiryDate(String jwtToken) {
13+
Date now = new Date();
14+
15+
String payload = extractPayload(jwtToken);
16+
ObjectMapper objectMapper = new ObjectMapper();
17+
18+
JsonNode payloadJson = null;
19+
try {
20+
payloadJson = objectMapper.readTree(payload);
21+
} catch (JsonProcessingException e) {
22+
return false;
23+
}
24+
25+
long exp = payloadJson.get("exp").asLong();
26+
Date expirationDate = new Date(exp * 1000);
27+
return now.before(expirationDate);
28+
}
29+
30+
private static String extractPayload(String jwtToken) {
31+
String[] tokenSegments = jwtToken.split("\\.");
32+
if (tokenSegments.length != 3) {
33+
throw new IllegalArgumentException("Invalid JWT token format");
34+
}
35+
36+
Base64.Decoder decoder = Base64.getUrlDecoder();
37+
return new String(decoder.decode(tokenSegments[1]));
38+
}
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.unzer.payment.communication.api;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
class ApiConfigTest {
8+
9+
@Test
10+
void getPaymentApiConfig() {
11+
assertEquals("https://api.unzer.com", ApiConfigs.PAYMENT_API.getBaseUrl());
12+
assertEquals("https://sbx-api.unzer.com", ApiConfigs.PAYMENT_API.getTestBaseUrl());
13+
}
14+
15+
@Test
16+
void getTokenServiceConfig() {
17+
assertEquals("https://token.upcgw.com", ApiConfigs.TOKEN_SERVICE_API.getBaseUrl());
18+
assertEquals("https://token.test.upcgw.com", ApiConfigs.TOKEN_SERVICE_API.getTestBaseUrl());
19+
}
20+
21+
@Test
22+
void getPaypageServiceConfig() {
23+
assertEquals("https://paypage.unzer.com", ApiConfigs.PAYPAGE_API.getBaseUrl());
24+
assertEquals("https://paypage.test.unzer.io", ApiConfigs.PAYPAGE_API.getTestBaseUrl());
25+
}
26+
}

src/test/java/com/unzer/payment/integration/paymenttypes/InstallmentSecuredTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void testCreateInstallmentSecuredTypeIbanLater()
161161
}
162162

163163
@Test
164-
164+
@Disabled("deprecated")
165165
@Deprecated
166166
public void testAuthorizeViaTypeWithIbanBasketV1()
167167
throws HttpCommunicationException, ParseException {
@@ -229,7 +229,7 @@ public void testAuthorizeViaTypeWithIbanBasketV2()
229229
}
230230

231231
@Test
232-
232+
@Disabled("deprecated")
233233
@Deprecated
234234
public void testAuthorizeViaUnzerTypeIdWithIbanBasketV1()
235235
throws HttpCommunicationException, ParseException, MalformedURLException {
@@ -292,7 +292,7 @@ public void testAuthorizeViaUnzerTypeIdWithIbanBasketV2()
292292
}
293293

294294
@Test
295-
295+
@Disabled("deprecated")
296296
@Deprecated
297297
public void testChargeViaAuthorizeBasketV1() throws HttpCommunicationException, ParseException {
298298
Unzer unzer = getUnzer(Keys.LEGACY_PRIVATE_KEY);
@@ -349,7 +349,7 @@ public void testChargeViaAuthorizeBasketV2() throws HttpCommunicationException,
349349

350350
@Test
351351
@Deprecated
352-
352+
@Disabled("deprecated")
353353
public void testFullCancellationBeforeShipmentBasketV1()
354354
throws HttpCommunicationException, ParseException {
355355
Unzer unzer = getUnzer(Keys.LEGACY_PRIVATE_KEY);
@@ -412,7 +412,7 @@ public void testFullCancellationBeforeShipmentBasketV2()
412412
}
413413

414414
@Test
415-
415+
@Disabled("deprecated")
416416
@Deprecated
417417
public void testPartialCancellationBeforeShipmentBasketV1()
418418
throws HttpCommunicationException, ParseException {
@@ -478,6 +478,7 @@ public void testPartialCancellationBeforeShipmentBasketV2()
478478
}
479479

480480
@Test
481+
@Disabled("deprecated")
481482
@Deprecated
482483
public void testShipmentBasketV1() throws HttpCommunicationException, ParseException {
483484
InstallmentSecuredRatePlan ratePlan = getInstallmentSecuredRatePlan();
@@ -536,7 +537,7 @@ public void testShipmentBasketV2() throws HttpCommunicationException, ParseExcep
536537

537538
@Test
538539
@Deprecated
539-
540+
@Disabled("deprecated")
540541
public void testFullCancelAfterShipmentBasketV1()
541542
throws HttpCommunicationException, ParseException {
542543
InstallmentSecuredRatePlan ratePlan = getInstallmentSecuredRatePlan();
@@ -597,7 +598,7 @@ public void testFullCancelAfterShipmentBasketV2()
597598
}
598599

599600
@Test
600-
601+
@Disabled("deprecated")
601602
@Deprecated
602603
public void testAuthorizeHirePurchaseDirectDebitBasketV1()
603604
throws HttpCommunicationException, ParseException {

0 commit comments

Comments
 (0)