Skip to content

Commit cf15c22

Browse files
author
fsociety
committed
feat(payment): add WebLN payment functionality
Implemented functionality to create an invoice and make payment using WebLN. Also added error handling and logging for payment process. Updated UI to display payment information and a button to initiate payment.
1 parent 18032ad commit cf15c22

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

app/Http/Livewire/Test/WebLN.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,34 @@
22

33
namespace App\Http\Livewire\Test;
44

5+
use App\Traits\LNBitsTrait;
56
use Illuminate\Support\Facades\Log;
67
use Livewire\Component;
78

89
class WebLN extends Component
910
{
11+
use LNBitsTrait;
12+
13+
public $invoice;
14+
15+
public function mount()
16+
{
17+
$this->invoice = $this->createInvoice(
18+
sats: 1,
19+
memo: 'Test Payment from WebLN',
20+
lnbits: [
21+
'read_key' => '97f6120563e3498b8be4c67023c912ae',
22+
'url' => 'https://bits.codingarena.top',
23+
]
24+
);
25+
}
26+
27+
public function reloadMe()
28+
{
29+
// full reload current page
30+
return redirect()->route('webln');
31+
}
32+
1033
public function logThis($text)
1134
{
1235
Log::info('WEBLN: ' . $text);

resources/js/webln/webln.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@ import {requestProvider} from "webln";
22

33
export default (livewireComponent) => ({
44

5+
invoice: livewireComponent.entangle('invoice'),
6+
7+
async pay() {
8+
console.log('payment_request: ' + this.invoice.payment_request);
9+
await webln.sendPayment(this.invoice.payment_request)
10+
.then(response => {
11+
console.log('Payment response:', response);
12+
this.$wire.call('logThis', 'Payment response: ' + JSON.stringify(response));
13+
this.$wire.call('reloadMe');
14+
})
15+
.catch(error => {
16+
console.error('Payment failed:', error);
17+
this.$wire.call('logThis', 'Payment failed: ' + error);
18+
this.$wire.call('reloadMe');
19+
});
20+
},
21+
522
async init() {
623
console.log('WebLN initialized');
724

825
let webln;
926
try {
27+
console.log(this.invoice);
28+
1029
webln = await requestProvider();
1130
console.log('WebLN provider acquired');
1231
this.$wire.call('logThis', 'WebLN provider acquired');
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
<div x-data="webln(@this)">
2-
<webview id="webln-webview" src="https://webln-capable-site.com" style="width:100%; height:100%;"></webview>
1+
<div x-data="webln(@this)" class="p-2 sm:p-4" wire:ignore>
2+
<div class="font-mono space-y-1 p-2 sm:p-4 text-white break-words">
3+
<div class="text-xs sm:text-base break-words">Test Payment from WebLN to The Ben</div>
4+
<div class="text-xs sm:text-base break-words">1 sat</div>
5+
<div class="text-xs sm:text-base break-words">hash: {{ $invoice['payment_hash'] }}</div>
6+
<div class="text-xs sm:text-base break-words">payment_request: {{ $invoice['payment_request'] }}</div>
7+
<div class="mt-6">
8+
<div class="flex justify-center">
9+
<button x-on:click="pay" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Pay Invoice</button>
10+
</div>
11+
</div>
12+
</div>
313
</div>

0 commit comments

Comments
 (0)