# Invoice Service

If you only require specific functionality from the invoice service, you can import the `InvoiceService` module separately to the main SDK.

For example:

```javascript
const { Invoice } = require('n-membership-sdk');
const invoiceService = new Invoice(config);
```

## Table of Contents

* [Config](#config)
	* [How to obtain a key](#how-to-obtain-a-key)
* [Methods](#methods)
  * [download](#download)
  * [getInvoicesBySubscriptionNumber](#getInvoicesBySubscriptionNumber)
  * [getInvoicesByBalance](#getInvoicesByBalance)
* [Documentation](#documentation)

## Config

* See common configuration options in the [README file](./README.doc)
* `invoiceApiKey`

```javascript
const config = new ProductionConfiguration({
	invoiceApiKey: 'secret',
});
```

### How to obtain a key

Do one of the following:

1. Check in Doppler for your app to see if the API key you need already exists.
2. Contact #membership-support slack channel for a key for the service i.e. invoice-svc.

## Methods

### download

```javascript
const buffer = invoiceService.download(invoiceId, sessionToken);
```

| Parameters  | Required | Type   | Example | Description   |
| ----------- | -------- | -------| --------| ------------- |
| invoiceId | yes | String | string 32 alphanum | id of an invoice. See [getInvoices](INVOICE.md#getInvoices) |
| sessionToken | Yes      | String | zyoRrJB7YEJC...   | This is the FTSession_s token which can be retrieved from [authorisation.login](AUTHORISATION.md#login) |

**Response**:
(promise with buffered binary payload)
pdf file with Content-Type `application/pdf`

### getInvoicesBySubscriptionNumber

```javascript
const invoices = invoiceService.getInvoicesBySubscriptionNumber(subscriptionNumber, sessionToken);
```

| Parameters         | Required | Type   | Example         | Description   |
| -----------        | -------- | -------| --------        | ------------- |
| subscriptionNumber | yes      | String | A-XXXXXXXXXXX   | User's subscription number. |
| sessionToken       | Yes      | String | zyoRrJB7YEJC... | This is the FTSession_s token which can be retrieved from [authorisation.login](AUTHORISATION.md#login) |
| status             | no       | String | Posted          | The required status of the invoices to be returned. Can be one of Posted, Draft or Cancelled |

**Response**:
(promise with the following json payload)

```javascript
{
  items: [
    {
      id: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
      invoiceNumber: 'INV09999999',
      invoiceDate: '2019-09-25',
      dueDate: '2019-09-25',
      amount: {
        currency: 'GBP',
        value: 100,
        symbol: 'GBP'
      },
      amountWithoutTax: {
        currency: 'GBP',
        value: 100,
        symbol: 'GBP'
      },
      taxAmount: {
        currency: 'GBP',
        value: 0,
        symbol: 'GBP'
      },
      paymentAmount: {
        currency: 'GBP',
        value: 100,
        symbol: 'GBP'
      },
      status: 'Posted',
      balance: {
        currency: 'GBP',
        value: 0,
        symbol: 'GBP'
      },
      links: [
        {
          uri: '/invoices/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/invoiceitems',
          rel: 'invoiceItems'
        }
      ]
    }
  ]
}
```

### getInvoicesByBalance

```javascript
const invoices = invoiceService.getInvoicesByBalance(subscriptionNumber, sessionToken, balance);
```

| Parameters         | Required | Type   | Example         | Description   |
| -----------        | -------- | -------| --------        | ------------- |
| subscriptionNumber | yes      | String | A-XXXXXXXXXXX   | User's subscription number. |
| sessionToken       | Yes      | String | zyoRrJB7YEJC... | This is the FTSession_s token which can be retrieved from [authorisation.login](AUTHORISATION.md#login) |
| balance | Yes      | String | Outstanding, NonZero, etc  | These are the possible balance type that we can query for in membership

**Response**:
(promise with the following json payload)

```javascript
{
  items: [
    {
      id: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
      invoiceNumber: 'INV09999999',
      invoiceDate: '2019-09-25',
      dueDate: '2019-09-25',
      amount: {
        currency: 'GBP',
        value: 100,
        symbol: 'GBP'
      },
      amountWithoutTax: {
        currency: 'GBP',
        value: 100,
        symbol: 'GBP'
      },
      taxAmount: {
        currency: 'GBP',
        value: 0,
        symbol: 'GBP'
      },
      paymentAmount: {
        currency: 'GBP',
        value: 100,
        symbol: 'GBP'
      },
      status: 'Posted',
      balance: {
        currency: 'GBP',
        value: 0,
        symbol: 'GBP'
      },
      links: [
        {
          uri: '/invoices/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/invoiceitems',
          rel: 'invoiceItems'
        }
      ]
    }
  ]
}
```

## Documentation

| API Documentation | Confluence  | Description | Owner |
| -------------- | -------- | -------- | -------- |
| [Invoice API](https://invoice-svc-eu-prod.memb.ft.com/swagger#/Invoices%20API/getInvoices_1) |  [Invoice Documentation](https://confluence.ft.com/display/MEMB/Invoice+Service) | Documentation for the invoice service | FT Core - Membership |
