Completed
Push — master ( 312f94...196d3f )
by Monthon
04:04
created

ScbQr   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 29.03%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 78
ccs 9
cts 31
cp 0.2903
rs 10
c 1
b 0
f 0
wmc 6

5 Methods

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