1
|
|
|
<?php |
2
|
|
|
namespace vansales; |
3
|
|
|
|
4
|
|
|
require_once './vendor/autoload.php'; |
5
|
|
|
use Endroid\QrCode\QrCode; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class ScbQr{ |
9
|
|
|
|
10
|
|
|
const RELEASE_VERSION_OF_QR = '000201'; |
11
|
|
|
const INITIAL_METHOD_OF_QR = '010212'; |
12
|
|
|
const SCB_PAYMENT_CODE = '0016A000000677010112'; |
13
|
|
|
const SCB_REFERENCE_CODE = '0706SCB001'; |
14
|
|
|
const TRANSACTION_CURRENCY_THB = '5303764'; |
15
|
|
|
const COUNTRY_CODE_TH = '5802TH'; |
16
|
|
|
const CHECKSUM_PREFIX = '6304'; |
17
|
|
|
|
18
|
|
|
public function getqrcode($amount = 0, $ref_1 = 'none', $ref_2 = 'none', $billerId = '') |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
$command = ''; |
22
|
|
|
|
23
|
|
|
# Release version of QR |
24
|
|
|
$command .= self::RELEASE_VERSION_OF_QR; |
25
|
|
|
|
26
|
|
|
# Initial Method of QR |
27
|
|
|
$command .= self::INITIAL_METHOD_OF_QR; |
28
|
|
|
|
29
|
|
|
# Reference Code |
30
|
|
|
$ref_01 = $this->f('02', $ref_1); |
31
|
|
|
$ref_02 = $this->f('03', $ref_2); |
32
|
|
|
$ref_command = self::SCB_PAYMENT_CODE . $billerId . $ref_01 . $ref_02; |
33
|
|
|
|
34
|
|
|
$command .= $this->f('30', $ref_command); |
35
|
|
|
|
36
|
|
|
# สกุลเงินบาท |
37
|
|
|
$command .= self::TRANSACTION_CURRENCY_THB; |
38
|
|
|
|
39
|
|
|
# จำนวนเงิน |
40
|
|
|
$command .= $this->f('54', $this->formatAmount($amount)); |
41
|
|
|
|
42
|
|
|
# CountryCode “TH” |
43
|
|
|
$command .= self::COUNTRY_CODE_TH; |
44
|
|
|
|
45
|
|
|
# SCB reference ไว้ใช้อ้างอิงระหว่าง ร้านค้ากับธนาคาร |
46
|
|
|
$command .= $this->f('62', self::SCB_REFERENCE_CODE); |
47
|
|
|
|
48
|
|
|
# CRC16 checksum |
49
|
|
|
$command .= self::CHECKSUM_PREFIX; |
50
|
|
|
$command .= $this->CRC16HexDigest($command); |
51
|
|
|
|
52
|
|
|
$qrCode = new QrCode($command); |
53
|
|
|
|
54
|
|
|
header('Content-Type: ' . $qrCode->getContentType()); |
55
|
|
|
echo $qrCode->writeString(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function f($prefix, $data) { |
59
|
|
|
return $prefix . sprintf("%02d", strlen($data)) . $data; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function formatAmount($amount) { |
63
|
|
|
return number_format($amount, 2, '.', ''); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function crc16($data) { |
67
|
|
|
$crc = 0xFFFF; |
68
|
|
|
for ($i = 0; $i < strlen($data); $i++) { |
69
|
|
|
$x = (($crc >> 8) ^ ord($data[$i])) & 0xFF; |
70
|
|
|
$x ^= $x >> 4; |
71
|
|
|
$crc = (($crc << 8) ^ ($x << 12) ^ ($x << 5) ^ $x) & 0xFFFF; |
72
|
|
|
} |
73
|
|
|
return $crc; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* |
77
|
|
|
* Returns CRC16 of a string as hexadecimal string |
78
|
|
|
*/ |
79
|
|
|
private function CRC16HexDigest($str) |
80
|
|
|
{ |
81
|
|
|
return sprintf('%04X', $this->crc16($str)); |
82
|
|
|
} |
83
|
|
|
} |