Skip to content

Commit a1ddf72

Browse files
committed
chore: use InvoiceTransfer instead of BackendInvoiceResponse
1 parent 60b477a commit a1ddf72

File tree

14 files changed

+178
-181
lines changed

14 files changed

+178
-181
lines changed

src/Invoice/Application/InvoiceGenerator.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace PhpLightning\Invoice\Application;
66

77
use PhpLightning\Invoice\Domain\BackendInvoice\BackendInvoiceInterface;
8-
use PhpLightning\Shared\Transfer\BackendInvoiceResponse;
8+
9+
use PhpLightning\Shared\Transfer\InvoiceTransfer;
910
use PhpLightning\Shared\Value\SendableRange;
1011

1112
use function sprintf;
@@ -40,18 +41,19 @@ public function generateInvoice(int $milliSats): array
4041
return $this->mapResponseAsArray($invoice);
4142
}
4243

43-
private function mapResponseAsArray(BackendInvoiceResponse $invoice): array
44+
private function mapResponseAsArray(InvoiceTransfer $invoice): array
4445
{
4546
return [
46-
'pr' => $invoice->getPaymentRequest(),
47-
'status' => $invoice->getStatus(),
47+
'bolt11' => $invoice->bolt11,
48+
'status' => $invoice->status,
49+
'memo' => $invoice->memo,
4850
'successAction' => [
4951
'tag' => 'message',
5052
'message' => $this->successMessage,
5153
],
5254
'routes' => [],
5355
'disposable' => false,
54-
'reason' => $invoice->getReason(),
56+
'error' => $invoice->error,
5557
];
5658
}
5759
}

src/Invoice/Domain/BackendInvoice/BackendInvoiceInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace PhpLightning\Invoice\Domain\BackendInvoice;
66

7-
use PhpLightning\Shared\Transfer\BackendInvoiceResponse;
7+
use PhpLightning\Shared\Transfer\InvoiceTransfer;
88

99
interface BackendInvoiceInterface
1010
{
11-
public function requestInvoice(int $satsAmount, string $metadata): BackendInvoiceResponse;
11+
public function requestInvoice(int $satsAmount, string $metadata): InvoiceTransfer;
1212
}

src/Invoice/Domain/BackendInvoice/EmptyBackendInvoice.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44

55
namespace PhpLightning\Invoice\Domain\BackendInvoice;
66

7-
use PhpLightning\Shared\Transfer\BackendInvoiceResponse;
7+
use PhpLightning\Shared\Transfer\InvoiceTransfer;
88

99
final readonly class EmptyBackendInvoice implements BackendInvoiceInterface
1010
{
1111
public function __construct(private string $name)
1212
{
1313
}
1414

15-
public function requestInvoice(int $satsAmount, string $metadata): BackendInvoiceResponse
15+
public function requestInvoice(int $satsAmount, string $metadata): InvoiceTransfer
1616
{
17-
return (new BackendInvoiceResponse())
18-
->setStatus('ERROR')
19-
->setReason('Unknown Backend: ' . $this->name);
17+
return new InvoiceTransfer(status: 'ERROR', error: 'Unknown Backend: ' . $this->name);
2018
}
2119
}

src/Invoice/Domain/BackendInvoice/LnbitsBackendInvoice.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
namespace PhpLightning\Invoice\Domain\BackendInvoice;
66

77
use PhpLightning\Invoice\Domain\Http\HttpApiInterface;
8-
use PhpLightning\Shared\Transfer\BackendInvoiceResponse;
8+
9+
use PhpLightning\Shared\Transfer\InvoiceTransfer;
910

1011
final class LnbitsBackendInvoice implements BackendInvoiceInterface
1112
{
1213
/**
13-
* @param array{api_key:string, api_endpoint:string} $options
14+
* @param array{api_key:string, api_endpoint:string} $options
1415
*/
1516
public function __construct(
1617
private readonly HttpApiInterface $httpApi,
1718
private array $options,
1819
) {
1920
}
2021

21-
public function requestInvoice(int $satsAmount, string $metadata = ''): BackendInvoiceResponse
22+
public function requestInvoice(int $satsAmount, string $metadata = ''): InvoiceTransfer
2223
{
2324
$endpoint = $this->options['api_endpoint'] . '/api/v1/payments';
2425

@@ -39,18 +40,9 @@ public function requestInvoice(int $satsAmount, string $metadata = ''): BackendI
3940
);
4041

4142
if ($response === null) {
42-
return (new BackendInvoiceResponse())
43-
->setStatus('ERROR')
44-
->setPaymentRequest('Backend "LnBits" unreachable');
45-
}
46-
47-
if (!isset($response['payment_request'])) {
48-
return (new BackendInvoiceResponse())
49-
->setStatus('ERROR')
50-
->setPaymentRequest('No payment_request found');
43+
return new InvoiceTransfer(status: 'ERROR', error: 'Backend "LnBits" unreachable');
5144
}
5245

53-
return (new BackendInvoiceResponse())
54-
->setPaymentRequest($response['payment_request']);
46+
return InvoiceTransfer::fromArray($response);
5547
}
5648
}

src/Invoice/Domain/Http/HttpApiInterface.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,33 @@
77
interface HttpApiInterface
88
{
99
/**
10-
* @param array<string,string> $headers
10+
* @param array<string, string> $headers
1111
*
12-
* @return ?array{
13-
* payment_hash?: string,
14-
* payment_request?: string,
15-
* checking_id?: string,
16-
* lnurl_response: ?string,
17-
* } null if the endpoint is unreachable
12+
* @return array{
13+
* checking_id: string,
14+
* payment_hash: string,
15+
* wallet_id: string,
16+
* amount: int,
17+
* fee: int,
18+
* bolt11: string,
19+
* status: string,
20+
* memo: string,
21+
* expiry: string|null,
22+
* webhook: string|null,
23+
* webhook_status: string|null,
24+
* preimage: string|null,
25+
* tag: string|null,
26+
* extension: string|null,
27+
* time: string,
28+
* created_at: string,
29+
* updated_at: string,
30+
* extra: array{
31+
* wallet_fiat_currency: string,
32+
* wallet_fiat_amount: float,
33+
* wallet_fiat_rate: float,
34+
* wallet_btc_rate: float
35+
* }
36+
* }|null
1837
*/
1938
public function postRequestInvoice(string $uri, string $body, array $headers = []): ?array;
2039
}

src/Invoice/Infrastructure/Controller/InvoiceController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __invoke(string $username = ''): JsonResponse
4040
$this->getFacade()->generateInvoice($username, $amount),
4141
);
4242
} catch (Throwable $e) {
43+
dump($e);
4344
return new JsonResponse([
4445
'status' => 'ERROR',
4546
'message' => $e->getMessage(),

src/Shared/Transfer/BackendInvoiceResponse.php

Lines changed: 0 additions & 100 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLightning\Shared\Transfer;
6+
7+
final class InvoiceExtraTransfer
8+
{
9+
public function __construct(
10+
public string $walletFiatCurrency,
11+
public float $walletFiatAmount,
12+
public float $walletFiatRate,
13+
public float $walletBtcRate,
14+
) {
15+
}
16+
17+
public static function fromArray(array $array): self
18+
{
19+
return new self(
20+
walletFiatCurrency: (string) $array['wallet_fiat_currency'],
21+
walletFiatAmount: (float) $array['wallet_fiat_amount'],
22+
walletFiatRate: (float) $array['wallet_fiat_rate'],
23+
walletBtcRate: (float) $array['wallet_btc_rate'],
24+
);
25+
}
26+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLightning\Shared\Transfer;
6+
7+
use function array_key_exists;
8+
use function is_array;
9+
10+
final class InvoiceTransfer
11+
{
12+
public function __construct(
13+
public string $checkingId = '',
14+
public string $paymentHash = '',
15+
public string $walletId = '',
16+
public int $amount = 0,
17+
public int $fee = 0,
18+
public string $bolt11 = '',
19+
public string $status = 'OK',
20+
public string $memo = '',
21+
public ?string $expiry = null,
22+
public ?string $webhook = null,
23+
public ?string $webhookStatus = null,
24+
public ?string $preimage = null,
25+
public ?string $tag = null,
26+
public ?string $extension = null,
27+
public string $time = '',
28+
public string $createdAt = '',
29+
public string $updatedAt = '',
30+
public ?string $error = null,
31+
public ?InvoiceExtraTransfer $extra = null,
32+
) {
33+
}
34+
35+
public static function fromArray(array $array): self
36+
{
37+
return new self(
38+
checkingId: (string) ($array['checking_id'] ?? ''),
39+
paymentHash: (string) ($array['payment_hash'] ?? ''),
40+
walletId: (string) ($array['wallet_id'] ?? ''),
41+
amount: (int) ($array['amount'] ?? 0),
42+
fee: (int) ($array['fee'] ?? 0),
43+
bolt11: (string) ($array['bolt11'] ?? ''),
44+
status: (string) ($array['status'] ?? ''),
45+
memo: (string) ($array['memo'] ?? ''),
46+
expiry: array_key_exists('expiry', $array) ? (string) $array['expiry'] : null,
47+
webhook: array_key_exists('webhook', $array) ? (string) $array['webhook'] : null,
48+
webhookStatus: array_key_exists('webhook_status', $array) ? (string) $array['webhook_status'] : null,
49+
preimage: array_key_exists('preimage', $array) ? (string) $array['preimage'] : null,
50+
tag: array_key_exists('tag', $array) ? (string) $array['tag'] : null,
51+
extension: array_key_exists('extension', $array) ? (string) $array['extension'] : null,
52+
time: (string) ($array['time'] ?? ''),
53+
createdAt: (string) ($array['created_at'] ?? ''),
54+
updatedAt: (string) ($array['updated_at'] ?? ''),
55+
extra: isset($array['extra']) && is_array($array['extra'])
56+
? InvoiceExtraTransfer::fromArray($array['extra'])
57+
: null,
58+
);
59+
}
60+
}

tests/Feature/Fake/FakeHttpApi.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,29 @@ final class FakeHttpApi implements HttpApiInterface
1111
public function postRequestInvoice(string $uri, string $body, array $headers = []): ?array
1212
{
1313
return [
14-
'payment_hash' => 'fake payment_hash',
15-
'payment_request' => 'lnbc10u1pjzh489...fake payment_request',
16-
'checking_id' => 'fake checking_id',
17-
'lnurl_response' => 'fake lnurl_response',
14+
'checking_id' => '8efa8998bca6e298ae63dc7425c8a34b5373511d88a70f6f29ba98f833a63f04',
15+
'payment_hash' => '8efa8998bca6e298ae63dc7425c8a34b5373511d88a70f6f29ba98f833a63f04',
16+
'wallet_id' => 'd73709a1301146b7ae0c748e3a0ecef2',
17+
'amount' => 1000000,
18+
'fee' => 0,
19+
'bolt11' => 'lnbc10u1p5r9lmwpp53magnx9u5m3f3tnrm36ztj9rfdfhx5ga3zns7mefh2v0svax8uzqcqzyssp54twf429a8cvz6tflw5lt705gfnvuykhdeewey009tugjcuamt38q9q7sqqqqqqqqqqqqqqqqqqqsqqqqqysgqdqqmqz9gxqrrssrzjqwryaup9lh50kkranzgcdnn2fgvx390wgj5jd07rwr3vxeje0glclll4ttz7sp6kpvqqqqlgqqqqqeqqjq0uu89sejjllry5ye43x0v42jn48c6alfc9mfnjla2u6kmwy444pzrjmtu25nk2shshuh2mrqtehygmzya9xg89ppszuuhd9296vvcxspkpwc68',
20+
'status' => 'pending',
21+
'memo' => '',
22+
'expiry' => '2025-05-25T12:30:54',
23+
'webhook' => null,
24+
'webhook_status' => null,
25+
'preimage' => null,
26+
'tag' => null,
27+
'extension' => null,
28+
'time' => '2025-05-25T11:30:54.433442+00:00',
29+
'created_at' => '2025-05-25T11:30:54.433447+00:00',
30+
'updated_at' => '2025-05-25T11:30:54.433449+00:00',
31+
'extra' => [
32+
'wallet_fiat_currency' => 'USD',
33+
'wallet_fiat_amount' => 1.071,
34+
'wallet_fiat_rate' => 933.31818946794,
35+
'wallet_btc_rate' => 107144.595625,
36+
],
1837
];
1938
}
2039
}

0 commit comments

Comments
 (0)