How to update index when payment is received with Blockchain PHP API?

0

I want to start accepting Bitcoins payment on my site, I got the callback and the index working fine, the only thing is that it won't update the index once the payment is received, I'd like to give the user some kind of live feedback when the payment is received, how can I do that?

Below is what I got.

Index.php

<?php

$api_key = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx";

$xpub = "xpubxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$secret = "xxxxxxxxxxxxxxxxxxxx";

$rootURL = "https://xxxxxxxxxx.org/BlockChain";

$orderID = uniqid();

$callback_url = $rootURL . "/callback.php?invoice=" . $orderID . "&secret=" . $secret . "&username=Teste" . "&password=qwerty123";
$receive_url = "https://api.blockchain.info/v2/receive?key=" . $api_key . "&xpub=" . $xpub . "&callback=" . urlencode($callback_url);

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $receive_url);
$ccc = curl_exec($ch);

$json = json_decode($ccc, true);

$payTo = $json['address'];

 echo $payTo;

?>

Callback.php

    $secret = "xxxxxxxxxxxxxxxxxxxx";

    if ($_GET['secret'] != $secret) {
        die("Stop doing that!");
    }

    else {
        //Register new user, insert more days, etc...

        //# START DEBUG #
        $fff = fopen("text.txt", "w");
        $value = $_GET['value'] . " - ";
        $fw = fwrite($fff, $value);

        $txhash = $_GET['transaction_hash'] . " - ";
        $fw = fwrite($fff, $txhash);

        $invoice = $_GET['invoice'] . " - ";
        $fw = fwrite($fff, $invoice);

        $value_in_btc = $_GET['value'] / 100000000 . " - ";
        $fw = fwrite($fff, $value_in_btc);

        $username = $_GET['username'] . " - ";
        $fw = fwrite($fff, $username);

        $password = $_GET['password'] . " - ";
        $fw = fwrite($fff, $password);

        fclose($fff);
        //# END DEBUG #

        echo "*ok*"; //Tell blockchain everything is ok, so they stop.
    }

    ?>

Vinny

Posted 2017-01-09T16:57:14.000

Reputation: 3

Answers

1

Your callback will update your server but the user would have to refresh the page to see the change, or you could use AJAX (but that can be wasteful as it will send several requests until there's an update). I'd suggest you use their websocket API to monitor for a transaction so that you can notify the user live on the page.

Below is a simple example, just be sure you update the address variable with the address you want to monitor. You can put your PHP output in the javascript like var address = <?php echo $payTo; ?>;

<html>
User Page<br>
<div id="notifications">Waiting for Payment...</div>
<script>
var address = "BTC_ADDRESS_TO_MONITOR";
var btcs = new WebSocket('wss://ws.blockchain.info/inv');
btcs.onopen = function(){
    btcs.send(JSON.stringify({"op":"addr_sub", "addr":address}));
};
btcs.onmessage = function(onmsg)
{
  var response = JSON.parse(onmsg.data);
  var getOuts = response.x.out;
  var countOuts = getOuts.length; 
  for(i = 0; i < countOuts; i++)
  {
    //check every output to see if it matches specified address
    var outAdd = response.x.out[i].addr;
    var specAdd = address;
       if (outAdd == specAdd)
       {
       var amount = response.x.out[i].value;
       var calAmount = amount / 100000000;
       document.getElementById("notifications").innerHTML = "Received: " + calAmount + "BTC";
       };
  }; 
}
</script>
</html>

m1xolyd1an

Posted 2017-01-09T16:57:14.000

Reputation: 3 356