Building a Lightning App (Lapp) - Discussion

Yep, that worked. Thanks!

– Now however, I am having issues with generating the merchant key:


D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master> node -e "const btcpay=require('btcpay'); new btcpay.BTCPayClient('https://lightning.filipmartinsson.com', btcpay.crypto.load_keypair(Buffer.from('a51ea8abb97400f918169346a315886dfaca9adc1cf570592157629349103d4a', 'hex'))).pair_client('yBXayvJ').then(console.log).catch(console.error)"
StatusCodeError: 400 - {"error":"Error while pairing (Expired)"}
    at new StatusCodeError (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\request-promise-core\lib\errors.js:32:15)
    at Request.plumbing.callback (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\request-promise-core\lib\plumbing.js:104:33)
    at Request.RP$callback [as _callback] (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\request-promise-core\lib\plumbing.js:46:31)
    at Request.self.callback (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\request\request.js:185:22)
    at Request.emit (events.js:310:20)
    at Request.<anonymous> (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:310:20)
    at IncomingMessage.<anonymous> (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:416:28)
    at IncomingMessage.emit (events.js:322:22)
    at endReadableNT (_stream_readable.js:1187:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  statusCode: 400,
  error: { error: 'Error while pairing (Expired)' },

Thanks for the help!

please try to create a new store and get a new pair_client code, probably the code has expired already.
Also, you should get a Json file after " error: { error: ‘Error while pairing (Expired)’ }"…
please try to get a new pair code fisrt and rerun the command with the new pair code, i will be waiting for your reply. :slightly_smiling_face:

Remember you can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.


I am a happy Preformatted Text box, please use me wisely!

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

Thanks thecil!! That worked fine. Thanks again for the tip with the preformatted text… I’ll take note of that.

Kind regards!

1 Like

HI all, I’m a bit stuck again. This time trying to create invoices. Around the 2:45 mark in the video, when putting 10 in the amount box I get a series of 404 not found errors on the front end:

Not Found
404
NotFoundError: Not Found
    at D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\app.js:28:8
    at Layer.handle [as handle_request] (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:317:13)
    at D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:275:10)
    at D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:635:15
    at next (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:260:14)
    at Function.handle (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:174:3)
    at router (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:317:13)
    at D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:335:12)
    at next (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:275:10)
    at D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:635:15
    at next (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:260:14)
    at Function.handle (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:174:3)
    at router (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:317:13)
    at D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master\node_modules\express\lib\router\index.js:284:7

Here is my code from the video in invoices.js

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;
  client.create_invoice({price: dollarAmount, currency: "USD"})
  .then(function(invoice){
  console.log(invoice);
})
  .catch(err => console.log(err));
});


module.exports = router;

Finally, here is what appears in PowerShell:

> [email protected] start D:\Documents\Education\Blockchain - Crypto\IVANONTECH\Lightning\lightning-app-boilerplate-master
> node ./bin/www

GET / 304 3123.009 ms - -
GET /favicon.ico 404 19.514 ms - 3341
GET /invoice?amount=10&submit=Submit 404 15.076 ms - 3763
GET /stylesheets/style.css 304 1.617 ms - -
GET /invoice?amount=10&submit=Submit 404 17.130 ms - 3763
GET /stylesheets/style.css 304 0.628 ms - -

Thanks for your help with this!

1 Like

Hello sir, could you please post the code from your “invoice.jade” file?

Probably the issue is on the create invoice function “router.post”.

var dollarAmount = req.body.amount;

Should be:

var dollarAmount = product.price;

But I could be wrong, that’s why i need to see your “invoice.jade” file.

router.post('/', function(req, res, next) {
  var dollarAmount = product.price;
  client.create_invoice({price: dollarAmount, currency: "USD"})
  .then(function(invoice){
  console.log(invoice);
})
  .catch(err => console.log(err));
});

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

Thanks Carlos. I tried replacing
var dollarAmount = req.body.amount;
with
var dalloarAmount = product.price;

but I got the same errors. My “invoice.jade” file only has a source link

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

My code (var dollarAmount = req.body.amount;) is the same as in the video

Thanks for any other suggestions!

1 Like

Ok, sometimes an error could be that you have the “Blockchain - Crypto” folder with spaces on it’s name, could you rename it to something like “Blockchain-Crypto” instead (no spaces).

Also, my bad, sorry, is not the “invoice.jade” that i need to look at it, it’s the “index.jade” file that is in the “views” folder, would like to see how are you naming the variables and the syntax there.

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

So I’m in the “Creating Invoices” lesson, and when I try to generate the invoce, I get this error:

'Warning: No wallet has been linked to your BTCPay Store. See the following link for more information on how to connect your store and wallet. (https://docs.btcpayserver.org/btcpay-basics/gettingstarted#connecting-btcpay-store-to-your-wallet)\n5/23/20 2:16:56 AM: Creation of invoice starting\n5/23/20 2:16:57 AM: BTC_USD: The rating rule is coinaverage(BTC_USD)\n5/23/20 2:16:57 AM: BTC_USD: The evaluated rating rule is ERR_RATE_UNAVAILABLE(coinaverage, BTC_USD)\n5/23/20 2:16:57 AM: BTC_USD: Rate rule error (RateUnavailable)\n5/23/20 2:16:57 AM: BTC_USD: Exception reaching exchange coinaverage (Unauthorized access to the API, premium plan needed)\n'

Then I go to the BTCPay Server section of the wallets, but it gives me no option to create one:

Yes, I have created the store and made it all the way up here.

Hi, thanks for your help thecil, I renamed the folder but nothing has changed.

Here is the content of my index.jade file:

h1=message
p This is a payment service for lightning. You're very welcome!
div
  h2 Submit Payments
  form(mthods='post', action='/invoice')
    p
      label Amount
      input(type='number', name='amount')
    p
      input(type='submit', name='submit')
1 Like

Hello sir, yes that issue happens sometime, i think that should be related to the filip node, i tested in different days on different hours and i got the same, mostly when it was at night on Europe.

Still, i will try to figure it out if it has something to do with Filip’s node, or something else.
For now, just try it on a different hour time, it should work most of the days.

Carlos Z.

Ok sir, thats weird, the issue is that you are loading a “favicon.ico” that you do not have in the index.jade

GET /favicon.ico 404 19.514 ms - 3341

I suppose you have not change anything on the “index.js” file on the router folder right?

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

Hi @filip

probably this has already been reported a few times, but nevertheless it’s an issue, so I report it too. I did the pair and added the settings in the invoice.js file correctly, still I get this error (I retried the pairing 2 times):

{ StatusCodeError: 400 - {“error”:“Warning: No wallet has been linked to your BTCPay Store. See the following link for more information on how to connect your store and wallet. (https://docs.btcpayserver.org/btcpay-basics/gettingstarted#connecting-btcpay-store-to-your-wallet)\n5/25/20 2:28:37 PM: Creation of invoice starting\n5/25/20 2:28:37 PM: BTC_USD: The rating rule is coinaverage(BTC_USD)\n5/25/20 2:28:37 PM: BTC_USD: The evaluated rating rule is ERR_RATE_UNAVAILABLE(coinaverage, BTC_USD)\n5/25/20 2:28:37 PM: BTC_USD: Rate rule error (RateUnavailable)\n5/25/20 2:28:37 PM: BTC_USD: Exception reaching exchange coinaverage (Unauthorized access to the API, premium plan needed)\n”}

I retried again a few minutes later, with a low amount like 1 USD, then it worked. Then I retried again with a big amount 100 USD, it worked too. So I was it probably a temporary issue?

1 Like

@filip Right now your lightning node seems down, check it out: https://lightning.filipmartinsson.com/

502 Bad Gateway

nginx

UPDATE: now (20 mins after posting this message) it works. Would be nice if you prepare a custom error page you can show during maintenance. E.g. with a text such as “We are updating the server, please try again later” or something like that… (or even more precise in case you know when you’ll be back online)

1 Like

@pmk
Hello sir(s), sorry for the inconveniences, we were updating the btcpayserver node, to see if we fix the issue that you mention in your 1st post.

For now we have been testing the node and should be working OK. Please try to run again your request and you should not have any issue, if you still have one, post your issue so we can fix it ASAP.

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

@thecil

Hi, I’m now having another issue, similar to a previous poster, after I pay or cancel, I get the following error message on the browser:

Not Found404NotFoundError: Not Found at C:\Users\Angel\Documents\IvanOnTechCourses\lightning\app.js:28:8 at Layer.handle [as handle_request] (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:317:13) at C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:275:10) at C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:635:15 at next (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:260:14) at Function.handle (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:174:3) at router (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:317:13) at C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\express\lib\router\index.js:275:10) at SendStream.error (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\serve-static\index.js:121:7) at SendStream.emit (events.js:182:13) at SendStream.error (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\send\index.js:270:17) at SendStream.onStatError (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\send\index.js:421:12) at next (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\send\index.js:734:16) at onstat (C:\Users\Angel\Documents\IvanOnTechCourses\lightning\node_modules\send\index.js:723:14) at FSReqCallback.oncomplete (fs.js:161:21)

This is my code deliver from invoice.js:

//Get invoice/{invoiceId}
router.get('/:id', async function(req, res, next) {
//payment verification, check if invoice is paid
  var invoiceId = req.params.id;
  client.get_invoice(invoiceId)
  .then(invoice =>{
    if(invoice.status == "complete" || invoice.status == "paid"){
      //Deliver the product
      res.end("<html>Thank you</html>");
    }
    else{
      res.end("<html>Not paid</html>");
    }
  }).catch(err =>{
    console.log(err);
  })
});

The strange thing is this was working 2 days ago, so I don’t know if it is a problem on the server.

1 Like

Hello sir, below “//Deliver the product”, you should use “red.render” instead “res.end”.

So it should looks like this

//Get invoice/{invoiceid}
router.get('/:id', async function(req, res, next) {
  var invoiceId = req.params.id;
  client.get_invoice(invoiceId)
  .then(invoice => {
    if(invoice.status == "complete" || invoice.status == "paid"){
      //Deliver product to customer
      res.render("<html>Thank you</html>");
    }
    else{
      res.render("<html>Not paid</html>");
    }
  }).catch(err => {
    console.log(err);
  })
});

Hope you find this useful! :slight_smile:

If you have any doubt, please let us know so we can help you!

Carlos Z.

Nope, I’m getting the same error still.

1 Like

Ok, could be probably an error with the server, but is not for sure, so lets just discard that issue.
We update the btcpayserver of the Academy days ago, could be a problem on it, thats why 2 days ago it was working great for you.

I advice you to delete the store that you have on the filip btc pay, create a new one and re pair again your btcpayserver with the store.

Probably will need to reapply this step from the course: Creating API Keys

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

@thecil

I did that, created a new store and created new API keys, and still the same error, I leave here the github to the code, maybe you could give it a look:

1 Like

Hello sir, i have checked over your code from your github repository, it all looks correct to me.

Now the issue might be on your npm files (probably corrupted), could you please try to delete them and re download them again? just to discard that issue.

I try to download your repository, run it on a VM instance and it works great for me (ubuntu server).

Proof of Valid Code:

GET / 200 960.258 ms - 263
GET /favicon.ico 404 170.211 ms - 2439
{ url: 'https://lightning.filipmartinsson.com/invoice?id=97JkNh6oJeJ1y3qgC9RRsj',
  posData: null,
  status: 'new',
  btcPrice: null,
  btcDue: null,
  cryptoInfo:
   [ { cryptoCode: 'BTC',
       paymentType: 'LightningLike',
       rate: 9520.609,
       exRates: [Object],
       paid: '0.00000000',
       price: '0.00010504',
       due: '0.00010504',
       paymentUrls: [Object],
       address: 'lnbc105040n1p0dd9m3pp56dy4aw9fslctwr5s78u409c8gn2t9fmd67fq932yhsq82g573dhsdpc2pskjepqw3hjqsm0w4e8xefq2d6x7un9yqezq2z0wfjx2u3qf9zr5gpfxqzurcqp2sp5fmwrnwp2vsspadrzzsmuhdnyfrfrssent028083ffg7lszn5m30s9qy9qsq6qfs3g2a5wwqrq9kzlczhetcaqqsa0qphf3q2xf48nd0tjj6qr3pxs3xagv6n6k352qyvp4j6md6wzlkcsdhntmsz6e75ujdd4zt9vqqazjgmx',
       url: 'https://lightning.filipmartinsson.com/i/BTC_LightningLike/97JkNh6oJeJ1y3qgC9RRsj',
       totalDue: '0.00010504',
       networkFee: '0.00000000',
       txCount: 0,
       cryptoPaid: '0.00000000',
       payments: [] } ],
  price: 1,
  currency: 'USD',
  exRates: null,
  buyerTotalBtcAmount: null,
  itemDesc: null,
  itemCode: null,
  orderId: null,
  guid: 'e983581e-4c7c-41db-b6e8-7936b0aec763',
  id: '97JkNh6oJeJ1y3qgC9RRsj',
  invoiceTime: 1591121777000,
  expirationTime: 1591122677000,
  currentTime: 1591121777750,
  lowFeeDetected: false,
  btcPaid: null,
  rate: 0,
  exceptionStatus: false,
  paymentUrls: null,
  refundAddressRequestPending: false,
  buyerPaidBtcMinerFee: null,
  bitcoinAddress: null,
  token: 'Q2VmT2ts97EA2gS71hL6hM',
  flags: { refundable: false },
  paymentSubtotals: { BTC_LightningLike: 10504 },
  paymentTotals: { BTC_LightningLike: 10504 },
  amountPaid: 0,
  minerFees: {},
  exchangeRates: { BTC: { USD: 0 } },
  supportedTransactionCurrencies: { BTC: { enabled: true, reason: null } },
  addresses:
   { BTC_LightningLike: 'lnbc105040n1p0dd9m3pp56dy4aw9fslctwr5s78u409c8gn2t9fmd67fq932yhsq82g573dhsdpc2pskjepqw3hjqsm0w4e8xefq2d6x7un9yqezq2z0wfjx2u3qf9zr5gpfxqzurcqp2sp5fmwrnwp2vsspadrzzsmuhdnyfrfrssent028083ffg7lszn5m30s9qy9qsq6qfs3g2a5wwqrq9kzlczhetcaqqsa0qphf3q2xf48nd0tjj6qr3pxs3xagv6n6k352qyvp4j6md6wzlkcsdhntmsz6e75ujdd4zt9vqqazjgmx' },
  paymentCodes:
   { BTC_LightningLike:
      { BIP21: null,
        BIP72: null,
        BIP72b: null,
        BIP73: null,
        BOLT11: 'lightning:lnbc105040n1p0dd9m3pp56dy4aw9fslctwr5s78u409c8gn2t9fmd67fq932yhsq82g573dhsdpc2pskjepqw3hjqsm0w4e8xefq2d6x7un9yqezq2z0wfjx2u3qf9zr5gpfxqzurcqp2sp5fmwrnwp2vsspadrzzsmuhdnyfrfrssent028083ffg7lszn5m30s9qy9qsq6qfs3g2a5wwqrq9kzlczhetcaqqsa0qphf3q2xf48nd0tjj6qr3pxs3xagv6n6k352qyvp4j6md6wzlkcsdhntmsz6e75ujdd4zt9vqqazjgmx' } },
  buyer:
   { name: null,
     address1: null,
     address2: null,
     locality: null,
     region: null,
     postalCode: null,
     country: null,
     phone: null,
     email: null } }

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.