Building a Lightning App (Lapp) - Discussion

Thank you very much, now it’s working :blush: But anyway, Windows does not allow to copy the text from command prompt and I had to copy the key manually… :unamused:

Anyway this is not the problem. The problem is that on the next step I have no way to go on. With the command from GitHub I obtain an error that “BTCPAY_URL is not an accepted command” (See attached screenshot). With the suggested command for Windows in the comments I obtain another more sophisticated error. “Cannot read property ‘fromRed’ of null”. It is clear that the instructions start to be executed, going to file short.js and this file returns a ‘fromRed’ variable that causes the error. See the screenshot. What to do? :thinking:

THIS IS THE ERROR WITH THE SUGGESTED COMMAND LINES

THIS IS THE ERROR WITH THE COMMAND LINES FROM GITHUB

Ok, so I checked the commands on my Windows VM and figured out the issue. Are you using PowerShell?
For executing in power shell you must set the env variables first before executing command. This is how I did it:

$env:BTCPAY_URL='https://lightning.filipmartinsson.com/'
$env:BTCPAY_KEY='KEY'
$env:BTCPAY_PAIRCODE='PAIRCODE'
node -e "const btcpay=require('btcpay'); new btcpay.BTCPayClient(process.env.BTCPAY_URL, btcpay.crypto.load_keypair(Buffer.from(process.env.BTCPAY_KEY, 'hex'))).pair_client(process.env.BTCPAY_PAIRCODE).then(console.log).catch(console.error)"

Let me know if this works.
By the way, you can use the right mouse click to copy and paste in windows shell :slight_smile:

1 Like

Thank you very much. Anyway, it’s unfortunately still not working…

I send you the screenshot. For first three lines I receive a syntax error. For last I receive this message: “The first argument must be of type string or an instance of buffer, Arraybuffer, Arraylike object. Received undefined at function from (buffer.js:313.9) and …”

I am just using the predefinied Windows shell “Prompt of commands”

Can you try using Power Shell instead?

1 Like

Thank you, I used PowerShell (actually also Filip said to use in the lecture, I didn’t get that). I see it works but I cannot see any line giving the result of merchant code (like in the lesson, it should be put on the invoice.js file). I attach the screenshot with the result. Where is my mistake?

![Result|62x500]

You must create a new pair code in the UI, the one you used has expired :slight_smile: I think they only valid for a few hours or so.

1 Like

Now working!! Thank you very very much!!!

1 Like

Asking help once again! In the lecture “Creating Lightening Invoices” I followed step by step the instruction from Filip, also copied literally the code lines he wrote. Anyway I get this error as result: Price must be a float. See attached screenshot. Where is my mistake?

Hey, can you show the code how you set the price parameter in create_invoice function? :slight_smile:

1 Like

//THIS IS THE CODE IN THE INVOICE.JS FILE

var express = require(‘express’);
var router = express.Router();
const BTCPAY_PRIV_KEY = “4c6a32…and so on with the rest of the private key”;
const BTCPAY_MERCHANT_KEY = “BuxgfR…and so on with the rest of the merchant key”;

// Initialize the client
const btcpay = require(‘btcpay’)
const keypair = btcpay.crypto.load_keypair(new Buffer.from(BTCPAY_PRIV_KEY, ‘hex’));
const client = new btcpay.BTCPayClient(‘https://lightning.filipmartinsson.com’, keypair, {merchant: BTCPAY_MERCHANT_KEY})

/* get & verify invoice. */
router.get(’/:id’, async function(req, res, next) {

});

/* Create invoice. */
router.post(’/’, function(req, res, next) {
var dollarAmount = req.body.amount;
//Create invoice
client.create_invoice({price: dollarAmount, currency: “USD”})
.then(function(invoice){
console.log(invoice);
res.render(“invoice”, {invoiceId: invoice.id})
})
.catch(err => console.log(err));
//Display
//What happens after
});

module.exports = router;

//END OF THE INVOICE.JS CODE

the invoice.jade file has only 1 line, the following:

script(src=“https://lightning.filipmartinsson.com/modal/btcpay.js”)

How do you call the /invoice route? Check that req.body.amount is actually defined. You can use console.log('Amount: ' + req.body.amount) and check that the amount is defined when the route is called.

1 Like

How do you call the /invoice route?
I left all like in the video, the code is exactly the same

You can use console.log('Amount: ' + req.body.amount) and check that the amount is defined when the route is called.
Where should I write this command? On PowerShell or on browser console?

So you did defined a form element in index.jade with the required inputs? Specifically one where you define the amount.
The code I imagine is a bit different for every student since it is intended so that students write their own solution :slight_smile:

You can input the console.log command in the invoice.js file in the index route of invoice or /, the one defined after /* Create invoice. */ comment. This is the route that gets called when you submit the form you defined in the index.jade file.
After that you can call the route and you will see the log in the console of the element that was received, you can also log the entire req, then you will get a large object that will contain all the elements received into the route.
If the amount is undefined, you will have to fix the form to send the amount value into the invoice route.

1 Like

The interface is showed properly (See the attached screenshot). The invoice.js file is compiled as suggested in the lecture. (text is attached below). I think the point is that the route is not specified properly, but I followed literally all the instructions in the video.

PATH OF THE invoice.jade FILE: D:\LNApp\views
PATH OF THE app.js FILE: D:\LNApp
PATH OF THE invoice.js FILE: D:\LNApp\routes
PATH OF THE index.js FILE: D:\LNApp\routes

CODE OF THE INDEX.JADE FILE
h1 Welcome to Lightning Payment!
p This is a payment service for Lightning. Very welcome!
div
h2 submit Payments
form(method=‘post’, action=’/invoice’)
p
label Amount
input(type=‘number’, name=‘Amount’)
p
input(type=‘submit’, name=‘submit’)

CODE OF THE INVOICE.JS FILE
var express = require(‘express’);
var router = express.Router();
const BTCPAY_PRIV_KEY = “4c6a32116362b1c9dbece16785d0c93ac5bab6e1b75717582269aca3671a1c2d”;
const BTCPAY_MERCHANT_KEY = “BuxgfRGbe2XHN1LnBi1SJv7RJoiWxYf7uYPJ7je7nebb”;

// Initialize the client
const btcpay = require(‘btcpay’)
const keypair = btcpay.crypto.load_keypair(new Buffer.from(BTCPAY_PRIV_KEY, ‘hex’));
const client = new btcpay.BTCPayClient(‘https://lightning.filipmartinsson.com’, keypair, {merchant: BTCPAY_MERCHANT_KEY})

/* get & verify invoice. */
router.get(’/:id’, async function(req, res, next) {

});

/* Create invoice. */
router.post(’/’, function(req, res, next) {
var dollarAmount = req.body.amount;
console.log(dollarAmount);
//Create invoice
client.create_invoice({price: dollarAmount, currency: “USD”})
.then(function(invoice){
console.log(invoice);
res.render(“invoice”, {invoiceId: invoice.id})
})
.catch(err => console.log(err));
//Display
//What happens after
});
module.exports = router;

2020-08-22_18-02-14

CODE OF THE APP.JS FILE
var createError = require(‘http-errors’);
var express = require(‘express’);
var path = require(‘path’);
var cookieParser = require(‘cookie-parser’);
var logger = require(‘morgan’);

var indexRouter = require(’./routes/index’);
var invoiceRouter = require(’./routes/invoice’);

var app = express();

// view engine setup
app.set(‘views’, path.join(__dirname, ‘views’));
app.set(‘view engine’, ‘jade’);

app.use(logger(‘dev’));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, ‘public’)));

app.use(’/’, indexRouter);
app.use(’/invoice’, invoiceRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get(‘env’) === ‘development’ ? err : {};

// render the error page
res.status(err.status || 500);
res.render(‘error’);
});
module.exports = app;

Your amount here is defined with a capital letter :slight_smile:
If this doesn’t work, check your spacing in jade file, the templating engine is sensitive to identations.

The routes seem to be correct, to make sure you can see the undefined console log for dollarAmount in the invoice route this should be logged in the console where you are running the app if you navigate to the /invoice route.
Did you try replacing that with the res.body so you can debug what values you get into the function after you click the button?

1 Like

I corrected the “Amount” with the “amount”. With this sole amendment I receive a completely different error (See file). Maybe comparing this error with the previous one might help in some way?

Unfortunately I’m unable to enlarge the image.
If you’re console logging the entire res or res.body its expected to get a large output, you should see the amount in that log. Is there anything happening on the UI?

1 Like

Yes, (see attached new file), the console on PowerShell displays the amount I wrote (123321) but after displays a long queue starting with “StatusCodeError: 400”. What does it mean?

Ok, this is good, it means the amount goes through.
Now I think you set up a way to high amount. Try 1$.

1 Like

With 1 dollar amount I receive the same error massage. Amount displayed.