Skip to content

Commit

Permalink
Merge branch 'ppatidar2021-retrieve_invoice_using_guid'
Browse files Browse the repository at this point in the history
  • Loading branch information
nitsujlangston committed Apr 27, 2022
2 parents 42ee19f + d1cf554 commit 670bf99
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
45 changes: 40 additions & 5 deletions docs/invoice.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,32 +646,67 @@ invoice = bitpay.createInvoice(invoice);

`GET /invoices/:invoiceid`

Facade **`POS` `MERCHANT`**
Facade **`MERCHANT`**

### HTTP Request

**URL Parameters**

| Parameter | Description | Type | Presence |
| --- | --- | :---: | :---: |
| ?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** |
| ?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** |

**Headers**

| Fields | Description | Presence |
| --- | --- | :---: |
| X-Accept-Version | Must be set to `2.0.0` for requests to the BitPay API. | **Mandatory** |
| Content-Type | must be set to `application/json` for requests to the BitPay API. | **Mandatory** |
| 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** |
| 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** |
| 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** |
| 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** |


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

```java
Invoice getInvoice = bitpay.getInvoice(invoice.getId());
Invoice invoice = new Invoice(100.0, Currency.USD)
Invoice createInvoice = bitpay.createInvoice(invoice);
Invoice getInvoice = bitpay.getInvoice(createInvoice.getId());
```

## Retrieve an invoice using guid

`GET /invoices/guid/:guid`

Facade **`MERCHANT`**

### HTTP Request

**URL Parameters**

| Parameter | Description | Type | Presence |
| --- | --- | :---: | :---: |
| ?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** |

**Headers**

| Fields | Description | Presence |
| --- | --- | :---: |
| X-Accept-Version | Must be set to `2.0.0` for requests to the BitPay API. | **Mandatory** |
| Content-Type | must be set to `application/json` for requests to the BitPay API. | **Mandatory** |
| 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** |
| 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** |


To get the generated invoice details, pass the guid with URL parameter

```java
Invoice invoice = new Invoice(100.0, Currency.USD)
Invoice createInvoice = bitpay.createInvoice(invoice);
Invoice getInvoice = bitpay.getInvoiceByGuid(createInvoice.getGuid(), Facade.Merchant, true);
```


## Retrieve invoices filtered by query

Facade **`MERCHANT`**
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/com/bitpay/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public Invoice getInvoice(String invoiceId) throws InvoiceQueryException {
}

/**
* 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).
* Retrieve a BitPay invoice by invoice id using the specified facade. The client must have been previously authorized for the specified facade.
*
* @param invoiceId The id of the invoice to retrieve.
* @param facade The facade used to create it.
Expand Down Expand Up @@ -329,6 +329,35 @@ public Invoice getInvoice(String invoiceId, String facade, Boolean signRequest)

return invoice;
}

/**
* Retrieve a BitPay invoice by guid using the specified facade. The client must have been previously authorized for the specified facade.
*
* @param guid The guid of the invoice to retrieve.
* @param facade The facade used to create it.
* @param signRequest Signed request.
* @return A BitPay Invoice object.
* @throws BitPayException BitPayException class
* @throws InvoiceQueryException InvoiceQueryException class
*/
public Invoice getInvoiceByGuid(String guid, String facade, Boolean signRequest) throws BitPayException, InvoiceQueryException {
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("token", this.getAccessToken(facade)));
Invoice invoice;

try {
HttpResponse response = this.get("invoices/guid/" + guid, params, signRequest);
invoice = new ObjectMapper().readValue(this.responseToJsonString(response), Invoice.class);
} catch (BitPayException ex) {
throw new InvoiceQueryException(ex.getStatusCode(), ex.getReasonPhrase());
} catch (JsonProcessingException e) {
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
} catch (Exception e) {
throw new InvoiceQueryException(null, "failed to deserialize BitPay server response (Invoice) : " + e.getMessage());
}

return invoice;
}

/**
* Retrieve a collection of BitPay invoices.
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/test/BitPayTestMerchant.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ public void testShouldGetInvoice() {
}
assertEquals(invoice.getId(), retreivedInvoice.getId());
}

@Test
public void testShouldGetInvoiceByGuid() {
Invoice invoice = new Invoice(27.50, "USD");
Invoice retreivedInvoice = null;
try {
invoice = this.bitpay.createInvoice(invoice);
//
// Must use a merchant token to retrieve this invoice since it was not created on the public facade.
retreivedInvoice = this.bitpay.getInvoiceByGuid(invoice.getGuid(), Facade.Merchant, true);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
assertEquals(invoice.getId(), retreivedInvoice.getId());
}

@Test
public void testShouldCreateInvoiceWithAdditionalParams() {
Expand Down

0 comments on commit 670bf99

Please sign in to comment.