diff --git a/src/controller/BitPay.java b/src/controller/BitPay.java index e89c871..e4db3b4 100644 --- a/src/controller/BitPay.java +++ b/src/controller/BitPay.java @@ -33,7 +33,9 @@ import java.util.List; public class BitPay { - private static final String BITPAY_API_VERSION = "2.0.0"; + private static final BitPayLogger _log = new BitPayLogger(BitPayLogger.DEBUG); + + private static final String BITPAY_API_VERSION = "2.0.0"; private static final String BITPAY_PLUGIN_INFO = "BitPay Java Client " + BITPAY_API_VERSION; public static final String BITPAY_URL = "https://bitpay.com/"; public static final String BITPAY_TEST_URL = "https://test.bitpay.com/"; @@ -59,7 +61,6 @@ public class BitPay { * @param envUrl The target server URL. * @throws BitPayException */ - @Deprecated public BitPay(String clientName, String envUrl) throws BitPayException { if (clientName.equals(BITPAY_PLUGIN_INFO)) { try { @@ -99,7 +100,6 @@ public class BitPay { * @param clientName The label for this client. * @throws BitPayException */ - @Deprecated public BitPay(String clientName) throws BitPayException { this(clientName, BITPAY_URL); } @@ -109,7 +109,6 @@ public class BitPay { * * @throws BitPayException */ - @Deprecated public BitPay() throws BitPayException { this(BITPAY_PLUGIN_INFO, BITPAY_URL); } @@ -156,7 +155,7 @@ public class BitPay { * @throws BitPayException */ public BitPay(ECKey ecKey, String clientName, String envUrl) throws BitPayException { - _ecKey = ecKey; + _ecKey = ecKey; this.deriveIdentity(); @@ -605,6 +604,119 @@ public class BitPay { return new Rates(rates, this); } + /** + * Submit a BitPay Payout batch. + * + * @param batch A PayoutBatch object with request parameters defined. + * @return A BitPay generated PayoutBatch object. + * @throws BitPayException + */ + public PayoutBatch submitPayoutBatch(PayoutBatch batch) throws BitPayException { + String token = this.getAccessToken(FACADE_PAYROLL); + batch.setToken(token); + batch.setGuid(this.getGuid()); + + ObjectMapper mapper = new ObjectMapper(); + + String json; + + try { + json = mapper.writeValueAsString(batch); + } catch (JsonProcessingException e) { + throw new BitPayException("Error - failed to serialize PayoutBatch object : " + e.getMessage()); + } + + HttpResponse response = this.postWithSignature("payouts", json); + + try { + batch = mapper.readerForUpdating(batch).readValue(this.responseToJsonString(response)); + } catch (JsonProcessingException e) { + throw new BitPayException("Error - failed to deserialize BitPay server response (PayoutBatch) : " + e.getMessage()); + } catch (IOException e) { + throw new BitPayException("Error - failed to deserialize BitPay server response (PayoutBatch) : " + e.getMessage()); + } + + this.cacheAccessToken(batch.getId(), batch.getToken()); + return batch; + } + + /** + * Retrieve a collection of BitPay payout batches. + * + * @return A list of BitPay PayoutBatch objects. + * @throws BitPayException + */ + public List getPayoutBatches() throws BitPayException { + + final List params = new ArrayList(); + params.add(new BasicNameValuePair("token", this.getAccessToken(FACADE_PAYROLL))); + + HttpResponse response = this.get("payouts", params); + + List batches; + + try { + batches = Arrays.asList(new ObjectMapper().readValue(this.responseToJsonString(response), PayoutBatch[].class)); + } catch (JsonProcessingException e) { + throw new BitPayException("Error - failed to deserialize BitPay server response (PayoutBatch) : " + e.getMessage()); + } catch (IOException e) { + throw new BitPayException("Error - failed to deserialize BitPay server response (PayoutBatch) : " + e.getMessage()); + } + + return batches; + } + + /** + * Retrieve a BitPay payout batch by batch id using. The client must have been previously authorized for the payroll facade. + * + * @param batchId The id of the batch to retrieve. + * @return A BitPay PayoutBatch object. + * @throws BitPayException + */ + public PayoutBatch getPayoutBatch(String batchId) throws BitPayException { + String token = this.getAccessToken(FACADE_PAYROLL); + final List params = new ArrayList(); + params.add(new BasicNameValuePair("token", token)); + + HttpResponse response = this.get("payouts/" + batchId, params, true); + + PayoutBatch b; + try { + b = new ObjectMapper().readValue(this.responseToJsonString(response), PayoutBatch.class); + } catch (JsonProcessingException e) { + throw new BitPayException("Error - failed to deserialize BitPay server response (PayoutBatch) : " + e.getMessage()); + } catch (IOException e) { + throw new BitPayException("Error - failed to deserialize BitPay server response (PayoutBatch) : " + e.getMessage()); + } + + return b; + } + + /** + * Cancel a BitPay Payout batch. + * + * @param batchId The id of the batch to cancel. + * @return A BitPay generated PayoutBatch object. + * @throws BitPayException + */ + public PayoutBatch cancelPayoutBatch(String batchId) throws BitPayException { + PayoutBatch b = getPayoutBatch(batchId); + final List params = new ArrayList(); + params.add(new BasicNameValuePair("token", b.getToken())); + + HttpResponse response = this.delete("payouts/" + batchId, params); + + try { + b = new ObjectMapper().readValue(this.responseToJsonString(response), PayoutBatch.class); + } catch (JsonProcessingException e) { + throw new BitPayException("Error - failed to deserialize BitPay server response (PayoutBatch) : " + e.getMessage()); + } catch (IOException e) { + throw new BitPayException("Error - failed to deserialize BitPay server response (PayoutBatch) : " + e.getMessage()); + } + + return b; + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -694,11 +806,11 @@ public class BitPay { } - public HttpResponse get(String uri, List parameters) throws BitPayException { + private HttpResponse get(String uri, List parameters) throws BitPayException { return get(uri, parameters, true); } - public HttpResponse get(String uri, List parameters, boolean signatureRequired) throws BitPayException { + private HttpResponse get(String uri, List parameters, boolean signatureRequired) throws BitPayException { try { String fullURL = _baseUrl + uri; @@ -717,6 +829,7 @@ public class BitPay { get.addHeader("x-accept-version", BITPAY_API_VERSION); + _log.info(get.toString()); return _httpClient.execute(get); } catch (URISyntaxException e) { @@ -728,11 +841,11 @@ public class BitPay { } } - public HttpResponse get(String uri) throws BitPayException { + private HttpResponse get(String uri) throws BitPayException { return this.get(uri, null, false); } - public HttpResponse post(String uri, String json, boolean signatureRequired) throws BitPayException { + private HttpResponse post(String uri, String json, boolean signatureRequired) throws BitPayException { try { HttpPost post = new HttpPost(_baseUrl + uri); @@ -747,6 +860,7 @@ public class BitPay { post.addHeader("x-bitpay-plugin-info", BITPAY_PLUGIN_INFO); post.addHeader("Content-Type", "application/json"); + _log.info(post.toString()); return _httpClient.execute(post); } catch (UnsupportedEncodingException e) { @@ -758,11 +872,11 @@ public class BitPay { } } - public HttpResponse post(String uri, String json) throws BitPayException { + private HttpResponse post(String uri, String json) throws BitPayException { return this.post(uri, json, false); } - public HttpResponse postWithSignature(String uri, String json) throws BitPayException { + private HttpResponse postWithSignature(String uri, String json) throws BitPayException { return this.post(uri, json, true); } @@ -784,6 +898,7 @@ public class BitPay { delete.addHeader("x-identity", KeyUtils.bytesToHex(_ecKey.getPubKey())); } + _log.info(delete.toString()); return _httpClient.execute(delete); } catch (URISyntaxException e) { @@ -795,7 +910,7 @@ public class BitPay { } } - public String responseToJsonString(HttpResponse response) throws BitPayException { + private String responseToJsonString(HttpResponse response) throws BitPayException { if (response == null) { throw new BitPayException("Error: HTTP response is null"); } @@ -807,6 +922,7 @@ public class BitPay { String jsonString; jsonString = EntityUtils.toString(entity, "UTF-8"); + _log.info("RESPONSE: " + jsonString); ObjectMapper mapper = new ObjectMapper(); @@ -854,4 +970,5 @@ public class BitPay { return Min + (int) (Math.random() * ((Max - Min) + 1)) + ""; } + }