Menu Close

Tutorial script notifikasi perubahan saldo Bank BCA

Berikut adalah tutorial untuk script yang akan menampilkan data saldo pada rekening Bank BCA yang kita miliki, serta dapat mengirimkan email pemberitahuan jika ada perubahan (penambahan ataupun pengurangan)

Ada tiga file yang diperlukan, serta satu cron untuk memonitor perubahan dan mengirimkan email.

/var/www/saldo/ 
---index.php 
---IbParser.php 
---notifikasi.php

 

 

Berikut adalah isi file index.php

<html>
<head>
<title>Mutasi BCA</title>

<style>
body, th, td, pre {
	font-size:12px;
	color: #000;
	font-family:'Open Sans',Verdana, Geneva, sans-serif;
}
</style>

<head>
<body>

<?php
error_reporting( E_ALL );
require( 'IbParser.php' );
$parser = new IbParser();
?>


<?php
$bank    = 'BCA';
$user    = 'useridKlikBCA';
$pass    = 'passwordKlikBCA';
$balance = $parser->getBalance( $bank, $user, $pass );
?>

<table>
	<tr>
		<th align="left">Transaction History:</th>
	</tr>

	<tr>
		<td><?php echo $bank . ' ' . $user ?></td>
		<td><?php echo "Last access: ".date("F d Y H:i:s.",fileatime("index.php")); ?></td>
	</tr>

</table>

<hr />

<?php $transactions = $parser->getTransactions( $bank, $user, $pass ); ?>

<table>
	<tr>
		<th>&nbsp;</th>
		<th align="left">Tgl</th>
		<th align="left">Keterangan</th>
		<th>DB/CR</th>
		<th>Nominal</th>
	</tr>
	
	<?php foreach( $transactions as $index => $baris ) : ?>
	
	<tr>
		<td>&nbsp;</td>
		<td><?php echo $baris[0]; ?></td>
		<td><?php echo $baris[1]; ?></td>
		<td><?php echo $baris[2]; ?></td>
		<td><?php echo $baris[3]; ?></td>
	</tr>
	<?php endforeach; ?>
</table>


<table>
	<tr>
	<th>&nbsp;</th>
	<th>&nbsp;</th>
	<th>SALDO:</th>
	<th>&nbsp;</th>
		<th align="left"><?php echo ( !$balance )? 'Gagal mengambil saldo': number_format( $balance, 2 ); ?></th>
	</tr>
</table>

<body>

Berikut adalah isi file IbParser.php

<?php




class IbParser
{




    function __construct()
    {
        $this->conf['ip']       = json_decode( file_get_contents( 'http://myjsonip.appspot.com/' ) )->ip;
        $this->conf['time']     = time() + ( 3600 * 1 );
	// Pada fungsi diatas, kita memakai time() + ( 3600 *14 )
	// Artinya jam server ditambah 14 jam. 
	// Jika nanti gagal mengambil transaksi, maka rubah angka 14 menjadi 1, yaitu jam server ditambah 1
        $this->conf['path']     = dirname( __FILE__ );
    }




    function instantiate( $bank )
    {
        $class = $bank . 'Parser';
        $this->bank = new $class( $this->conf ) or trigger_error( 'Undefined parser: ' . $class, E_USER_ERROR );
    }




    function getBalance( $bank, $username, $password )
    {

        $this->instantiate( $bank );
        $this->bank->login( $username, $password );
        $balance = $this->bank->getBalance();
        $this->bank->logout();
        return $balance;

    }




    function getTransactions( $bank, $username, $password )
    {

        $this->instantiate( $bank );
        $this->bank->login( $username, $password );
        $transactions = $this->bank->getTransactions();
        $this->bank->logout();
        return $transactions;

    }

}




class BCAParser
{




    function __construct( $conf )
    {

        $this->conf = $conf;

        $d          = explode( '|', date( 'Y|m|d|H|i|s', $this->conf['time'] ) );
        $start      = mktime( $d[3], $d[4], $d[5], $d[1], ( $d[2] - 3 ), $d[0] );
	// Fungsi diatas, pada $d[2] - 3, berarti menampilkan transaksi dalam 3 hari terakhir
	// Bisa dirubah menjadi 30 atau 7 hari terakhir
        $this->post_time['end']['y'] = $d[0];
        $this->post_time['end']['m'] = $d[1];
        $this->post_time['end']['d'] = $d[2];
        $this->post_time['start']['y'] = date( 'Y', $start );
        $this->post_time['start']['m'] = date( 'm', $start );
        $this->post_time['start']['d'] = date( 'd', $start );
    }




    function curlexec()
    {
        curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1 );
	// Untuk menampilkan hasil CURL, dan debugging error, rubah nilai 1 diatas menjadi 0
	
	// Fungsi tambahan untuk mencegah error
	curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0); //skipping SSL_CERT for host
	curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); //skipping SSL_CERT
	curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 0); //ignoring server redirect
	//

        return curl_exec( $this->ch );
    }




    function login( $username, $password )
    {

        $this->ch = curl_init();

        curl_setopt( $this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/GRK39F) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' );
        curl_setopt( $this->ch, CURLOPT_URL, 'https://m.klikbca.com/login.jsp' );
        curl_setopt( $this->ch, CURLOPT_COOKIEFILE, $this->conf['path'] . '/cookie' );
        curl_setopt( $this->ch, CURLOPT_COOKIEJAR, $this->conf['path'] . '/cookiejar' );

        $this->curlexec();

        $params = implode( '&', array( 'value(user_id)=' . $username, 'value(pswd)=' . $password, 'value(Submit)=LOGIN', 'value(actions)=login', 'value(user_ip)=' . $this->conf['ip'], 'user_ip=' . $this->conf['ip'], 'value(mobile)=true', 'mobile=true' ) );

        curl_setopt( $this->ch, CURLOPT_URL, 'https://m.klikbca.com/authentication.do' );
        curl_setopt( $this->ch, CURLOPT_REFERER, 'https://m.klikbca.com/login.jsp' );
        curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $params );
        curl_setopt( $this->ch, CURLOPT_POST, 1 );

        $this->curlexec();

    }




    function logout()
    {
        curl_setopt( $this->ch, CURLOPT_URL, 'https://m.klikbca.com/authentication.do?value(actions)=logout' );
        curl_setopt( $this->ch, CURLOPT_REFERER, 'https://m.klikbca.com/authentication.do?value(actions)=menu' );
        $this->curlexec();
        return curl_close( $this->ch );
    }




    function getBalance()
    {

        curl_setopt( $this->ch, CURLOPT_URL, 'https://m.klikbca.com/accountstmt.do?value(actions)=menu' );
        curl_setopt( $this->ch, CURLOPT_REFERER, 'https://m.klikbca.com/authentication.do' );

        $this->curlexec();

        curl_setopt( $this->ch, CURLOPT_URL, 'https://m.klikbca.com/balanceinquiry.do' );
        curl_setopt( $this->ch, CURLOPT_REFERER, 'https://m.klikbca.com/accountstmt.do?value(actions)=menu' );

        $src = $this->curlexec();

        $parse = explode( "<td align='right'><font size='1' color='#0000a7'><b>", $src );

        if ( empty( $parse[1] ) )
            return false;

        $parse = explode( '</td>', $parse[1] );

        if ( empty( $parse[0] ) )
            return false;

        $parse = str_replace( ',', '', $parse[0] );

        return ( is_numeric( $parse ) )? $parse: false;

    }




    function getTransactions()
    {

        curl_setopt( $this->ch, CURLOPT_URL, 'https://m.klikbca.com/accountstmt.do?value(actions)=menu' );
        curl_setopt( $this->ch, CURLOPT_REFERER, 'https://m.klikbca.com/authentication.do' );

        $this->curlexec();

        curl_setopt( $this->ch, CURLOPT_URL, 'https://m.klikbca.com/accountstmt.do?value(actions)=acct_stmt' );
        curl_setopt( $this->ch, CURLOPT_REFERER, 'https://m.klikbca.com/accountstmt.do?value(actions)=menu' );

        $this->curlexec();

        $params = implode( '&', array( 'r1=1', 'value(D1)=0', 'value(startDt)=' . $this->post_time['start']['d'], 'value(startMt)=' . $this->post_time['start']['m'], 'value(startYr)=' . $this->post_time['start']['y'],'value(endDt)=' . $this->post_time['end']['d'], 'value(endMt)=' . $this->post_time['end']['m'], 'value(endYr)=' . $this->post_time['end']['y'] ) );

        curl_setopt( $this->ch, CURLOPT_URL, 'https://m.klikbca.com/accountstmt.do?value(actions)=acctstmtview' );
        curl_setopt( $this->ch, CURLOPT_REFERER, 'https://m.klikbca.com/accountstmt.do?value(actions)=acct_stmt' );
        curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $params );
        curl_setopt( $this->ch, CURLOPT_POST, 1 );

        $src = $this->curlexec();

        $parse = explode( '<table width="100%" class="blue">', $src );

        if ( empty( $parse[1] ) )
            return false;

        $parse = explode( '</table>', $parse[1] );
        $parse = explode( '<tr', $parse[0] );

        $rows = array();

        foreach( $parse as $val )
            if ( substr( $val, 0, 8 ) == ' bgcolor' )
                $rows[] = $val;

        foreach( $rows as $key => $val )
        {
            $rows[$key]     = explode( '</td>', $val );
            $rows[$key][0]  = substr( $rows[$key][0], -5 );
            if ( stristr( $rows[$key][0], 'pend' ) )
                $rows[$key][0] = 'PEND';
            $detail         = explode( "<td valign='top'>", $rows[$key][1] );
            $rows[$key][2]  = $detail[1];
            $rows[$key][1]  = explode( '<br>', $detail[0] );
            $rows[$key][3]  = str_replace( ',', '', $rows[$key][1][count($rows[$key][1])-1] );
            unset( $rows[$key][1][count($rows[$key][1])-1] );
            foreach( $rows[$key][1] as $k => $v )
                $rows[$key][1][$k] = trim( strip_tags( $v ) );
            $rows[$key][1] = implode( " ", $rows[$key][1] );
        }

        return ( !empty( $rows ) )? $rows: false;

    }

}

Berikut adalah isi file notifikasi.php

#!/usr/local/bin/php
<?php

error_reporting( E_ALL );





// Akun-akun yang akan dicek.
// Parameter terakhir adalah interval pengecekan agar independen dari cron.
// File ini bisa dipanggil tiap 5-10 menit sekali lewat cron.

$accounts = array(

    array( 'BCA', 'useridKlikBCA', 'passwordKlikBCA', 'alamat.email@domain.com', ( 60 * 30 ) ), // tiap 30 menit
    

);

// Saya mendapat tips legal bahwa otomasi login ke klik BCA dibolehkan
// selama dilakukan oleh pemilik akun.
// Saya juga mendapat tips teknis yang menyebutkan agar login dilakukan
// tidak lebh dari 100x per hari.




// Jalan

run( $accounts );




// run() logic

function run( $accounts )
{

    require( 'IbParser.php' );

    $notifier   = new IbParser;
    $datadir    = dirname( __FILE__ ) . '/data';

    if ( !is_dir( $datadir ) )
        mkdir( $datadir );




    // Langkah-langkah untuk setiap akun

    foreach( $accounts as $account )
    {


        // Periksa file data, kalau false langsung lanjut ke akun berikut

        if ( !$balance = checkDataFile( $account, $datadir ) )
            continue;


        // Ambil balance, kalau false langsung lanjut

        if ( !$new_balance = $notifier->getBalance( $account[0], $account[1], $account[2] ) )
            continue;


        $balance = json_decode( $balance )->balance;


        // Update file data walaupun balancenya sama

        updateDataFile( $account, $datadir, $new_balance );


        // Bandingkan balance, kalau sama langsung lanjut

        if ( $balance == $new_balance )
            continue;


        // Ambil transaksi

        $transactions = $notifier->getTransactions( $account[0], $account[1], $account[2] );


        // Kirim email

        notify( $account, $balance, $new_balance, $transactions );

    }

}




function checkDataFile( $account, $datadir )
{

    $datafile = $datadir . '/' . md5( $account[0] . $account[1] );

    if ( !file_exists( $datafile ) )
    {
        touch( $datafile );
        return json_encode( array( 'balance' => 0 ) );
    }

    if ( filemtime( $datafile ) > time() - $account[4] )
        return false;
    else
        return file_get_contents( $datafile );

}




function updateDataFile( $account, $datadir, $new_balance )
{
    $datafile = $datadir . '/' . md5( $account[0] . $account[1] );
    $fh = fopen( $datafile, 'w' );
    fwrite( $fh, json_encode( array( 'balance' => $new_balance ) ) );
    fclose( $fh );
}




function notify( $account, $balance, $new_balance, $transactions )
{

    $difference = $new_balance - $balance;

    $subject    = ( $difference > 0 )? 'Peningkatan ': 'Penurunan ';
    $subject   .= 'Saldo ' . $account[0] . ' sebesar Rp ' . number_format( abs( $difference ), 2 );


     // Ganti dengan email anda (yang boleh dikirim dari server)

    $headers    = 'From: "BCA Notifier"<notifier@namadomain.com>' . "\n";


    $body       = 'Saldo saat ini Rp ' . number_format( ( $new_balance ), 2 ); // 50 ribu saldo wajib di BCA
    $body      .= "\n\n" . 'Transaksi:' . "\n";


    if ( $transactions )
    {

        foreach( $transactions as $transaction )
        {

            $drcr   = ( $transaction[2] == 'CR' )? 'CREDIT': 'DEBIT';
            $body  .= "\n" . $transaction[0] . ' === ' . number_format( $transaction[3], 2 ) . ' === ' . $drcr . "\n" . strtolower( str_replace( "\n", ' ', $transaction[1] ) ) . "\n";

        }

    }
    else
    {
        $body  .= 'Gagal mengambil transaksi';
    }

    mail( $account[3], $subject, $body, $headers );


}

Dan berikut adalah contoh cron

*/15 * * * * /usr/bin/php /var/www/saldo/notifikasi.php > /dev/null 2>&1

Karena berisi data login ke KlikBCA, maka silakan diamankan lokasi dan akses ke script tadi dari akses publik, misalnya memakai .htaccess atau authentifikasi user atau IP

Bisa juga didownload dari [sini]

Ada informasi juga, kalau maksimal akses ke KlikBCA adalah 100 kali  per hari, jadi silakan disesuaikan

JIka script ini bermanfaat, jangan sungkan untuk memberikan donasinya 😀

Leave a Reply

Your email address will not be published. Required fields are marked *