1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpLightning\Invoice\Domain\BackendInvoice; |
6
|
|
|
|
7
|
|
|
use PhpLightning\Invoice\Domain\Http\HttpApiInterface; |
8
|
|
|
use PhpLightning\Shared\Transfer\BackendInvoiceResponse; |
9
|
|
|
|
10
|
|
|
final class LnbitsBackendInvoice implements BackendInvoiceInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param array{api_key:string, api_endpoint:string} $options |
14
|
|
|
*/ |
15
|
4 |
|
public function __construct( |
16
|
|
|
private HttpApiInterface $httpApi, |
17
|
|
|
private array $options, |
18
|
|
|
) { |
19
|
4 |
|
} |
20
|
|
|
|
21
|
4 |
|
public function requestInvoice(int $satsAmount, string $metadata = ''): BackendInvoiceResponse |
22
|
|
|
{ |
23
|
4 |
|
$endpoint = $this->options['api_endpoint'] . '/api/v1/payments'; |
24
|
|
|
|
25
|
4 |
|
$content = [ |
26
|
4 |
|
'out' => false, |
27
|
4 |
|
'amount' => $satsAmount, |
28
|
4 |
|
'unhashed_description' => bin2hex($metadata), |
29
|
4 |
|
// 'description_hash' => hash('sha256', $metadata), |
30
|
4 |
|
]; |
31
|
|
|
|
32
|
4 |
|
$response = $this->httpApi->postRequestInvoice( |
33
|
4 |
|
$endpoint, |
34
|
4 |
|
body: json_encode($content, JSON_THROW_ON_ERROR), |
35
|
4 |
|
headers: [ |
36
|
4 |
|
'Content-Type' => 'application/json', |
37
|
4 |
|
'X-Api-Key' => $this->options['api_key'], |
38
|
4 |
|
], |
39
|
4 |
|
); |
40
|
|
|
|
41
|
4 |
|
if ($response === null) { |
42
|
1 |
|
return (new BackendInvoiceResponse()) |
43
|
1 |
|
->setStatus('ERROR') |
44
|
1 |
|
->setPaymentRequest('Backend "LnBits" unreachable'); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
if (!isset($response['payment_request'])) { |
48
|
1 |
|
return (new BackendInvoiceResponse()) |
49
|
1 |
|
->setStatus('ERROR') |
50
|
1 |
|
->setPaymentRequest('No payment_request found'); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
return (new BackendInvoiceResponse()) |
54
|
2 |
|
->setPaymentRequest($response['payment_request']); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|