Giter Club home page Giter Club logo

lnbits-webhook-example's Introduction

Webhook handler example code for LNbits in PHP

Various endpoints in the LNbits API support using webhooks in form of adding a webhook URL in the webhook field. This is an example of using PHP and the create or pay invoice endpoint in the LNbits API (https://legend.lnbits.com/docs#/default/api_payments_create_api_v1_payments_post).

Example of adding the webhook URL using cURL (this could be done on the dashboard as well):

curl -X 'POST'
https://your-lnbits-endpoint.com/api/v1/payments
-H "X-Api-Key: <INVOICE KEY>"
-H "Content-type: application/json"
-d '{"out": false, "amount": 1, "memo": "test webhook", "webhook": "https://example.com/webhook.php"}'

The value of the webhook field refers to where you have the code that will process the data from the webhook. This example assumes that the code processing the data is found in https://example.com/webhook.php

Example code in webhook.php to process the webhook data:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Webhook json content
    $json = file_get_contents('php://input'); 

    // Decode the json content and store it in $object
    $object = json_decode($json);

    // Check if the json is valid
    if (json_last_error() !== JSON_ERROR_NONE) {
    
        die(header('HTTP/1.0 415 Unsupported Media Type'));
        
    } else {

        /** Log content to a file to inspect it to see the values of $object. 
         * This would give you the opportunity to see what you want to process. 
         * Don't forget to comment out or delete this part when you're done.
        **/
        
        file_put_contents('object.txt', print_r($object, true));
        
        //In this example, I'm storing the checking_id into a variable.
        $checking_id = $object->checking_id;
        
        
        /**
        ** From here, you write your code to process the data the way you want.
        **
        **
        **/

    }

}

?>

Note:

If you use https://webhook.site/ in your initial tests, you can skip the step on logging the content to a file.

Webhook data and paying external invoices using the LNbits API

When using the LNbits pay invoice API to pay an external invoice, the webhook field defaults to null despite adding a webhook URL. If you need to send webhook data after paying an external invoice, an approach that can be followed is to use the resulting payment_hash that is outputed to check an invoice and send the JSON output to the webhook URL:

Checking the status of the bolt11 invoice:

curl -X 'GET'
https://your-lnbits-endpoint.com/api/v1/payments/<payment_hash>
-H "X-Api-Key: <invoice key>"
-H "Content-type: application/json"

Example of sending resulting JSON data to webhook URL:

curl -X 'POST'
https://example.com/webhook.php
-H "Content-type: application/json"
-d '{
<JSON output from checking invoice>
}'

References

  1. https://stackoverflow.com/questions/47565321/how-to-get-webhook-response-data

lnbits-webhook-example's People

Contributors

jimasuen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.