Skip to content

Commit e5fbd2b

Browse files
Merge pull request #127 from shauny-xendit/feat/add-qrs-endpoints
feat: update qrcodes for v2
2 parents ae640dc + 34eb7b7 commit e5fbd2b

8 files changed

Lines changed: 132 additions & 54 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,16 +1334,16 @@ CardlessCredit cardlessCredit = xenditClient.cardlessCredit.create(
13341334
```java
13351335
/* Without client */
13361336
QRCode qrCode = QRCode.create(
1337-
"external_id",
1337+
"reference_id",
13381338
QRCode.QRCodeType.DYNAMIC,
1339-
"https://callback.site",
1339+
"IDR",
13401340
10000
13411341
);
13421342
/* With client */
13431343
QRCode qrCode = xenditClient.qrCode.create(
1344-
"external_id",
1344+
"reference_id",
13451345
QRCode.QRCodeType.DYNAMIC,
1346-
"https://callback.site",
1346+
"IDR",
13471347
10000
13481348
);
13491349
```
@@ -1352,9 +1352,9 @@ QRCode qrCode = xenditClient.qrCode.create(
13521352

13531353
```java
13541354
/* Without client */
1355-
QRCode qrCode = QRCode.getQRCode("external_id");
1355+
QRCode qrCode = QRCode.getQRCodeByQRId("qr_004a0427-b194-49be-9439-657ef77ee4f3");
13561356
/* With client */
1357-
QRCode qrCode = xenditClient.qrCode.getQRCode("external_id");
1357+
QRCode qrCode = xenditClient.qrCode.getQRCodeByQRId("qr_004a0427-b194-49be-9439-657ef77ee4f3");
13581358
```
13591359

13601360
### Customer

xendit-java-lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group 'com.xendit'
11-
version '1.20.7'
11+
version '1.21.0'
1212

1313
sourceCompatibility = 1.8
1414

xendit-java-lib/src/main/java/com/xendit/Xendit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public String getXenditURL() {
3737
}
3838

3939
public String getVersion() {
40-
return "1.20.7";
40+
return "1.21.0";
4141
}
4242
}
4343
}

xendit-java-lib/src/main/java/com/xendit/model/QRCode.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,29 @@ public enum QRCodeStatus {
2424
@SerializedName("id")
2525
private String id;
2626

27-
@SerializedName("external_id")
28-
private String externalId;
27+
@SerializedName("reference_id")
28+
private String referenceId;
29+
30+
@SerializedName("business_id")
31+
private String businessId;
32+
33+
@SerializedName("currency")
34+
private String currency;
2935

3036
@SerializedName("amount")
3137
private Number amount;
3238

3339
@SerializedName("qr_string")
3440
private String qrString;
3541

36-
@SerializedName("callback_url")
37-
private String callbackUrl;
42+
@SerializedName("webhook_url")
43+
private String webhookUrl;
44+
45+
@SerializedName("channel_code")
46+
private String channelCode;
47+
48+
@SerializedName("expires_at")
49+
private String expiresAt;
3850

3951
@SerializedName("type")
4052
private String type;
@@ -48,28 +60,33 @@ public enum QRCodeStatus {
4860
@SerializedName("updated")
4961
private String updated;
5062

63+
@SerializedName("basket")
64+
private Object basket;
65+
66+
@SerializedName("metadata")
67+
private Object metadata;
68+
5169
private static QRCodeClient qrCodeClient;
5270

5371
/**
5472
* Create QR Code with given parameters
5573
*
56-
* @param externalId An ID of your choice. Often it is unique identifier in your system like
74+
* @param referenceId An ID of your choice. Often it is unique identifier in your system like
5775
* customer ID or order ID
5876
* @param type DYNAMIC or STATIC. DYNAMIC QR code contains the payment value upon scanning and can
5977
* be paid multiple times. STATIC QR code requires end user to input the payment value and can
6078
* be paid multiple times
61-
* @param callbackUrl The URL to receive payment notification after payment has been made by end
62-
* user
79+
* @param currency Currency of the QR code generated
6380
* @param amount The payment value embedded in the QR code, end user can only pay the specified
6481
* amount after scanning the QR code. For STATIC QR code, amount parameter will be ignored.
6582
* @return QRCode
6683
* @throws XenditException XenditException
6784
*/
6885
public static QRCode createQRCode(
69-
String externalId, QRCodeType type, String callbackUrl, Number amount)
86+
String referenceId, QRCode.QRCodeType type, String currency, Number amount)
7087
throws XenditException {
7188
QRCodeClient client = getClient();
72-
return client.createQRCode(externalId, type, callbackUrl, amount);
89+
return client.createQRCode(referenceId, type, currency, amount);
7390
}
7491

7592
/**
@@ -109,6 +126,19 @@ public static QRCode getQRCode(String externalId) throws XenditException {
109126
return client.getQRCode(externalId);
110127
}
111128

129+
/**
130+
* Get QR Code by qr id id
131+
*
132+
* @param qrId Xendit provided unique ID returned when generating a qr code request. Starts with
133+
* qr_
134+
* @return QRCode
135+
* @throws XenditException XenditException
136+
*/
137+
public static QRCode getQRCodeByQRId(String qrId) throws XenditException {
138+
QRCodeClient client = getClient();
139+
return client.getQRCodeByQRId(qrId);
140+
}
141+
112142
/**
113143
* Its create a client for QRCode
114144
*

xendit-java-lib/src/main/java/com/xendit/model/QRCodeClient.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ public Xendit.Option getOpt() {
1919
}
2020

2121
public QRCode createQRCode(
22-
String externalId, QRCode.QRCodeType type, String callbackUrl, Number amount)
22+
String referenceId, QRCode.QRCodeType type, String currency, Number amount)
2323
throws XenditException {
2424
Map<String, Object> params = new HashMap<>();
25-
params.put("external_id", externalId);
25+
params.put("reference_id", referenceId);
2626
params.put("type", type.toString());
27-
params.put("callback_url", callbackUrl);
27+
params.put("currency", currency);
2828
params.put("amount", amount);
29+
30+
// Use new API version
31+
Map<String, String> headers = new HashMap<>();
32+
headers.put("api-version", "2022-07-31");
2933
String url = String.format("%s%s", opt.getXenditURL(), "/qr_codes");
3034
return this.requestClient.request(
31-
RequestResource.Method.POST, url, params, opt.getApiKey(), QRCode.class);
35+
RequestResource.Method.POST, url, headers, params, opt.getApiKey(), QRCode.class);
3236
}
3337

3438
public QRCode createQRCode(Map<String, Object> params) throws XenditException {
@@ -38,6 +42,8 @@ public QRCode createQRCode(Map<String, Object> params) throws XenditException {
3842
public QRCode createQRCode(Map<String, String> headers, Map<String, Object> params)
3943
throws XenditException {
4044
String url = String.format("%s%s", opt.getXenditURL(), "/qr_codes");
45+
// Use new API version
46+
headers.put("api-version", "2022-07-31");
4147
return this.requestClient.request(
4248
RequestResource.Method.POST, url, headers, params, opt.getApiKey(), QRCode.class);
4349
}
@@ -47,4 +53,14 @@ public QRCode getQRCode(String externalId) throws XenditException {
4753
return this.requestClient.request(
4854
RequestResource.Method.GET, url, null, opt.getApiKey(), QRCode.class);
4955
}
56+
57+
public QRCode getQRCodeByQRId(String qrId) throws XenditException {
58+
String url = String.format("%s%s%s", opt.getXenditURL(), "/qr_codes/", qrId);
59+
60+
Map<String, String> headers = new HashMap<>();
61+
headers.put("api-version", "2022-07-31");
62+
63+
return this.requestClient.request(
64+
RequestResource.Method.GET, url, headers, null, opt.getApiKey(), QRCode.class);
65+
}
5066
}

xendit-java-lib/src/test/java/com/xenditclient/QRCodeTest.java

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
public class QRCodeTest {
2020
private static String URL = String.format("%s%s", Xendit.Opt.getXenditURL(), "/qr_codes");
2121
private static String TEST_ID = "test_id";
22-
private static String TEST_EXTERNAL_ID = "test_external_id";
22+
private static String TEST_REFERENCE_ID = "test_reference_id";
2323
private static String TEST_QR_STRING = "this_is_qr_string";
24-
private static String TEST_CALLBACK_URL = "https://callback.url";
2524
private static String TEST_QR_TYPE = QRCode.QRCodeType.DYNAMIC.toString();
2625
private static String TEST_QR_STATUS = QRCode.QRCodeStatus.ACTIVE.toString();
26+
private static String TEST_QR_CURRENCY = "IDR";
2727
private static Map<String, Object> PARAMS = new HashMap<>();
2828
private static Map<String, String> HEADERS = new HashMap<>();
2929
private static QRCode VALID_PAYMENT =
3030
QRCode.builder()
3131
.id(TEST_ID)
32-
.externalId(TEST_EXTERNAL_ID)
32+
.referenceId(TEST_REFERENCE_ID)
33+
.currency(TEST_QR_CURRENCY)
3334
.qrString(TEST_QR_STRING)
34-
.callbackUrl(TEST_CALLBACK_URL)
3535
.status(TEST_QR_STATUS)
3636
.amount(10000)
3737
.build();
@@ -49,9 +49,9 @@ public void initMocks() {
4949
}
5050

5151
private void initCreateParams() {
52-
PARAMS.put("external_id", TEST_EXTERNAL_ID);
52+
PARAMS.put("reference_id", TEST_REFERENCE_ID);
5353
PARAMS.put("type", TEST_QR_TYPE);
54-
PARAMS.put("callback_url", TEST_CALLBACK_URL);
54+
PARAMS.put("currency", TEST_QR_CURRENCY);
5555
PARAMS.put("amount", 10000);
5656
}
5757

@@ -60,14 +60,14 @@ public void createQRCode_Success_IfMethodCalledCorrectly() throws XenditExceptio
6060
initCreateParams();
6161

6262
when(this.requestClient.request(
63-
RequestResource.Method.POST, URL, PARAMS, opt.getApiKey(), QRCode.class))
63+
RequestResource.Method.POST, URL, HEADERS, PARAMS, opt.getApiKey(), QRCode.class))
6464
.thenReturn(VALID_PAYMENT);
6565
when(qrCodeClient.createQRCode(
66-
TEST_EXTERNAL_ID, QRCode.QRCodeType.DYNAMIC, TEST_CALLBACK_URL, 10000))
66+
TEST_REFERENCE_ID, QRCode.QRCodeType.DYNAMIC, TEST_QR_CURRENCY, 10000))
6767
.thenReturn(VALID_PAYMENT);
6868
QRCode qrCode =
6969
qrCodeClient.createQRCode(
70-
TEST_EXTERNAL_ID, QRCode.QRCodeType.DYNAMIC, TEST_CALLBACK_URL, 10000);
70+
TEST_REFERENCE_ID, QRCode.QRCodeType.DYNAMIC, TEST_QR_CURRENCY, 10000);
7171

7272
assertEquals(qrCode, VALID_PAYMENT);
7373
}
@@ -77,12 +77,7 @@ public void createQRCode_Success_IfParamsIsValid() throws XenditException {
7777
initCreateParams();
7878

7979
when(this.requestClient.request(
80-
RequestResource.Method.POST,
81-
URL,
82-
new HashMap<>(),
83-
PARAMS,
84-
opt.getApiKey(),
85-
QRCode.class))
80+
RequestResource.Method.POST, URL, HEADERS, PARAMS, opt.getApiKey(), QRCode.class))
8681
.thenReturn(VALID_PAYMENT);
8782
when(qrCodeClient.createQRCode(PARAMS)).thenReturn(VALID_PAYMENT);
8883
QRCode qrCode = qrCodeClient.createQRCode(PARAMS);
@@ -96,12 +91,7 @@ public void createQRCode_ThrowsException_IfParamsIsInvalid() throws XenditExcept
9691
PARAMS.put("type", "NOT_DYNAMIC");
9792

9893
when(this.requestClient.request(
99-
RequestResource.Method.POST,
100-
URL,
101-
new HashMap<>(),
102-
PARAMS,
103-
opt.getApiKey(),
104-
QRCode.class))
94+
RequestResource.Method.POST, URL, HEADERS, PARAMS, opt.getApiKey(), QRCode.class))
10595
.thenThrow(new XenditException("QR Code type is invalid"));
10696
when(qrCodeClient.createQRCode(PARAMS))
10797
.thenThrow(new XenditException("QR Code type is invalid"));
@@ -114,8 +104,17 @@ public void createQRCode_Success_WithHeaderProvided() throws XenditException {
114104
initCreateParams();
115105
HEADERS.put("for-user-id", "user-id");
116106

107+
Map<String, String> RequestHeaders = new HashMap<>();
108+
RequestHeaders.put("for-user-id", "user-id");
109+
RequestHeaders.put("api-version", "2022-07-31");
110+
117111
when(this.requestClient.request(
118-
RequestResource.Method.POST, URL, HEADERS, PARAMS, opt.getApiKey(), QRCode.class))
112+
RequestResource.Method.POST,
113+
URL,
114+
RequestHeaders,
115+
PARAMS,
116+
opt.getApiKey(),
117+
QRCode.class))
119118
.thenReturn(VALID_PAYMENT);
120119
when(qrCodeClient.createQRCode(HEADERS, PARAMS)).thenReturn(VALID_PAYMENT);
121120

@@ -126,27 +125,60 @@ public void createQRCode_Success_WithHeaderProvided() throws XenditException {
126125

127126
@Test
128127
public void GetQRCode_Success_WithExternalId() throws XenditException {
129-
String url = String.format("%s/%s", URL, TEST_EXTERNAL_ID);
128+
String url = String.format("%s/%s", URL, TEST_REFERENCE_ID);
130129

131130
when(this.requestClient.request(
132131
RequestResource.Method.GET, url, null, opt.getApiKey(), QRCode.class))
133132
.thenReturn(VALID_PAYMENT);
134-
when(qrCodeClient.getQRCode(TEST_EXTERNAL_ID)).thenReturn(VALID_PAYMENT);
135-
QRCode qrCode = qrCodeClient.getQRCode(TEST_EXTERNAL_ID);
133+
when(qrCodeClient.getQRCode(TEST_REFERENCE_ID)).thenReturn(VALID_PAYMENT);
134+
QRCode qrCode = qrCodeClient.getQRCode(TEST_REFERENCE_ID);
136135

137136
assertEquals(qrCode, VALID_PAYMENT);
138137
}
139138

140139
@Test(expected = XenditException.class)
141140
public void GetQRCode_ThrowsException_OnExternalIDNotFound() throws XenditException {
142-
String NOT_VALID_EXTERNAL_ID = "not_valid_external_id";
143-
String url = String.format("%s/%s", URL, NOT_VALID_EXTERNAL_ID);
141+
String NOT_VALID_REFERENCE_ID = "not_valid_reference_id";
142+
String url = String.format("%s/%s", URL, NOT_VALID_REFERENCE_ID);
144143

145144
when(this.requestClient.request(
146145
RequestResource.Method.GET, url, null, opt.getApiKey(), QRCode.class))
147146
.thenThrow(new XenditException("not found"));
148-
when(qrCodeClient.getQRCode(NOT_VALID_EXTERNAL_ID)).thenThrow(new XenditException("not found"));
147+
when(qrCodeClient.getQRCode(NOT_VALID_REFERENCE_ID))
148+
.thenThrow(new XenditException("not found"));
149+
150+
qrCodeClient.getQRCode(NOT_VALID_REFERENCE_ID);
151+
}
152+
153+
@Test
154+
public void GetQRCode_Success_WithQRId() throws XenditException {
155+
String url = String.format("%s/%s", URL, TEST_ID);
156+
157+
Map<String, String> headers = new HashMap<>();
158+
headers.put("api-version", "2022-07-31");
159+
160+
when(this.requestClient.request(
161+
RequestResource.Method.GET, url, headers, null, opt.getApiKey(), QRCode.class))
162+
.thenReturn(VALID_PAYMENT);
163+
when(qrCodeClient.getQRCodeByQRId(TEST_ID)).thenReturn(VALID_PAYMENT);
164+
QRCode qrCode = qrCodeClient.getQRCodeByQRId(TEST_ID);
165+
166+
assertEquals(qrCode, VALID_PAYMENT);
167+
}
168+
169+
@Test(expected = XenditException.class)
170+
public void GetQRCode_ThrowsException_OnReferenceIDNotFound() throws XenditException {
171+
String NOT_VALID_QR_ID = "not_valid_qr_id";
172+
String url = String.format("%s/%s", URL, NOT_VALID_QR_ID);
173+
174+
Map<String, String> headers = new HashMap<>();
175+
headers.put("api-version", "2022-07-31");
176+
177+
when(this.requestClient.request(
178+
RequestResource.Method.GET, url, headers, null, opt.getApiKey(), QRCode.class))
179+
.thenThrow(new XenditException("not found"));
180+
when(qrCodeClient.getQRCodeByQRId(NOT_VALID_QR_ID)).thenThrow(new XenditException("not found"));
149181

150-
qrCodeClient.getQRCode(NOT_VALID_EXTERNAL_ID);
182+
qrCodeClient.getQRCodeByQRId(NOT_VALID_QR_ID);
151183
}
152184
}

xendit-java-library-example/src/main/java/ExampleWithClient/ExampleGetQRCode.java renamed to xendit-java-library-example/src/main/java/ExampleWithClient/ExampleGetQRCodeByQRId.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import com.xendit.XenditClient;
55
import com.xendit.model.QRCode;
66

7-
public class ExampleGetQRCode {
7+
public class ExampleGetQRCodeByQRId {
88
public static void main(String[] args) {
99
//create xendit client which holds value of apikey
1010
XenditClient xenditClient = new XenditClient.Builder()
1111
.setApikey("xnd_development_...")
1212
.build();
1313

1414
try {
15-
QRCode qrCode = xenditClient.qrCode.getQRCode("12");
15+
QRCode qrCode = xenditClient.qrCode.getQRCodeByQRId("qr_004a0427-b194-49be-9439-657ef77ee4f3");
1616
System.out.println(qrCode.getId());
1717
} catch (XenditException e) {
1818
e.printStackTrace();

xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleGetQRCode.java renamed to xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleGetQRCodeByQRId.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.xendit.Xendit;
55
import com.xendit.model.QRCode;
66

7-
public class ExampleGetQRCode {
7+
public class ExampleGetQRCodeByQRId {
88
public static void main(String[] args) {
99
// access key with Option
1010
Xendit.Opt.setApiKey("xnd_development_...");
@@ -13,7 +13,7 @@ public static void main(String[] args) {
1313
// Xendit.apiKey = "xnd_development_...";
1414

1515
try {
16-
QRCode qrCode = QRCode.getQRCode("12");
16+
QRCode qrCode = QRCode.getQRCodeByQRId("qr_004a0427-b194-49be-9439-657ef77ee4f3");
1717
System.out.println(qrCode.getId());
1818
} catch (XenditException e) {
1919
e.printStackTrace();

0 commit comments

Comments
 (0)