b0y-101 Mini Shell


Current Path : E:/www/b-group.old/spfin/application/api/apipro03/callbackpayment/
File Upload :
Current File : E:/www/b-group.old/spfin/application/api/apipro03/callbackpayment/index.php

<?php
header("Content-type: application/json; charset=utf-8");

function myfunction_key($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}

function myfunction_value($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}

if($_SERVER['REQUEST_METHOD'] != "POST") {
    header("HTTP/1.0 403 Forbidden");
    print("Forbidden");
    exit();
}else  {

   echo $_SERVER['REQUEST_METHOD'];
	
   $data = json_decode(file_get_contents('php://input'), true);
   $dtransdate1 = $data['TransDate'];
 //server post
  $strFileName = 'SERVER03' . $dtransdate1 . ".txt";
  file_put_contents($strFileName, $_SERVER['REQUEST_METHOD'],json_encode($data));
	//var_dump($data);

if(!empty($data)){
    //$data ข้อมูลไม่ empty
    
    include("apipay/callid.php");
    $dbankref = $data["BankRef"];
    $didbk = $data["BillerNo"];
    //$dcodebk = $data["QRId"];  ธนาคารไม่ได้เช็คตัวนี้ ไว้เช็คที่หลัง
    $dtransdate = $data['TransDate'];
	//echo($dbankref);
	//echo($didbk);


    if($idbk != $didbk){
        //echo 'BillerNo ไม่ตรง ';
        $dbankref = $data["BankRef"];
        $dtransdate = $data['TransDate'];
        $dresultcode = "003";
                 $dresultdesc = "Invalid BillerNo";
                 $databillerno = array('BankRef'=>$dbankref , 'ResCode'=>$dresultcode , 'ResDesc'=>$dresultdesc , 'TransDate'=>$dtransdate);

                 $strFileName = 'error_BillerNo_tmb_' . $dtransdate . ".txt";
                 file_put_contents($strFileName, json_encode($databillerno));
               
                 $curl = curl_init();

                curl_setopt_array($curl, array(
                  //CURLOPT_URL => 'datagateway.ttbbank.com',
				  CURLOPT_URL => 'www.datagateway.ttbbank.com',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => '',
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 0,
                  CURLOPT_FOLLOWLOCATION => true,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => 'POST',
                  CURLOPT_POSTFIELDS =>json_encode($databillerno),
                  CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json'
                  ),
                ));

                $response = curl_exec($curl);

                curl_close($curl);
                echo $response;
                echo json_encode($databillerno); //ส่งให้ธนาคาร

            }//if($idbk != $didbk )
         else{
       //BillerNo เท่ากับระบบ
       //if  ($codebk == $dcodebk) { ไม่เช็ค QRID เพราะธนาคารไม่ได้เช็ค ถ้า QRID ไม่ตรงก็จ่ายเงินได้
       //BillerNo และ QRId ตรงกับระบบ
       // echo 'QRId ตรง ';
       //  echo 'BillerNo ตรง ';

       //เชื่อมต่อ database
	    
       include("../../../../conapps/dbcon.php");
	   //connect database
       $objConnect = mysqli_connect($servername,$username, $password, $dbname);
       
       //คำสั่ง SQL แสดงข้อมูลทั้งหมด
            $dref2 = $data['Ref2'];
			 //แก้ไข tbsub.qrid
            $strSQL = "SELECT tbqr.ref1,tbqr.ref2,tbqr.amount,tbqr.amount2,tbsub.subbiller,tbbk.billeridru,tbqr.timepay,tbpro.pnameem,tbqr.ref3,tbqr.payername,tbqr.payerbank,tbqr.bankref,tbqr.statuspay,tbqr.dstatus,tbqr.time FROM tb_qrapicp tbqr, tb_projectem tbpro, tb_subbank tbsub, tb_bank tbbk WHERE tbqr.ref2='".$dref2."' and tbqr.idproject=tbpro.idproem and tbpro.idsubbank=tbsub.idsub and tbsub.tb_bank_idbank=tbbk.idbank order by tbqr.idqrapi" or die("Error:" . mysqli_error()); 
            $objQuery = $objConnect->query($strSQL);  
            $objQuery1 = $objConnect->query($strSQL); 
            $objResult1 = $objQuery1->fetch_assoc();

            $sbankref = $objResult1["bankref"];
            //echo $sbankref;
            if (empty($sbankref)) {
              $sbilleridru= $objResult1["billeridru"];  
      //$sqrid=$objResult1["qrid"];
      //$sqrid="DRUEE2112262400ZZZZZ";
      $sqrid=$objResult1["timepay"];				
      $ssubbiller=$objResult1["subbiller"];
	  $ssref1=$objResult1["ref1"]; 			
      $sref1=$objResult1["subbiller"] . $objResult1["ref1"];  
      $sref2=$objResult1["ref2"];
      $samount=$objResult1["amount"];
	  $samount2=$objResult1["amount2"];			

      //$dbru = array("$sbilleridru", "$sqrid", "$ssubbiller", "$sref1" ,"$sref2" ,"$samount");
      $dbru = array("$sbilleridru", "$sqrid", "$sref1" ,"$sref2" ,"$samount");

      $dbankref = $data['BankRef'];
      $dbillerNo = $data['BillerNo'];
      $dref1 = $data['Ref1'];
      $dref2 = $data['Ref2'];
	  $dref3 = $data['Ref1'];			
      $dqrid = $data['QRId'];
	  $dpayername = $data['PayerName'];			
	  //$dpayername = iconv("tis-620","utf-8",$data['PayerName']); 			
	  $dpayerbank = $data['PayerBank'];			
      $damount = $data['Amount'];
      $dresultcode = $data['ResultCode'];
      $dresultdesc = $data['ResultDesc'];
      $dtransdate = $data['TransDate'];

      $dbtmb = array("$dbillerNo", "$dqrid", "$dref1", "$dref2" ,"$damount");

      

$ddata = array('BankRef'=>$dbankref , 'ResCode'=>$dresultcode , 'ResDesc'=>$dresultdesc , 'TransDate '=>$dtransdate);
//เพิ่มเติม เก็บข้อมูลเพิ่ม 27-08-2021				
$dadd = array('BankRef'=>$dbankref , 'Ref1'=>$ssref1 , 'Ref3'=>$dref3 , 'Ref2'=>$dref2 , 'PayerName'=>$dpayername ,'PayerBank'=>$dpayerbank , 'Amount'=> $damount , 'ResCode'=>$dresultcode , 'ResDesc'=>$dresultdesc , 'TransDate '=>$dtransdate);		

//amount2 กรณีชำระเงินครึ่งจำนวน
$daddam2 = array('BankRef'=>$dbankref , 'Ref1'=>$ssref1 , 'Ref3'=>$dref3 , 'Ref2'=>$dref2 , 'PayerName'=>$dpayername ,'PayerBank'=>$dpayerbank , 'Amount'=> $damount , 'Amount2'=> $samount2 , 'ResCode'=>$dresultcode , 'ResDesc'=>$dresultdesc , 'TransDate '=>$dtransdate);

 $result=array_udiff_uassoc($dbtmb,$dbru,"myfunction_key","myfunction_value");
 if (empty($result)){
   $strFileName = 'SuccessQRAPI03_' . $dtransdate . ".txt";
                 file_put_contents($strFileName, json_encode($dadd));
               
                 $curl = curl_init();

                curl_setopt_array($curl, array(
               //   CURLOPT_URL => 'datagateway.ttbbank.com',
			    	CURLOPT_URL => 'www.datagateway.ttbbank.com',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => '',
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 0,
                  CURLOPT_FOLLOWLOCATION => true,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => 'POST',
                  CURLOPT_POSTFIELDS =>json_encode($ddata),
                  CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json'
                  ),
                ));

                $response = curl_exec($curl);

                curl_close($curl);
                echo $response;
                //var_dump($ddata);
             
          
        // $sqldata = "UPDATE tb_qrapi SET ref3 = '$dref3' , statuspay = 'ชำระเงินแล้ว (รอการยืนยัน ม.ราม)' , dstatus = '$dtransdate' , time = '$dtransdate' , bankref = '$dbankref' WHERE ref1=$ssref1 and ref2=$dref2";
	  
	 $sqldata = "UPDATE tb_qrapicp SET ref3 = '$dref3' , statusamount='ปกติ' , statuspay = 'ชำระเงินแล้ว (รอการยืนยัน ม.ราม)' , dstatus = '$dtransdate' , time = '$dtransdate' , bankref = '$dbankref' WHERE ref1='".$ssref1."' and ref2='".$dref2."'";


         if ($objConnect->query($sqldata) === TRUE) {
            //echo "Record updated successfully";
            echo json_encode($ddata); //ส่งข้อมูลให้ธนาคาร
        } else {
           echo "Error updating record: " . $objConnect->error;

          }

         // echo 'Record updated successfully QRId ตรง ';
         // echo 'Record updated successfully BillerNo ตรง ';
       //   echo $response;
         

 }else{
  //else ของ if (empty($result)) ข้อมูลที่ธนาคารส่งมา ไม่ตรงกับ ข้อมูลระบบ
    //print_r($result);
    $dbankref = $data["BankRef"];
    $dtransdate = $data['TransDate'];
    $dresultcode = "999";
    $dresultdesc = "Exception error";
	$dref2 = $data['Ref2'];
	  $dref3 = $data['Ref1'];			
      $dqrid = $data['QRId']; 
	 $damount = $data['Amount']; //amount จาก QR ที่ส่งมาจากธนาคาร TTB
	 $samount2=$objResult1["amount2"]; //amount2 จากฐานข้อมูล
	 $samount=$objResult1["amount"];//amount จากฐานข้อมูล
	 if ($damount == $samount2) {
		    $strFileName = 'SuccessQRAPI03_am2' . $dtransdate . ".txt";
                 file_put_contents($strFileName, json_encode($daddam2));
               
                 $curl = curl_init();

                curl_setopt_array($curl, array(
                  //CURLOPT_URL => 'datagateway.ttbbank.com',
				  CURLOPT_URL => 'www.datagateway.ttbbank.com',
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => '',
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 0,
                  CURLOPT_FOLLOWLOCATION => true,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => 'POST',
                  CURLOPT_POSTFIELDS =>json_encode($ddata),
                  CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json'
                  ),
                ));

                $response = curl_exec($curl);

                curl_close($curl);
                echo $response;
		 $sqldata = "UPDATE tb_qrapicp SET ref3 = '$dref3' , statusamount='ชำระเงินครึ่งจำนวน', statuspay = 'ชำระเงินแล้ว (รอการยืนยัน ม.ราม)' , dstatus = '$dtransdate' , time = '$dtransdate' , bankref = '$dbankref' WHERE ref1='".$ssref1."' and ref2='".$dref2."' and amount=$samount and amount2=$damount";


         if ($objConnect->query($sqldata) === TRUE) {
            //echo "Record updated successfully";
            echo json_encode($ddata); //ส่งข้อมูลให้ธนาคาร
        } else {
           echo "Error updating record: " . $objConnect->error;

          }
		 
	   }else{
                 $dataerror = array('BankRef'=>$dbankref , 'ResCode'=>$dresultcode , 'ResDesc'=>$dresultdesc , 'TransDate'=>$dtransdate , 'Ref1'=>$ssref1 , 'Ref3'=>$dref3 , 'Ref2'=>$dref2 , 'QRId'=>$dqrid , 'Amount'=>$damount ,'Amount2'=>$samount2);

                 $strFileName = 'error03_tmb_' . $dtransdate . ".txt";
                 file_put_contents($strFileName, json_encode($dataerror));
               
                 $curl = curl_init();

                curl_setopt_array($curl, array(
                  //CURLOPT_URL => 'datagateway.ttbbank.com',
				  CURLOPT_URL => 'www.datagateway.ttbbank.com',	
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => '',
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 0,
                  CURLOPT_FOLLOWLOCATION => true,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => 'POST',
                  CURLOPT_POSTFIELDS =>json_encode($dataerror),
                  CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json'
                  ),
                ));

                $response = curl_exec($curl);

                curl_close($curl);
                echo $response;
                echo json_encode($dataerror); //ส่งข้อมูลให้ธนาคาร
		 }//ปิด if $damount == $samount2

 }//ปิด else if (empty($result))

            }else{
              //else ของ if (empty($sbankref)) ใช้ในการตรวจสอบข้อมูลที่ส่งมา มีการชำระเงินแล้ว
              // echo "ส่งข้อมูลซ้ำ -->" . ($objResult1["status"]);
                 $dbankref = $data["BankRef"];
                 $dtransdate = $data['TransDate'];
                 $dresultcode = "999";
                 $dresultdesc = "Exception error";
				$dref2 = $data['Ref2'];
	  $dref3 = $data['Ref1'];			
      $dqrid = $data['QRId']; 
                 $dataduplicate = array('BankRef'=>$dbankref , 'ResCode'=>$dresultcode , 'ResDesc'=>$dresultdesc , 'TransDate'=>$dtransdate, 'Ref1'=>$ssref1 , 'Ref3'=>$dref3 , 'Ref2'=>$dref2 );

                 $strFileName = 'error03_tmb_dup' . $dtransdate . ".txt";
                 file_put_contents($strFileName, json_encode($dataduplicate));
               
                 $curl = curl_init();

                curl_setopt_array($curl, array(
                 // CURLOPT_URL => 'datagateway.ttbbank.com',
			      CURLOPT_URL => 'www.datagateway.ttbbank.com',	
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => '',
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 0,
                  CURLOPT_FOLLOWLOCATION => true,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => 'POST',
                  CURLOPT_POSTFIELDS =>json_encode($dataduplicate),
                  CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json'
                  ),
                ));

                $response = curl_exec($curl);

                curl_close($curl);
                echo $response;
                echo json_encode($dataduplicate); //ส่งข้อมูลให้ธนาคาร

            } //ปิด else if (empty($sbankref))

     } //ปิด else if($idbk != $didbk)



}else{
    //else ของ if(!empty($data))
      //ข้อมูลที่ส่งมาไม่มีข้อมูล
     // echo 'data empty ';
     // $dbankref = $data["BankRef"];
   // $dtransdate = $data['TransDate'];
    $dresultcode = "001";
    $dresultdesc = "Invalid Input";
                $dataempty = array('BankRef'=>'data empty' , 'ResCode'=>$dresultcode , 'ResDesc'=>$dresultdesc , 'TransDate'=>'data empty');

                 $strFileName = 'error03_tmb_' . 'data_empty' . ".txt";
                 file_put_contents($strFileName, json_encode($dataempty));
               
                 $curl = curl_init();

                curl_setopt_array($curl, array(
                  //CURLOPT_URL => 'datagateway.ttbbank.com',
					CURLOPT_URL => 'www.datagateway.ttbbank.com',	
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_ENCODING => '',
                  CURLOPT_MAXREDIRS => 10,
                  CURLOPT_TIMEOUT => 0,
                  CURLOPT_FOLLOWLOCATION => true,
                  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                  CURLOPT_CUSTOMREQUEST => 'POST',
                  CURLOPT_POSTFIELDS =>json_encode($dataempty),
                  CURLOPT_HTTPHEADER => array(
                    'Content-Type: application/json'
                  ),
                ));

                $response = curl_exec($curl);

                curl_close($curl);
                echo $response;
                echo json_encode($dataempty); //ส่งข้อมูลให้ธนาคาร /*

    }//ปิด else ของ if(!empty($data))
} //ปิด else ของ if($_SERVER['REQUEST_METHOD'] != "POST")
?>

Copyright © 2019 by b0y-101