Advertisement
Guest User

BCAParser(library)

a guest
Feb 27th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.14 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. namespace BCAParser;
  4. /**
  5.  * Handle login request on m.bca
  6.  *
  7.  * no long description
  8.  *
  9.  * @author     kadekjayak <kadekjayak@yahoo.co.id>
  10.  * @copyright  2016 kadekjayak
  11.  * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
  12.  * @version    0.1
  13.  */
  14.  
  15. use DOMDocument;
  16.  
  17. define('BCA_PARSER_DEBUG', false);
  18.  
  19. class BCAParser {
  20.    
  21.     private $username;
  22.     private $password;
  23.    
  24.     protected $curlHandle;
  25.  
  26.     public $_defaultTargets = [
  27.         'loginUrl' => 'https://m.klikbca.com/login.jsp',
  28.         'loginAction' => 'https://m.klikbca.com/authentication.do',
  29.         'logoutAction' => 'https://m.klikbca.com/authentication.do?value(actions)=logout',
  30.         'cekSaldoUrl' => 'https://m.klikbca.com/balanceinquiry.do'
  31.     ];
  32.     protected $isLoggedIn = false;
  33.    
  34.     protected $ipAddress;
  35.    
  36.     public $_defaultHeaders = array(
  37.         'GET /login.jsp HTTP/1.1',
  38.         'Host: m.klikbca.com',
  39.         'Connection: keep-alive',
  40.         'Cache-Control: max-age=0',
  41.         'Upgrade-Insecure-Requests: 1',
  42.         'User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36',
  43.         'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  44.         'Accept-Encoding: gzip, deflate, sdch, br',
  45.         'Accept-Language: en-US,en;q=0.8,id;q=0.6,fr;q=0.4'
  46.     );
  47.    
  48.     /**
  49.     * The Constructor
  50.     * this class will make login request to BCA when initialized
  51.     *
  52.     * @param string $username
  53.     * @param string $password
  54.     */
  55.     public function __construct($username, $password)
  56.     {
  57.         if( BCA_PARSER_DEBUG == true ) error_reporting(E_ALL);
  58.         $this->username = $username;
  59.         $this->password = $password;
  60.         $this->curlHandle = curl_init();
  61.         $this->setupCurl();
  62.         $this->login($this->username, $this->password);
  63.     }
  64.    
  65.     /**
  66.     * Get ip address, required on login parameters
  67.     *
  68.     * @return String;
  69.     */
  70.     private function getIpAddress()
  71.     {
  72.         if($this->ipAddress !== null) $this->ipAddress = json_decode( file_get_contents( 'http://myjsonip.appspot.com/' ) )->ip;
  73.         return $this->ipAddress;
  74.  
  75.     }
  76.    
  77.     /**
  78.     * Execute the CURL and return result
  79.     *
  80.     * @return curl result
  81.     */
  82.     public function exec()
  83.     {
  84.         $result = curl_exec($this->curlHandle);
  85.         if( BCA_PARSER_DEBUG == true ) {
  86.             $http_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
  87.             print_r($result);
  88.  
  89.             /**
  90.             * Perlu diwapadai jangan melakukan pengecekan dengan interval waktu dibawah 10 menit !
  91.             */
  92.             if($http_code == 302) {
  93.                 echo 'HALAMAN DIREDIRECT, harap tunggu beberapa menit ( biasanya 10 Menit! )';
  94.                 exit;
  95.             }
  96.  
  97.         }
  98.         return $result;
  99.     }
  100.    
  101.     /**
  102.     * Register default CURL parameters
  103.     */
  104.     protected function setupCurl()
  105.     {
  106.         curl_setopt( $this->curlHandle, CURLOPT_URL, $this->_defaultTargets['loginUrl'] );
  107.         curl_setopt( $this->curlHandle, CURLOPT_POST, 0 );
  108.         curl_setopt( $this->curlHandle, CURLOPT_HTTPGET, 1 );
  109.         curl_setopt( $this->curlHandle, CURLOPT_HTTPHEADER, $this->_defaultHeaders);
  110.         curl_setopt( $this->curlHandle, CURLOPT_SSL_VERIFYPEER, false );
  111.         curl_setopt( $this->curlHandle, CURLOPT_RETURNTRANSFER, true );
  112.         curl_setopt( $this->curlHandle, CURLOPT_COOKIEFILE,'cookie' );
  113.         curl_setopt( $this->curlHandle, CURLOPT_COOKIEJAR, 'cookiejar' );
  114.     }
  115.    
  116.     /**
  117.     * Set request method on CURL to GET
  118.     */
  119.     protected function curlSetGet()
  120.     {
  121.         curl_setopt( $this->curlHandle, CURLOPT_POST, 0 );
  122.         curl_setopt( $this->curlHandle, CURLOPT_HTTPGET, 1 );
  123.     }
  124.    
  125.     /**
  126.     * Set request method on CURL to POST
  127.     */
  128.     protected function curlSetPost()
  129.     {
  130.         curl_setopt( $this->curlHandle, CURLOPT_POST, 1 );
  131.         curl_setopt( $this->curlHandle, CURLOPT_HTTPGET, 0 );
  132.     }
  133.    
  134.     /**
  135.     * Login to BCA
  136.     */
  137.     private function login($username, $password)
  138.     {
  139.         //Just to Get Cookies
  140.         curl_setopt( $this->curlHandle, CURLOPT_URL, $this->_defaultTargets['loginUrl'] );
  141.         $this->curlSetGet();
  142.         $this->exec();
  143.        
  144.         //Sending Login Info
  145.         $this->getIpAddress();
  146.         $params = array(
  147.             "value(user_id)={$username}",
  148.             "value(pswd)={$password}",
  149.             'value(Submit)=LOGIN',
  150.             'value(actions)=login',
  151.             "value(user_ip)={$this->ipAddress}",
  152.             "user_ip={$this->ipAddress}",
  153.             'value(mobile)=true',
  154.             'mobile=true'
  155.         );
  156.         $params = implode( '&', $params );
  157.         $this->curlSetPost();
  158.         curl_setopt( $this->curlHandle, CURLOPT_URL, $this->_defaultTargets['loginAction'] );
  159.         curl_setopt( $this->curlHandle, CURLOPT_REFERER, $this->_defaultTargets['loginUrl'] );
  160.         curl_setopt( $this->curlHandle, CURLOPT_POSTFIELDS, $params );
  161.         $this->exec();
  162.         $this->isLoggedIn = true;
  163.     }
  164.  
  165.     /**
  166.      * Get saldo rekening pages
  167.      *
  168.      * @return string
  169.      */
  170.     public function getSaldo()
  171.     {
  172.         if( !$this->isLoggedIn ) $this->login( $this->username, $this->password );
  173.        
  174.         $this->curlSetPost();
  175.        
  176.         curl_setopt( $this->curlHandle, CURLOPT_URL, 'https://m.klikbca.com/accountstmt.do?value(actions)=menu' );
  177.         curl_setopt( $this->curlHandle, CURLOPT_REFERER, $this->_defaultTargets['loginAction'] );
  178.         $this->exec();
  179.         curl_setopt( $this->curlHandle, CURLOPT_URL, $this->_defaultTargets['cekSaldoUrl'] );
  180.         curl_setopt( $this->curlHandle, CURLOPT_REFERER, 'https://m.klikbca.com/accountstmt.do?value(actions)=menu' );
  181.         $result = $this->exec();
  182.         $result = $this->getSaldoRekeningTable($result);
  183.         $result = $this->getArrayValuesSaldo($result);
  184.         return $result;
  185.     }
  186.    
  187.     /**
  188.     * Parse the pages on saldo rekening
  189.     * this method will return only elements on <table> tag that contain only rekening and its saldo
  190.     *
  191.     * @param string $html
  192.     * @return string
  193.     */
  194.     private function getSaldoRekeningTable($html)
  195.     {
  196.         $dom = new DOMDocument();
  197.    
  198.         if ( BCA_PARSER_DEBUG ) {
  199.             $dom->loadHTML($html); 
  200.         } else {
  201.             @$dom->loadHTML($html);
  202.         }
  203.        
  204.         $dom->getElementById('pagebody');
  205.        
  206.         $table = $dom->getElementsByTagName('table');
  207.         $table = $table->item(3);
  208.         return $dom->saveHTML($table);
  209.     }
  210.  
  211.     /**
  212.      * Get array value from data saldo page
  213.      *
  214.      * @param  string $html
  215.      * @return array
  216.      *  {
  217.      *     {'rekening'=>'norek1', 'saldo'=>'100.000'},
  218.      *     {'rekening'=>'norek2', 'saldo'=>'100.000'}
  219.      *  }
  220.      */
  221.     private function getArrayValuesSaldo($html)
  222.     {
  223.         $dom = new DOMDocument();
  224.         $dom->loadHTML($html);
  225.         $table = $dom->getElementsByTagName('table');
  226.         $rows = $dom->getElementsByTagName('tr');
  227.         $datas = [];
  228.         for ($i = 0; $i < $rows->length; $i++) {
  229.             if($i == 0) continue; // skip head
  230.            
  231.             $cols = $rows->item($i)->getElementsbyTagName("td");
  232.             $rekening = $cols->item(0)->nodeValue;
  233.             $saldo = $cols->item(2)->nodeValue;
  234.             $data = compact('rekening','saldo');
  235.             $datas[] = $data;
  236.         }
  237.         return $datas;
  238.     }
  239.    
  240.     /**
  241.     * Get mutasi rekening pages
  242.     *
  243.     * @param string $from 'Y-m-d'
  244.     * @param string $to 'Y-m-d'
  245.     * @return string
  246.     */
  247.     public function getMutasiRekening($from, $to)
  248.     {
  249.         if( !$this->isLoggedIn ) $this->login( $this->username, $this->password );
  250.        
  251.         $this->curlSetPost();
  252.        
  253.         curl_setopt( $this->curlHandle, CURLOPT_URL, 'https://m.klikbca.com/accountstmt.do?value(actions)=menu' );
  254.         curl_setopt( $this->curlHandle, CURLOPT_REFERER, $this->_defaultTargets['loginAction'] );
  255.         $this->exec();
  256.  
  257.         curl_setopt( $this->curlHandle, CURLOPT_URL, 'https://m.klikbca.com/accountstmt.do?value(actions)=acct_stmt' );
  258.         curl_setopt( $this->curlHandle, CURLOPT_REFERER, 'https://m.klikbca.com/accountstmt.do?value(actions)=menu' );
  259.         $this->exec();
  260.        
  261.         $params = array(
  262.                 'r1=1',
  263.                 'value(D1)=0',
  264.                 'value(startDt)=' . date( 'j', strtotime($from) ),
  265.                 'value(startMt)=' . date( 'n', strtotime($from) ),
  266.                 'value(startYr)=' . date( 'Y', strtotime($from) ),
  267.                 'value(endDt)=' . date( 'j', strtotime($to) ),
  268.                 'value(endMt)=' . date( 'n', strtotime($to) ),
  269.                 'value(endYr)=' . date( 'Y', strtotime($to) )
  270.                 );
  271.         $params = implode( '&', $params );
  272.        
  273.         curl_setopt( $this->curlHandle, CURLOPT_URL, 'https://m.klikbca.com/accountstmt.do?value(actions)=acctstmtview' );
  274.         curl_setopt( $this->curlHandle, CURLOPT_REFERER, 'https://m.klikbca.com/accountstmt.do?value(actions)=acct_stmt' );
  275.         curl_setopt( $this->curlHandle, CURLOPT_POSTFIELDS, $params );
  276.         $html = $this->exec();
  277.  
  278.         return $this->getMutasiRekeningTable($html);
  279.     }
  280.    
  281.     /**
  282.     * Parse the pages on mutasi rekening
  283.     * this method will return only elements on <table> tag that contain only list of transaction
  284.     *
  285.     * @param string $html
  286.     * @return string
  287.     */
  288.     private function getMutasiRekeningTable($html)
  289.     {
  290.         $dom = new DOMDocument();
  291.    
  292.         if ( BCA_PARSER_DEBUG ) {
  293.             $dom->loadHTML($html); 
  294.         } else {
  295.             @$dom->loadHTML($html);
  296.         }
  297.        
  298.         $dom->getElementById('pagebody');
  299.        
  300.         $table = $dom->getElementsByTagName('table');
  301.         $table = $table->item(4);
  302.         return $dom->saveHTML($table);
  303.     }
  304.  
  305.  
  306.     private function getArrayValues($html)
  307.     {
  308.         $dom = new DOMDocument();
  309.         $dom->loadHTML($html);
  310.         $table = $dom->getElementsByTagName('table');
  311.         $rows = $dom->getElementsByTagName('tr');
  312.  
  313.         $datas = [];
  314.         for ($i = 0; $i < $rows->length; $i++) {
  315.             if($i== 0 ) continue;
  316.             $cols = $rows->item($i)->getElementsbyTagName("td");
  317.  
  318.             $date = $cols->item(0)->nodeValue;
  319.             $date = explode('/', $date);
  320.             $date = date('Y') . '-' . $date[1] . '-' . $date[0];
  321.  
  322.             $description = $cols->item(1);
  323.             $flows = $cols->item(2)->nodeValue;
  324.             $descriptionText = $dom->saveHTML($description);
  325.  
  326.             $descriptionText = str_replace('<td>', '', $descriptionText);
  327.             $descriptionText = str_replace('</td>', '', $descriptionText);
  328.             $description = explode('<br>', $descriptionText);
  329.  
  330.             $data = compact('date','description', 'flows');
  331.             $datas[] = $data;
  332.         }
  333.         return $datas;
  334.     }
  335.  
  336.  
  337.     /**
  338.     * Ambil daftar transaksi pada janga waktu tertentu
  339.     *
  340.     *
  341.     * @param string $from 'Y-m-d'
  342.     * @param string $to 'Y-m-d'
  343.     * @return array
  344.     **/
  345.     public function getListTransaksi($from, $to)
  346.     {
  347.         $result = $this->getMutasiRekening($from, $to);
  348.         $result = $this->getArrayValues($result);
  349.         return $result;
  350.     }
  351.  
  352.     /**
  353.     * getTransaksiCredit
  354.     *
  355.     * Ambil semua list transaksi credit (kas Masuk)
  356.     *
  357.     * @param string $from 'Y-m-d'
  358.     * @param string $to 'Y-m-d'
  359.     * @return array
  360.     */
  361.     public function getTransaksiCredit($from, $to)
  362.     {
  363.         $result = $this->getListTransaksi($from, $to);
  364.         $result = array_filter($result, function($row){
  365.             return $row['flows'] == 'CR';
  366.         });
  367.         return $result;
  368.     }
  369.  
  370.     /**
  371.     * getTransaksiDebit
  372.     *
  373.     * Ambil semua list transaksi debit (kas Keluar)
  374.     * Struktur data tidak konsisten !, tergantung dari jenis transaksi
  375.     *
  376.     * @param string $from 'Y-m-d'
  377.     * @param string $to 'Y-m-d'
  378.     * @return array
  379.     */
  380.     public function getTransaksiDebit($from, $to)
  381.     {
  382.         $result = $this->getListTransaksi($from, $to);
  383.         $result = array_filter($result, function($row){
  384.             return $row['flows'] == 'DB';
  385.         });
  386.         return $result;
  387.     }
  388.  
  389.  
  390.     /**
  391.     * Logout
  392.     *
  393.     * Logout from KlikBca website
  394.     * Lakukan logout setiap transaksi berakhir!
  395.     *
  396.     * @return string
  397.     */
  398.     public function logout()
  399.     {
  400.         $this->curlSetGet();
  401.         curl_setopt( $this->curlHandle, CURLOPT_URL, $this->_defaultTargets['logoutAction'] );
  402.         curl_setopt( $this->curlHandle, CURLOPT_REFERER, $this->_defaultTargets['loginUrl'] );
  403.         return $this->exec();
  404.     }
  405.  
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement