forked from microlancer/LightningPayment
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SpecialLightningPayment.php
86 lines (71 loc) · 2.75 KB
/
SpecialLightningPayment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
class SpecialLightningPayment extends SpecialPage {
public function __construct() {
parent::__construct('LightningPayment');
}
public function grantTrusted($wgUserId) {
if (false === $wgUserId > 0) {
throw new \Exception('Bad userId');
}
$user = User::newFromId($wgUserId);
$user->addGroup('trusted');
}
public function execute($subPage) {
global $wgUser;
global $wgLightningPaymentApiKey;
$request = $this->getRequest();
$output = $this->getOutput();
$this->setHeaders();
if ($wgUser->isAnon()) {
$wikitext = 'You need to [[Special:UserLogin|login]] to access this page';
$wikitext .= "\n\n{{LightningPayment|status=nologin}}";
$output->addWikiTextAsInterface($wikitext);
return;
}
$groups = $wgUser->getGroups();
if (array_search('trusted', $groups) !== false) {
$wikitext = 'You are already trusted, thank you!';
$wikitext .= "\n\n{{LightningPayment|status=done}}";
$output->addWikiTextAsInterface($wikitext);
return;
}
$invoiceId = $wgUser->getOption('invoice-id');
$ret = LightningPayment::getInvoice($wgUser->getId(), $invoiceId);
if (!isset($ret['bolt11'])) {
$arrtext = implode(" ",$ret);
$wikitext = 'An error occured, please retry later';
// $output->addWikiText($wikitext);
$wikitext = $arrtext;
$output->addWikiTextAsInterface($wikitext);
return;
}
if ($ret['status'] == 'paid' || $ret['status'] == 1) {
$this->grantTrusted($wgUser->getId());
$wikitext = 'Payment detected! You are now trusted, thank you!';
$wikitext .= "\n\n{{LightningPayment|status=done}}";
$output->addWikiTextAsInterface($wikitext);
return;
}
$wgUser->setOption('invoice-id', $ret['invoiceId']);
$wgUser->saveSettings();
//
$wikitext = <<<EOT
In order to be able to edit pages on this wiki, you will need to send a payment of 1 satoshi to the lightning invoice:
<div style="overflow:wrap;word-break:break-all">{$ret['bolt11']}</div>
<p>
<img src="https://chart.googleapis.com/chart?chs=250x250&cht=qr&chl=lightning:{$ret['bolt11']}">
</p>
<p>
[lightning:{$ret['bolt11']} (open in wallet)]
expires in {$ret['expiry']}
</p>
<p>
Please note that you will need to wait for your transfer to be confirmed.
Once payment has been sent, please refresh this page to check if the payment was detected.
</p>
<p>{{LightningPayment|status=todo|addr={$ret['bolt11']}}}</p>
\n\n
EOT;
$output->addWikiTextAsInterface($wikitext);
}
}