Skip to content

Commit 670bf99

Browse files
Merge branch 'ppatidar2021-retrieve_invoice_using_guid'
2 parents 42ee19f + d1cf554 commit 670bf99

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed

docs/invoice.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,32 +646,67 @@ invoice = bitpay.createInvoice(invoice);
646646

647647
`GET /invoices/:invoiceid`
648648

649-
Facade **`POS` `MERCHANT`**
649+
Facade **`MERCHANT`**
650650

651651
### HTTP Request
652652

653653
**URL Parameters**
654654

655655
| Parameter | Description | Type | Presence |
656656
| --- | --- | :---: | :---: |
657-
| ?token= | When fetching an invoice via the `merchant` or the `pos` facade, pass the API token as a URL parameter - the same token used to create the invoice in the first place. | `string` | **Mandatory** |
657+
| ?token= | When fetching an invoice via the `merchant`facade, pass the API token as a URL parameter - the same token used to create the invoice in the first place. | `string` | **Mandatory** |
658658

659659
**Headers**
660660

661661
| Fields | Description | Presence |
662662
| --- | --- | :---: |
663663
| X-Accept-Version | Must be set to `2.0.0` for requests to the BitPay API. | **Mandatory** |
664664
| Content-Type | must be set to `application/json` for requests to the BitPay API. | **Mandatory** |
665-
| X-Identity | the hexadecimal public key generated from the client private key. This header is required when using tokens with higher privileges (`merchant` facade). When using standard `pos` facade token directly from the [BitPay dashboard](https://test.bitpay.com/dashboard/merchant/api-tokens) (with `"Require Authentication"` disabled), this header is not needed. | **Mandatory** |
666-
| X-Signature | header is the ECDSA signature of the full request URL concatenated with the request body, signed with your private key. This header is required when using tokens with higher privileges (`merchant` facade). When using standard `pos` facade token directly from the [BitPay dashboard](https://test.bitpay.com/dashboard/merchant/api-tokens) (with `"Require Authentication"` disabled), this header is not needed. | **Mandatory** |
665+
| X-Identity | the hexadecimal public key generated from the client private key. This header is required when using tokens with higher privileges (`merchant` facade). When using standard `merchant` facade token directly from the [BitPay dashboard](https://test.bitpay.com/dashboard/merchant/api-tokens) (with `"Require Authentication"` disabled), this header is not needed. | **Mandatory** |
666+
| X-Signature | header is the ECDSA signature of the full request URL concatenated with the request body, signed with your private key. This header is required when using tokens with higher privileges (`merchant` facade). When using standard `merchant` facade token directly from the [BitPay dashboard](https://test.bitpay.com/dashboard/merchant/api-tokens) (with `"Require Authentication"` disabled), this header is not needed. | **Mandatory** |
667667

668668

669669
To get the generated invoice details, pass the Invoice Id with URL parameter
670670

671671
```java
672-
Invoice getInvoice = bitpay.getInvoice(invoice.getId());
672+
Invoice invoice = new Invoice(100.0, Currency.USD)
673+
Invoice createInvoice = bitpay.createInvoice(invoice);
674+
Invoice getInvoice = bitpay.getInvoice(createInvoice.getId());
673675
```
674676

677+
## Retrieve an invoice using guid
678+
679+
`GET /invoices/guid/:guid`
680+
681+
Facade **`MERCHANT`**
682+
683+
### HTTP Request
684+
685+
**URL Parameters**
686+
687+
| Parameter | Description | Type | Presence |
688+
| --- | --- | :---: | :---: |
689+
| ?token= | When fetching an invoice via the `merchant` facade, pass the API token as a URL parameter - the same token used to create the invoice in the first place. | `string` | **Mandatory** |
690+
691+
**Headers**
692+
693+
| Fields | Description | Presence |
694+
| --- | --- | :---: |
695+
| X-Accept-Version | Must be set to `2.0.0` for requests to the BitPay API. | **Mandatory** |
696+
| Content-Type | must be set to `application/json` for requests to the BitPay API. | **Mandatory** |
697+
| X-Identity | the hexadecimal public key generated from the client private key. This header is required when using tokens with higher privileges (`merchant` facade). When using standard `merchant` facade token directly from the [BitPay dashboard](https://test.bitpay.com/dashboard/merchant/api-tokens) (with `"Require Authentication"` disabled), this header is not needed. | **Mandatory** |
698+
| X-Signature | header is the ECDSA signature of the full request URL concatenated with the request body, signed with your private key. This header is required when using tokens with higher privileges (`merchant` facade). When using standard `merchant` facade token directly from the [BitPay dashboard](https://test.bitpay.com/dashboard/merchant/api-tokens) (with `"Require Authentication"` disabled), this header is not needed. | **Mandatory** |
699+
700+
701+
To get the generated invoice details, pass the guid with URL parameter
702+
703+
```java
704+
Invoice invoice = new Invoice(100.0, Currency.USD)
705+
Invoice createInvoice = bitpay.createInvoice(invoice);
706+
Invoice getInvoice = bitpay.getInvoiceByGuid(createInvoice.getGuid(), Facade.Merchant, true);
707+
```
708+
709+
675710
## Retrieve invoices filtered by query
676711

677712
Facade **`MERCHANT`**

src/main/java/com/bitpay/sdk/Client.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public Invoice getInvoice(String invoiceId) throws InvoiceQueryException {
301301
}
302302

303303
/**
304-
* Retrieve a BitPay invoice by invoice id using the specified facade. The client must have been previously authorized for the specified facade (the public facade requires no authorization).
304+
* Retrieve a BitPay invoice by invoice id using the specified facade. The client must have been previously authorized for the specified facade.
305305
*
306306
* @param invoiceId The id of the invoice to retrieve.
307307
* @param facade The facade used to create it.
@@ -329,6 +329,35 @@ public Invoice getInvoice(String invoiceId, String facade, Boolean signRequest)
329329

330330
return invoice;
331331
}
332+
333+
/**
334+
* Retrieve a BitPay invoice by guid using the specified facade. The client must have been previously authorized for the specified facade.
335+
*
336+
* @param guid The guid of the invoice to retrieve.
337+
* @param facade The facade used to create it.
338+
* @param signRequest Signed request.
339+
* @return A BitPay Invoice object.
340+
* @throws BitPayException BitPayException class
341+
* @throws InvoiceQueryException InvoiceQueryException class
342+
*/
343+
public Invoice getInvoiceByGuid(String guid, String facade, Boolean signRequest) throws BitPayException, InvoiceQueryException {
344+
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
345+
params.add(new BasicNameValuePair("token", this.getAccessToken(facade)));
346+
Invoice invoice;
347+
348+
try {
349+
HttpResponse response = this.get("invoices/guid/" + guid, params, signRequest);
350+
invoice = new ObjectMapper().readValue(this.responseToJsonString(response), Invoice.class);
351+
} catch (BitPayException ex) {
352+
throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase());
353+
} catch (JsonProcessingException e) {
354+
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
355+
} catch (Exception e) {
356+
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
357+
}
358+
359+
return invoice;
360+
}
332361

333362
/**
334363
* Retrieve a collection of BitPay invoices.

src/test/java/test/BitPayTestMerchant.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,22 @@ public void testShouldGetInvoice() {
263263
}
264264
assertEquals(invoice.getId(), retreivedInvoice.getId());
265265
}
266+
267+
@Test
268+
public void testShouldGetInvoiceByGuid() {
269+
Invoice invoice = new Invoice(27.50, "USD");
270+
Invoice retreivedInvoice = null;
271+
try {
272+
invoice = this.bitpay.createInvoice(invoice);
273+
//
274+
// Must use a merchant token to retrieve this invoice since it was not created on the public facade.
275+
retreivedInvoice = this.bitpay.getInvoiceByGuid(invoice.getGuid(), Facade.Merchant, true);
276+
} catch (Exception e) {
277+
e.printStackTrace();
278+
fail(e.getMessage());
279+
}
280+
assertEquals(invoice.getId(), retreivedInvoice.getId());
281+
}
266282

267283
@Test
268284
public void testShouldCreateInvoiceWithAdditionalParams() {

0 commit comments

Comments
 (0)