Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
.vscode
node_modules
47 changes: 37 additions & 10 deletions client/app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
let currentInvoice = null;

document.getElementById('pay-button').onclick = async () => {
const amount = document.getElementById('amount').value;
if (!amount) return alert("Enter an amount!");
const amount = document.getElementById('amount').value;
if (!amount) return alert("Enter an amount!");

const res = await fetch('http://localhost:3000/invoices/create', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ amount })
});

const invoice = await res.json();
currentInvoice = invoice;

const qrURL = `https://api.qrserver.com/v1/create-qr-code/?data=lightning:${invoice.payment_request}`;
document.getElementById('qrcode').innerHTML = `<img src="${qrURL}" alt="Invoice QR Code">`;

// Show payment section
document.querySelector('.payment-section').style.display = 'block';
document.getElementById('payment-status').textContent = '';
};

document.getElementById('pay-invoice-button').onclick = async () => {
if (!currentInvoice) return alert("No invoice to pay.");

const res = await fetch('http://localhost:3000/create-invoice', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ amount })
});
const res = await fetch('http://localhost:3000/invoices/pay', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ payment_request: currentInvoice.payment_request })
});

const invoice = await res.json();
const qrURL = `https://api.qrserver.com/v1/create-qr-code/?data=lightning:${invoice.payment_request}`;
const result = await res.json();

document.getElementById('qrcode').innerHTML = `<img src="${qrURL}" alt="Invoice QR Code">`;
if (res.ok) {
document.getElementById('payment-status').textContent =
`Payment successful: ${result.amount} sats`;
} else {
document.getElementById('payment-status').textContent =
`Payment failed: ${result.details || result.error}`;
}
};
22 changes: 16 additions & 6 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@
<head>
<meta charset="UTF-8">
<title>Lightning Payment App</title>
<link rel="stylesheet" href="styles.css">
</head>

<body>
<h1>Pay with Lightning!</h1>
<input type="number" id="amount" placeholder="Amount in sats" />
<button id="pay-button">Generate Invoice</button>
<div id="qrcode"></div>
<div class="container">
<h1>Pay with Lightning!</h1>
<input type="number" id="amount" placeholder="Amount in sats" />
<button id="pay-button">Generate Invoice</button>

<script src="app.js"></script>
<div id="qrcode"></div>

<!-- Payment section -->
<div class="payment-section" style="display: none;">
<h3>Send payment:</h3>
<button id="pay-invoice-button">Pay</button>
<div id="payment-status"></div>
</div>

<script src="app.js"></script>
</body>

</html>
</html>
15 changes: 15 additions & 0 deletions client/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
body {
font-family: Arial, sans-serif;
background-color: #f4f7fa;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}

h1 {
color: #333;
}
15 changes: 0 additions & 15 deletions package.json

This file was deleted.

37 changes: 20 additions & 17 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
const express = require('express');
const { createInvoice, subscribeToInvoices } = require('./invoice');
const {connectToMongoDB} = require('./src/database/db');
const cors = require('cors');
require('dotenv').config();
const bodyParser = require('body-parser');
const invoiceRouter = require('./src/routes/invoice.route');
const { initSocket } = require('./src/services/socket');
const { subscribeToInvoices } = require('./src/controllers/invoice.controller');
require('dotenv').config({ path: '../.env' });
require('./src/jobs/expireInvoices');

const app = express();
const PORT = 3000;

// Connect to MongoDB
connectToMongoDB();

app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));

const PORT = 3000;
app.use('/invoices', invoiceRouter);

// Create Invoice endpoint
app.post('/create-invoice', async (req, res) => {
const { amount } = req.body;
try {
const invoice = await createInvoice(amount);
res.json(invoice);
} catch (err) {
console.error(err);
res.status(500).send('Error creating invoice');
}
});

app.listen(PORT, () => {
const server = app.listen(PORT, () => {
console.log(`Server listening at http://localhost:${PORT}`);
subscribeToInvoices(); // start payment listener
});


initSocket(server);

subscribeToInvoices();
30 changes: 0 additions & 30 deletions server/invoice.js

This file was deleted.

Loading