diff --git a/README.md b/README.md index 18372c5..f005429 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,16 @@ This library is the abstraction of Xendit API for access from applications writt - [Gradle](#gradle) - [Usage](#usage) - [Disbursement Services](#disbursement-services) - - [Create a disbursement](#create-a-disbursement) - - [Get banks with available disbursement service](#get-banks-with-available-disbursement-service) - - [Get a disbursement by external ID](#get-a-disbursement-by-external-id) - - [Get a disbursement by ID](#get-a-disbursement-by-id) + - [Create an IDR disbursement](#create-an-idr-disbursement) + - [Create a PHP disbursement](#create-a-php-disbursement) + - [Get banks with available IDR disbursement service](#get-banks-with-available-idr-disbursement-service) + - [Get disbursements channels](#get-disbursements-channels) + - [Get disbursements channels by channel category](#get-disbursement-channels-by-channel-category) + - [Get disbursements channels by channel code](#get-disbursement-channels-by-channel-code) + - [Get an IDR disbursement by external ID](#get-an-idr-disbursement-by-external-id) + - [Get a PHP disbursement by reference ID](#get-a-php-disbursement-by-reference-id) + - [Get an IDR disbursement by ID](#get-an-idr-disbursement-by-id) + - [Get a PHP disbursement by ID](#get-a-php-disbursement-by-id) - [Invoice services](#invoice-services) - [Create an invoice](#create-an-invoice) - [Get an invoice by ID](#get-an-invoice-by-id) @@ -135,7 +141,7 @@ More information: https://search.maven.org/artifact/com.xendit/xendit-java-lib You need to use secret API key in order to use functionality in this library. The key can be obtained from your [Xendit Dashboard](https://dashboard.xendit.co/settings/developers#api-keys). ### Without Client -If you're only dealing with a single secret key, you can simply import the packages required for the products you're interacting with without the need to create a client. +If you're only dealing with a single secret key, you can simply import the packages required for the products you're interacting with without the need to create a client. Xendit Disbursement class is being used for IDR Disbursement. There is another way to set secret key using **Xendit.Opt.setApiKey(")** which is recommended way to use instead of **Xendit.apiKey**. @@ -151,7 +157,7 @@ public class Example { } ``` ### With Client -If you're dealing with multiple secret keys, it is recommended that you use **XenditClient**. This allows you to create as many clients as needed, each with their own individual key. +If you're dealing with multiple secret keys, it is recommended that you use **XenditClient**. This allows you to create as many clients as needed, each with their own individual key. Xendit Disbursement Client is being used for IDR Disbursements. ```java import com.xendit.XenditClient; @@ -168,7 +174,7 @@ public class Example { } } ``` -Example: Create a disbursement +Example: Create an IDR disbursement ###### Without Client ```java @@ -202,6 +208,7 @@ public class ExampleCreateDisbursement { } ``` ###### With Client +Xendit Disbursement Client is being used for IDR Disbursement. ```java import com.xendit.exception.XenditException; import com.xendit.XenditClient; @@ -241,9 +248,9 @@ There are some examples provided for you [here](https://github.com/xendit/xendit ### Disbursement Services -#### Create a disbursement +#### Create an IDR disbursement -You can choose whether want to put the attributes as parameters or to put in inside a Map object. +You can choose whether want to put the attributes as parameters or to put in inside a Map object. @@ -288,7 +295,7 @@ Disbursement disbursement = Disbursement.create(params); Disbursement disbursement = xenditClient.disbursement.create(params); ``` -#### Get banks with available disbursement service +#### Get banks with available IDR disbursement service ```java /* Without client */ @@ -297,7 +304,7 @@ AvailableBank[] banks = Disbursement.getAvailableBanks(); AvailableBank[] banks = xenditClient.disbursement.getAvailableBanks(); ``` -#### Get a disbursement by external ID +#### Get an IDR disbursement by external ID ```java /* Without client */ @@ -306,7 +313,7 @@ Disbursement disbursement = Disbursement.getByExternalId("EXAMPLE_ID"); Disbursement disbursement = xenditClient.disbursement.getByExternalId("EXAMPLE_ID"); ``` -#### Get a disbursement by ID +#### Get an IDR disbursement by ID ```java /* Without client */ @@ -315,6 +322,123 @@ Disbursement disbursement = Disbursement.getById("EXAMPLE_ID"); Disbursement disbursement = xenditClient.disbursement.getById("EXAMPLE_ID"); ``` +#### Create a PHP disbursement + +You can choose whether want to put the attributes as parameters or to put in inside a Map object. + +
+ + + + +
+
+DisbursementPHP.createPHPDisbursement(
+    String xendit_idempotency_key,
+    String reference_id,
+    String currency,
+    String channel_code,
+    String account_name,
+    String account_number,
+    String description,
+    Integer amount,
+    ReceiptNotification receiptNotification,
+    Beneficiary beneficiary
+);
+ReceiptNotification receiptNotification = ReceiptNotification.builder()
+    .emailTo(new String[] { "test@emailTo.com" })
+    .emailCC(new String[] { "test@emailCC.com" })
+    .emailBcc(new String[] { "test@emailBcc.com" })
+    .build();
+Beneficiary beneficiary =
+    Beneficiary.builder()
+        .type("test-type")
+        .givenNames("Test Name")
+        .middleName("Middle Name")
+        .surname("Sur Name")
+        .businessName("Test")
+        .streetLine1("Jl. 123")
+        .streetLine2("Jl. 456")
+        .city("Jakarta Selatan")
+        .province("DKI Jakarta")
+        .state("Test")
+        .country("Test")
+        .zipCode("12345")
+        .mobileNumber("123456789")
+        .phoneNumber("12345678")
+        .email("email@test.com")
+        .build();
+
+
+
+DisbursementPHP.createPHPDisbursement(
+    Map<String, String> headers, Map<String, Object> params
+);
+
+
+ +```java +Map params = new HashMap<>(); +Map headers = new HashMap<>(); +headers.put("xendit-idempotency-key", "xendit-idempotency-key"); +params.put("reference_id", "reference_id_value"); +params.put("currency", "PHP"); +params.put("channel_code", "required_channel_code"); +params.put("account_name", "John etc"); +params.put("account_number", "123456"); +params.put("description", "Disbursement description"); +params.put("amount", 50000); +params.put("receipt_notification", receiptNotification); + +/* Without client */ +DisbursementPHP disbursement = DisbursementPHP.createPHPDisbursement(headers, params); + +/* With client */ +DisbursementPHP disbursement = xenditClient.disbursementPHP.createPHPDisbursement(headers, params); +``` + +#### Get disbursements Channels + +```java +/* Without client */ +DisbursementChannel[] disbursementChannels = DisbursementChannel.getDisbursementChannels(); +/* With client */ +DisbursementChannel[] disbursementChannels = xenditClient.disbursementPHP.getDisbursementChannels(); +``` +#### Get disbursement channels by channel category + +```java +/* Without client */ +DisbursementChannel[] disbursementChannels = DisbursementChannel.getByChannelCategory("channel-category"); +/* With client */ +DisbursementChannel[] disbursementChannels = xenditClient.disbursementPHP.getByChannelCategory("channel-category"); +``` +#### Get disbursement channels by channel code + +```java +/* Without client */ +DisbursementChannel[] disbursementChannels = DisbursementChannel.getByChannelCode("channel-code"); +/* With client */ +DisbursementChannel[] disbursementChannels = xenditClient.disbursementPHP.getByChannelCode("channel-code"); +``` +#### Get a PHP disbursement by reference ID + +```java +/* Without client */ +DisbursementPHP disbursement = DisbursementPHP.getPHPByReferenceId("EXAMPLE_ID"); +/* With client */ +DisbursementPHP disbursement = xenditClient.disbursementPHP.getPHPByReferenceId("EXAMPLE_ID"); +``` + +#### Get a PHP disbursement by ID + +```java +/* Without client */ +DisbursementPHP disbursement = Disbursement.getPHPById("EXAMPLE_ID"); +/* With client */ +DisbursementPHP disbursement = xenditClient.disbursementPHP.getPHPById("EXAMPLE_ID"); +``` + [Back to top](#table-of-contents) ### Invoice services diff --git a/xendit-java-lib/src/main/java/com/xendit/Xendit.java b/xendit-java-lib/src/main/java/com/xendit/Xendit.java index 8d4b17f..6bf2d18 100644 --- a/xendit-java-lib/src/main/java/com/xendit/Xendit.java +++ b/xendit-java-lib/src/main/java/com/xendit/Xendit.java @@ -37,7 +37,7 @@ public String getXenditURL() { } public String getVersion() { - return "1.19.6"; + return "1.20.0"; } } } diff --git a/xendit-java-lib/src/main/java/com/xendit/XenditClient.java b/xendit-java-lib/src/main/java/com/xendit/XenditClient.java index 11ab710..1fe5bee 100644 --- a/xendit-java-lib/src/main/java/com/xendit/XenditClient.java +++ b/xendit-java-lib/src/main/java/com/xendit/XenditClient.java @@ -7,6 +7,7 @@ import com.xendit.model.CustomerClient; import com.xendit.model.DirectDebitPaymentClient; import com.xendit.model.DisbursementClient; +import com.xendit.model.DisbursementPHPClient; import com.xendit.model.EWalletClient; import com.xendit.model.FixedVirtualAccountClient; import com.xendit.model.InvoiceClient; @@ -23,6 +24,7 @@ public class XenditClient { public BalanceClient balance; public PayoutClient payout; public DisbursementClient disbursement; + public DisbursementPHPClient disbursementPHP; public EWalletClient eWallet; public QRCodeClient qrCode; public CustomerClient customer; @@ -70,6 +72,7 @@ private static void buildClient(Xendit.Option option, XenditClient xenditClient) xenditClient.invoice = new InvoiceClient(option, Xendit.getRequestClient()); xenditClient.balance = new BalanceClient(option, Xendit.getRequestClient()); xenditClient.disbursement = new DisbursementClient(option, Xendit.getRequestClient()); + xenditClient.disbursementPHP = new DisbursementPHPClient(option, Xendit.getRequestClient()); xenditClient.payout = new PayoutClient(option, Xendit.getRequestClient()); xenditClient.eWallet = new EWalletClient(option, Xendit.getRequestClient()); xenditClient.qrCode = new QRCodeClient(option, Xendit.getRequestClient()); diff --git a/xendit-java-lib/src/main/java/com/xendit/model/Beneficiary.java b/xendit-java-lib/src/main/java/com/xendit/model/Beneficiary.java new file mode 100644 index 0000000..d0b6f99 --- /dev/null +++ b/xendit-java-lib/src/main/java/com/xendit/model/Beneficiary.java @@ -0,0 +1,54 @@ +package com.xendit.model; + +import com.google.gson.annotations.SerializedName; +import lombok.*; + +@Getter +@Setter +@Builder +public class Beneficiary { + @SerializedName("type") + private String type; + + @SerializedName("given_names") + private String givenNames; + + @SerializedName("middle_name") + private String middleName; + + @SerializedName("surname") + private String surname; + + @SerializedName("business_name") + private String businessName; + + @SerializedName("street_line1") + private String streetLine1; + + @SerializedName("street_line2") + private String streetLine2; + + @SerializedName("city") + private String city; + + @SerializedName("province") + private String province; + + @SerializedName("state") + private String state; + + @SerializedName("country") + private String country; + + @SerializedName("zip_code") + private String zipCode; + + @SerializedName("mobile_number") + private String mobileNumber; + + @SerializedName("phone_number") + private String phoneNumber; + + @SerializedName("email") + private String email; +} diff --git a/xendit-java-lib/src/main/java/com/xendit/model/CurrencyEnum.java b/xendit-java-lib/src/main/java/com/xendit/model/CurrencyEnum.java new file mode 100644 index 0000000..9a2ecbe --- /dev/null +++ b/xendit-java-lib/src/main/java/com/xendit/model/CurrencyEnum.java @@ -0,0 +1,8 @@ +package com.xendit.model; + +public class CurrencyEnum { + public enum CurrencyType { + PHP, + IDR + } +} diff --git a/xendit-java-lib/src/main/java/com/xendit/model/Disbursement.java b/xendit-java-lib/src/main/java/com/xendit/model/Disbursement.java index 3cc241a..d7f9432 100644 --- a/xendit-java-lib/src/main/java/com/xendit/model/Disbursement.java +++ b/xendit-java-lib/src/main/java/com/xendit/model/Disbursement.java @@ -8,6 +8,7 @@ import java.util.Map; import lombok.*; +/** Disbursement Class is being used for IDR Disbursements. */ @Getter @Setter @Builder diff --git a/xendit-java-lib/src/main/java/com/xendit/model/DisbursementChannel.java b/xendit-java-lib/src/main/java/com/xendit/model/DisbursementChannel.java new file mode 100644 index 0000000..2568a41 --- /dev/null +++ b/xendit-java-lib/src/main/java/com/xendit/model/DisbursementChannel.java @@ -0,0 +1,134 @@ +package com.xendit.model; + +import com.google.gson.annotations.SerializedName; +import com.xendit.Xendit; +import com.xendit.exception.XenditException; +import java.util.HashMap; +import java.util.Map; +import lombok.*; + +@Getter +@Setter +@Builder +public class DisbursementChannel { + @SerializedName("channel_code") + private String channelCode; + + @SerializedName("name") + private String name; + + @SerializedName("channel_category") + private String channelCategory; + + @SerializedName("currency") + private String currency; + + @SerializedName("minimum") + private Integer minimum; + + @SerializedName("maximum") + private Integer maximum; + + @SerializedName("minimum_increment") + private Double minimumIncrement; + + private static DisbursementPHPClient disbursementPHPClient; + + /** + * Get array of object disbursements channels + * + * @return + * @throws XenditException + */ + public static DisbursementChannel[] getDisbursementChannels() throws XenditException { + DisbursementPHPClient client = getClient(); + return client.getDisbursementChannels(new HashMap<>()); + } + + /** + * Get array of object disbursements channels by channel category + * + * @param channelCategory + * @return + * @throws XenditException + */ + public static DisbursementChannel[] getByChannelCategory(String channelCategory) + throws XenditException { + return getByChannelCategory(new HashMap<>(), channelCategory); + } + + /** + * Get array of object disbursements channels by channel category + * + * @param headers + * @param channelCategory + * @return + * @throws XenditException + */ + public static DisbursementChannel[] getByChannelCategory( + Map headers, String channelCategory) throws XenditException { + DisbursementPHPClient client = getClient(); + return client.getByChannelCategory(headers, channelCategory); + } + + /** + * Get array of object disbursements channels by channel code + * + * @param channelCode + * @return + * @throws XenditException + */ + public static DisbursementChannel[] getByChannelCode(String channelCode) throws XenditException { + return getByChannelCode(new HashMap<>(), channelCode); + } + + /** + * Get array of object disbursements channels by channel category + * + * @param headers + * @param channelCode + * @return + * @throws XenditException + */ + public static DisbursementChannel[] getByChannelCode( + Map headers, String channelCode) throws XenditException { + DisbursementPHPClient client = getClient(); + return client.getByChannelCode(headers, channelCode); + } + + /** + * Its create a client for Disbursement + * + * @return DisbursementPHPClient + */ + private static DisbursementPHPClient getClient() { + if (isApiKeyExist()) { + if (disbursementPHPClient == null + || !disbursementPHPClient.getOpt().getApiKey().trim().equals(Xendit.apiKey.trim())) { + return disbursementPHPClient = + new DisbursementPHPClient( + Xendit.Opt.setApiKey(Xendit.apiKey), Xendit.getRequestClient()); + } + } else { + if (disbursementPHPClient == null + || !disbursementPHPClient + .getOpt() + .getApiKey() + .trim() + .equals(Xendit.Opt.getApiKey().trim())) { + return disbursementPHPClient = + new DisbursementPHPClient(Xendit.Opt, Xendit.getRequestClient()); + } + } + return disbursementPHPClient; + } + + /** + * check if api-key is exist or not + * + * @return boolean + */ + private static boolean isApiKeyExist() { + return Xendit.apiKey != null; + } +} diff --git a/xendit-java-lib/src/main/java/com/xendit/model/DisbursementPHP.java b/xendit-java-lib/src/main/java/com/xendit/model/DisbursementPHP.java new file mode 100644 index 0000000..c46daff --- /dev/null +++ b/xendit-java-lib/src/main/java/com/xendit/model/DisbursementPHP.java @@ -0,0 +1,315 @@ +package com.xendit.model; + +import com.google.gson.annotations.SerializedName; +import com.xendit.Xendit; +import com.xendit.exception.XenditException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import lombok.*; + +@Getter +@Setter +@Builder +public class DisbursementPHP { + @SerializedName("id") + private String id; + + @SerializedName("reference_id") + private String referenceId; + + @SerializedName("currency") + private String currency; + + @SerializedName("amount") + private Integer amount; + + @SerializedName("channel_code") + private String channelCode; + + @SerializedName("description") + private String description; + + @SerializedName("status") + private String status; + + @SerializedName("created") + private Date created; + + @SerializedName("updated") + private Date updated; + + @SerializedName("failure_code") + private String failureCode; + + // optionals + @SerializedName("receipt_notification") + private ReceiptNotification receiptNotification; + + private static DisbursementPHPClient disbursementPHPClient; + + /** + * Get object disbursementPHP by id + * + * @param headers + * @param id + * @return + * @throws XenditException + */ + public static DisbursementPHP getPHPById(Map headers, String id) + throws XenditException { + DisbursementPHPClient client = getClient(); + return client.getPHPById(headers, id); + } + + /** + * Get object disbursementPHP by id + * + * @param id + * @return + * @throws XenditException + */ + public static DisbursementPHP getPHPById(String id) throws XenditException { + return getPHPById(new HashMap<>(), id); + } + + /** + * Its create a client for DisbursementPH + * + * @return DisbursementPHPClient + */ + private static DisbursementPHPClient getClient() { + if (isApiKeyExist()) { + if (disbursementPHPClient == null + || !disbursementPHPClient.getOpt().getApiKey().trim().equals(Xendit.apiKey.trim())) { + return disbursementPHPClient = + new DisbursementPHPClient( + Xendit.Opt.setApiKey(Xendit.apiKey), Xendit.getRequestClient()); + } + } else { + if (disbursementPHPClient == null + || !disbursementPHPClient + .getOpt() + .getApiKey() + .trim() + .equals(Xendit.Opt.getApiKey().trim())) { + return disbursementPHPClient = + new DisbursementPHPClient(Xendit.Opt, Xendit.getRequestClient()); + } + } + return disbursementPHPClient; + } + + /** + * check if api-key is exist or not + * + * @return boolean + */ + private static boolean isApiKeyExist() { + return Xendit.apiKey != null; + } + + /** + * Create disbursementPHP with all parameter as HashMap + * + * @param params listed here https://xendit.github.io/apireference/#create-php-disbursement. + * @return DisbursementPHP + * @throws XenditException + */ + public static DisbursementPHP createPHPDisbursement( + Map headers, Map params) throws XenditException { + return createRequest(headers, params); + } + + /** + * Create v1 disbursementPHP with required parameters + * + * @param xendit_idempotency_key + * @param reference_id + * @param currency + * @param channel_code + * @param account_name + * @param account_number + * @param description + * @param amount + * @return DisbursementPHP + * @throws XenditException + */ + public static DisbursementPHP createPHPDisbursement( + String xendit_idempotency_key, + String reference_id, + String currency, + String channel_code, + String account_name, + String account_number, + String description, + Integer amount) + throws XenditException { + Map params = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", xendit_idempotency_key); + params.put("reference_id", reference_id); + params.put("currency", currency); + params.put("channel_code", channel_code); + params.put("account_name", account_name); + params.put("account_number", account_number); + params.put("description", description); + params.put("amount", amount); + return createRequest(headers, params); + } + + /** + * Create v1 disbursement with required and optional parameters + * + * @param xendit_idempotency_key + * @param reference_id + * @param currency + * @param channel_code + * @param account_name + * @param account_number + * @param description + * @param amount + * @param receiptNotification ReceiptNotification + * @return DisbursementPHP + * @throws XenditException + */ + public static DisbursementPHP createPHPDisbursement( + String xendit_idempotency_key, + String reference_id, + String currency, + String channel_code, + String account_name, + String account_number, + String description, + Integer amount, + ReceiptNotification receiptNotification) + throws XenditException { + Map params = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", xendit_idempotency_key); + params.put("reference_id", reference_id); + params.put("currency", currency); + params.put("channel_code", channel_code); + params.put("account_name", account_name); + params.put("account_number", account_number); + params.put("description", description); + params.put("amount", amount); + params.put("receipt_notification", receiptNotification); + return createRequest(headers, params); + } + + /** + * Create v1 disbursementPHP with required and optional parameters + * + * @param xendit_idempotency_key + * @param reference_id + * @param currency + * @param channel_code + * @param account_name + * @param account_number + * @param description + * @param amount + * @param beneficiary Beneficiary + * @return DisbursementPHP + * @throws XenditException + */ + public static DisbursementPHP createPHPDisbursement( + String xendit_idempotency_key, + String reference_id, + String currency, + String channel_code, + String account_name, + String account_number, + String description, + Integer amount, + Beneficiary beneficiary) + throws XenditException { + Map params = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", xendit_idempotency_key); + params.put("reference_id", reference_id); + params.put("currency", currency); + params.put("channel_code", channel_code); + params.put("account_name", account_name); + params.put("account_number", account_number); + params.put("description", description); + params.put("amount", amount); + params.put("beneficiary", beneficiary); + return createRequest(headers, params); + } + + /** + * Create v1 disbursementPHP with required and optional parameters + * + * @param xendit_idempotency_key + * @param reference_id + * @param currency + * @param channel_code + * @param account_name + * @param account_number + * @param description + * @param amount + * @param receiptNotification ReceiptNotification + * @param beneficiary Beneficiary + * @return DisbursementPHP + * @throws XenditException + */ + public static DisbursementPHP createPHPDisbursement( + String xendit_idempotency_key, + String reference_id, + String currency, + String channel_code, + String account_name, + String account_number, + String description, + Integer amount, + ReceiptNotification receiptNotification, + Beneficiary beneficiary) + throws XenditException { + Map params = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", xendit_idempotency_key); + params.put("reference_id", reference_id); + params.put("currency", currency); + params.put("channel_code", channel_code); + params.put("account_name", account_name); + params.put("account_number", account_number); + params.put("description", description); + params.put("amount", amount); + params.put("receipt_notification", receiptNotification); + params.put("beneficiary", beneficiary); + return createRequest(headers, params); + } + + /** + * Get array of object PHP disbursements by refernce id + * + * @param referenceId + * @return + * @throws XenditException + */ + public static DisbursementPHP[] getPHPByReferenceId(String referenceId) throws XenditException { + return getPHPByReferenceId(new HashMap(), referenceId); + } + + /** + * Get array of object PHP disbursements by refernce id + * + * @param headers + * @param referenceId + * @return + * @throws XenditException + */ + public static DisbursementPHP[] getPHPByReferenceId( + Map headers, String referenceId) throws XenditException { + + DisbursementPHPClient client = getClient(); + return client.getPHPByReferenceId(headers, referenceId); + } + + private static DisbursementPHP createRequest( + Map headers, Map params) throws XenditException { + DisbursementPHPClient client = getClient(); + return client.createPHPRequest(headers, params); + } +} diff --git a/xendit-java-lib/src/main/java/com/xendit/model/DisbursementPHPClient.java b/xendit-java-lib/src/main/java/com/xendit/model/DisbursementPHPClient.java new file mode 100644 index 0000000..7f77ba1 --- /dev/null +++ b/xendit-java-lib/src/main/java/com/xendit/model/DisbursementPHPClient.java @@ -0,0 +1,209 @@ +package com.xendit.model; + +import com.xendit.Xendit; +import com.xendit.exception.XenditException; +import com.xendit.network.NetworkClient; +import com.xendit.network.RequestResource; +import java.util.HashMap; +import java.util.Map; +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class DisbursementPHPClient { + private Xendit.Option opt; + + private NetworkClient requestClient; + + public Xendit.Option getOpt() { + return opt; + } + + public DisbursementPHP createPHPDisbursement( + Map headers, Map params) throws XenditException { + return createPHPRequest(headers, params); + } + + public DisbursementPHP createPHPDisbursement( + String xendit_idempotency_key, + String reference_id, + String currency, + String channel_code, + String account_name, + String account_number, + String description, + Integer amount) + throws XenditException { + Map params = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", xendit_idempotency_key); + params.put("reference_id", reference_id); + params.put("currency", currency); + params.put("channel_code", channel_code); + params.put("account_name", account_name); + params.put("account_number", account_number); + params.put("description", description); + params.put("amount", amount); + return createPHPRequest(headers, params); + } + + public DisbursementPHP createPHPDisbursement( + String xendit_idempotency_key, + String reference_id, + String currency, + String channel_code, + String account_name, + String account_number, + String description, + Integer amount, + ReceiptNotification receiptNotification) + throws XenditException { + Map params = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", xendit_idempotency_key); + params.put("reference_id", reference_id); + params.put("currency", currency); + params.put("channel_code", channel_code); + params.put("account_name", account_name); + params.put("account_number", account_number); + params.put("description", description); + params.put("amount", amount); + params.put("receipt_notification", receiptNotification); + return createPHPRequest(headers, params); + } + + public DisbursementPHP createPHPDisbursement( + String xendit_idempotency_key, + String reference_id, + String currency, + String channel_code, + String account_name, + String account_number, + String description, + Integer amount, + Beneficiary beneficiary) + throws XenditException { + Map params = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", xendit_idempotency_key); + params.put("reference_id", reference_id); + params.put("currency", currency); + params.put("channel_code", channel_code); + params.put("account_name", account_name); + params.put("account_number", account_number); + params.put("description", description); + params.put("amount", amount); + params.put("beneficiary", beneficiary); + return createPHPRequest(headers, params); + } + + public DisbursementPHP createPHPDisbursement( + String xendit_idempotency_key, + String reference_id, + String currency, + String channel_code, + String account_name, + String account_number, + String description, + Integer amount, + ReceiptNotification receiptNotification, + Beneficiary beneficiary) + throws XenditException { + Map params = new HashMap<>(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", xendit_idempotency_key); + params.put("reference_id", reference_id); + params.put("currency", currency); + params.put("channel_code", channel_code); + params.put("account_name", account_name); + params.put("account_number", account_number); + params.put("description", description); + params.put("amount", amount); + params.put("receipt_notification", receiptNotification); + params.put("beneficiary", beneficiary); + return createPHPRequest(headers, params); + } + + public DisbursementPHP[] getPHPByReferenceId(String referenceId) throws XenditException { + return getPHPByReferenceId(new HashMap(), referenceId); + } + + public DisbursementPHP[] getPHPByReferenceId(Map headers, String referenceId) + throws XenditException { + String url = + String.format("%s%s%s", opt.getXenditURL(), "/disbursements?reference_id=", referenceId); + return this.requestClient.request( + RequestResource.Method.GET, url, headers, null, opt.getApiKey(), DisbursementPHP[].class); + } + + public DisbursementPHP getPHPById(String id) throws XenditException { + return getPHPById(new HashMap<>(), id); + } + + public DisbursementPHP getPHPById(Map headers, String id) throws XenditException { + String url = String.format("%s%s%s", Xendit.Opt.getXenditURL(), "/disbursements/", id); + return this.requestClient.request( + RequestResource.Method.GET, url, headers, null, opt.getApiKey(), DisbursementPHP.class); + } + + public DisbursementPHP createPHPRequest(Map headers, Map params) + throws XenditException { + String url = String.format("%s%s", opt.getXenditURL(), "/disbursements"); + + return this.requestClient.request( + RequestResource.Method.POST, url, headers, params, opt.getApiKey(), DisbursementPHP.class); + } + + public DisbursementChannel[] getDisbursementChannels() throws XenditException { + return getDisbursementChannels(new HashMap<>()); + } + + public DisbursementChannel[] getDisbursementChannels(Map headers) + throws XenditException { + String url = String.format("%s%s", Xendit.Opt.getXenditURL(), "/disbursement-channels"); + return this.requestClient.request( + RequestResource.Method.GET, + url, + headers, + null, + opt.getApiKey(), + DisbursementChannel[].class); + } + + public DisbursementChannel[] getByChannelCategory(String channelCategory) throws XenditException { + return getByChannelCategory(new HashMap<>(), channelCategory); + } + + public DisbursementChannel[] getByChannelCategory( + Map headers, String channelCategory) throws XenditException { + String url = + String.format( + "%s%s%s", + Xendit.Opt.getXenditURL(), "/disbursement-channels?channel_category=", channelCategory); + return this.requestClient.request( + RequestResource.Method.GET, + url, + headers, + null, + opt.getApiKey(), + DisbursementChannel[].class); + } + + public DisbursementChannel[] getByChannelCode(String channelCode) throws XenditException { + return getByChannelCode(new HashMap<>(), channelCode); + } + + public DisbursementChannel[] getByChannelCode(Map headers, String channelCode) + throws XenditException { + String url = + String.format( + "%s%s%s", + Xendit.Opt.getXenditURL(), "/disbursement-channels?channel_code=", channelCode); + return this.requestClient.request( + RequestResource.Method.GET, + url, + headers, + null, + opt.getApiKey(), + DisbursementChannel[].class); + } +} diff --git a/xendit-java-lib/src/main/java/com/xendit/model/ReceiptNotification.java b/xendit-java-lib/src/main/java/com/xendit/model/ReceiptNotification.java new file mode 100644 index 0000000..97a5d56 --- /dev/null +++ b/xendit-java-lib/src/main/java/com/xendit/model/ReceiptNotification.java @@ -0,0 +1,18 @@ +package com.xendit.model; + +import com.google.gson.annotations.SerializedName; +import lombok.*; + +@Getter +@Setter +@Builder +public class ReceiptNotification { + @SerializedName("email_to") + private String[] emailTo; + + @SerializedName("email_cc") + private String[] emailCC; + + @SerializedName("email_bcc") + private String[] emailBcc; +} diff --git a/xendit-java-lib/src/test/java/com/xenditclient/DisbursementChannelTest.java b/xendit-java-lib/src/test/java/com/xenditclient/DisbursementChannelTest.java new file mode 100644 index 0000000..4518d2a --- /dev/null +++ b/xendit-java-lib/src/test/java/com/xenditclient/DisbursementChannelTest.java @@ -0,0 +1,177 @@ +package com.xenditclient; + +import static org.junit.Assert.assertArrayEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.xendit.Xendit; +import com.xendit.exception.XenditException; +import com.xendit.model.DisbursementChannel; +import com.xendit.model.DisbursementPHPClient; +import com.xendit.network.BaseRequest; +import com.xendit.network.NetworkClient; +import com.xendit.network.RequestResource; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class DisbursementChannelTest { + private static String URL = + String.format("%s%s", Xendit.Opt.getXenditURL(), "/disbursement-channels"); + private static String CHANNEL_CODE = "PH_CIMB"; + private static String NAME = "CIMB Bank Philippines Inc"; + private static String CHANNEL_CATEGORY = "BANK"; + private static String CURRENCY = "PHP"; + private static Integer MINIMUM = 50000; + private static Integer MAXIMUM = 200000000; + private static Double MINIMUM_INCREMENT = 0.01; + + private static Map PARAMS = new HashMap<>(); + private static Map HEADERS = new HashMap<>(); + NetworkClient requestClient = mock(BaseRequest.class); + Xendit.Option opt = mock(Xendit.Option.class); + DisbursementPHPClient disbursementPHPClient = mock(DisbursementPHPClient.class); + + private static DisbursementChannel VALID_CHANNEL = + DisbursementChannel.builder() + .channelCode(CHANNEL_CODE) + .name(NAME) + .channelCategory(CHANNEL_CATEGORY) + .currency(CURRENCY) + .minimum(MINIMUM) + .maximum(MAXIMUM) + .minimumIncrement(MINIMUM_INCREMENT) + .build(); + private static DisbursementChannel[] DISBURSEMENTCHANNEL_ARRAY = + new DisbursementChannel[] {VALID_CHANNEL}; + + @Before + public void initMocks() { + Xendit.Opt.setApiKey( + "xnd_development_Z568GecuIH66011GIILkDFNJOoR1wFZdGqOOMFBrRVeX64DISB1o7hnNKB"); + Xendit.setRequestClient(requestClient); + HEADERS.clear(); + PARAMS.clear(); + } + + @Test + public void getDisbursementChannels_Success_IfMethodCalledCorrectly() throws XenditException { + when(this.requestClient.request( + RequestResource.Method.GET, + URL, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementChannel[].class)) + .thenReturn(DISBURSEMENTCHANNEL_ARRAY); + when(disbursementPHPClient.getDisbursementChannels(HEADERS)) + .thenReturn(DISBURSEMENTCHANNEL_ARRAY); + + DisbursementChannel disbursementChannels[] = + disbursementPHPClient.getDisbursementChannels(HEADERS); + + assertArrayEquals(DISBURSEMENTCHANNEL_ARRAY, disbursementChannels); + } + + @Test(expected = XenditException.class) + public void getDisbursementChannels_ThrowsException_IfServerError() throws XenditException { + when(this.requestClient.request( + RequestResource.Method.GET, + URL, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementChannel[].class)) + .thenThrow(new XenditException("Something went wrong")); + when(disbursementPHPClient.getDisbursementChannels(new HashMap<>())) + .thenThrow(new XenditException("Something went wrong")); + disbursementPHPClient.getDisbursementChannels(new HashMap<>()); + } + + @Test + public void getByChannelCategory_Success_IfChannelCategoryIsAvailable() throws XenditException { + String url = String.format("%s?channel_category=%s", URL, CHANNEL_CATEGORY); + + when(this.requestClient.request( + RequestResource.Method.GET, url, null, opt.getApiKey(), DisbursementChannel[].class)) + .thenReturn(DISBURSEMENTCHANNEL_ARRAY); + when(disbursementPHPClient.getByChannelCategory(HEADERS, CHANNEL_CATEGORY)) + .thenReturn(DISBURSEMENTCHANNEL_ARRAY); + + DisbursementChannel[] disbursementChannels = + disbursementPHPClient.getByChannelCategory(HEADERS, CHANNEL_CATEGORY); + + assertArrayEquals(DISBURSEMENTCHANNEL_ARRAY, disbursementChannels); + } + + @Test(expected = XenditException.class) + public void getByChannelCategory_ThrowsException_IfChannelCategoryIsNotAvailable() + throws XenditException { + String url = String.format("%s?channel_category=%s", URL, CHANNEL_CATEGORY); + + when(this.requestClient.request( + RequestResource.Method.GET, url, null, opt.getApiKey(), DisbursementChannel[].class)) + .thenThrow(new XenditException("Channel not found")); + when(disbursementPHPClient.getByChannelCategory(HEADERS, CHANNEL_CATEGORY)) + .thenThrow(new XenditException("Channel not found")); + + disbursementPHPClient.getByChannelCategory(HEADERS, CHANNEL_CATEGORY); + } + + @Test(expected = XenditException.class) + public void getByChannelCategory_ThrowsException_IfInvalidArgs() throws XenditException { + String url = String.format("%s?channel_category=%s", URL, CHANNEL_CATEGORY); + + when(this.requestClient.request( + RequestResource.Method.GET, url, null, opt.getApiKey(), DisbursementChannel[].class)) + .thenThrow(new XenditException("Invalid Arguments")); + when(disbursementPHPClient.getByChannelCategory(HEADERS, "")) + .thenThrow(new XenditException("Invalid Arguments")); + + disbursementPHPClient.getByChannelCategory(HEADERS, ""); + } + + @Test + public void getByChannelCode_Success_IfChannelCodeIsAvailable() throws XenditException { + String url = String.format("%s?channel_code=%s", URL, CHANNEL_CODE); + + when(this.requestClient.request( + RequestResource.Method.GET, url, null, opt.getApiKey(), DisbursementChannel[].class)) + .thenReturn(DISBURSEMENTCHANNEL_ARRAY); + when(disbursementPHPClient.getByChannelCode(HEADERS, CHANNEL_CODE)) + .thenReturn(DISBURSEMENTCHANNEL_ARRAY); + + DisbursementChannel[] disbursementChannels = + disbursementPHPClient.getByChannelCode(HEADERS, CHANNEL_CODE); + + assertArrayEquals(DISBURSEMENTCHANNEL_ARRAY, disbursementChannels); + } + + @Test(expected = XenditException.class) + public void getByChannelCode_ThrowsException_IfChannelCodeIsNotAvailable() + throws XenditException { + String url = String.format("%s?channel_code=%s", URL, CHANNEL_CODE); + + when(this.requestClient.request( + RequestResource.Method.GET, url, null, opt.getApiKey(), DisbursementChannel[].class)) + .thenThrow(new XenditException("Channel not found")); + when(disbursementPHPClient.getByChannelCode(HEADERS, CHANNEL_CODE)) + .thenThrow(new XenditException("Channel not found")); + + disbursementPHPClient.getByChannelCode(HEADERS, CHANNEL_CODE); + } + + @Test(expected = XenditException.class) + public void getByChannelCode_ThrowsException_IfInvalidArgs() throws XenditException { + String url = String.format("%s?channel_code=%s", URL, CHANNEL_CODE); + + when(this.requestClient.request( + RequestResource.Method.GET, url, null, opt.getApiKey(), DisbursementChannel[].class)) + .thenThrow(new XenditException("Invalid Arguments")); + when(disbursementPHPClient.getByChannelCode(HEADERS, "")) + .thenThrow(new XenditException("Invalid Arguments")); + + disbursementPHPClient.getByChannelCode(HEADERS, ""); + } +} diff --git a/xendit-java-lib/src/test/java/com/xenditclient/DisbursementPHPTest.java b/xendit-java-lib/src/test/java/com/xenditclient/DisbursementPHPTest.java new file mode 100644 index 0000000..48d00c8 --- /dev/null +++ b/xendit-java-lib/src/test/java/com/xenditclient/DisbursementPHPTest.java @@ -0,0 +1,292 @@ +package com.xenditclient; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.xendit.Xendit; +import com.xendit.exception.XenditException; +import com.xendit.model.Beneficiary; +import com.xendit.model.DisbursementPHP; +import com.xendit.model.DisbursementPHPClient; +import com.xendit.model.ReceiptNotification; +import com.xendit.network.BaseRequest; +import com.xendit.network.NetworkClient; +import com.xendit.network.RequestResource; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class DisbursementPHPTest { + private static String URL = String.format("%s%s", Xendit.Opt.getXenditURL(), "/disbursement"); + private static String ID = "test-id"; + private static String REFERNCEID = "test-refernce-id"; + private static String CURRENCY = "PHP"; + private static Integer AMOUNT = 50000; + private static String CHANNEL_CODE = "PH_CIMB"; + private static String DESCRIPTION = "test description"; + private static String STATUS = "PENDING"; + private static Date CREATED = new Date(); + private static Date UPDATED = new Date(); + + private static Map PARAMS = new HashMap<>(); + private static Map HEADERS = new HashMap<>(); + NetworkClient requestClient = mock(BaseRequest.class); + Xendit.Option opt = mock(Xendit.Option.class); + DisbursementPHPClient disbursementPHPClient = mock(DisbursementPHPClient.class); + + private static ReceiptNotification RECEIPTNOTIFICATION = + ReceiptNotification.builder() + .emailTo(new String[] {"test@emailTo.com"}) + .emailCC(new String[] {"test@emailCC.com"}) + .emailBcc(new String[] {"test@emailBcc.com"}) + .build(); + private static Beneficiary BENIFICIARY = + Beneficiary.builder() + .type("test-type") + .givenNames("Test Name") + .middleName("Middle Name") + .surname("Sur Name") + .businessName("Test") + .streetLine1("Jl. 123") + .streetLine2("Jl. 456") + .city("Jakarta Selatan") + .province("DKI Jakarta") + .state("Test") + .country("Test") + .zipCode("12345") + .mobileNumber("123456789") + .phoneNumber("123456789") + .email("email@test.com") + .build(); + private static DisbursementPHP VALID_PHP_DISBURSEMENT = + DisbursementPHP.builder() + .id(ID) + .referenceId(REFERNCEID) + .currency(CURRENCY) + .amount(AMOUNT) + .channelCode(CHANNEL_CODE) + .description(DESCRIPTION) + .status(STATUS) + .created(CREATED) + .updated(UPDATED) + .receiptNotification(RECEIPTNOTIFICATION) + .build(); + private static DisbursementPHP[] VALID_PHP_DISBURSEMENT_ARRAY = + new DisbursementPHP[] {VALID_PHP_DISBURSEMENT}; + + @Before + public void initMocks() { + Xendit.Opt.setApiKey( + "xnd_development_Z568GecuIH66011GIILkDFNJOoR1wFZdGqOOMFBrRVeX64DISB1o7hnNKB"); + Xendit.setRequestClient(requestClient); + HEADERS.clear(); + PARAMS.clear(); + } + + @Test + public void getPHPById_Success_IfMethodCalledCorrectly() throws XenditException { + String url = String.format("%s/", URL, ID); + when(this.requestClient.request( + RequestResource.Method.GET, + url, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementPHP.class)) + .thenReturn(VALID_PHP_DISBURSEMENT); + when(disbursementPHPClient.getPHPById(HEADERS, ID)).thenReturn(VALID_PHP_DISBURSEMENT); + + DisbursementPHP disbursement = disbursementPHPClient.getPHPById(HEADERS, ID); + + assertEquals(VALID_PHP_DISBURSEMENT, disbursement); + } + + @Test(expected = XenditException.class) + public void getPHPById_ThrowsException_IfServerError() throws XenditException { + String url = String.format("%s/", URL, ID); + when(this.requestClient.request( + RequestResource.Method.GET, + url, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementPHP.class)) + .thenThrow(new XenditException("Something went wrong")); + when(disbursementPHPClient.getPHPById(HEADERS, ID)) + .thenThrow(new XenditException("Something went wrong")); + disbursementPHPClient.getPHPById(HEADERS, ID); + } + + @Test(expected = XenditException.class) + public void getPHPById_ThrowsException_IfInvalidArgs() throws XenditException { + String url = String.format("%s/", URL, ""); + + when(this.requestClient.request( + RequestResource.Method.GET, url, null, opt.getApiKey(), DisbursementPHP.class)) + .thenThrow(new XenditException("Invalid Arguments")); + when(disbursementPHPClient.getPHPById(HEADERS, "")) + .thenThrow(new XenditException("Invalid Arguments")); + + disbursementPHPClient.getPHPById(HEADERS, ""); + } + + @Test + public void getPHPByReferenceId_Success_IfMethodCalledCorrectly() throws XenditException { + String url = String.format("%s/disbursements?reference_id=", URL, REFERNCEID); + when(this.requestClient.request( + RequestResource.Method.GET, + url, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementPHP[].class)) + .thenReturn(VALID_PHP_DISBURSEMENT_ARRAY); + when(disbursementPHPClient.getPHPByReferenceId(HEADERS, REFERNCEID)) + .thenReturn(VALID_PHP_DISBURSEMENT_ARRAY); + + DisbursementPHP[] disbursements = + disbursementPHPClient.getPHPByReferenceId(HEADERS, REFERNCEID); + + assertArrayEquals(VALID_PHP_DISBURSEMENT_ARRAY, disbursements); + } + + @Test(expected = XenditException.class) + public void getPHPByReferenceId_ThrowsException_IfServerError() throws XenditException { + String url = String.format("%s/disbursements?reference_id=", URL, REFERNCEID); + when(this.requestClient.request( + RequestResource.Method.GET, + url, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementPHP[].class)) + .thenThrow(new XenditException("Something went wrong")); + when(disbursementPHPClient.getPHPByReferenceId(HEADERS, REFERNCEID)) + .thenThrow(new XenditException("Something went wrong")); + disbursementPHPClient.getPHPByReferenceId(HEADERS, REFERNCEID); + } + + @Test(expected = XenditException.class) + public void getPHPByReferenceId_ThrowsException_IfInvalidArgs() throws XenditException { + String url = String.format("%s/disbursements?reference_id=", URL, REFERNCEID); + + when(this.requestClient.request( + RequestResource.Method.GET, url, null, opt.getApiKey(), DisbursementPHP[].class)) + .thenThrow(new XenditException("Invalid Arguments")); + when(disbursementPHPClient.getPHPByReferenceId(HEADERS, "")) + .thenThrow(new XenditException("Invalid Arguments")); + + disbursementPHPClient.getPHPByReferenceId(HEADERS, ""); + } + + private void initCreateParams() { + PARAMS.put("xendit_idempotency_key", "xendit_idempotency_key"); + PARAMS.put("reference_id", REFERNCEID); + PARAMS.put("currency", CURRENCY); + PARAMS.put("channel_code", CHANNEL_CODE); + PARAMS.put("account_name", "account_name"); + PARAMS.put("account_number", "account_number"); + PARAMS.put("description", DESCRIPTION); + PARAMS.put("amount", AMOUNT); + } + + @Test + public void createDisbursementPHP_Success_IfOnlyRequiredParamsAreProvided() + throws XenditException { + initCreateParams(); + + when(this.requestClient.request( + RequestResource.Method.POST, + URL, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementPHP.class)) + .thenReturn(VALID_PHP_DISBURSEMENT); + when(disbursementPHPClient.createPHPRequest(HEADERS, PARAMS)) + .thenReturn(VALID_PHP_DISBURSEMENT); + DisbursementPHP disbursement = disbursementPHPClient.createPHPRequest(HEADERS, PARAMS); + + assertEquals(disbursement, VALID_PHP_DISBURSEMENT); + } + + @Test + public void createDisbursementPHP_Success_IfOptionalParamsAreProvided() throws XenditException { + initCreateParams(); + PARAMS.put("beneficiary", BENIFICIARY); + PARAMS.put("receipt_notification", RECEIPTNOTIFICATION); + when(this.requestClient.request( + RequestResource.Method.POST, + URL, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementPHP.class)) + .thenReturn(VALID_PHP_DISBURSEMENT); + when(disbursementPHPClient.createPHPRequest(HEADERS, PARAMS)) + .thenReturn(VALID_PHP_DISBURSEMENT); + DisbursementPHP disbursement = disbursementPHPClient.createPHPRequest(HEADERS, PARAMS); + + assertEquals(disbursement, VALID_PHP_DISBURSEMENT); + } + + @Test + public void createDisbursementPHP_Success_IfOptionalBenificiaryIsValid() throws XenditException { + initCreateParams(); + PARAMS.put("beneficiary", BENIFICIARY); + when(this.requestClient.request( + RequestResource.Method.POST, + URL, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementPHP.class)) + .thenReturn(VALID_PHP_DISBURSEMENT); + when(disbursementPHPClient.createPHPRequest(HEADERS, PARAMS)) + .thenReturn(VALID_PHP_DISBURSEMENT); + DisbursementPHP disbursement = disbursementPHPClient.createPHPRequest(HEADERS, PARAMS); + + assertEquals(disbursement, VALID_PHP_DISBURSEMENT); + } + + @Test + public void createDisbursementPHP_Success_IfOptionalRecepIsValid() throws XenditException { + initCreateParams(); + PARAMS.put("receipt_notification", RECEIPTNOTIFICATION); + System.out.println(PARAMS); + when(this.requestClient.request( + RequestResource.Method.POST, + URL, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementPHP.class)) + .thenReturn(VALID_PHP_DISBURSEMENT); + when(disbursementPHPClient.createPHPRequest(HEADERS, PARAMS)) + .thenReturn(VALID_PHP_DISBURSEMENT); + DisbursementPHP disbursement = disbursementPHPClient.createPHPRequest(HEADERS, PARAMS); + + assertEquals(disbursement, VALID_PHP_DISBURSEMENT); + } + + @Test(expected = XenditException.class) + public void createDisbursementPHP_ThrowsException_IfParamsAreInvalid() throws XenditException { + PARAMS.put("amount", "amount"); + + when(this.requestClient.request( + RequestResource.Method.POST, + URL, + HEADERS, + PARAMS, + opt.getApiKey(), + DisbursementPHP.class)) + .thenThrow(new XenditException("Amount is invalid")); + when(disbursementPHPClient.createPHPRequest(HEADERS, PARAMS)) + .thenThrow(new XenditException("Amount is invalid")); + disbursementPHPClient.createPHPRequest(HEADERS, PARAMS); + } +} diff --git a/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleCreatePHPDisbursement.java b/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleCreatePHPDisbursement.java new file mode 100644 index 0000000..ced8d6e --- /dev/null +++ b/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleCreatePHPDisbursement.java @@ -0,0 +1,128 @@ +package ExampleWithClient; + +import com.xendit.exception.XenditException; +import com.xendit.model.Beneficiary; +import com.xendit.model.DisbursementChannel; +import com.xendit.XenditClient; +import com.xendit.model.DisbursementPHP; +import com.xendit.model.ReceiptNotification; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public class ExampleCreatePHPDisbursement { + public static void main(String[] args) { + + // create xendit client which holds value of apikey + // you must replace api key with actual api key + XenditClient xenditClient = new XenditClient.Builder() + .setApikey("xnd_dummy_...") + .build(); + + try { + /** + * [OPTIONAL] + * Before requesting disbursement to Xendit, you should get the supported + * channels + * code via getDisbursementChannels or if you know the channel category or code + * you can getByChannelCategory or getByChannelCode + * method. You can skip this step if your system is already familiar with our + * standard code. + * In this example, we call available disbursement bank function. + */ + DisbursementChannel[] disbursementChannels = xenditClient.disbursementPHP + .getDisbursementChannels(); + /** + * Let's say that we want to use first channel in that list. + */ + DisbursementChannel disbursementChannel = disbursementChannels[0]; + /** + * There are several options to create disbursement. + * First option. Create directly from a properly named hashmap key value pair. + * Check https://xendit.github.io/apireference/#create-a-ph-disbursement for + * field + * name. + */ + Map params = new HashMap(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", "xendit_idempotency_key".concat(new Date().toString())); + params.put("reference_id", "reference_id_value"); + params.put("currency", "PHP"); + params.put("channel_code", disbursementChannel.getChannelCode()); + params.put("account_name", "John etc"); + params.put("account_number", "123456"); + params.put("description", "Disbursement description"); + params.put("amount", 50000); + + DisbursementPHP disbursement = xenditClient.disbursementPHP.createPHPDisbursement(headers, + params); + System.out.print(disbursement); + + /** + * Second option. Create with individual value of required params. + */ + DisbursementPHP disbursement2 = xenditClient.disbursementPHP.createPHPDisbursement( + "xendit_idempotency_key".concat(new Date().toString()), "reference_id_value", + "PHP", + disbursementChannel.getChannelCode(), "John etc", "123456", + "Disbursement description", 50000); + System.out.print(disbursement2); + /** + * Third option. Create with individual value of required params + optional + * ReceiptNotification. + */ + ReceiptNotification receiptNotification = ReceiptNotification.builder() + .emailTo(new String[] { "test@emailTo.com" }) + .emailCC(new String[] { "test@emailCC.com" }) + .emailBcc(new String[] { "test@emailBcc.com" }) + .build(); + DisbursementPHP disbursement3 = xenditClient.disbursementPHP.createPHPDisbursement( + "xendit_idempotency_key".concat(new Date().toString()), "reference_id_value", + "PHP", + disbursementChannel.getChannelCode(), "John etc", "123456", + "Disbursement description", 50000, receiptNotification); + System.out.print(disbursement3); + /** + * Fourth option. Create with individual value of required params + optional + * Beneficiary. + */ + Beneficiary beneficiary = Beneficiary.builder() + .type("INDIVIDUAL") + .givenNames("Test Name") + .middleName("Middle Name") + .surname("Sur Name") + .businessName("Test") + .streetLine1("Jl. 123") + .streetLine2("Jl. 456") + .city("City") + .province("Province") + .state("Test") + .country("Test") + .zipCode("12345") + .mobileNumber("9876543210") + .phoneNumber("987654321") + .email("email@test.com") + .build(); + DisbursementPHP disbursement4 = xenditClient.disbursementPHP.createPHPDisbursement( + "xendit_idempotency_key".concat(new Date().toString()), "reference_id_value", + "PHP", + disbursementChannel.getChannelCode(), "John etc", "123456", + "Disbursement description", 50000, beneficiary); + System.out.print(disbursement4); + /** + * Fifth option. Create with individual value of required params + optional + * Beneficiary + */ + DisbursementPHP disbursement5 = xenditClient.disbursementPHP.createPHPDisbursement( + "xendit_idempotency_key".concat(new Date().toString()), "reference_id_value", + "PHP", + disbursementChannel.getChannelCode(), "John etc", "123456", + "Disbursement description", 50000, receiptNotification, beneficiary); + System.out.print(disbursement5); + } catch (XenditException e) { + System.out.print(e); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleGetDisbursementChannels.java b/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleGetDisbursementChannels.java new file mode 100644 index 0000000..47ba334 --- /dev/null +++ b/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleGetDisbursementChannels.java @@ -0,0 +1,44 @@ +package ExampleWithClient; + +import com.xendit.exception.XenditException; +import com.xendit.XenditClient; +import com.xendit.model.DisbursementChannel; + +public class ExampleGetDisbursementChannels { + public static void main(String[] args) { + // create xendit client which holds value of apikey. You must replace api key + // with actual api key + XenditClient xenditClient = new XenditClient.Builder() + .setApikey("xnd_dummy_...") + .build(); + + try { + /** + * Get all disbursement channels + */ + DisbursementChannel[] disbursementChannels = xenditClient.disbursementPHP.getDisbursementChannels(); + + System.out.println(disbursementChannels); + + /** + * Get all disbursement channels by channel category + */ + String channelCategory = "BANK"; + DisbursementChannel[] disbursementChannels2 = xenditClient.disbursementPHP + .getByChannelCategory(channelCategory); + + System.out.println(disbursementChannels2); + + /** + * Get all disbursement channels by channel code + */ + String channelCode = "PH_CITI"; + DisbursementChannel[] disbursementChannels3 = xenditClient.disbursementPHP + .getByChannelCode(channelCode); + + System.out.println(disbursementChannels3); + } catch (XenditException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleGetPHPDisbursement.java b/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleGetPHPDisbursement.java new file mode 100644 index 0000000..9fb46b0 --- /dev/null +++ b/xendit-java-library-example/src/main/java/ExampleWithClient/ExampleGetPHPDisbursement.java @@ -0,0 +1,38 @@ +package ExampleWithClient; + +import com.xendit.exception.XenditException; + +import java.util.Arrays; + +import com.xendit.XenditClient; +import com.xendit.model.DisbursementPHP; + +public class ExampleGetPHPDisbursement { + public static void main(String[] args) { + // create xendit client which holds value of apikey + // you must replace api key with actual api key + XenditClient xenditClient = new XenditClient.Builder() + .setApikey("xnd_dummy_...") + .build(); + + try { + /** + * Get disbursement object by ID. + */ + String disbursementId = "disb-12345678abcdef"; + DisbursementPHP disbursement = xenditClient.disbursementPHP.getPHPById(disbursementId); + + System.out.println(disbursement); + + /** + * Get array of php disbursement object by refernce ID. + */ + String referenceId = "my_reference_id"; + DisbursementPHP[] disbursements = xenditClient.disbursementPHP.getPHPByReferenceId(referenceId); + + System.out.println(Arrays.toString(disbursements)); + } catch (XenditException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleCreatePHPDisbursement.java b/xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleCreatePHPDisbursement.java new file mode 100644 index 0000000..b334aab --- /dev/null +++ b/xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleCreatePHPDisbursement.java @@ -0,0 +1,124 @@ +package ExampleWithoutClient; + +import com.xendit.Xendit; +import com.xendit.exception.XenditException; +import com.xendit.model.Beneficiary; +import com.xendit.model.DisbursementChannel; +import com.xendit.model.DisbursementPHP; +import com.xendit.model.ReceiptNotification; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public class ExampleCreatePHPDisbursement { + public static void main(String[] args) { + // access key with Option + // you must replace api key with actual api key + Xendit.Opt.setApiKey("xnd_development_..."); + + try { + /** + * [OPTIONAL] + * Before requesting disbursement to Xendit, you should get the supported + * channels + * code via getDisbursementChannels or if you know the channel category or code + * you can getByChannelCategory or getByChannelCode + * method. You can skip this step if your system is already familiar with our + * standard code. + * In this example, we call available disbursement bank function. + */ + DisbursementChannel[] disbursementChannels = DisbursementChannel.getDisbursementChannels(); + /** + * Let's say that we want to use first channel in that list. + */ + DisbursementChannel disbursementChannel = disbursementChannels[0]; + /** + * There are several options to create disbursement. + * First option. Create directly from a properly named hashmap key value pair. + * Check https://xendit.github.io/apireference/#create-a-ph-disbursement for + * field + * name. + */ + Map disbursementMap = new HashMap(); + Map headers = new HashMap<>(); + headers.put("xendit-idempotency-key", + "xendit_idempotency_key".concat(new Date().toString())); + disbursementMap.put("reference_id", "reference_id_value"); + disbursementMap.put("currency", "PHP"); + disbursementMap.put("channel_code", disbursementChannel.getChannelCode()); + disbursementMap.put("account_name", "John etc"); + disbursementMap.put("account_number", "123456"); + disbursementMap.put("description", "Disbursement description"); + disbursementMap.put("amount", 50000); + + DisbursementPHP disbursement = DisbursementPHP.createPHPDisbursement(headers, disbursementMap); + System.out.print(disbursement); + + /** + * Second option. Create with individual value of required params. + */ + DisbursementPHP disbursement2 = DisbursementPHP.createPHPDisbursement( + "xendit_idempotency_key".concat(new Date().toString()), "reference_id_value", + "PHP", + disbursementChannel.getChannelCode(), "John etc", "123456", + "Disbursement description", 50000); + System.out.print(disbursement2); + /** + * Third option. Create with individual value of required params + optional + * ReceiptNotification. + */ + ReceiptNotification receiptNotification = ReceiptNotification.builder() + .emailTo(new String[] { "test@emailTo.com" }) + .emailCC(new String[] { "test@emailCC.com" }) + .emailBcc(new String[] { "test@emailBcc.com" }) + .build(); + DisbursementPHP disbursement3 = DisbursementPHP.createPHPDisbursement( + "xendit_idempotency_key".concat(new Date().toString()), "reference_id_value", + "PHP", + disbursementChannel.getChannelCode(), "John etc", "123456", + "Disbursement description", 50000, receiptNotification); + System.out.print(disbursement3); + /** + * Fourth option. Create with individual value of required params + optional + * Beneficiary. + */ + Beneficiary beneficiary = Beneficiary.builder() + .type("INDIVIDUAL") + .givenNames("Test Name") + .middleName("Middle Name") + .surname("Sur Name") + .businessName("Test") + .streetLine1("Jl. 123") + .streetLine2("Jl. 456") + .city("City") + .province("Province") + .state("Test") + .country("Test") + .zipCode("12345") + .mobileNumber("+639876543210") + .phoneNumber("+63987654321") + .email("email@test.com") + .build(); + DisbursementPHP disbursement4 = DisbursementPHP.createPHPDisbursement( + "xendit_idempotency_key".concat(new Date().toString()), "reference_id_value", + "PHP", + disbursementChannel.getChannelCode(), "John etc", "123456", + "Disbursement description", 50000, beneficiary); + System.out.print(disbursement4); + /** + * Fifth option. Create with individual value of required params + optional + * Beneficiary + */ + DisbursementPHP disbursement5 = DisbursementPHP.createPHPDisbursement( + "xendit_idempotency_key".concat(new Date().toString()), "reference_id_value", + "PHP", + disbursementChannel.getChannelCode(), "John etc", "123456", + "Disbursement description", 50000, receiptNotification, beneficiary); + System.out.print(disbursement5); + } catch (XenditException e) { + System.out.print(e); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleGetDisbursementChannels.java b/xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleGetDisbursementChannels.java new file mode 100644 index 0000000..d4a010a --- /dev/null +++ b/xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleGetDisbursementChannels.java @@ -0,0 +1,42 @@ +package ExampleWithoutClient; + +import com.xendit.exception.XenditException; +import com.xendit.Xendit; +import com.xendit.model.DisbursementChannel; + +public class ExampleGetDisbursementChannels { + public static void main(String[] args) { + // access key with Option + // you must replace api key with actual api key + Xendit.Opt.setApiKey("xnd_development_..."); + + try { + /** + * Get all disbursement channels + */ + DisbursementChannel[] disbursementChannels = DisbursementChannel.getDisbursementChannels(); + + System.out.println(disbursementChannels); + + /** + * Get all disbursement channels by channel category + */ + String channelCategory = "BANK"; + DisbursementChannel[] disbursementChannels2 = DisbursementChannel + .getByChannelCategory(channelCategory); + + System.out.println(disbursementChannels2); + + /** + * Get all disbursement channels by channel code + */ + String channelCode = "PH_CITI"; + DisbursementChannel[] disbursementChannels3 = DisbursementChannel + .getByChannelCode(channelCode); + + System.out.println(disbursementChannels3); + } catch (XenditException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleGetPHPDisbursement.java b/xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleGetPHPDisbursement.java new file mode 100644 index 0000000..4d8e1b9 --- /dev/null +++ b/xendit-java-library-example/src/main/java/ExampleWithoutClient/ExampleGetPHPDisbursement.java @@ -0,0 +1,36 @@ +package ExampleWithoutClient; + +import com.xendit.exception.XenditException; + +import java.util.Arrays; + +import com.xendit.Xendit; +import com.xendit.model.DisbursementPHP; + +public class ExampleGetPHPDisbursement { + public static void main(String[] args) { + // access key with Option + // you must replace api key with actual api key + Xendit.Opt.setApiKey("xnd_development_..."); + + try { + /** + * Get disbursement object by ID. + */ + String disbursementId = "disb-12345678abcdef"; + DisbursementPHP disbursement = DisbursementPHP.getPHPById(disbursementId); + + System.out.println(disbursement); + + /** + * Get array of php disbursement object by refernce ID. + */ + String referenceId = "my_reference_id"; + DisbursementPHP[] disbursements = DisbursementPHP.getPHPByReferenceId(referenceId); + + System.out.println(Arrays.toString(disbursements)); + } catch (XenditException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file