Plugin Directory

Changeset 2732965


Ignore:
Timestamp:
05/28/2022 01:22:19 AM (4 years ago)
Author:
pseudozach
Message:

Add btcpayserver and lnbits support

Location:
wp-lightning-comments/trunk
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • wp-lightning-comments/trunk/README.md

    r1893155 r2732965  
    11# Pay to Comment with Lightning
     2- Requires the user to send a small payment before being able to comment. 
     3- Disables the comment form until a user has sent a bitcoin micropayment over Lightning Network.
     4- Remembers if a user paid to comment on a certain page.
     5- Supports multiple providers with simple configuration.
    26
    3 Require the user to send a small payment before being able to comment.
    4 
    5 This plugin disables the comment form until a user has sent a bitcoin micropayment over Lightning Network.
    6 
    7 The plugin is made for Wordpress, but the JavaScript component can easily be implemented into other CMS systems as well.
     7## Supported Lightning Backends
     8* [LNBits](https://lnbits.com)
     9* [BTCPayServer](https://btcpayserver.org)
    810
    911## Usage
     
    12141. Upload it to your plugins-folder
    13151. Enable the plugin in your wordpress backend
    14 1. Select if you want testnet or mainnet in the post admin interface
    15 1. Start using it!
     161. Configure provider url and invoice key in the plugin settings screen.
     171. Enjoy spam-free comment section!
  • wp-lightning-comments/trunk/lightningcomments.css

    r1893155 r2732965  
    3131}
    3232
    33 #lncomments_checkbox_field_0 {
     33/* #lncomments_checkbox_field_0 {
    3434  visibility: hidden;
    35 }
     35} */
  • wp-lightning-comments/trunk/lightningcomments.js

    r1893155 r2732965  
    4545  };
    4646
    47   var buildQuiz = function(quizNode){
     47  var buildQuiz = async function(quizNode){
    4848    var formNode = quizNode.nextElementSibling;
    4949    var errorText = quizNode.getAttribute('data-' + LightningComments + '-error');
     
    8181    });
    8282
    83     //testnet / mainnet
    84     var mainnet = document.getElementsByName("lncomments_settings[lncomments_checkbox_field_0]")[0].checked;
    85     var iframe = document.createElement('iframe');
    86     iframe.style.height = '150px';
    87     iframe.style.width = '100%';
    88     iframe.style.transition = 'height 1s';
    89     if(mainnet) {
    90       iframe.src = "https://api.yunik.co:8113";
    91     } else {
    92       iframe.src = "https://api.yunik.co:8112";
     83    //testnet / mainnet - remove testnet it's not 2018 anymore!
     84    // var testnet = document.getElementsByName("lncomments_settings[lncomments_checkbox_field_0]")[0].checked;
     85    // var iframe = document.createElement('iframe');
     86
     87    let invoice;
     88    const invoiceCheckInterval = 20000;
     89
     90    var invoiceDiv = document.createElement('div');
     91    // invoiceDiv.style.height = '550px';
     92    invoiceDiv.style.width = '100%';
     93    invoiceDiv.style.transition = 'height 1s';
     94    const invoiceAmount = document.getElementsByName("lncomments_settings[lncomments_text_field_3]")[0].value
     95    const lnbitsUrl = document.getElementsByName("lncomments_settings[lncomments_text_field_1]")[0].value
     96    const lnbitsKey = document.getElementsByName("lncomments_settings[lncomments_text_field_2]")[0].value
     97    const btcpayserverUrl = document.getElementsByName("lncomments_settings[lncomments_text_field_4]")[0].value
     98    const btcpayserverStoreId = document.getElementsByName("lncomments_settings[lncomments_text_field_5]")[0].value
     99    const btcpayserverToken = document.getElementsByName("lncomments_settings[lncomments_text_field_6]")[0].value
     100
     101    try {
     102      if(lnbitsUrl && lnbitsKey) {
     103        // fetch invoice from lnbits
     104        const invoiceResponse = await fetch(`${lnbitsUrl}/api/v1/payments`,
     105        {
     106            headers: {
     107              'Accept': 'application/json',
     108              'Content-Type': 'application/json',
     109              'X-Api-Key': lnbitsKey
     110            },
     111            method: "POST",
     112            body: JSON.stringify({"out": false, "amount": parseInt(invoiceAmount), "memo": "Pay to comment on " + window.location.host})
     113        })
     114        invoice = await invoiceResponse.json()
     115        console.log('invoice: ', invoice)
     116       
     117        if(invoice?.payment_request) {
     118          // check if invoice is paid
     119          const interval = setInterval(async() => {
     120            const paidResponse = await fetch(`${lnbitsUrl}/api/v1/payments/${invoice.payment_hash}`,
     121            {
     122              headers: {
     123                'Accept': 'application/json',
     124                'Content-Type': 'application/json',
     125                'X-Api-Key': lnbitsKey
     126              },
     127            })
     128            const paid = await paidResponse.json()
     129            if(paid?.paid) {
     130              localStorage.setItem(correctId, correctId);
     131              setTimeout(removeQuizDelay, 2500, quizNode, formNode);
     132              clearInterval(interval)
     133            }
     134          }, invoiceCheckInterval)
     135        }
     136      } else if(btcpayserverUrl && btcpayserverStoreId && btcpayserverToken) {
     137        // fetch invoice from lnbits
     138        const invoiceResponse = await fetch(`${btcpayserverUrl}/api/v1/stores/${btcpayserverStoreId}/invoices`,
     139        {
     140            headers: {
     141              'Accept': 'application/json',
     142              'Content-Type': 'application/json',
     143              'Authorization': `token ${btcpayserverToken}`,
     144            },
     145            method: "POST",
     146            body: JSON.stringify({"amount": parseInt(invoiceAmount), "currency": "SATS"})
     147        })
     148        invoice = await invoiceResponse.json()
     149        console.log('notinvoice ', invoice)
     150        const actualInvoiceResponse = await fetch(`${btcpayserverUrl}/api/v1/stores/${btcpayserverStoreId}/invoices/${invoice.id}/payment-methods`,
     151        {
     152            headers: {
     153              'Accept': 'application/json',
     154              'Content-Type': 'application/json',
     155              'Authorization': `token ${btcpayserverToken}`,
     156            },
     157        })
     158        actualInvoice = (await actualInvoiceResponse.json())[0].destination
     159        console.log('invoice: ', actualInvoice)
     160        invoice.payment_request = actualInvoice
     161       
     162        if(actualInvoice) {
     163          // check if invoice is paid
     164          const interval = setInterval(async() => {
     165            const paidResponse = await fetch(`${btcpayserverUrl}/api/v1/stores/${btcpayserverStoreId}/invoices/${invoice.id}`,
     166            {
     167              headers: {
     168                'Accept': 'application/json',
     169                'Content-Type': 'application/json',
     170                'Authorization': `token ${btcpayserverToken}`,
     171              },
     172            })
     173            const paid = await paidResponse.json()
     174            if(paid?.status === 'Settled') {
     175              localStorage.setItem(correctId, correctId);
     176              setTimeout(removeQuizDelay, 2500, quizNode, formNode);
     177              clearInterval(interval)
     178            }
     179          }, invoiceCheckInterval)
     180        }
     181      }
     182
     183      var p1 = document.createElement('p')
     184      p1.style.textAlign = 'center'
     185      p1.innerHTML = 'Scan/Tap/Copy to Pay'
     186      var p2 = document.createElement('p')
     187      p2.style.wordBreak = 'break-all'
     188      p2.innerHTML = invoice?.payment_request
     189      var anchor = document.createElement('a')
     190      anchor.target = '_blank'
     191      var qrcode = document.createElement('canvas');
     192      qrcode.setAttribute("id","qr")
     193      qrcode.style.margin = 'auto'
     194      qrcode.style.display = 'block'
     195      anchor.append(qrcode)
     196      anchor.href = 'lightning:' + invoice?.payment_request?.toUpperCase()
     197      invoiceDiv.append(p1)
     198      invoiceDiv.append(anchor)
     199      invoiceDiv.append(p2)
     200 
     201      quizNode.appendChild(invoiceDiv);
     202 
     203      //add the qrcode after append
     204      new QRious({
     205        element: document.getElementById('qr'),
     206        value: invoice?.payment_request,
     207        padding: '4',
     208        size: 300,
     209      });
     210
     211    } catch (error) {
     212      console.log('failed to create Lightning invoice: ', error.message)
    93213    }
    94214
    95     quizNode.appendChild(iframe);
    96 
     215    // instead of cross-origin events - lets check if invoice was paid with interval
    97216    // Listen to message from child window
    98     bindEvent(window, 'message', function (e) {
    99         // results.innerHTML = e.data;
    100         // console.log("fromiframe: " + e.data);
    101         if(e.data == "fromclientjs") {
    102           // console.log("payment received!");
    103           correct = true;
    104           localStorage.setItem(correctId, correctId);
    105           // removeQuiz(quizNode, formNode);
    106           setTimeout(removeQuizDelay, 2500, quizNode, formNode);
    107 
    108           var fool = document.getElementById("pmg_comment_titlez");
    109           fool.value = "youreok";
    110 
    111         } else if(e.data == "buyclicked") {
    112           iframe.style.height = "650px";
    113         } else {
    114           // console.log("received something else from iframe");
    115         }
    116     });
     217    // bindEvent(window, 'message', function (e) {
     218    //     // results.innerHTML = e.data;
     219    //     // console.log("fromiframe: " + e.data);
     220    //     if(e.data == "fromclientjs") {
     221    //       // console.log("payment received!");
     222    //       correct = true;
     223    //       localStorage.setItem(correctId, correctId);
     224    //       // removeQuiz(quizNode, formNode);
     225    //       setTimeout(removeQuizDelay, 2500, quizNode, formNode);
     226
     227    //       var fool = document.getElementById("pmg_comment_titlez");
     228    //       fool.value = "youreok";
     229
     230    //     } else if(e.data == "buyclicked") {
     231    //       iframe.style.height = "650px";
     232    //     } else {
     233    //       // console.log("received something else from iframe");
     234    //     }
     235    // });
    117236
    118237  };
  • wp-lightning-comments/trunk/lightningcomments.php

    r1893155 r2732965  
    22/*
    33Plugin Name: Pay to Comment with Lightning
    4 Version: 0.1.0
     4Version: 0.2.0
    55Plugin URI: https://wordpress.org/plugins/wp-lightning-comments/
    66Author: @citlayik
    7 Author URI:  https://blog.kriptode.com/lightning-comments-for-wordpress/
     7Author URI:  https://kriptode.com
    88Description: Require the user to send a bitcoin payment to be able to post comments.
    99*/
     
    1313
    1414add_action('wp_enqueue_scripts', function(){
     15  wp_enqueue_script('qrious', plugins_url('qrious.min.js', __FILE__));
    1516  wp_enqueue_script(LightningComments, plugins_url('lightningcomments.js', __FILE__));
    1617  wp_enqueue_style(LightningComments, plugins_url('lightningcomments.css', __FILE__));
     
    2324    data-<?php echo LightningComments; ?>-error="<?php echo esc_attr(__('You have not sent the payment correctly. Try again.', LightningComments)); ?>">
    2425    <!-- Would you like to comment?  -->
    25     <h2>Please send a small payment via Lightning to enable comments.</h2>
    26     <?php lncomments_checkbox_field_0_render(); ?>
     26    <h5 style="text-align: center;">Please send a small Bitcoin payment via Lightning to enable comments.</h5>
     27    <div id="lncomments-settings" style="display:none;">
     28      <?php lncomments_text_field_1_render(); ?>
     29      <?php lncomments_text_field_2_render(); ?>
     30      <?php lncomments_text_field_3_render(); ?>
     31      <?php lncomments_text_field_4_render(); ?>
     32      <?php lncomments_text_field_5_render(); ?>
     33      <?php lncomments_text_field_6_render(); ?>
     34    </div>
    2735    <!-- <p>
    2836      We love comments & hate spam.
     
    158166    );
    159167
    160     add_settings_field(
    161         'lncomments_checkbox_field_0',
    162         __( 'Mainnet if checked', 'wordpress' ),
    163         'lncomments_checkbox_field_0_render',
    164         'pluginPage',
    165         'lncomments_pluginPage_section'
    166     );
    167 
     168    // add_settings_field(
     169    //  'lncomments_checkbox_field_0',
     170    //  __( 'Testnet if checked', 'wordpress' ),
     171    //  'lncomments_checkbox_field_0_render',
     172    //  'pluginPage',
     173    //  'lncomments_pluginPage_section'
     174    // );
     175
     176
     177    add_settings_field(
     178        'lncomments_text_field_3',
     179        __( 'Comment Amount (sats)', 'wordpress' ),
     180        'lncomments_text_field_3_render',
     181        'pluginPage',
     182        'lncomments_pluginPage_section'
     183    );
    168184    add_settings_field(
    169185        'lncomments_text_field_1',
    170         __( 'Your Bitcoin Wallet Address', 'wordpress' ),
     186        __( 'LNBits URL (e.g. https://legend.lnbits.com)', 'wordpress' ),
    171187        'lncomments_text_field_1_render',
    172188        'pluginPage',
    173189        'lncomments_pluginPage_section'
    174190    );
     191    add_settings_field(
     192        'lncomments_text_field_2',
     193        __( 'LNBits Invoice/Read Key', 'wordpress' ),
     194        'lncomments_text_field_2_render',
     195        'pluginPage',
     196        'lncomments_pluginPage_section'
     197    );
     198    add_settings_field(
     199        'lncomments_text_field_4',
     200        __( 'BTCPayServer URL (e.g. https://mainnet.demo.btcpayserver.org)', 'wordpress' ),
     201        'lncomments_text_field_4_render',
     202        'pluginPage',
     203        'lncomments_pluginPage_section'
     204    );
     205    add_settings_field(
     206        'lncomments_text_field_5',
     207        __( 'BTCPayServer Store ID', 'wordpress' ),
     208        'lncomments_text_field_5_render',
     209        'pluginPage',
     210        'lncomments_pluginPage_section'
     211    );
     212    add_settings_field(
     213        'lncomments_text_field_6',
     214        __( 'BTCPayServer Greenfield Api Key (permissions: canviewinvoices & cancreateinvoice)', 'wordpress' ),
     215        'lncomments_text_field_6_render',
     216        'pluginPage',
     217        'lncomments_pluginPage_section'
     218    );
     219  // btcpay.store.cancreateinvoice
    175220
    176221}
     
    188233
    189234function lncomments_text_field_1_render(  ) {
    190 
    191     $options = get_option( 'lncomments_settings' );
    192     ?>
    193     <input type='text' name='lncomments_settings[lncomments_text_field_1]' value='<?php echo $options['lncomments_text_field_1']; ?>'>
    194     <?php
    195 
    196 }
    197 
     235    $options = get_option( 'lncomments_settings' );
     236    ?>
     237    <input type='text' style='min-width: 300px;' name='lncomments_settings[lncomments_text_field_1]' value='<?php echo $options['lncomments_text_field_1']; ?>'>
     238    <?php
     239}
     240
     241function lncomments_text_field_2_render(  ) {
     242    $options = get_option( 'lncomments_settings' );
     243    ?>
     244  <input type='text' style='min-width: 300px;' name='lncomments_settings[lncomments_text_field_2]' value='<?php echo $options['lncomments_text_field_2']; ?>'>
     245    <?php
     246}
     247
     248function lncomments_text_field_3_render(  ) {
     249    $options = get_option( 'lncomments_settings' );
     250    ?>
     251    <input type='text' style='min-width: 300px;' name='lncomments_settings[lncomments_text_field_3]' value='<?php echo $options['lncomments_text_field_3']; ?>'>
     252    <?php
     253}
     254
     255function lncomments_text_field_4_render(  ) {
     256    $options = get_option( 'lncomments_settings' );
     257    ?>
     258    <input type='text' style='min-width: 300px;' name='lncomments_settings[lncomments_text_field_4]' value='<?php echo $options['lncomments_text_field_4']; ?>'>
     259    <?php
     260}
     261function lncomments_text_field_5_render(  ) {
     262    $options = get_option( 'lncomments_settings' );
     263    ?>
     264    <input type='text' style='min-width: 300px;' name='lncomments_settings[lncomments_text_field_5]' value='<?php echo $options['lncomments_text_field_5']; ?>'>
     265    <?php
     266}
     267function lncomments_text_field_6_render(  ) {
     268    $options = get_option( 'lncomments_settings' );
     269    ?>
     270    <input type='text' style='min-width: 300px;' name='lncomments_settings[lncomments_text_field_6]' value='<?php echo $options['lncomments_text_field_6']; ?>'>
     271    <?php
     272}
    198273
    199274function lncomments_settings_section_callback(  ) {
    200275
    201     echo __( 'No information is saved or tracked. Currently payouts are disabled.', 'wordpress' );
     276    echo __( 'Populate URL and invoice key for your preferred provider', 'wordpress' );
    202277
    203278}
  • wp-lightning-comments/trunk/readme.txt

    r1893334 r2732965  
    11=== Pay to Comment with Lightning ===
    22Contributors: pseudozach
    3 Tags: comments, bitcoin, lightning, micropayment, spamblocker
     3Tags: comments, bitcoin, lightning, micropayment, spam, captcha
    44Requires at least: 4.6
    5 Tested up to: 4.9.6
     5Tested up to: 6.0
    66Stable tag: trunk
    77License: GPLv2 or later
    88License URI: https://www.gnu.org/licenses/gpl-2.0.html
    9 Donate link: https://blog.kriptode.com/donations/
     9Donate link: [email protected]
    1010
    1111Require the user to send a small payment before being able to comment on a wordpress blog.
     
    19191. Upload the plugin files to the `/wp-content/plugins/plugin-name` directory, or install the plugin through the WordPress plugins screen directly.
    20201. Activate the plugin through the 'Plugins' screen in WordPress
    21 1. Select if you want to accept payments on testnet or mainnet under Settings > Pay to Comment with Lightning section
    22 1. Start using it!
     211. Configure provider url and invoice key under Settings > Pay to Comment with Lightning section
     221. Enjoy a spam-free comment section!
    2323
    2424== Frequently Asked Questions ==
    2525
    26 Noone asked anything yet.
     26Q: Which Lightning payment processors are supported?
     27A: LNBits and BTCPayServer
     28
     29Q: Which Lightning Network implementations are supported?
     30A: Both LNBits and BTCPayServer support almost all implementations (CLN, Eclair, LND)
Note: See TracChangeset for help on using the changeset viewer.