1 | <?php |
||
9 | class PromptPay { |
||
10 | |||
11 | const ID_PAYLOAD_FORMAT = '00'; |
||
12 | const ID_POI_METHOD = '01'; |
||
13 | const ID_MERCHANT_INFORMATION_BOT = '29'; |
||
14 | const ID_TRANSACTION_CURRENCY = '53'; |
||
15 | const ID_TRANSACTION_AMOUNT = '54'; |
||
16 | const ID_COUNTRY_CODE = '58'; |
||
17 | const ID_CRC = '63'; |
||
18 | |||
19 | const PAYLOAD_FORMAT_EMV_QRCPS_MERCHANT_PRESENTED_MODE = '01'; |
||
20 | const POI_METHOD_STATIC = '11'; |
||
21 | const POI_METHOD_DYNAMIC = '12'; |
||
22 | const MERCHANT_INFORMATION_TEMPLATE_ID_GUID = '00'; |
||
23 | const BOT_ID_MERCHANT_PHONE_NUMBER = '01'; |
||
24 | const BOT_ID_MERCHANT_TAX_ID = '02'; |
||
25 | const BOT_ID_MERCHANT_EWALLET_ID = '03'; |
||
26 | const GUID_PROMPTPAY = 'A000000677010111'; |
||
27 | const TRANSACTION_CURRENCY_THB = '764'; |
||
28 | const COUNTRY_CODE_TH = 'TH'; |
||
29 | |||
30 | 2 | public function generatePayload($target, $amount = null) { |
|
31 | |||
32 | 2 | $target = $this->sanitizeTarget($target); |
|
33 | |||
34 | 2 | $targetType = strlen($target) >= 15 ? self::BOT_ID_MERCHANT_EWALLET_ID : (strlen($target) >= 13 ? self::BOT_ID_MERCHANT_TAX_ID : self::BOT_ID_MERCHANT_PHONE_NUMBER); |
|
35 | |||
36 | $data = [ |
||
37 | 2 | $this->f(self::ID_PAYLOAD_FORMAT, self::PAYLOAD_FORMAT_EMV_QRCPS_MERCHANT_PRESENTED_MODE), |
|
38 | 2 | $this->f(self::ID_POI_METHOD, $amount ? self::POI_METHOD_DYNAMIC : self::POI_METHOD_STATIC), |
|
39 | 2 | $this->f(self::ID_MERCHANT_INFORMATION_BOT, $this->serialize([ |
|
40 | 2 | $this->f(self::MERCHANT_INFORMATION_TEMPLATE_ID_GUID, self::GUID_PROMPTPAY), |
|
41 | 2 | $this->f($targetType, $this->formatTarget($target)) |
|
42 | ])), |
||
43 | 2 | $this->f(self::ID_COUNTRY_CODE, self::COUNTRY_CODE_TH), |
|
44 | 2 | $this->f(self::ID_TRANSACTION_CURRENCY, self::TRANSACTION_CURRENCY_THB), |
|
45 | ]; |
||
46 | |||
47 | 2 | if ($amount !== null) { |
|
48 | 2 | array_push($data, $this->f(self::ID_TRANSACTION_AMOUNT, $this->formatAmount($amount))); |
|
49 | } |
||
50 | |||
51 | 2 | $dataToCrc = $this->serialize($data) . self::ID_CRC . '04'; |
|
52 | 2 | array_push($data, $this->f(self::ID_CRC, $this->crc16($dataToCrc))); |
|
53 | 2 | return $this->serialize($data); |
|
54 | } |
||
55 | |||
56 | 3 | public function f($id, $value) { |
|
59 | |||
60 | 2 | public function serialize($xs) { |
|
63 | |||
64 | 3 | public function sanitizeTarget($str) { |
|
68 | |||
69 | 3 | public function formatTarget($target) { |
|
81 | |||
82 | 3 | public function formatAmount($amount) { |
|
85 | |||
86 | 3 | public function crc16($data) { |
|
92 | |||
93 | 1 | public function generateQrCode($savePath, $target, $amount = null, $width = 500) { |
|
104 | |||
105 | } |
||
106 |