-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAbacatePay.java
More file actions
90 lines (72 loc) · 2.89 KB
/
AbacatePay.java
File metadata and controls
90 lines (72 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.abacatepay;
import com.abacatepay.clients.AbacatePayClient;
import com.abacatepay.clients.factories.AbacatePayClientFactory;
import com.abacatepay.model.IAbacatePay;
import com.abacatepay.model.IAbacatePayBilling;
import com.abacatepay.model.IAbacatePayPixQrCode;
import com.abacatepay.model.billing.CreateBillingData;
import com.abacatepay.model.billing.CreateBillingResponse;
import com.abacatepay.model.billing.ListBillingResponse;
import com.abacatepay.model.pixqrcode.CheckPixQRCodeResponse;
import com.abacatepay.model.pixqrcode.CreatePixQRCodeData;
import com.abacatepay.model.pixqrcode.CreatePixQRCodeResponse;
import feign.FeignException;
import feign.RequestInterceptor;
public class AbacatePay implements IAbacatePay {
private static final String API_BASE_URL = "https://api.abacatepay.com/v1";
private final AbacatePayClient client;
private final String apiKey;
private final String userAgent;
public AbacatePay(String apiKey) {
this.apiKey = apiKey;
this.client = AbacatePayClientFactory.create(API_BASE_URL, requestInterceptor());
//TODO: Pegar a versão do SDK dinamicamente
this.userAgent = "Java SDK (1.0.0)";
}
private RequestInterceptor requestInterceptor() {
return template -> {
if (apiKey == null || apiKey.isEmpty()) {
throw new IllegalArgumentException("API key not provided");
}
template.header("Authorization", "Bearer " + apiKey);
template.header("Content-Type", "application/json");
template.header("User-Agent", userAgent);
};
}
@Override
public IAbacatePayBilling billing() {
class AbacatePayBilling implements IAbacatePayBilling {
@Override
public CreateBillingResponse create(CreateBillingData data) {
try {
return client.create(data);
} catch (IllegalArgumentException | FeignException e) {
return new CreateBillingResponse(e.getMessage());
}
}
@Override
public ListBillingResponse list() {
try {
return client.list();
} catch (IllegalArgumentException | FeignException e) {
return new ListBillingResponse(e.getMessage());
}
}
}
return new AbacatePayBilling();
}
@Override
public IAbacatePayPixQrCode pixQrCode() {
class AbacatePayPixQrCode implements IAbacatePayPixQrCode {
@Override
public CreatePixQRCodeResponse create(CreatePixQRCodeData pixQRCode) {
return client.create(pixQRCode);
}
@Override
public CheckPixQRCodeResponse check(String id) {
return client.check(id);
}
}
return new AbacatePayPixQrCode();
}
}