Skip to content

How I Built An E-Commerce Web Store With Lightning Payments

How I Built A Web Store With Lightning Payments

Accepting Bitcoin Lightning Payments Online

The Lightning Network seamlessly enables small payments (micropayments) on the Bitcoin blockchain, avoiding the longer confirmation times and potential hefty fees associated with traditional Bitcoin transactions. This emphasis on micropayments has played a pivotal role in driving the adoption of the Lightning Network, opening up possibilities that were previously impractical.

With the Lightning Network, individuals can make swift payments for items like coffee, gifts, airtime, and charitable donations. Millions of users can conduct Lightning transactions off the main Bitcoin blockchain, providing a solution for online shopping scenarios where buyers seek immediate payment processing and sellers aim for quick and trustless payments to accelerate product delivery.

In this article, we would be exploring how the Lightning Network would typically be used to build web store with lightning payments where buyers can easily shop, scan an invoice and send payments instantly to the vendor.

Note: This article is based on a working demo of a Bitcoin shop that uses the Lightning Network to facilitate payments. A live demo link to the project can be found here. If you are code-savvy, you can also view the source code for the project here.

The User Flow

A typical user flow for a lightning-powered e-commerce app should not be much different from that of classical examples and should follow the flow below:

  1. User picks the products they would like to buy and adds them to their cart
  2. User clicks on checkout and the system calculates the total amount due for payment
  3. Payment invoice is generated for the payment session which the user needs to pay to complete the transaction.
  4. Status of the payment invoice is checked and setup to fire a function once the invoice has been settled. This should also notify the seller of the transaction and show a success/confirmation to the buyer.

As the Lightning Network is to be used to handle the payment aspect, a BOLT 11 lightning invoice can be generated which would contain the payment amount, the payment hash and other metadata about the payment. For instance, some information about the buyer like the buyer’s name and delivery address of the product can be passed as metadata to the lightning invoice such that only the seller is privy to these details once the invoice has been settled, further alluding to the privacy of the Lightning Network.  Bolt 11 lightning invoices are human-readable Bech32 encoded strings which start with “lnbc” and then show some details about the amount to be paid.

An example of a Bolt11 Lightning invoice can be found below:

lnbc10u1pjsyvhypp5edq07hgudms0f6fwknslpt4tzjhkawst07u55pz5mlpt7xfescrsdq5g9kxy7fqd9h8vmmfvdjscqzzsxqyz5vqsp5eqqdllffgl2anp400kyz84rkvj87353gqjl7dv5ecsa6he96tpus9qyyssqtz8m0tnkjm3wv857vftey4dmjk79muf5kus0lwlpf4z9jwz554n3yevzpppgpejzzhe5h4h4vgwahqgye8cmz6aqrcjvdhj9spm8c4cqytkadn

Explanation: The first four characters (lnbc) indicate the network type (mainnet, testnet, etc.), followed by the amount 10. The “u” stands for microbitcoin unit which is equal to 100 sats, hence the invoice is for a payment of 1,000 sats. The next portion of the data segment has 3 sections: timestamp, tagged key-value pairs, and the signature of the entire invoice encoded using bech32. Lightning invoices are typically encoded in a QR code that can be scanned by a Lightning wallet for a cleaner user experience. A major disadvantage of BOLT 11 invoices is that they are not reusable, hence a new invoice needs to be generated by buyers for each order.

Generating a Bolt 11 Lightning Invoice

Now that we have a sketch of the user experience of our Bitcoin-powered ecommerce app. let’s discuss all the possible ways we can generate a Lightning invoice within our project.

  1. By running your own Lightning Node:  This is the most sophisticated way to generate a Lightning invoice. Some of the most popular Lightning node implementations include Lightning Network Node (LND), c-lightning, Eclair and Rust Lightning. To generate a lightning invoice using any of these, you would need to pass the merchant’s invoice.macaroon file and/or Node Id which can be quite complicated to achieve.
  2.  By using a third-party Lightning Node Provider:  A second option that exists for generating a Lightning invoice is to use a third-party lightning node provider like Voltage.cloud or Opennode. This options are less technical and have a rich documentation, but might also attract some pricing implications.
  3. Using LNURL: The third and easiest way to generate lightning invoices in your ecommerce project would be to use LNURL which is a set of open-source protocols that facilitate easy interactions between Lightning wallets and external services or applications. To generate an invoice using LNURL, all that is required is the seller’s Lightning Address which is like an email address, but for Bitcoin. An example of a Lightning address is name@getalby.com or name@bitnob.io.

Listening for Invoice’s status

The final part of building our project is the part where we need to listen for changes to the status of our lightning invoice and fire a function once the status of the invoice changes to settled. There are three major ways this can be done:

  1. Using WebSockets: Websockets is the technology that powers all real-time web applications and chats on the internet. With Websockets, we can create a two-way communication between our backend which would hold information about our invoice and the frontend which presents the invoice as a QR code to the buyer. Once the backend detects the invoice has been settled, it can immediately send a response to the frontend to show a success screen. Although this method is effective, it is always to pair using a websocket with a webhook in case there is a disconnection between the backend and frontend due to internet disruption or device switch-off.
  2. Using Webhooks: Webhooks are typically the best practice for handling payments to Lightning invoices. With a webhook, we can send a notification to the buyer from the backend once the invoice has been settled. This notification is sent regardless of any disruptions from the frontend.
  3. Using Polling: In the polling method, we are constantly sending a request from the frontend to check the status of the lightning invoice and we then update the status of the transaction on both the frontend and the backend once the status of the invoice is considered to be settled. This is generally not the best practice as it is susceptible to hacks, internet disruptions and excessive API calls.

Conclusion

The integration of the Lightning Network into ecommerce applications brings forth a transformative payment experience.  By exploring the implementation of the Lightning Network in ecommerce, we’ve unveiled an efficient user flow for lightning-powered applications. This flow ensures a seamless checkout process, secure payment handling, and swift notifications upon successful transactions. Incorporating the Lightning Network into ecommerce applications not only enhances payment processing but also lays the foundation for the future of fast, secure and efficient online payments.

Thank You For Reading

If you found this article helpful, please consider sharing it, supporting one of these affiliates, or making a value for value donation so that we can continue to publish more Bitcoin-only content.