|
| 1 | +package br.com.moip.models; |
| 2 | + |
| 3 | +import br.com.moip.api.request.RequestMaker; |
| 4 | +import br.com.moip.api.request.RequestProperties; |
| 5 | +import br.com.moip.api.request.RequestPropertiesBuilder; |
| 6 | +import org.apache.http.entity.ContentType; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +public class Accounts { |
| 12 | + |
| 13 | + private static final String ENDPOINT = "/v2/accounts"; |
| 14 | + private static final ContentType CONTENT_TYPE = ContentType.APPLICATION_JSON; |
| 15 | + private RequestMaker requestMaker; |
| 16 | + |
| 17 | + /** |
| 18 | + * This method is used to validate the argument of bellow method, if probably it's a tax document or not. |
| 19 | + * |
| 20 | + * @param argument |
| 21 | + * {@code String} the received argument. |
| 22 | + * |
| 23 | + * @return {@code boolean} |
| 24 | + */ |
| 25 | + private boolean isTaxDocument(String argument) { |
| 26 | + try { |
| 27 | + Integer.parseInt(argument.substring(0,1)); |
| 28 | + } catch (Exception ex) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * This method allows you to check if a person already has a Moip account, by it's tax document or e-mail. |
| 36 | + * The tax document must be write with punctuation, for example: 123.456.789-00. |
| 37 | + * |
| 38 | + * @param argument |
| 39 | + * {@code String} the person's tax document or e-mail. |
| 40 | + * |
| 41 | + * @param setup |
| 42 | + * {@code Setup} the setup object. |
| 43 | + * |
| 44 | + * @return {@code Map<String, Object>} |
| 45 | + */ |
| 46 | + public Map<String, Object> checkExistence(String argument, Setup setup) { |
| 47 | + this.requestMaker = new RequestMaker(setup); |
| 48 | + String argumentType; |
| 49 | + |
| 50 | + if (isTaxDocument(argument)) argumentType = "tax_document"; |
| 51 | + |
| 52 | + else argumentType = "email"; |
| 53 | + |
| 54 | + RequestProperties props = new RequestPropertiesBuilder() |
| 55 | + .method("GET") |
| 56 | + .endpoint(String.format("%s/exists?%s=%s", ENDPOINT, argumentType, argument)) |
| 57 | + .type(Accounts.class) |
| 58 | + .contentType(CONTENT_TYPE) |
| 59 | + .build(); |
| 60 | + |
| 61 | + return this.requestMaker.doRequest(props); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * This method allows you to create a Moip account (classical or transparent). To differentiate the |
| 66 | + * two accounts types you have to set the boolean attribute {@code transparentAccount}, <b>true</b> value |
| 67 | + * (you will create a transparent accounts) or <b>false</b> value (you will create a classical accounts). |
| 68 | + * |
| 69 | + * @param body |
| 70 | + * {@code Map<String, Object>} the request body. |
| 71 | + * |
| 72 | + * @param setup |
| 73 | + * {@code Setup} the setup object. |
| 74 | + * |
| 75 | + * @return {@code Map<String, Object>} |
| 76 | + */ |
| 77 | + public Map<String, Object> create(Map<String, Object> body, Setup setup) { |
| 78 | + this.requestMaker = new RequestMaker(setup); |
| 79 | + RequestProperties props = new RequestPropertiesBuilder() |
| 80 | + .method("POST") |
| 81 | + .endpoint(ENDPOINT) |
| 82 | + .body(body) |
| 83 | + .type(Accounts.class) |
| 84 | + .contentType(CONTENT_TYPE) |
| 85 | + .build(); |
| 86 | + |
| 87 | + return this.requestMaker.doRequest(props); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * This method is used to get a created accounts by Moip account external ID. |
| 92 | + * |
| 93 | + * @param id |
| 94 | + * {@code String} the Moip account external ID. |
| 95 | + * |
| 96 | + * @param setup |
| 97 | + * {@code Setup} the setup object. |
| 98 | + * |
| 99 | + * @return {@code Map<String, Object>} |
| 100 | + */ |
| 101 | + public Map<String, Object> get(String id, Setup setup) { |
| 102 | + this.requestMaker = new RequestMaker(setup); |
| 103 | + RequestProperties props = new RequestPropertiesBuilder() |
| 104 | + .method("GET") |
| 105 | + .endpoint(String.format("%s/%s", ENDPOINT, id)) |
| 106 | + .type(Accounts.class) |
| 107 | + .contentType(CONTENT_TYPE) |
| 108 | + .build(); |
| 109 | + |
| 110 | + return this.requestMaker.doRequest(props); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * This method is used to get the Basic Auth keys and the public key of a Moip account. |
| 115 | + * |
| 116 | + * @param setup |
| 117 | + * {@code Setup} the setup object. |
| 118 | + * |
| 119 | + * @return {@code Map<String, Object>} |
| 120 | + */ |
| 121 | + public Map<String, Object> getKeys(Setup setup) { |
| 122 | + this.requestMaker = new RequestMaker(setup); |
| 123 | + RequestProperties props = new RequestPropertiesBuilder() |
| 124 | + .method("GET") |
| 125 | + .endpoint("/v2/keys") |
| 126 | + .type(Accounts.class) |
| 127 | + .contentType(CONTENT_TYPE) |
| 128 | + .build(); |
| 129 | + |
| 130 | + return this.requestMaker.doRequest(props); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * This method allows you to create a bank account to a Moip account. You can use a bank account to make |
| 135 | + * transfers and refunds on Moip. |
| 136 | + * |
| 137 | + * @param body |
| 138 | + * {@code Map<String, Object>} the request body. |
| 139 | + * |
| 140 | + * @param moipAccountId |
| 141 | + * {@code String} the Moip account external ID. |
| 142 | + * |
| 143 | + * @param setup |
| 144 | + * {@code Setup} the setup object. |
| 145 | + * |
| 146 | + * @return {@code Map<String, Object>} |
| 147 | + */ |
| 148 | + public Map<String, Object> createBankAccount(Map<String, Object> body, String moipAccountId, Setup setup) { |
| 149 | + this.requestMaker = new RequestMaker(setup); |
| 150 | + RequestProperties props = new RequestPropertiesBuilder() |
| 151 | + .method("POST") |
| 152 | + .endpoint(String.format("%s/%s/bankaccounts", ENDPOINT, moipAccountId)) |
| 153 | + .body(body) |
| 154 | + .type(Accounts.class) |
| 155 | + .contentType(CONTENT_TYPE) |
| 156 | + .build(); |
| 157 | + |
| 158 | + return this.requestMaker.doRequest(props); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * This method allows you to list all bank account of a Moip account. |
| 163 | + * |
| 164 | + * @param moipAccountId |
| 165 | + * {@code String} the Moip account external ID. |
| 166 | + * |
| 167 | + * @param setup |
| 168 | + * {@code Setup} the setup object. |
| 169 | + * |
| 170 | + * @return {@code Map<String, Object>} |
| 171 | + */ |
| 172 | + public List<Map<String, Object>> listBankAccounts(String moipAccountId, Setup setup) { |
| 173 | + this.requestMaker = new RequestMaker(setup); |
| 174 | + RequestProperties props = new RequestPropertiesBuilder() |
| 175 | + .method("GET") |
| 176 | + .endpoint(String.format("%s/%s/bankaccounts", ENDPOINT, moipAccountId)) |
| 177 | + .type(BankAccounts.class) |
| 178 | + .contentType(CONTENT_TYPE) |
| 179 | + .build(); |
| 180 | + |
| 181 | + return this.requestMaker.getList(props); |
| 182 | + } |
| 183 | +} |
0 commit comments