1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpLightning\Invoice\Domain\LnAddress; |
6
|
|
|
|
7
|
|
|
use PhpLightning\Http\HttpFacadeInterface; |
8
|
|
|
use PhpLightning\Invoice\Domain\BackendInvoice\BackendInvoiceInterface; |
9
|
|
|
use PhpLightning\Invoice\Domain\Transfer\SendableRange; |
10
|
|
|
|
11
|
|
|
final class InvoiceGenerator |
12
|
|
|
{ |
13
|
|
|
public const MESSAGE_PAYMENT_RECEIVED = 'Payment received!'; |
14
|
|
|
|
15
|
3 |
|
public function __construct( |
16
|
|
|
private HttpFacadeInterface $httpFacade, |
17
|
|
|
private BackendInvoiceInterface $backendInvoice, |
18
|
|
|
private SendableRange $sendableRange, |
19
|
|
|
private string $lnAddress, |
20
|
|
|
) { |
21
|
3 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $imageFile The picture you want to display, if you don't want to show a picture, leave an empty string. |
25
|
|
|
* Beware that a heavy picture will make the wallet fails to execute lightning address process! 136536 bytes maximum for base64 encoded picture data |
26
|
|
|
*/ |
27
|
3 |
|
public function generateInvoice(int $milliSats, string $imageFile = ''): array |
28
|
|
|
{ |
29
|
3 |
|
if (!$this->sendableRange->contains($milliSats)) { |
30
|
1 |
|
return [ |
31
|
1 |
|
'status' => 'ERROR', |
32
|
1 |
|
'reason' => 'Amount is not between minimum and maximum sendable amount', |
33
|
1 |
|
]; |
34
|
|
|
} |
35
|
|
|
// Modify the description if you want to custom it |
36
|
|
|
// This will be the description on the wallet that pays your ln address |
37
|
|
|
// TODO: Make this customizable from some external configuration file |
38
|
2 |
|
$description = 'Pay to ' . $this->lnAddress; |
39
|
|
|
|
40
|
2 |
|
$imageMetadata = $this->generateImageMetadata($imageFile); |
41
|
2 |
|
$metadata = '[["text/plain","' . $description . '"],["text/identifier","' . $this->lnAddress . '"]' . $imageMetadata . ']'; |
42
|
|
|
|
43
|
2 |
|
$invoice = $this->backendInvoice->requestInvoice($milliSats / 1000, $metadata); |
44
|
|
|
|
45
|
2 |
|
if ($invoice['status'] === 'OK') { |
46
|
1 |
|
return $this->okResponse($invoice); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return $this->errorResponse($invoice); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
private function generateImageMetadata(string $imageFile): string |
53
|
|
|
{ |
54
|
2 |
|
if ($imageFile === '') { |
55
|
|
|
return ''; |
56
|
|
|
} |
57
|
2 |
|
$response = $this->httpFacade->get($imageFile); |
58
|
2 |
|
if ($response === null) { |
59
|
1 |
|
return ''; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return ',["image/jpeg;base64","' . base64_encode($response) . '"]'; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
private function okResponse(array $invoice): array |
66
|
|
|
{ |
67
|
1 |
|
return [ |
68
|
1 |
|
'pr' => $invoice['pr'], |
69
|
1 |
|
'status' => 'OK', |
70
|
1 |
|
'successAction' => [ |
71
|
1 |
|
'tag' => 'message', |
72
|
1 |
|
'message' => self::MESSAGE_PAYMENT_RECEIVED, |
73
|
1 |
|
], |
74
|
1 |
|
'routes' => [], |
75
|
1 |
|
'disposable' => false, |
76
|
1 |
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
private function errorResponse(array $invoice): array |
80
|
|
|
{ |
81
|
1 |
|
return [ |
82
|
1 |
|
'status' => $invoice['status'], |
83
|
1 |
|
'reason' => $invoice['reason'], |
84
|
1 |
|
]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|