PHP Example

You can automate the receipt of SIB payments on your website. To do so, you need to install and run a full SIBCoin node. Generate a unique SIB address to each client for receiving payments. Periodically, check incoming transactions on your wallet via script. When you discover a new incoming transaction, find out the address where the funds come. Find out which client related to this address and increase his balance.Thereafter, add a record about the transaction to the database to be sure that deposit was counted only once. As you can see the algorithm is quite simple. See the example below:
require_once 'easysibcoin.php'; // https://github.com/ivansib/EasySibcoin-PHP

use easysibcoin\EasySibcoin as EasySibcoin;
use easysibcoin\EasySibcoinException as EasySibcoinException;

$db = new PDO("mysql:host=localhost;dbname=shop", 'login', 'password');  
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$db->exec("set names utf8");

$sibcoin = new EasySibcoin('sibcoinrpc','sibcoinpassword');

/* $sibcoin->getnewaddress("NAME"); => this way you can generate a new address to the client */

try {
    // Get list of transactions 
    $transactions = $sibcoin->listtransactions("*", 100000);
    
} catch (EasySibcoinException $easyException) {
    die("Can't get list of transaction, error: "
        .$easyException->getMessage()."\n".$easyException->getTraceAsString()
    );
}

// Iterate through array of transactions     
foreach ($transactions as $idx => $tx) {

    // Check tx's type, confirmations and amount 
    if($tx["category"] != "receive" || $tx["confirmations"] < 6 || $tx["amount"] < 0.001) continue;

    // Check if tx is already in DB 
    $select = $db->prepare("SELECT count(*) FROM `bills` WHERE `pid` =:id");
    $select->execute(['id' => $tx["txid"]]);
    $rowCount = $select->fetch()[0];
    if($rowCount > 0) continue;

    // Check if there is a user with tx address in DB 
    $select = $db->prepare("SELECT * FROM `users` WHERE `sibcoin` =:address");
    $select->bindParam(':address', $tx["address"]);
    $select->execute();
    if($select->rowCount() != 1) continue;
    $row = $select->fetch();
    $user_id = $row['user_id'];        

    // Get the exchange rate for RUB 
    $ticker = json_decode(file_get_contents("https://yobit.net/api/3/ticker/sib_rur"), true);
    if(empty($ticker['sib_rur']['last'])) die('cant get latest RUB rate');
    $rate = (double)$ticker['sib_rur']['last'];

    // Start DB transaction 
    $db->beginTransaction();

    try{
        // Update user's balance 
        $money = round($tx["amount"]*$rate,2);
        $update = $db->prepare("UPDATE `users` SET `money` = `money`+:money WHERE `user_id` = :user_id");
        $update->bindParam(':money', $money);
        $update->bindParam(':user_id', $user_id);
        $update->execute();        

        // Create a new bill record 
        $insert = $db->prepare("INSERT INTO `bills`(`pid`, `amount`, `date`, `system`, `user_id`) "
                             . "VALUES ( :id, :money, NOW(), 'SIB', :user_id)");        
        $insert->bindParam(':id', $tx["txid"]);
        $insert->bindParam(':money', $money);
        $insert->bindParam(':user_id', $user_id);
        $insert->execute();

        $db->commit();

    } catch (Exception $ex) {
        // Rollback DB transaction 
        $db->rollBack();
        // Print error message 
        print "DB error while updating payments for user\n";
        print "User ID: ".$user_id."\n";
        print "Tx ID: ".$tx["txid"]."\n";
        print "Error message: ".$ex->getMessage()."\n";
        print "Trace: ".$ex->getTraceAsString()."\n";
        
        continue;
    }
    
    print date('Y-m-d H:i:s')." Received ".$money." RUB (".$tx["amount"]." SIB) for user ".$user_id."\n";
}
loader