Skip to content

Commit

Permalink
Merge pull request #127 from shauny-xendit/feat/add-qrs-endpoints
Browse files Browse the repository at this point in the history
feat: update qrcodes for v2
  • Loading branch information
xen-HendryZheng authored Dec 12, 2022
2 parents ae640dc + 34eb7b7 commit e5fbd2b
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 54 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1334,16 +1334,16 @@ CardlessCredit cardlessCredit = xenditClient.cardlessCredit.create(
```java
/* Without client */
QRCode qrCode = QRCode.create(
"external_id",
"reference_id",
QRCode.QRCodeType.DYNAMIC,
"https://callback.site",
"IDR",
10000
);
/* With client */
QRCode qrCode = xenditClient.qrCode.create(
"external_id",
"reference_id",
QRCode.QRCodeType.DYNAMIC,
"https://callback.site",
"IDR",
10000
);
```
Expand All @@ -1352,9 +1352,9 @@ QRCode qrCode = xenditClient.qrCode.create(

```java
/* Without client */
QRCode qrCode = QRCode.getQRCode("external_id");
QRCode qrCode = QRCode.getQRCodeByQRId("qr_004a0427-b194-49be-9439-657ef77ee4f3");
/* With client */
QRCode qrCode = xenditClient.qrCode.getQRCode("external_id");
QRCode qrCode = xenditClient.qrCode.getQRCodeByQRId("qr_004a0427-b194-49be-9439-657ef77ee4f3");
```

### Customer
Expand Down
2 changes: 1 addition & 1 deletion xendit-java-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.xendit'
version '1.20.7'
version '1.21.0'

sourceCompatibility = 1.8

Expand Down
2 changes: 1 addition & 1 deletion xendit-java-lib/src/main/java/com/xendit/Xendit.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public String getXenditURL() {
}

public String getVersion() {
return "1.20.7";
return "1.21.0";
}
}
}
48 changes: 39 additions & 9 deletions xendit-java-lib/src/main/java/com/xendit/model/QRCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,29 @@ public enum QRCodeStatus {
@SerializedName("id")
private String id;

@SerializedName("external_id")
private String externalId;
@SerializedName("reference_id")
private String referenceId;

@SerializedName("business_id")
private String businessId;

@SerializedName("currency")
private String currency;

@SerializedName("amount")
private Number amount;

@SerializedName("qr_string")
private String qrString;

@SerializedName("callback_url")
private String callbackUrl;
@SerializedName("webhook_url")
private String webhookUrl;

@SerializedName("channel_code")
private String channelCode;

@SerializedName("expires_at")
private String expiresAt;

@SerializedName("type")
private String type;
Expand All @@ -48,28 +60,33 @@ public enum QRCodeStatus {
@SerializedName("updated")
private String updated;

@SerializedName("basket")
private Object basket;

@SerializedName("metadata")
private Object metadata;

private static QRCodeClient qrCodeClient;

/**
* Create QR Code with given parameters
*
* @param externalId An ID of your choice. Often it is unique identifier in your system like
* @param referenceId An ID of your choice. Often it is unique identifier in your system like
* customer ID or order ID
* @param type DYNAMIC or STATIC. DYNAMIC QR code contains the payment value upon scanning and can
* be paid multiple times. STATIC QR code requires end user to input the payment value and can
* be paid multiple times
* @param callbackUrl The URL to receive payment notification after payment has been made by end
* user
* @param currency Currency of the QR code generated
* @param amount The payment value embedded in the QR code, end user can only pay the specified
* amount after scanning the QR code. For STATIC QR code, amount parameter will be ignored.
* @return QRCode
* @throws XenditException XenditException
*/
public static QRCode createQRCode(
String externalId, QRCodeType type, String callbackUrl, Number amount)
String referenceId, QRCode.QRCodeType type, String currency, Number amount)
throws XenditException {
QRCodeClient client = getClient();
return client.createQRCode(externalId, type, callbackUrl, amount);
return client.createQRCode(referenceId, type, currency, amount);
}

/**
Expand Down Expand Up @@ -109,6 +126,19 @@ public static QRCode getQRCode(String externalId) throws XenditException {
return client.getQRCode(externalId);
}

/**
* Get QR Code by qr id id
*
* @param qrId Xendit provided unique ID returned when generating a qr code request. Starts with
* qr_
* @return QRCode
* @throws XenditException XenditException
*/
public static QRCode getQRCodeByQRId(String qrId) throws XenditException {
QRCodeClient client = getClient();
return client.getQRCodeByQRId(qrId);
}

/**
* Its create a client for QRCode
*
Expand Down
24 changes: 20 additions & 4 deletions xendit-java-lib/src/main/java/com/xendit/model/QRCodeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ public Xendit.Option getOpt() {
}

public QRCode createQRCode(
String externalId, QRCode.QRCodeType type, String callbackUrl, Number amount)
String referenceId, QRCode.QRCodeType type, String currency, Number amount)
throws XenditException {
Map<String, Object> params = new HashMap<>();
params.put("external_id", externalId);
params.put("reference_id", referenceId);
params.put("type", type.toString());
params.put("callback_url", callbackUrl);
params.put("currency", currency);
params.put("amount", amount);

// Use new API version
Map<String, String> headers = new HashMap<>();
headers.put("api-version", "2022-07-31");
String url = String.format("%s%s", opt.getXenditURL(), "/qr_codes");
return this.requestClient.request(
RequestResource.Method.POST, url, params, opt.getApiKey(), QRCode.class);
RequestResource.Method.POST, url, headers, params, opt.getApiKey(), QRCode.class);
}

public QRCode createQRCode(Map<String, Object> params) throws XenditException {
Expand All @@ -38,6 +42,8 @@ public QRCode createQRCode(Map<String, Object> params) throws XenditException {
public QRCode createQRCode(Map<String, String> headers, Map<String, Object> params)
throws XenditException {
String url = String.format("%s%s", opt.getXenditURL(), "/qr_codes");
// Use new API version
headers.put("api-version", "2022-07-31");
return this.requestClient.request(
RequestResource.Method.POST, url, headers, params, opt.getApiKey(), QRCode.class);
}
Expand All @@ -47,4 +53,14 @@ public QRCode getQRCode(String externalId) throws XenditException {
return this.requestClient.request(
RequestResource.Method.GET, url, null, opt.getApiKey(), QRCode.class);
}

public QRCode getQRCodeByQRId(String qrId) throws XenditException {
String url = String.format("%s%s%s", opt.getXenditURL(), "/qr_codes/", qrId);

Map<String, String> headers = new HashMap<>();
headers.put("api-version", "2022-07-31");

return this.requestClient.request(
RequestResource.Method.GET, url, headers, null, opt.getApiKey(), QRCode.class);
}
}
90 changes: 61 additions & 29 deletions xendit-java-lib/src/test/java/com/xenditclient/QRCodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
public class QRCodeTest {
private static String URL = String.format("%s%s", Xendit.Opt.getXenditURL(), "/qr_codes");
private static String TEST_ID = "test_id";
private static String TEST_EXTERNAL_ID = "test_external_id";
private static String TEST_REFERENCE_ID = "test_reference_id";
private static String TEST_QR_STRING = "this_is_qr_string";
private static String TEST_CALLBACK_URL = "https://callback.url";
private static String TEST_QR_TYPE = QRCode.QRCodeType.DYNAMIC.toString();
private static String TEST_QR_STATUS = QRCode.QRCodeStatus.ACTIVE.toString();
private static String TEST_QR_CURRENCY = "IDR";
private static Map<String, Object> PARAMS = new HashMap<>();
private static Map<String, String> HEADERS = new HashMap<>();
private static QRCode VALID_PAYMENT =
QRCode.builder()
.id(TEST_ID)
.externalId(TEST_EXTERNAL_ID)
.referenceId(TEST_REFERENCE_ID)
.currency(TEST_QR_CURRENCY)
.qrString(TEST_QR_STRING)
.callbackUrl(TEST_CALLBACK_URL)
.status(TEST_QR_STATUS)
.amount(10000)
.build();
Expand All @@ -49,9 +49,9 @@ public void initMocks() {
}

private void initCreateParams() {
PARAMS.put("external_id", TEST_EXTERNAL_ID);
PARAMS.put("reference_id", TEST_REFERENCE_ID);
PARAMS.put("type", TEST_QR_TYPE);
PARAMS.put("callback_url", TEST_CALLBACK_URL);
PARAMS.put("currency", TEST_QR_CURRENCY);
PARAMS.put("amount", 10000);
}

Expand All @@ -60,14 +60,14 @@ public void createQRCode_Success_IfMethodCalledCorrectly() throws XenditExceptio
initCreateParams();

when(this.requestClient.request(
RequestResource.Method.POST, URL, PARAMS, opt.getApiKey(), QRCode.class))
RequestResource.Method.POST, URL, HEADERS, PARAMS, opt.getApiKey(), QRCode.class))
.thenReturn(VALID_PAYMENT);
when(qrCodeClient.createQRCode(
TEST_EXTERNAL_ID, QRCode.QRCodeType.DYNAMIC, TEST_CALLBACK_URL, 10000))
TEST_REFERENCE_ID, QRCode.QRCodeType.DYNAMIC, TEST_QR_CURRENCY, 10000))
.thenReturn(VALID_PAYMENT);
QRCode qrCode =
qrCodeClient.createQRCode(
TEST_EXTERNAL_ID, QRCode.QRCodeType.DYNAMIC, TEST_CALLBACK_URL, 10000);
TEST_REFERENCE_ID, QRCode.QRCodeType.DYNAMIC, TEST_QR_CURRENCY, 10000);

assertEquals(qrCode, VALID_PAYMENT);
}
Expand All @@ -77,12 +77,7 @@ public void createQRCode_Success_IfParamsIsValid() throws XenditException {
initCreateParams();

when(this.requestClient.request(
RequestResource.Method.POST,
URL,
new HashMap<>(),
PARAMS,
opt.getApiKey(),
QRCode.class))
RequestResource.Method.POST, URL, HEADERS, PARAMS, opt.getApiKey(), QRCode.class))
.thenReturn(VALID_PAYMENT);
when(qrCodeClient.createQRCode(PARAMS)).thenReturn(VALID_PAYMENT);
QRCode qrCode = qrCodeClient.createQRCode(PARAMS);
Expand All @@ -96,12 +91,7 @@ public void createQRCode_ThrowsException_IfParamsIsInvalid() throws XenditExcept
PARAMS.put("type", "NOT_DYNAMIC");

when(this.requestClient.request(
RequestResource.Method.POST,
URL,
new HashMap<>(),
PARAMS,
opt.getApiKey(),
QRCode.class))
RequestResource.Method.POST, URL, HEADERS, PARAMS, opt.getApiKey(), QRCode.class))
.thenThrow(new XenditException("QR Code type is invalid"));
when(qrCodeClient.createQRCode(PARAMS))
.thenThrow(new XenditException("QR Code type is invalid"));
Expand All @@ -114,8 +104,17 @@ public void createQRCode_Success_WithHeaderProvided() throws XenditException {
initCreateParams();
HEADERS.put("for-user-id", "user-id");

Map<String, String> RequestHeaders = new HashMap<>();
RequestHeaders.put("for-user-id", "user-id");
RequestHeaders.put("api-version", "2022-07-31");

when(this.requestClient.request(
RequestResource.Method.POST, URL, HEADERS, PARAMS, opt.getApiKey(), QRCode.class))
RequestResource.Method.POST,
URL,
RequestHeaders,
PARAMS,
opt.getApiKey(),
QRCode.class))
.thenReturn(VALID_PAYMENT);
when(qrCodeClient.createQRCode(HEADERS, PARAMS)).thenReturn(VALID_PAYMENT);

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

@Test
public void GetQRCode_Success_WithExternalId() throws XenditException {
String url = String.format("%s/%s", URL, TEST_EXTERNAL_ID);
String url = String.format("%s/%s", URL, TEST_REFERENCE_ID);

when(this.requestClient.request(
RequestResource.Method.GET, url, null, opt.getApiKey(), QRCode.class))
.thenReturn(VALID_PAYMENT);
when(qrCodeClient.getQRCode(TEST_EXTERNAL_ID)).thenReturn(VALID_PAYMENT);
QRCode qrCode = qrCodeClient.getQRCode(TEST_EXTERNAL_ID);
when(qrCodeClient.getQRCode(TEST_REFERENCE_ID)).thenReturn(VALID_PAYMENT);
QRCode qrCode = qrCodeClient.getQRCode(TEST_REFERENCE_ID);

assertEquals(qrCode, VALID_PAYMENT);
}

@Test(expected = XenditException.class)
public void GetQRCode_ThrowsException_OnExternalIDNotFound() throws XenditException {
String NOT_VALID_EXTERNAL_ID = "not_valid_external_id";
String url = String.format("%s/%s", URL, NOT_VALID_EXTERNAL_ID);
String NOT_VALID_REFERENCE_ID = "not_valid_reference_id";
String url = String.format("%s/%s", URL, NOT_VALID_REFERENCE_ID);

when(this.requestClient.request(
RequestResource.Method.GET, url, null, opt.getApiKey(), QRCode.class))
.thenThrow(new XenditException("not found"));
when(qrCodeClient.getQRCode(NOT_VALID_EXTERNAL_ID)).thenThrow(new XenditException("not found"));
when(qrCodeClient.getQRCode(NOT_VALID_REFERENCE_ID))
.thenThrow(new XenditException("not found"));

qrCodeClient.getQRCode(NOT_VALID_REFERENCE_ID);
}

@Test
public void GetQRCode_Success_WithQRId() throws XenditException {
String url = String.format("%s/%s", URL, TEST_ID);

Map<String, String> headers = new HashMap<>();
headers.put("api-version", "2022-07-31");

when(this.requestClient.request(
RequestResource.Method.GET, url, headers, null, opt.getApiKey(), QRCode.class))
.thenReturn(VALID_PAYMENT);
when(qrCodeClient.getQRCodeByQRId(TEST_ID)).thenReturn(VALID_PAYMENT);
QRCode qrCode = qrCodeClient.getQRCodeByQRId(TEST_ID);

assertEquals(qrCode, VALID_PAYMENT);
}

@Test(expected = XenditException.class)
public void GetQRCode_ThrowsException_OnReferenceIDNotFound() throws XenditException {
String NOT_VALID_QR_ID = "not_valid_qr_id";
String url = String.format("%s/%s", URL, NOT_VALID_QR_ID);

Map<String, String> headers = new HashMap<>();
headers.put("api-version", "2022-07-31");

when(this.requestClient.request(
RequestResource.Method.GET, url, headers, null, opt.getApiKey(), QRCode.class))
.thenThrow(new XenditException("not found"));
when(qrCodeClient.getQRCodeByQRId(NOT_VALID_QR_ID)).thenThrow(new XenditException("not found"));

qrCodeClient.getQRCode(NOT_VALID_EXTERNAL_ID);
qrCodeClient.getQRCodeByQRId(NOT_VALID_QR_ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import com.xendit.XenditClient;
import com.xendit.model.QRCode;

public class ExampleGetQRCode {
public class ExampleGetQRCodeByQRId {
public static void main(String[] args) {
//create xendit client which holds value of apikey
XenditClient xenditClient = new XenditClient.Builder()
.setApikey("xnd_development_...")
.build();

try {
QRCode qrCode = xenditClient.qrCode.getQRCode("12");
QRCode qrCode = xenditClient.qrCode.getQRCodeByQRId("qr_004a0427-b194-49be-9439-657ef77ee4f3");
System.out.println(qrCode.getId());
} catch (XenditException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.xendit.Xendit;
import com.xendit.model.QRCode;

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

try {
QRCode qrCode = QRCode.getQRCode("12");
QRCode qrCode = QRCode.getQRCodeByQRId("qr_004a0427-b194-49be-9439-657ef77ee4f3");
System.out.println(qrCode.getId());
} catch (XenditException e) {
e.printStackTrace();
Expand Down

0 comments on commit e5fbd2b

Please sign in to comment.