Skip to content

Commit 114c42e

Browse files
registerForSalesInvoiceId
1 parent ceba91e commit 114c42e

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sandorian\Moneybird\Api\SalesInvoices\Payments;
6+
7+
use Saloon\Contracts\Body\HasBody;
8+
use Saloon\Traits\Body\HasJsonBody;
9+
use Sandorian\Moneybird\Api\Support\BaseJsonPostRequest;
10+
11+
class RegisterSalesInvoicePaymentRequest extends BaseJsonPostRequest implements HasBody
12+
{
13+
use HasJsonBody;
14+
15+
public function __construct(
16+
protected readonly string $salesInvoiceId,
17+
protected readonly array $data,
18+
) {
19+
//
20+
}
21+
22+
public function resolveEndpoint(): string
23+
{
24+
return 'sales_invoices/'.$this->salesInvoiceId.'/payments';
25+
}
26+
27+
public function defaultBody(): array
28+
{
29+
return ['payment' => $this->data];
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sandorian\Moneybird\Api\SalesInvoices\Payments;
6+
7+
use Sandorian\Moneybird\Api\Support\BaseDto;
8+
9+
class SalesInvoicePayment extends BaseDto
10+
{
11+
public string $id;
12+
public string $administration_id;
13+
public string $invoice_id;
14+
public string $payment_date;
15+
public string $price;
16+
public string $price_base;
17+
public string $payment_method;
18+
public string $created_at;
19+
public string $updated_at;
20+
21+
public static function createFromResponseData(array $data): static
22+
{
23+
$instance = new static();
24+
foreach ($data as $key => $value) {
25+
if (property_exists($instance, $key)) {
26+
$instance->{$key} = $value;
27+
}
28+
}
29+
return $instance;
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sandorian\Moneybird\Api\SalesInvoices\Payments;
6+
7+
use Sandorian\Moneybird\Api\Support\BaseEndpoint;
8+
9+
class SalesInvoicePaymentsEndpoint extends BaseEndpoint
10+
{
11+
public function registerForSalesInvoiceId(string $salesInvoiceId, array $data): SalesInvoicePayment
12+
{
13+
$request = new RegisterSalesInvoicePaymentRequest($salesInvoiceId, $data);
14+
$response = $this->client->send($request);
15+
16+
return SalesInvoicePayment::createFromResponseData($response->json());
17+
}
18+
}

src/Api/SalesInvoices/SalesInvoicesEndpoint.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sandorian\Moneybird\Api\SalesInvoices;
66

77
use Saloon\PaginationPlugin\Paginator;
8+
use Sandorian\Moneybird\Api\SalesInvoices\Payments\SalesInvoicePaymentsEndpoint;
89
use Sandorian\Moneybird\Api\Support\BaseEndpoint;
910

1011
class SalesInvoicesEndpoint extends BaseEndpoint
@@ -48,4 +49,9 @@ public function paginate(): Paginator
4849

4950
return $this->client->paginate($request);
5051
}
52+
53+
public function payments(): SalesInvoicePaymentsEndpoint
54+
{
55+
return new SalesInvoicePaymentsEndpoint($this->client);
56+
}
5157
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sandorian\Moneybird\Tests\Api\SalesInvoices\Payments;
6+
7+
class SalesInvoicePaymentResponseStub
8+
{
9+
public static function get(): array
10+
{
11+
return [
12+
'id' => '446241800938587779',
13+
'administration_id' => '123456789',
14+
'invoice_id' => '446241800938587778',
15+
'payment_date' => '2025-02-24',
16+
'price' => '129.95',
17+
'price_base' => '129.95',
18+
'payment_method' => 'credit_card',
19+
'created_at' => '2025-02-24T21:56:09Z',
20+
'updated_at' => '2025-02-24T21:56:09Z',
21+
];
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sandorian\Moneybird\Tests\Api\SalesInvoices\Payments;
6+
7+
use Saloon\Http\Faking\MockResponse;
8+
use Sandorian\Moneybird\Api\SalesInvoices\Payments\RegisterSalesInvoicePaymentRequest;
9+
use Sandorian\Moneybird\Api\SalesInvoices\Payments\SalesInvoicePayment;
10+
use Sandorian\Moneybird\Tests\Api\BaseTestCase;
11+
12+
class SalesInvoicePaymentsEndpointTest extends BaseTestCase
13+
{
14+
public function testRegisterPaymentForSalesInvoice(): void
15+
{
16+
$moneybird = $this->getMoneybirdClientWithMocks([
17+
RegisterSalesInvoicePaymentRequest::class => MockResponse::make(SalesInvoicePaymentResponseStub::get(), 201),
18+
]);
19+
20+
$payment = $moneybird->salesInvoices()->payments()->registerForSalesInvoiceId('446241800938587778', [
21+
'payment_date' => '2025-02-24',
22+
'price' => '129.95',
23+
'payment_method' => 'credit_card',
24+
]);
25+
26+
$this->assertInstanceOf(SalesInvoicePayment::class, $payment);
27+
$this->assertEquals('446241800938587779', $payment->id);
28+
$this->assertEquals('446241800938587778', $payment->invoice_id);
29+
$this->assertEquals('2025-02-24', $payment->payment_date);
30+
$this->assertEquals('129.95', $payment->price);
31+
$this->assertEquals('credit_card', $payment->payment_method);
32+
}
33+
}

0 commit comments

Comments
 (0)