ScbQr::formatAmount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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 = '0700'; //'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 = '', $rawdata = 'no')
22
    {
23
        $command = '';
24
25
        # Release version of QR
26
        $command .= self::RELEASE_VERSION_OF_QR;
27
28
        # Initial Method of QR
29
        $command .= self::INITIAL_METHOD_OF_QR;
30
31
        # Reference Code
32
        $ref_01 = $this->f('02',  $ref_1);
33
        $ref_02 = $this->f('03',  $ref_2);
34
        $ref_command = self::SCB_PAYMENT_CODE . $billerId . $ref_01 . $ref_02;
35
36
        $command .=  $this->f('30', $ref_command);
37
38
        # สกุลเงินบาท
39
        $command .= self::TRANSACTION_CURRENCY_THB;
40
41
        # จำนวนเงิน
42
        $command .= $this->f('54', $this->formatAmount($amount));
43
44
        # CountryCode “TH”
45
        $command .= self::COUNTRY_CODE_TH;
46
47
        # SCB reference ไว้ใช้อ้างอิงระหว่าง ร้านค้ากับธนาคาร
48
        $command .= $this->f('62', self::SCB_REFERENCE_CODE);
49
50
        # CRC16 checksum
51
        $command .= self::CHECKSUM_PREFIX;
52
        $command .= $this->CRC16HexDigest($command);
53
54
        if ($rawdata == 'yes') {
55
            header('Content-Type: text/html;');
56
            echo $command;
57
        } else {
58
            $qrCode = new QrCode($command);
59
60
            header('Content-Type: ' . $qrCode->getContentType());
61
            echo $qrCode->writeString();
62
        }
63
    }
64
65
    private function f($prefix, $data)
66 1
    {
67
        return $prefix . sprintf("%02d", strlen($data)) . $data;
68 1
    }
69
70
    public function formatAmount($amount)
71 1
    {
72
        return number_format($amount, 2, '.', '');
73 1
    }
74 1
75 1
    public function crc16($data)
76 1
    {
77 1
        $crc = 0xFFFF;
78
        for ($i = 0; $i < strlen($data); $i++) {
79 1
            $x = (($crc >> 8) ^ ord($data[(string)$i])) & 0xFF;
80
            $x ^= $x >> 4;
81
            $crc = (($crc << 8) ^ ($x << 12) ^ ($x << 5) ^ $x) & 0xFFFF;
82
        }
83
        return $crc;
84
    }
85 1
86
    /*
87 1
    * Returns CRC16 of a string as hexadecimal string 
88
    */
89
    public function CRC16HexDigest($str)
90
    {
91
        return sprintf('%04X', $this->crc16($str));
92
    }
93
}
94