diff --git a/src/main/java/com/abacatepay/AbacatePay.java b/src/main/java/com/abacatepay/AbacatePay.java index 9925d76..fbfabef 100644 --- a/src/main/java/com/abacatepay/AbacatePay.java +++ b/src/main/java/com/abacatepay/AbacatePay.java @@ -4,9 +4,14 @@ import com.abacatepay.clients.factories.AbacatePayClientFactory; import com.abacatepay.model.IAbacatePay; import com.abacatepay.model.IAbacatePayBilling; +import com.abacatepay.model.IAbacatePayWithdraw; import com.abacatepay.model.billing.CreateBillingData; import com.abacatepay.model.billing.CreateBillingResponse; import com.abacatepay.model.billing.ListBillingResponse; +import com.abacatepay.model.withdraw.payloads.request.CreateWithdrawRequest; +import com.abacatepay.model.withdraw.payloads.response.CreateWithdrawResponse; +import com.abacatepay.model.withdraw.payloads.response.ListWithdrawResponse; +import com.abacatepay.model.withdraw.payloads.response.WithdrawResponse; import feign.FeignException; import feign.RequestInterceptor; @@ -22,7 +27,7 @@ public AbacatePay(String apiKey) { this.apiKey = apiKey; this.client = AbacatePayClientFactory.create(API_BASE_URL, requestInterceptor()); - //TODO: Pegar a versão do SDK dinamicamente + // TODO: Pegar a versão do SDK dinamicamente this.userAgent = "Java SDK (1.0.0)"; } @@ -64,4 +69,38 @@ public ListBillingResponse list() { return new AbacatePayBilling(); } + + @Override + public IAbacatePayWithdraw withdraw() { + class AbacatePayWithdraw implements IAbacatePayWithdraw { + @Override + public CreateWithdrawResponse createWithdraw(CreateWithdrawRequest body) { + try { + return client.createWithdraw(body); + } catch (IllegalArgumentException | FeignException e) { + return new CreateWithdrawResponse(e.getMessage()); + } + } + + @Override + public ListWithdrawResponse listWithdraw() { + try { + return client.listWithdraw(); + } catch (IllegalArgumentException | FeignException e) { + return new ListWithdrawResponse(e.getMessage()); + } + } + + @Override + public WithdrawResponse getWithdraw(String externalId) { + try { + return client.getWithdraw(externalId); + } catch (IllegalArgumentException | FeignException e) { + return new WithdrawResponse(e.getMessage()); + } + } + } + + return new AbacatePayWithdraw(); + } } diff --git a/src/main/java/com/abacatepay/clients/AbacatePayClient.java b/src/main/java/com/abacatepay/clients/AbacatePayClient.java index cdd1470..39bc67f 100644 --- a/src/main/java/com/abacatepay/clients/AbacatePayClient.java +++ b/src/main/java/com/abacatepay/clients/AbacatePayClient.java @@ -3,6 +3,11 @@ import com.abacatepay.model.billing.CreateBillingData; import com.abacatepay.model.billing.CreateBillingResponse; import com.abacatepay.model.billing.ListBillingResponse; +import com.abacatepay.model.withdraw.payloads.request.CreateWithdrawRequest; +import com.abacatepay.model.withdraw.payloads.response.CreateWithdrawResponse; +import com.abacatepay.model.withdraw.payloads.response.ListWithdrawResponse; +import com.abacatepay.model.withdraw.payloads.response.WithdrawResponse; +import feign.Param; import feign.RequestLine; public interface AbacatePayClient { @@ -12,4 +17,13 @@ public interface AbacatePayClient { @RequestLine("POST /billing/create") CreateBillingResponse create(CreateBillingData body); + + @RequestLine("POST /withdraw/create") + CreateWithdrawResponse createWithdraw(CreateWithdrawRequest body); + + @RequestLine("GET /withdraw/list") + ListWithdrawResponse listWithdraw(); + + @RequestLine("GET /withdraw/get?externalId={externalId}") + WithdrawResponse getWithdraw(@Param("externalId") String externalId); } diff --git a/src/main/java/com/abacatepay/model/IAbacatePay.java b/src/main/java/com/abacatepay/model/IAbacatePay.java index ca7cde0..ee2bd97 100644 --- a/src/main/java/com/abacatepay/model/IAbacatePay.java +++ b/src/main/java/com/abacatepay/model/IAbacatePay.java @@ -2,4 +2,6 @@ public interface IAbacatePay { IAbacatePayBilling billing(); + + IAbacatePayWithdraw withdraw(); } diff --git a/src/main/java/com/abacatepay/model/IAbacatePayWithdraw.java b/src/main/java/com/abacatepay/model/IAbacatePayWithdraw.java new file mode 100644 index 0000000..71221bf --- /dev/null +++ b/src/main/java/com/abacatepay/model/IAbacatePayWithdraw.java @@ -0,0 +1,14 @@ +package com.abacatepay.model; + +import com.abacatepay.model.withdraw.payloads.request.CreateWithdrawRequest; +import com.abacatepay.model.withdraw.payloads.response.CreateWithdrawResponse; +import com.abacatepay.model.withdraw.payloads.response.ListWithdrawResponse; +import com.abacatepay.model.withdraw.payloads.response.WithdrawResponse; + +public interface IAbacatePayWithdraw { + CreateWithdrawResponse createWithdraw(CreateWithdrawRequest body); + + ListWithdrawResponse listWithdraw(); + + WithdrawResponse getWithdraw(String externalId); +} diff --git a/src/main/java/com/abacatepay/model/withdraw/Withdraw.java b/src/main/java/com/abacatepay/model/withdraw/Withdraw.java new file mode 100644 index 0000000..43af2f3 --- /dev/null +++ b/src/main/java/com/abacatepay/model/withdraw/Withdraw.java @@ -0,0 +1,26 @@ +package com.abacatepay.model.withdraw; + +import java.time.LocalDateTime; + +import com.abacatepay.model.withdraw.enums.StatusWithdraw; +import com.abacatepay.model.withdraw.enums.WithdrawKind; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.Builder; +import lombok.Data; + +@Builder +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +public class Withdraw { + private String id; + private StatusWithdraw status; + private boolean devMode; + private String receiptUrl; + private WithdrawKind kind; + private Double amount; + private Integer platformFee; + private LocalDateTime createdAt; + private LocalDateTime updatedAt; + private String externalId; +} diff --git a/src/main/java/com/abacatepay/model/withdraw/enums/MethodWithdraw.java b/src/main/java/com/abacatepay/model/withdraw/enums/MethodWithdraw.java new file mode 100644 index 0000000..a1d6153 --- /dev/null +++ b/src/main/java/com/abacatepay/model/withdraw/enums/MethodWithdraw.java @@ -0,0 +1,15 @@ +package com.abacatepay.model.withdraw.enums; + +public enum MethodWithdraw { + PIX("PIX"); + + private final String valueMethod; + + MethodWithdraw(final String valueMethod) { + this.valueMethod = valueMethod; + } + + public String getValueMethod() { + return valueMethod; + } +} diff --git a/src/main/java/com/abacatepay/model/withdraw/enums/StatusWithdraw.java b/src/main/java/com/abacatepay/model/withdraw/enums/StatusWithdraw.java new file mode 100644 index 0000000..401906e --- /dev/null +++ b/src/main/java/com/abacatepay/model/withdraw/enums/StatusWithdraw.java @@ -0,0 +1,19 @@ +package com.abacatepay.model.withdraw.enums; + +public enum StatusWithdraw { + PENDING("PENDING"), + EXPIRED("EXPIRED"), + CANCELLED("CANCELLED"), + COMPLETE("COMPLETE"), + REFUNDED("REFUNDED"); + + private final String valueMethod; + + StatusWithdraw(final String valueMethod) { + this.valueMethod = valueMethod; + } + + public String getValueMethod() { + return valueMethod; + } +} diff --git a/src/main/java/com/abacatepay/model/withdraw/enums/TypeKeyPix.java b/src/main/java/com/abacatepay/model/withdraw/enums/TypeKeyPix.java new file mode 100644 index 0000000..bae3b00 --- /dev/null +++ b/src/main/java/com/abacatepay/model/withdraw/enums/TypeKeyPix.java @@ -0,0 +1,19 @@ +package com.abacatepay.model.withdraw.enums; + +public enum TypeKeyPix { + CPF("CPF"), + CNPJ("CNPJ"), + PHONE("PHONE"), + EMAIL("EMAIL"), + RANDOM("RANDOM"); + + private final String valueMethod; + + TypeKeyPix(final String valueMethod) { + this.valueMethod = valueMethod; + } + + public String getValueMethod() { + return valueMethod; + } +} diff --git a/src/main/java/com/abacatepay/model/withdraw/enums/WithdrawKind.java b/src/main/java/com/abacatepay/model/withdraw/enums/WithdrawKind.java new file mode 100644 index 0000000..cc6c4f3 --- /dev/null +++ b/src/main/java/com/abacatepay/model/withdraw/enums/WithdrawKind.java @@ -0,0 +1,16 @@ +package com.abacatepay.model.withdraw.enums; + +public enum WithdrawKind { + PAYMENT("PAYMENT"), + WITHDRAW("WITHDRAW"); + + private final String valueMethod; + + WithdrawKind(final String valueMethod) { + this.valueMethod = valueMethod; + } + + public String getValueMethod() { + return valueMethod; + } +} diff --git a/src/main/java/com/abacatepay/model/withdraw/payloads/request/CreateWithdrawRequest.java b/src/main/java/com/abacatepay/model/withdraw/payloads/request/CreateWithdrawRequest.java new file mode 100644 index 0000000..c83067a --- /dev/null +++ b/src/main/java/com/abacatepay/model/withdraw/payloads/request/CreateWithdrawRequest.java @@ -0,0 +1,32 @@ +package com.abacatepay.model.withdraw.payloads.request; + +import com.abacatepay.model.withdraw.enums.MethodWithdraw; +import com.abacatepay.model.withdraw.enums.TypeKeyPix; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Builder; +import lombok.Data; + +@Builder +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +public class CreateWithdrawRequest { + @JsonProperty(required = true) + private String externalId; + @JsonProperty(required = true) + private MethodWithdraw method; + @JsonProperty(required = true) + private Double amount; + @JsonProperty(required = false) + private String description; + @JsonProperty(required = true) + private Pix pix; + + @Builder + @Data + static class Pix { + private TypeKeyPix type; + private String key; + } +} diff --git a/src/main/java/com/abacatepay/model/withdraw/payloads/response/CreateWithdrawResponse.java b/src/main/java/com/abacatepay/model/withdraw/payloads/response/CreateWithdrawResponse.java new file mode 100644 index 0000000..1f30fea --- /dev/null +++ b/src/main/java/com/abacatepay/model/withdraw/payloads/response/CreateWithdrawResponse.java @@ -0,0 +1,21 @@ +package com.abacatepay.model.withdraw.payloads.response; + +import com.abacatepay.model.withdraw.Withdraw; +import com.fasterxml.jackson.annotation.JsonInclude; + +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class CreateWithdrawResponse { + @JsonInclude(JsonInclude.Include.NON_NULL) + private String error; + + @JsonInclude(JsonInclude.Include.NON_NULL) + private Withdraw withdraw; + + public CreateWithdrawResponse(String error) { + this.error = error; + } +} diff --git a/src/main/java/com/abacatepay/model/withdraw/payloads/response/ListWithdrawResponse.java b/src/main/java/com/abacatepay/model/withdraw/payloads/response/ListWithdrawResponse.java new file mode 100644 index 0000000..00d2610 --- /dev/null +++ b/src/main/java/com/abacatepay/model/withdraw/payloads/response/ListWithdrawResponse.java @@ -0,0 +1,22 @@ +package com.abacatepay.model.withdraw.payloads.response; + +import java.util.List; + +import com.abacatepay.model.withdraw.Withdraw; +import com.fasterxml.jackson.annotation.JsonInclude; + +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class ListWithdrawResponse { + @JsonInclude(JsonInclude.Include.NON_NULL) + private List withdrawals; + @JsonInclude(JsonInclude.Include.NON_NULL) + private String error; + + public ListWithdrawResponse(final String error) { + this.error = error; + } +} diff --git a/src/main/java/com/abacatepay/model/withdraw/payloads/response/WithdrawResponse.java b/src/main/java/com/abacatepay/model/withdraw/payloads/response/WithdrawResponse.java new file mode 100644 index 0000000..7c63e35 --- /dev/null +++ b/src/main/java/com/abacatepay/model/withdraw/payloads/response/WithdrawResponse.java @@ -0,0 +1,19 @@ +package com.abacatepay.model.withdraw.payloads.response; + +import com.abacatepay.model.withdraw.Withdraw; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@Data +public class WithdrawResponse { + @JsonInclude(JsonInclude.Include.NON_NULL) + private Withdraw withdraw; + @JsonInclude(JsonInclude.Include.NON_NULL) + private String error; + + public WithdrawResponse(final String error) { + this.error = error; + } +} diff --git a/src/test/java/com/abacatepay/AbacatePayTest.java b/src/test/java/com/abacatepay/AbacatePayTest.java index 9db2cde..b1ec4fe 100644 --- a/src/test/java/com/abacatepay/AbacatePayTest.java +++ b/src/test/java/com/abacatepay/AbacatePayTest.java @@ -4,14 +4,22 @@ import com.abacatepay.model.billing.CreateBillingData; import com.abacatepay.model.billing.CreateBillingResponse; import com.abacatepay.model.billing.ListBillingResponse; +import com.abacatepay.model.withdraw.Withdraw; +import com.abacatepay.model.withdraw.payloads.request.CreateWithdrawRequest; +import com.abacatepay.model.withdraw.payloads.response.CreateWithdrawResponse; +import com.abacatepay.model.withdraw.payloads.response.ListWithdrawResponse; +import com.abacatepay.model.withdraw.payloads.response.WithdrawResponse; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; import static org.mockito.Mockito.*; @@ -52,7 +60,7 @@ void shouldReturnCreateBillingResponseOnSuccess() { @Test void shouldCreateBillingThrowsAnException() { CreateBillingData data = CreateBillingData.builder().build(); - CreateBillingResponse expectedResponse = new CreateBillingResponse("API key not provided"); + CreateBillingResponse expectedResponse = new CreateBillingResponse(""); when(abacatePayClient.create(data)).thenThrow(new IllegalArgumentException("API key not provided")); @@ -82,4 +90,88 @@ void shouldListBillingThrowsAnException() { verify(abacatePayClient, atMostOnce()).list(); Assertions.assertEquals(expectedResponse, result, "Should return the expected response"); } + + @Test + void shouldCreateWithdrawReturnsResponse() { + final CreateWithdrawRequest request = CreateWithdrawRequest.builder() + .build(); + final CreateWithdrawResponse response = new CreateWithdrawResponse(); + + when(abacatePayClient.createWithdraw(request)) + .thenReturn(response); + + final CreateWithdrawResponse result = abacatePay.withdraw().createWithdraw(request); + Assertions.assertNotNull(result); + Assertions.assertEquals(response, result, "Should return the expected response"); + } + + @Test + void shouldCreateWithdrawThrowsAnException() { + final CreateWithdrawRequest request = CreateWithdrawRequest.builder() + .build(); + final CreateWithdrawResponse response = new CreateWithdrawResponse("API key not provided"); + + when(abacatePayClient.createWithdraw(request)) + .thenThrow(new IllegalArgumentException("API key not provided")); + + final CreateWithdrawResponse result = abacatePay.withdraw().createWithdraw(request); + Assertions.assertNotNull(result); + Assertions.assertEquals(response, result, "Should return the expected response"); + } + + @Test + void shouldListWithdrawalsReturnsResponse() { + final Withdraw withdraw = Withdraw.builder() + .amount(300.00) + .build(); + final ListWithdrawResponse response = new ListWithdrawResponse(); + response.setWithdrawals(new ArrayList<>(Arrays.asList(withdraw))); + + when(abacatePayClient.listWithdraw()) + .thenReturn(response); + + final ListWithdrawResponse result = abacatePay.withdraw().listWithdraw(); + Assertions.assertNotNull(result); + Assertions.assertEquals(response, result, "Should return the expected response"); + } + + @Test + void shouldListWithdrawalsThrowsAnException() { + final ListWithdrawResponse response = new ListWithdrawResponse("API key not provided"); + + when(abacatePayClient.listWithdraw()) + .thenThrow(new IllegalArgumentException("API key not provided")); + + final ListWithdrawResponse result = abacatePay.withdraw().listWithdraw(); + Assertions.assertNotNull(result); + Assertions.assertEquals(response, result, "Should return the expected response"); + } + + @Test + void shouldGetWithdrawsReturnsResponse() { + final Withdraw withdraw = Withdraw.builder() + .amount(300.00) + .build(); + final WithdrawResponse response = new WithdrawResponse(); + response.setWithdraw(withdraw); + + when(abacatePayClient.getWithdraw(ArgumentMatchers.anyString())) + .thenReturn(response); + + final WithdrawResponse result = abacatePay.withdraw().getWithdraw("withdraw-1234"); + Assertions.assertNotNull(result); + Assertions.assertEquals(response, result, "Should return the expected response"); + } + + @Test + void shouldGetWithdrawsThrowsAnException() { + final WithdrawResponse response = new WithdrawResponse("API key not provided"); + + when(abacatePayClient.getWithdraw(ArgumentMatchers.anyString())) + .thenThrow(new IllegalArgumentException("API key not provided")); + + final WithdrawResponse result = abacatePay.withdraw().getWithdraw("withdraw-1234"); + Assertions.assertNotNull(result); + Assertions.assertEquals(response, result, "Should return the expected response"); + } }