Skip to content

Commit aae9fe9

Browse files
authored
Merge pull request #23 from php-lightning/codex/add-memo-text-option-to-ln-invoice
Add invoice memo configuration
2 parents fb8201a + 5c92559 commit aae9fe9

File tree

10 files changed

+48
-6
lines changed

10 files changed

+48
-6
lines changed

lightning-config.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
->setReceiver('default-receiver')
1010
->setDescriptionTemplate('Pay to %s')
1111
->setSuccessMessage('Payment received!')
12+
->setInvoiceMemo('')
1213
->setSendableRange(min: 100_000, max: 10_000_000_000)
1314
->setCallbackUrl('localhost:8000/callback')
1415
->addBackendsFile(getcwd() . DIRECTORY_SEPARATOR . 'nostr.json');

src/Config/LightningConfig.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ final class LightningConfig implements JsonSerializable
1818
private ?string $callbackUrl = null;
1919
private ?string $descriptionTemplate = null;
2020
private ?string $successMessage = null;
21+
private ?string $invoiceMemo = null;
2122

2223
public function setDomain(string $domain): self
2324
{
@@ -56,6 +57,12 @@ public function setSuccessMessage(string $message): self
5657
return $this;
5758
}
5859

60+
public function setInvoiceMemo(string $memo): self
61+
{
62+
$this->invoiceMemo = $memo;
63+
return $this;
64+
}
65+
5966
public function addBackendsFile(string $path): self
6067
{
6168
$this->backends ??= new BackendsConfig();
@@ -112,6 +119,9 @@ public function jsonSerialize(): array
112119
if ($this->successMessage !== null) {
113120
$result['success-message'] = $this->successMessage;
114121
}
122+
if ($this->invoiceMemo !== null) {
123+
$result['invoice-memo'] = $this->invoiceMemo;
124+
}
115125

116126
return $result;
117127
}

src/Invoice/Application/InvoiceGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __construct(
1919
private string $lnAddress,
2020
private string $descriptionTemplate,
2121
private string $successMessage,
22+
private string $memo,
2223
) {
2324
}
2425

@@ -36,7 +37,7 @@ public function generateInvoice(int $milliSats): array
3637
$imageMetadata = '';
3738
$metadata = '[["text/plain","' . $description . '"],["text/identifier","' . $this->lnAddress . '"]' . $imageMetadata . ']';
3839

39-
$invoice = $this->backendInvoice->requestInvoice((int)($milliSats / 1000), $metadata);
40+
$invoice = $this->backendInvoice->requestInvoice((int)($milliSats / 1000), $metadata, $this->memo);
4041

4142
return $this->mapResponseAsArray($invoice);
4243
}

src/Invoice/Domain/BackendInvoice/BackendInvoiceInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88

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

src/Invoice/Domain/BackendInvoice/EmptyBackendInvoice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function __construct(private string $name)
1212
{
1313
}
1414

15-
public function requestInvoice(int $satsAmount, string $metadata): InvoiceTransfer
15+
public function requestInvoice(int $satsAmount, string $metadata, string $memo = ''): InvoiceTransfer
1616
{
1717
return new InvoiceTransfer(status: 'ERROR', error: 'Unknown Backend: ' . $this->name);
1818
}

src/Invoice/Domain/BackendInvoice/LnbitsBackendInvoice.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ public function __construct(
1818
) {
1919
}
2020

21-
public function requestInvoice(int $satsAmount, string $metadata = ''): InvoiceTransfer
21+
public function requestInvoice(int $satsAmount, string $metadata = '', string $memo = ''): InvoiceTransfer
2222
{
2323
$endpoint = $this->options['api_endpoint'] . '/api/v1/payments';
2424

2525
$content = [
2626
'out' => false,
2727
'amount' => $satsAmount,
28+
'memo' => $memo,
2829
'unhashed_description' => bin2hex($metadata),
2930
'description_hash' => hash('sha256', $metadata),
3031
];

src/Invoice/InvoiceConfig.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public function getSuccessMessage(): string
6464
return (string)$this->get('success-message', 'Payment received!');
6565
}
6666

67+
public function getInvoiceMemo(): string
68+
{
69+
return (string)$this->get('invoice-memo', '');
70+
}
71+
6772
public function getDomain(): string
6873
{
6974
return (string)$this->get('domain', $_SERVER['HTTP_HOST'] ?? 'localhost');

src/Invoice/InvoiceFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function createInvoiceGenerator(string $username): InvoiceGenerator
4141
$this->getConfig()->getDefaultLnAddress(),
4242
$this->getConfig()->getDescriptionTemplate(),
4343
$this->getConfig()->getSuccessMessage(),
44+
$this->getConfig()->getInvoiceMemo(),
4445
);
4546
}
4647

tests/Unit/Invoice/Domain/BackendInvoice/LnbitsBackendInvoiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function test_request_invoice_when_api_returns_null(): void
2121
'api_key' => 'key',
2222
]);
2323

24-
$actual = $invoice->requestInvoice(100);
24+
$actual = $invoice->requestInvoice(100, '', '');
2525
$expected = new InvoiceTransfer(error: 'Backend "LnBits" unreachable', status: 'ERROR');
2626

2727
self::assertEquals($expected, $actual);
@@ -40,7 +40,7 @@ public function test_request_invoice_when_api_returns_payment_request(): void
4040
'api_key' => 'key',
4141
]);
4242

43-
$actual = $invoice->requestInvoice(100);
43+
$actual = $invoice->requestInvoice(100, '', '');
4444
$expected = new InvoiceTransfer(bolt11: 'ln1234567890');
4545

4646
self::assertEquals($expected, $actual);

tests/Unit/Invoice/Domain/LnAddress/InvoiceGeneratorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function test_invalid_amount(): void
2323
'ln@address',
2424
'Pay to %s',
2525
'Payment received!',
26+
'',
2627
);
2728
$actual = $invoice->generateInvoice(100);
2829

@@ -44,6 +45,7 @@ public function test_unknown_backend(): void
4445
'ln@address',
4546
'Pay to %s',
4647
'Payment received!',
48+
'',
4749
);
4850
$actual = $invoice->generateInvoice(2_000);
4951

@@ -73,6 +75,7 @@ public function test_successful_payment_request_with_amount(): void
7375
'ln@address',
7476
'Pay to %s',
7577
'Payment received!',
78+
'',
7679
);
7780
$actual = $invoice->generateInvoice(2_000);
7881

@@ -89,4 +92,24 @@ public function test_successful_payment_request_with_amount(): void
8992
'error' => null,
9093
], $actual);
9194
}
95+
96+
public function test_passes_memo_to_backend(): void
97+
{
98+
$backend = $this->createMock(BackendInvoiceInterface::class);
99+
$backend->expects(self::once())
100+
->method('requestInvoice')
101+
->with(2, $this->anything(), 'Custom memo')
102+
->willReturn(new InvoiceTransfer());
103+
104+
$invoice = new InvoiceGenerator(
105+
$backend,
106+
SendableRange::withMinMax(1_000, 3_000),
107+
'ln@address',
108+
'Pay to %s',
109+
'Payment received!',
110+
'Custom memo',
111+
);
112+
113+
$invoice->generateInvoice(2_000);
114+
}
92115
}

0 commit comments

Comments
 (0)