Skip to content

Commit cebab00

Browse files
Jason SwenskiChris Kleeschulte
authored andcommitted
Fixing the fact that getInvoice(id,PUBLIC_NO_TOKEN) was actually still signing the requests and therefore not actually using the public API. This doesn't work in some cases because the /invoice endpoint doesn't support some facades.
Also refactors some of the parameter building to off-the-shelf apache commons methods
1 parent e84770e commit cebab00

File tree

2 files changed

+59
-73
lines changed

2 files changed

+59
-73
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
.DS_Store
2+
.classpath
3+
.project
4+
.settings/
5+
bin/.gitignore
6+
lib/.DS_Store
7+
pom.xml
8+
target/
19
locals/
210
*.key
311
java-bitpay-client.xml

src/main/java/controller/BitPay.java

Lines changed: 51 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import org.apache.http.client.methods.HttpDelete;
1515
import org.apache.http.client.methods.HttpGet;
1616
import org.apache.http.client.methods.HttpPost;
17+
import org.apache.http.client.utils.URLEncodedUtils;
1718
import org.apache.http.entity.ByteArrayEntity;
1819
import org.apache.http.impl.client.HttpClientBuilder;
20+
import org.apache.http.message.BasicNameValuePair;
1921
import org.apache.http.util.EntityUtils;
2022
import org.bitcoinj.core.ECKey;
2123

@@ -381,14 +383,13 @@ public Invoice getInvoice(String invoiceId) throws BitPayException
381383
*/
382384
public Invoice getInvoice(String invoiceId, String token) throws BitPayException
383385
{
384-
Hashtable<String, String> parameters = this.getParams();
385-
386-
parameters.put("token", token);
387-
388-
HttpResponse response = this.get("invoices/" + invoiceId, parameters);
386+
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
387+
params.add(new BasicNameValuePair("token", token));
388+
389+
boolean requireSignature = !PUBLIC_NO_TOKEN.equals(token);
390+
HttpResponse response = this.get("invoices/" + invoiceId, params, requireSignature);
389391

390392
Invoice i;
391-
392393
try {
393394
i = new ObjectMapper().readValue(this.responseToJsonString(response), Invoice.class);
394395
} catch (JsonProcessingException e) {
@@ -409,13 +410,13 @@ public Invoice getInvoice(String invoiceId, String token) throws BitPayException
409410
*/
410411
public List<Invoice> getInvoices(String dateStart, String dateEnd) throws BitPayException
411412
{
412-
Hashtable<String, String> parameters = this.getParams();
413-
414-
parameters.put("token", this.getAccessToken(FACADE_MERCHANT));
415-
parameters.put("dateStart", dateStart);
416-
parameters.put("dateEnd", dateEnd);
413+
414+
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
415+
params.add(new BasicNameValuePair("token", this.getAccessToken(FACADE_MERCHANT)));
416+
params.add(new BasicNameValuePair("dateStart", dateStart));
417+
params.add(new BasicNameValuePair("dateEnd", dateEnd));
417418

418-
HttpResponse response = this.get("invoices", parameters);
419+
HttpResponse response = this.get("invoices", params);
419420

420421
List<Invoice> invoices;
421422

@@ -510,17 +511,17 @@ public boolean cancelRefundRequest(String invoiceId, String refundId) throws Bit
510511
* @throws BitPayException
511512
*/
512513
public boolean cancelRefundRequest(Invoice invoice, String refundId) throws BitPayException
513-
{
514-
Refund refund = this.getRefund(invoice, refundId);
515-
if (refund == null)
516-
{
517-
throw new BitPayException("Error - refundId is not associated with specified invoice");
518-
}
519-
520-
Hashtable<String, String> parameters = this.getParams();
521-
parameters.put("token", refund.getToken());
514+
{
515+
Refund refund = this.getRefund(invoice, refundId);
516+
if (refund == null)
517+
{
518+
throw new BitPayException("Error - refundId is not associated with specified invoice");
519+
}
522520

523-
HttpResponse response = this.delete("invoices/" + invoice.getId() + "/refunds/" + refundId, parameters);
521+
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
522+
params.add(new BasicNameValuePair("token", refund.getToken()));
523+
524+
HttpResponse response = this.delete("invoices/" + invoice.getId() + "/refunds/" + refundId, params);
524525
String result = this.responseToJsonString(response);
525526

526527
return (result.equals("\"Success\""));
@@ -536,11 +537,10 @@ public boolean cancelRefundRequest(Invoice invoice, String refundId) throws BitP
536537
public Refund getRefund(Invoice invoice, String refundId) throws BitPayException
537538
{
538539
Refund refund = new Refund();
539-
Hashtable<String, String> parameters = this.getParams();
540-
541-
parameters.put("token", invoice.getToken());
542-
543-
HttpResponse response = this.get("invoices/" + invoice.getId() + "/refunds/" + refundId, parameters);
540+
541+
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
542+
params.add(new BasicNameValuePair("token", invoice.getToken()));
543+
HttpResponse response = this.get("invoices/" + invoice.getId() + "/refunds/" + refundId, params);
544544

545545
ObjectMapper mapper = new ObjectMapper();
546546

@@ -564,11 +564,10 @@ public Refund getRefund(Invoice invoice, String refundId) throws BitPayException
564564
public List<Refund> getAllRefunds(Invoice invoice) throws BitPayException
565565
{
566566
List<Refund> refunds;
567-
Hashtable<String, String> parameters = this.getParams();
568-
569-
parameters.put("token", invoice.getToken());
570-
571-
HttpResponse response = this.get("invoices/" + invoice.getId() + "/refunds", parameters);
567+
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
568+
params.add(new BasicNameValuePair("token", invoice.getToken()));
569+
570+
HttpResponse response = this.get("invoices/" + invoice.getId() + "/refunds", params);
572571

573572
try {
574573
refunds = Arrays.asList(new ObjectMapper().readValue(this.responseToJsonString(response), Refund[].class));
@@ -686,47 +685,37 @@ private int getAccessTokens() throws BitPayException
686685
{
687686
this.clearAccessTokenCache();
688687

689-
Hashtable<String, String> parameters = this.getParams();
690-
691-
parameters.put("id", this.getIdentity());
692-
693-
HttpResponse response = this.get("tokens", parameters);
688+
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
689+
params.add(new BasicNameValuePair("id", this.getIdentity()));
690+
691+
HttpResponse response = this.get("tokens", params);
694692

695693
_tokenCache = responseToTokenCache(response);
696694

697695
return _tokenCache.size();
698696
}
699-
700-
private Hashtable<String, String> getParams()
701-
{
702-
return new Hashtable<String, String>();
697+
698+
699+
private HttpResponse get(String uri, List<BasicNameValuePair> parameters) throws BitPayException {
700+
return get(uri, parameters, true);
703701
}
704702

705-
private HttpResponse get(String uri, Hashtable<String, String> parameters) throws BitPayException
703+
private HttpResponse get(String uri, List<BasicNameValuePair> parameters, boolean signatureRequired) throws BitPayException
706704
{
707705
try {
708706

709707
String fullURL = _baseUrl + uri;
710708
HttpGet get = new HttpGet(fullURL);
711709

712710
if (parameters != null) {
713-
fullURL += "?";
714-
715-
for (String key : parameters.keySet()) {
716-
fullURL += key + "=" + parameters.get(key) + "&";
717-
}
718-
719-
fullURL = fullURL.substring(0,fullURL.length() - 1);
720-
721-
get.setURI(new URI(fullURL));
722-
723-
String signature = KeyUtils.sign(_ecKey, fullURL);
724-
725-
get.addHeader("x-bitpay-plugin-info", BITPAY_PLUGIN_INFO);
726-
get.addHeader("x-accept-version", BITPAY_API_VERSION);
727-
get.addHeader("x-signature", signature);
711+
get.setURI(new URI(fullURL+URLEncodedUtils.format(parameters, "UTF-8")));
712+
}
713+
if(signatureRequired) {
714+
get.addHeader("x-signature", KeyUtils.sign(_ecKey, fullURL));
728715
get.addHeader("x-identity", KeyUtils.bytesToHex(_ecKey.getPubKey()));
729716
}
717+
get.addHeader("x-bitpay-plugin-info", BITPAY_PLUGIN_INFO);
718+
get.addHeader("x-accept-version", BITPAY_API_VERSION);
730719

731720
return _httpClient.execute(get);
732721

@@ -752,9 +741,7 @@ private HttpResponse post(String uri, String json, boolean signatureRequired) th
752741
post.setEntity(new ByteArrayEntity(json.getBytes("UTF8")));
753742

754743
if (signatureRequired) {
755-
String signature = KeyUtils.sign(_ecKey, _baseUrl + uri + json);
756-
757-
post.addHeader("x-signature", signature);
744+
post.addHeader("x-signature", KeyUtils.sign(_ecKey, _baseUrl + uri + json));
758745
post.addHeader("x-identity", KeyUtils.bytesToHex(_ecKey.getPubKey()));
759746
}
760747

@@ -783,29 +770,20 @@ private HttpResponse postWithSignature(String uri, String json) throws BitPayExc
783770
return this.post(uri, json, true);
784771
}
785772

786-
private HttpResponse delete(String uri, Hashtable<String, String> parameters) throws BitPayException
773+
private HttpResponse delete(String uri, List<BasicNameValuePair> parameters) throws BitPayException
787774
{
788775
try {
789776

790777
String fullURL = _baseUrl + uri;
791778
HttpDelete delete = new HttpDelete(fullURL);
792779

793780
if (parameters != null) {
794-
fullURL += "?";
795-
796-
for (String key : parameters.keySet()) {
797-
fullURL += key + "=" + parameters.get(key) + "&";
798-
}
799-
800-
fullURL = fullURL.substring(0,fullURL.length() - 1);
801-
802-
delete.setURI(new URI(fullURL));
803-
804-
String signature = KeyUtils.sign(_ecKey, fullURL);
781+
782+
delete.setURI(new URI(fullURL+URLEncodedUtils.format(parameters, "UTF-8")));
805783

806784
delete.addHeader("x-bitpay-plugin-info", BITPAY_PLUGIN_INFO);
807785
delete.addHeader("x-accept-version", BITPAY_API_VERSION);
808-
delete.addHeader("x-signature", signature);
786+
delete.addHeader("x-signature", KeyUtils.sign(_ecKey, fullURL));
809787
delete.addHeader("x-identity", KeyUtils.bytesToHex(_ecKey.getPubKey()));
810788
}
811789

0 commit comments

Comments
 (0)