bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

am having my script for receiving bitcoin payments similar to PHP Receive Bitcoin Payments. almost that's what inspired me to do one in Perl.

I can check the received amount or if payment received but my problem is that how my script can determine that payment received and form action

I had a plan of using javascript to keep reloading the page. if our Perl scripts print Thank You. We Have Received Your Payment. then javascript can determine and stops reloading page frequently

I appricaite for any idea, and sample to do it better than that

Please, Feedback should not include of using mojo, dancer or crystal. Thanks

The Blockchain API Key and XPUB is valid. anyone is allowed to test

#!/usr/bin/perl -w use HTTP::Request::Common qw(POST); use LWP::UserAgent; use JSON::XS; use Net::SSL; use CGI; use URI; use DBI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $CGI = CGI->new(); my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, ); # DB CONNECTION my $host = "localhost"; my $usr = "root"; my $pwd = ""; my $dbname = "tbtest"; my $DBH = DBI->connect("DBI:mysql:$dbname:$host", $usr, $pwd, { AutoCommit => 0, RaiseError => 1, }) or die $DBI::errstr; # ORDER DETAILS $ORDER_ID = '2018'; $BUYER_ID = 'test@test.com'; # BUYER AMOUNT HE/SHE SUPPOSED TO SEND IN USD $USD = $CGI->param("usd_amount"); # CONVERT BUYER USD TO BTC if ($USD){ my $amount_req = HTTP::Request->new(GET => "https://blockchain.info/to +btc?currency=USD&value=$USD"); $amount_req->content_type('application/json'); my $amount_res = $ua->request($amount_req); $amount = $amount_res->content; # GENERATE RECEIVING ADDRESS FOR PAYMENT $LINK = 'https://api.blockchain.info/v2/receive'; $API_KEY = "0698d13e-56cd-47ec-9e98-f166b1cba5ec"; $XPUB = "xpub6CrqZSp5gsjSGSfVjzEnKxJ6hrQ2AsPh1rUzGSHWM4bFRKqsWEyYZ3 +YTJeNcusJJToq9G9WAmRzZ8PJYanQW3mLQcJVcVUqEL7v1Z3SP6jk"; $CALL_BACK_URL = "https://localhost"; my $do = URI->new($LINK); $do->query_form( xpub => $XPUB, callback => $CALL_BACK_URL, key => $API_KEY, ); my $req = HTTP::Request->new(GET => $do); $req->content_type('application/json'); my $res = $ua->request($req); $respons = JSON::XS->new->decode ($res->content); $wallet = $respons->{address}; $html_p_tag = "Your Sending $amount BTC"; $html_p_tag2 = "To This Wallet $wallet"; if ($res) { #IF REQUEST SENT THEN # URL TO CHECK RECEIVED PAYMENTS $doLink = 'https://insight.bitpay.com/api/addr/19By3YDM62ivoPQSxsshnPt +cDzKh6nZMFR/?noTxList=1'; my $check_received = HTTP::Request->new(GET => $doLink); $check_received->content_type('application/json'); my $res_received = $ua->request($check_received); $respons_received = JSON::XS->new->decode ($res_received->content); $received = $respons_received->{totalReceived}; # CHECK FOR NOT RECEIVED PAYMENT if ($received ne $amount) { $msg_1 = "Not Yet Received Your Payment"; } # CHECK IF PAYMENT / ACTUAL AMOUNT RECEIVED if ($received eq $amount) { my $create_order = $DBH->prepare("INSERT INTO ORDERS(ORDER_ID, BUY +ER_ID) VALUES(?,?)"); $create_order->execute($ORDER_ID, $BUYER_ID); $create_order->finish(); $msg_2 = "Thank You. We Have Received Your Payment"; } }} print "Content-type: text/html\n\n"; print <<START_HTML; <!DOCTYPE html> <html lang="en"> <head> <title>Blockchain Receive Payment In Perl</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <style type="text/css"> <!-- .red {color: #FF0000} .green {color: #006600} --> </style> </head> <body> <form method="post"> <p> <input type="text" name="usd_amount" placeholder="Amount In USD" req +uired> </p> <label> <input type="submit" name="Submit" value="Pay Now" /> </label> </form> <p>$html_p_tag</p> <p>$html_p_tag2</p> <div class="red"> $msg_1 </div> </div> <div class="green"> $msg_2 </div> </div> </body> </html> START_HTML

Replies are listed 'Best First'.
Re: receiving btc payments
by marto (Cardinal) on Dec 07, 2018 at 16:42 UTC
    # BUYER AMOUNT HE/SHE SUPPOSED TO SEND IN USD $USD = $CGI->param("usd_amount");

    Users can alter these values easily. I can't see any obvious checking for double spends/chargebacks.

    "I had a plan of using javascript to keep reloading the page. if our Perl scripts print Thank You. We Have Received Your Payment. then javascript can determine and stops reloading page frequently"

    Reloading the page is a bad idea, just look at what your code does to understand why. You also don't take into account the processing time for a BTC transaction, this is totally out of your hands.

      ya reloading page is bad idea, but i have no way to do it. i have to use it

      uniless there any other way. i can do it

        Ignoring all the other issues, you can't even be following the code you wrote. You don't keep track of who has paid for what unless the transaction has hit the blockchain, each page refresh you throw everything away. This is unworkable as any form of payment system.