|
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
|
|
|
|
|
10
|
|
|
final class InvoiceGenerator |
|
11
|
|
|
{ |
|
12
|
|
|
public const MESSAGE_PAYMENT_RECEIVED = 'Payment received!'; |
|
13
|
|
|
|
|
14
|
|
|
/** @var int 100 Minimum in msat (sat/1000) */ |
|
15
|
|
|
public const MIN_SENDABLE = 100_000; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** @var int 10 000 000 Max in msat (sat/1000) */ |
|
18
|
|
|
public const MAX_SENDABLE = 10_000_000_000; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
3 |
|
public function __construct( |
|
21
|
|
|
private BackendInvoiceInterface $backendInvoice, |
|
22
|
|
|
private HttpFacadeInterface $httpFacade, |
|
23
|
|
|
private string $lnAddress, |
|
24
|
|
|
) { |
|
25
|
3 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $imageFile The picture you want to display, if you don't want to show a picture, leave an empty string. |
|
29
|
|
|
* Beware that a heavy picture will make the wallet fails to execute lightning address process! 136536 bytes maximum for base64 encoded picture data |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function generateInvoice( |
|
32
|
|
|
int $amount, |
|
33
|
|
|
string $imageFile = '', |
|
34
|
|
|
): array { |
|
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
|
3 |
|
$description = 'Pay to ' . $this->lnAddress; |
|
39
|
|
|
|
|
40
|
3 |
|
$imageMetadata = $this->generateImageMetadata($imageFile); |
|
41
|
3 |
|
$metadata = '[["text/plain","' . $description . '"],["text/identifier","' . $this->lnAddress . '"]' . $imageMetadata . ']'; |
|
42
|
|
|
|
|
43
|
3 |
|
if (!$this->isValidAmount($amount)) { |
|
44
|
1 |
|
return [ |
|
45
|
1 |
|
'status' => 'ERROR', |
|
46
|
1 |
|
'reason' => 'Amount is not between minimum and maximum sendable amount', |
|
47
|
1 |
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
$invoice = $this->backendInvoice->requestInvoice($amount / 1000, $metadata); |
|
51
|
|
|
|
|
52
|
2 |
|
if ($invoice['status'] === 'OK') { |
|
53
|
1 |
|
return $this->okResponse($invoice); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $this->errorResponse($invoice); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
private function generateImageMetadata(string $imageFile): string |
|
60
|
|
|
{ |
|
61
|
3 |
|
if ($imageFile === '') { |
|
62
|
|
|
return ''; |
|
63
|
|
|
} |
|
64
|
3 |
|
$response = $this->httpFacade->get($imageFile); |
|
65
|
3 |
|
if ($response === null) { |
|
66
|
2 |
|
return ''; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
return ',["image/jpeg;base64","' . base64_encode($response) . '"]'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
private function isValidAmount(int $amount): bool |
|
73
|
|
|
{ |
|
74
|
3 |
|
return $amount >= self::MIN_SENDABLE |
|
75
|
3 |
|
&& $amount <= self::MAX_SENDABLE; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
private function okResponse(array $invoice): array |
|
79
|
|
|
{ |
|
80
|
1 |
|
return [ |
|
81
|
1 |
|
'pr' => $invoice['pr'], |
|
82
|
1 |
|
'status' => 'OK', |
|
83
|
1 |
|
'successAction' => [ |
|
84
|
1 |
|
'tag' => 'message', |
|
85
|
1 |
|
'message' => self::MESSAGE_PAYMENT_RECEIVED, |
|
86
|
1 |
|
], |
|
87
|
1 |
|
'routes' => [], |
|
88
|
1 |
|
'disposable' => false, |
|
89
|
1 |
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
private function errorResponse(array $invoice): array |
|
93
|
|
|
{ |
|
94
|
1 |
|
return [ |
|
95
|
1 |
|
'status' => $invoice['status'], |
|
96
|
1 |
|
'reason' => $invoice['reason'], |
|
97
|
1 |
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|