Skip to content

Commit

Permalink
add sales invoices endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Dec 10, 2024
1 parent 526b270 commit 8f31fbd
Show file tree
Hide file tree
Showing 36 changed files with 1,676 additions and 2 deletions.
66 changes: 66 additions & 0 deletions docs/endpoint-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,72 @@ $refund = $mollie->refunds->createForPayment($paymentId, [
$refunds = $mollie->refunds->page();
```

## Sales Invoices

[Official Documentation TBA]

**Available Payloads:**
- `CreateSalesInvoicePayload` - For creating sales invoices
- `UpdateSalesInvoicePayload` - For updating existing sales invoices

**Available Queries:**
- `GetPaginatedSalesInvoiceQuery` - For listing sales invoices with pagination

### Sales Invoice Management

```php
use Mollie\Api\Types\VatMode;
use Mollie\Api\Types\VatScheme;
use Mollie\Api\Types\PaymentTerm;
use Mollie\Api\Types\RecipientType;
use Mollie\Api\Types\RecipientType;
use Mollie\Api\Types\SalesInvoiceStatus;

// Create a sales invoice
$salesInvoice = $mollie->salesInvoices->create([
'currency' => 'EUR',
'status' => SalesInvoiceStatus::DRAFT,
'vatScheme' => VatScheme::STANDARD,
'vatMode' => VatMode::INCLUSIVE,
'paymentTerm' => PaymentTerm::DAYS_30,
'recipientIdentifier' => 'XXXXX',
'recipient' => [
'type' => RecipientType::CONSUMER,
'email' => '[email protected]',
'streetAndNumber' => 'Sample Street 12b',
'postalCode' => '2000 AA',
'city' => 'Amsterdam',
'country' => 'NL',
'locale' => 'nl_NL'
],
'lines' => [
[
'description' => 'Monthly subscription fee',
'quantity' => 1,
'vatRate' => '21',
'unitPrice' => [
'currency' => 'EUR',
'value' => '10,00'
]
]
]
]);

// Get a sales invoice
$salesInvoice = $mollie->salesInvoices->get('invoice_12345');

// Update a sales invoice
$salesInvoice = $mollie->salesInvoices->update('invoice_12345', [
'description' => 'Updated description'
]);

// Delete a sales invoice
$mollie->salesInvoices->delete('invoice_12345');

// List sales invoices
$salesInvoices = $mollie->salesInvoices->page();
```

## Sessions

[Official Documentation](https://docs.mollie.com/reference/v2/sessions-api/create-session)
Expand Down
109 changes: 109 additions & 0 deletions src/EndpointCollection/SalesInvoiceEndpointCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Mollie\Api\EndpointCollection;

use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\Factories\CreateSalesInvoicePayloadFactory;
use Mollie\Api\Factories\PaginatedQueryFactory;
use Mollie\Api\Factories\UpdateSalesInvoicePayloadFactory;
use Mollie\Api\Http\Payload\CreateSalesInvoicePayload;
use Mollie\Api\Http\Payload\UpdateSalesInvoicePayload;
use Mollie\Api\Http\Requests\CreateSalesInvoiceRequest;
use Mollie\Api\Http\Requests\DeleteSalesInvoiceRequest;
use Mollie\Api\Http\Requests\GetPaginatedSalesInvoicesRequest;
use Mollie\Api\Http\Requests\GetSalesInvoiceRequest;
use Mollie\Api\Http\Requests\UpdateSalesInvoiceRequest;
use Mollie\Api\Resources\LazyCollection;
use Mollie\Api\Resources\SalesInvoice;
use Mollie\Api\Resources\SalesInvoiceCollection;

class SalesInvoiceEndpointCollection extends EndpointCollection
{
/**
* Retrieve a SalesInvoice from Mollie.
*
* @throws ApiException
*/
public function get(string $id): SalesInvoice
{
return $this->send(new GetSalesInvoiceRequest($id));
}

/**
* Creates a SalesInvoice in Mollie.
*
* @param array|CreateSalesInvoicePayload $payload
*
* @throws ApiException
*/
public function create($payload = []): SalesInvoice
{
if (! $payload instanceof CreateSalesInvoicePayload) {
$payload = CreateSalesInvoicePayloadFactory::new($payload)->create();
}

return $this->send(new CreateSalesInvoiceRequest($payload));
}

/**
* Update a specific SalesInvoice resource.
*
* @throws ApiException
*/
public function update(string $id, $payload = []): ?SalesInvoice
{
if (! $payload instanceof UpdateSalesInvoicePayload) {
$payload = UpdateSalesInvoicePayloadFactory::new($payload)->create();
}

return $this->send(new UpdateSalesInvoiceRequest($id, $payload));
}

/**
* Delete a SalesInvoice from Mollie.
*
* @throws ApiException
*/
public function delete(string $id): void
{
$this->send(new DeleteSalesInvoiceRequest($id));
}

/**
* Retrieves a collection of SalesInvoices from Mollie.
*
* @throws ApiException
*/
public function page(?string $from = null, ?int $limit = null): SalesInvoiceCollection
{
$query = PaginatedQueryFactory::new([
'from' => $from,
'limit' => $limit,
])->create();

return $this->send(new GetPaginatedSalesInvoicesRequest($query));
}

/**
* Create an iterator for iterating over sales invoices retrieved from Mollie.
*
* @param string|null $from The first resource ID you want to include in your list.
* @param bool $iterateBackwards Set to true for reverse order iteration (default is false).
*/
public function iterator(
?string $from = null,
?int $limit = null,
bool $iterateBackwards = false
): LazyCollection {
$query = PaginatedQueryFactory::new([
'from' => $from,
'limit' => $limit,
])->create();

return $this->send(
(new GetPaginatedSalesInvoicesRequest($query))
->useIterator()
->setIterationDirection($iterateBackwards)
);
}
}
40 changes: 40 additions & 0 deletions src/Factories/CreateSalesInvoicePayloadFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Mollie\Api\Factories;

use Mollie\Api\Http\Payload\CreateSalesInvoicePayload;
use Mollie\Api\Http\Payload\PaymentDetails;
use Mollie\Api\Http\Payload\EmailDetails;
use Mollie\Api\Http\Payload\Discount;

class CreateSalesInvoicePayloadFactory extends Factory
{
/**
* Create a new CreateSalesInvoicePayload instance.
*
* @return CreateSalesInvoicePayload
*/
public function create(): CreateSalesInvoicePayload
{
return new CreateSalesInvoicePayload(
$this->get('currency'),
$this->get('status'),
$this->get('vatScheme'),
$this->get('vatMode'),
$this->get('paymentTerm'),
$this->get('recipientIdentifier'),
RecipientFactory::new($this->get('recipient'))->create(),
$this
->mapIfNotNull(
'lines',
fn(array $items) => InvoiceLineCollectionFactory::new($items)->create()
),
$this->get('profileId'),
$this->get('memo'),
$this->mapIfNotNull('paymentDetails', fn(array $data) => PaymentDetails::fromArray($data)),
$this->mapIfNotNull('emailDetails', fn(array $data) => EmailDetails::fromArray($data)),
$this->get('webhookUrl'),
$this->mapIfNotNull('discount', fn(array $data) => Discount::fromArray($data))
);
}
}
16 changes: 16 additions & 0 deletions src/Factories/InvoiceLineCollectionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Mollie\Api\Factories;

use Mollie\Api\Http\Payload\DataCollection;

class InvoiceLineCollectionFactory extends Factory
{
public function create(): DataCollection
{
return new DataCollection(array_map(
fn(array $item) => InvoiceLineFactory::new($item)->create(),
$this->data
));
}
}
20 changes: 20 additions & 0 deletions src/Factories/InvoiceLineFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Mollie\Api\Factories;

use Mollie\Api\Http\Payload\Discount;
use Mollie\Api\Http\Payload\InvoiceLine;

class InvoiceLineFactory extends Factory
{
public function create(): InvoiceLine
{
return new InvoiceLine(
$this->get('description'),
$this->get('quantity'),
$this->get('vatRate'),
MoneyFactory::new($this->get('unitPrice'))->create(),
$this->mapIfNotNull('discount', fn(array $data) => Discount::fromArray($data))
);
}
}
30 changes: 30 additions & 0 deletions src/Factories/RecipientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Mollie\Api\Factories;

use Mollie\Api\Http\Payload\Recipient;

class RecipientFactory extends Factory
{
public function create(): Recipient
{
return new Recipient(
$this->get('type'),
$this->get('email'),
$this->get('streetAndNumber'),
$this->get('postalCode'),
$this->get('city'),
$this->get('country'),
$this->get('locale'),
$this->get('title'),
$this->get('givenName'),
$this->get('familyName'),
$this->get('organizationName'),
$this->get('organizationNumber'),
$this->get('vatNumber'),
$this->get('phone'),
$this->get('streetAdditional'),
$this->get('region'),
);
}
}
36 changes: 36 additions & 0 deletions src/Factories/UpdateSalesInvoicePayloadFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Mollie\Api\Factories;

use Mollie\Api\Http\Payload\UpdateSalesInvoicePayload;
use Mollie\Api\Http\Payload\PaymentDetails;
use Mollie\Api\Http\Payload\EmailDetails;
use Mollie\Api\Http\Payload\Discount;

class UpdateSalesInvoicePayloadFactory extends Factory
{
/**
* Create a new UpdateSalesInvoicePayload instance.
*
* @return UpdateSalesInvoicePayload
*/
public function create(): UpdateSalesInvoicePayload
{
return new UpdateSalesInvoicePayload(
$this->get('status'),
$this->get('recipientIdentifier'),
$this->get('paymentTerm'),
$this->get('memo'),
$this->mapIfNotNull('paymentDetails', fn(array $data) => PaymentDetails::fromArray($data)),
$this->mapIfNotNull('emailDetails', fn(array $data) => EmailDetails::fromArray($data)),
$this->mapIfNotNull('recipient', fn(array $data) => RecipientFactory::new($data)->create()),
$this
->mapIfNotNull(
'lines',
fn(array $items) => InvoiceLineCollectionFactory::new($items)->create()
),
$this->get('webhookUrl'),
$this->mapIfNotNull('discount', fn(array $data) => Discount::fromArray($data))
);
}
}
Loading

0 comments on commit 8f31fbd

Please sign in to comment.