b0y-101 Mini Shell


Current Path : E:/www/b-group.old/spfin - Copy/application/PromptPayQRM/lib/
File Upload :
Current File : E:/www/b-group.old/spfin - Copy/application/PromptPayQRM/lib/PromptPayQR-3Mtest.php

<?Php
require_once('qrlib.php');

class PromptPayQR {
	public $size = 4;
    public $id = '';
	public $ref0 = '';
	public $ref1 = '';
	public $ref2 = '';
	public $amount = 0;
	
	
	public function generate($file=false) {
	
		// format [Fee][Length][Data]
		$data =  '000201'; // Start
		$data .= '010212'; // accept recycle
		$data .= '30';
        $merchantInfo301 = '0016A000000677010112'; //FIX
		//$merchantInfo302 = '0115099400019348302'; //FIX
		$merchantInfo302 = '0115'; //FIX
		$merchantInfo3021 = $this->billeridru;
		$merchantInfo3022 = $merchantInfo302 . $merchantInfo3021;
		$merchantInfo303 = '0217'; //FIX
		$merchantInfo3031 = $this->subbiller;//'00YA'รับมาจาก DATABASE 
		$merchantInfo304 = $this->ref1;//'3640600111689'รับมาจาก DATABASE
		$merchantInfo1 = $this->ref2;//'0001TP10SAT'รับมาจาก DATABASE
		$merchantInfo305= "03" . strlen($merchantInfo1) . $merchantInfo1;   
        $data .=strlen($merchantInfo301) + strlen($merchantInfo3022) + strlen($merchantInfo303) + strlen($merchantInfo3031) + strlen($merchantInfo304) + strlen($merchantInfo305);
        $data .=$merchantInfo301;
        $data .=$merchantInfo3022;
        $data .=$merchantInfo303;
        $data .=$merchantInfo3031;
        $data .=$merchantInfo304;
        $data .=$merchantInfo305;
		$data .='5303764';
		$merchantInfo54 = "54";
		$merchantInfo541 = $this->amount;
		$str541=strlen($merchantInfo541);
		if ($str541 < "10") {
           $data .= $merchantInfo54 . "0" . strlen($merchantInfo541) . $merchantInfo541;      
  
          } else {
            $data .= $merchantInfo54 . strlen($merchantInfo541) . $merchantInfo541;
          }	
		//$data .='5406800.00';
		
		
		// merchant account information
		//$merchantInfo =  '0016A000000677010112'; // application ID  AID(fix)
		//$merchantInfo .=  '0115010753700001716';
		
		// PromptPay ID
		//$merchantInfo .= '01';
		//if (strlen($this->id) == 13) { // ID card
		//	$merchantInfo .= '1500' . $this->id;
		//} else if (strlen($this->id) == 10) {
		//	$merchantInfo .= '130066' . substr($this->id, -9);
		//} else {
		//	return false;
		//}
		
		//$data .= '29' . strlen($merchantInfo) . $merchantInfo; // set merchant account information
		$data .= '5802TH'; // Thai baht
		$data .= '59';
		$merchantInfo59='SAMMITI';
		$str=strlen($merchantInfo59);
		if ($str < "10") {
           $data .= "0" . strlen($merchantInfo59) . $merchantInfo59;      
  
          } else {
            $data .= strlen($merchantInfo59) . $merchantInfo59;
          }	
		$data .= '62';
		$merchantInfo07 ='07';
		//$merchantInfo071='DRAMEYYMMDDHHMMZZZZZ';
		$merchantInfo071=$this->qrid;
		$merchantInfo072= $merchantInfo07 . strlen($merchantInfo071);
		$data .= strlen($merchantInfo072) + strlen($merchantInfo071);
		$data .= $merchantInfo07 . strlen($merchantInfo071) . $merchantInfo071;
		
		
		
		// amount
		//if ($this->amount > 0) {
			//$amountText = number_format($this->amount, 2, '.', '');
		//	$amountLen = strlen($amountText);
		//	$data .= '54' . ($amountLen < 10 ? '0' . $amountLen : $amountLen) . $amountText;
		//}
		
		//$data .= '5303764'; // 764 is thai baht in ISO4217
		
		// check sum
		$data .= '6304';
		$sum = strtoupper(dechex($this->crc16($data)));
		$strsum=strlen($sum);
		
		while($strsum < 4 ) {
		  $sum = "0" . $sum;
		  $strsum=strlen($sum);
		}
		
            $data .= $sum;
        		
		
		//$data .= $sum;
		//$data .= 'EBC9';
		
		$file = $file === false ? 'TMP_FILE_QRCODE_PROMPTPAY.png' : $file;
		
		QRcode::png($data, $file, QR_ECLEVEL_H, $this->size, 2);
		if ($file == 'TMP_FILE_QRCODE_PROMPTPAY.png') {
			return file_exists($file) ? 'data:image/png;base64,' . base64_encode(file_get_contents($file)) : false;
		}
		
		return $data;
	}
	
	/*
	 * crc16 function from https://stackoverflow.com/questions/14018508/how-to-calculate-crc16-in-php
	 * RomKazanova, thank for code.
	 */
	private function crc16($data) {
		$crc = 0xFFFF;
		for ($i = 0; $i < strlen($data); $i++) {
			$x = (($crc >> 8) ^ ord($data[$i])) & 0xFF;
			$x ^= $x >> 4;
			$crc = (($crc << 8) ^ ($x << 12) ^ ($x << 5) ^ $x) & 0xFFFF;
		}
		return $crc;
	}
}

Copyright © 2019 by b0y-101