> For the complete documentation index, see [llms.txt](https://sapphiredao.gitbook.io/sapphire/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sapphiredao.gitbook.io/sapphire/technical-documentation/smart-contracts/payment-processor.md).

# Payment Processor

## **Payment Processor**

The payment processor Solidity smart contract is the main user interface contract. Most users will interact with the SapphireDao platform via the `PaymentProcessor.sol` contract. It shows invoice creation, management, payments, and escrow functionality on the blockchain.

`PaymentProcessor.sol` grants users access to

* Create invoice
* Payment of invoice
* Accept invoice
* Reject invoice
* Release invoice
* Invoice status

#### Invoice states

This ID increments by 1 for each new invoice created. Using `uint256` allows for a maximum of `2**256 - 1` invoice to be created

```solidity
uint256 public invoiceId;
```

### Create Invoice

```solidity
function createInvoice(uint256 _invoicePrice) external returns (uint256);
```

It is designed to initiate invoice creation. It covers important information, such as the payment amount. It also generates a unique and personalized ID which allows for any further transaction. The `_invoicePrice` must be greater than the SapphireDao fee. The status of this invoice is set to `CREATED`.

#### **Input Parameter**

| Name            | Type      | Description                                             |
| --------------- | --------- | ------------------------------------------------------- |
| `_invoicePrice` | `uint256` | This represents the price of the invoice to be created. |

### Invoice Payment

```solidity
function makeInvoicePayment(uint256 _invoiceId) external payable returns (address);
```

It is designed for processing payment of invoice. It requires the personalized ID generated by the creator. This function uses the [CREATE](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1014.md) opcode to create unique escrow address for this invoice. When this action is successfully carried out, the payment is sent into the escrow wallet, the fee to the `PaymentProcessor` contract and invoice status set to `PAID` . This action can only occur when the invoice has not expired. The

For example Alice creates an invoice, it is impossible for bob to make payment after 180 days of the invoice creation

#### **Input Parameter**

| Name         | Type      | Description                                                          |
| ------------ | --------- | -------------------------------------------------------------------- |
| `_invoiceId` | `uint256` | The personalized ID generated by the creator for payment of invoice. |

### Creators Action

```solidity
function creatorsAction(uint256 _invoiceId, bool _state) external;
```

Permits the creator of the invoice to either reject or accept the invoice after the payment. In a situation the creators decides to accept the invoice, the `holdPeriod` begins to count, and the status of the invoice changes to `ACCEPTED`, otherwise, the payer’s fund is refunded and invoice status is set to `REJECTED`.

#### **Input Parameter**

| Name         | Type      | Description                                                                   |
| ------------ | --------- | ----------------------------------------------------------------------------- |
| `_invoiceId` | `uint256` | The unique invoice ID which the creator will act upon.                        |
| `_state`     | `bool`    | This denotes the creators action either, `True` : Accept or `False` : Reject. |

### Cancel Invoice

```solidity
function cancelInvoice(uint256 _invoiceId) external;
```

Allows the creator to cancel an invoice, if payment hasn’t been made. The invoice can no longer be paid for and the status is set to `CANCELLED`.

#### Input Parameter

| Name         | Type      | Description                             |
| ------------ | --------- | --------------------------------------- |
| `_invoiceId` | `uint256` | The unique ID of the invoice to cancel. |

### Release Invoice

```solidity
function releaseInvoice(uint256 _invoiceId) external;
```

The function `releaseInvoice` enables the release of funds of a particular invoice to the creator, this action can only occur when the condition for release of funds are met. The invoice status is set to `RELEASED`.

#### Input Parameter

| Name         | Type      | Description                                                                        |
| ------------ | --------- | ---------------------------------------------------------------------------------- |
| `_invoiceId` | `uint256` | The unique ID of the invoice whose funds is to be released from the Escrow wallet. |

### Refund Payer

```solidity
   function refundPayerAfterWindow(uint256 _invoiceId) external;
```

Permits the payer to request for refunds from the Escrow wallet. This can only be done after the window for the acceptance or rejection of invoice by creator has been exceeded. The invoice status is set to `REFUNDED`.

For example, Alice creates an invoice and Bob makes a payment within the stipulated period, but Alice neither accept nor reject the invoice within 3 days of the payment, Bob has the permission to request for a refund after this 3-days period.

#### Input Parameter

| Name         | Type      | Description                                  |
| ------------ | --------- | -------------------------------------------- |
| `_invoiceId` | `uint256` | The unique ID of the invoice to be refunded. |

### Privilege Functions

#### Set Fee receivers address

```solidity
function setFeeReceiversAddress(address _newFeeReceiver) external;
```

Updates the address of the fee receiver.

#### Input Parameter

| Name             | Type      | Description                     |
| ---------------- | --------- | ------------------------------- |
| \_newFeeReceiver | `address` | The new address to receive fees |

#### Set invoice Hold period

```solidity
 function setInvoiceHoldPeriod(uint256 _invoiceId, uint32 _holdPeriod) external;
```

Sets a custom hold period for a specific invoice.

#### Input Parameter

| Name         | Type      | Description                     |
| ------------ | --------- | ------------------------------- |
| \_invoiceId  | `uint256` | The ID of the invoice           |
| \_holdPeriod | `uint32`  | The new hold period in seconds. |

#### Set Default Hold Period

```solidity
function setDefaultHoldPeriod(uint256 _newDefaultHoldPeriod) external;
```

Updates the default hold period for all new invoices.

#### Input Parameter

| Name                   | Type      | Description                            |
| ---------------------- | --------- | -------------------------------------- |
| \_newDefaultHoldPeriod | `uint256` | The new default hold period in seconds |

#### Set Fee

```solidity
function setFee(uint256 _newFee) external;
```

Updates the fee for using SapphireDao service.

#### Input Parameter

| Name     | Type      | Description              |
| -------- | --------- | ------------------------ |
| \_newFee | `uint256` | The amount of fee in wei |

## View Methods

### Get Current Invoice ID

```solidity
function getCurrentInvoiceId() external view returns (uint256);
```

The function `getCurrentInvoiceId` provides the Id of the next invoice that will be created. The total amount of invoice created in `getCurrentInvoiceId() - 1` .

| Type      | Description                                  |
| --------- | -------------------------------------------- |
| `uint256` | The current price of the invoice ID counter. |

### Get Invoice Data

```solidity
function getInvoiceData(uint256 _invoiceId) external view returns (Invoice memory);
```

The function `getInvoiceData` retrieves the details for a particular invoice, using the Unique ID of the Invoice.

Returns a struct containing the invoice details.

| Name           | Type      | Description                                                                                                                            |
| -------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `creator`      | `address` | This signifies the address of the person or service provider that created the invoice.                                                 |
| `payer`        | `address` | This signifies the address of the customer paying for the invoice created.                                                             |
| `escrow`       | `address` | The address of the escrow wallet that holds invoice funds.                                                                             |
| `price`        | `uint256` | This is the total price of the invoice to be paid.                                                                                     |
| `amountPayed`  | `uint256` | This tracks the amount of payment made from the total invoice.                                                                         |
| `creationTime` | `uint32`  | The Unix timestamp indicates when the invoice was issued; it specifically includes the time and date.                                  |
| `paymentTime`  | `uint32`  | The Unix timestamp indicates when the payment was made; it specifically includes the time and date.                                    |
| `holdPeriod`   | `uint32`  | This denoted the period for which the funds will be held in the escrow wallet before being released based on agreement by both parties |
| `status`       | `uint32`  | This represents the current status of the invoice; it could be paid, cancelled, or awaiting release.                                   |
|                |           |                                                                                                                                        |

### Get fee

```solidity
function getFee() external view returns (uint256);
```

The function `getfee` retrieves the current fee for using SapphireDao service.

#### Output Parameter

| Type      | Description                      |
| --------- | -------------------------------- |
| `uint256` | The fee for creating an invoice. |

### Get Fee Receiver

```solidity
function getFeeReceiver() external view returns (address);
```

The function `getFeeReciever` retrieves the address of the current fee receiver.

#### Output Parameter

| Type      | Description                            |
| --------- | -------------------------------------- |
| `address` | The address allocated to receive fees. |

### Get Default Hold Period

```solidity
 function getDefaultHoldPeriod() external view returns (uint256);
```

The function `getDefaultHoldPeriod` retrieve the default hold period for the invoices in seconds.

#### Output Parameter

| Type      | Description                                 |
| --------- | ------------------------------------------- |
| `uint256` | The default duration of hold is in seconds. |
