File manager - Edit - /home/webapp69.cm.in.th/u69319090030/030 shop/helpers/promptpay_helper.php
Back
<?php class PromptPayQR { /** * คำนวณ CRC16 (CCITT-FALSE / Poly 0x1021) */ private static 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 strtoupper(str_pad(dechex($crc), 4, '0', STR_PAD_LEFT)); } /** * ฟอร์แมต TLV (Tag-Length-Value) */ private static function tlv($tag, $value) { $length = str_pad(strlen($value), 2, '0', STR_PAD_LEFT); return $tag . $length . $value; } /** * สร้าง EMVCo Payload สำหรับ PromptPay พร้อมระบุจำนวนเงิน * @param string $target เบอร์โทรศัพท์ (เช่น 0812345678) หรือ เลขบัตรประชาชน * @param float $amount จำนวนเงิน (เช่น 150.00) * @return string Raw EMVCo Payload */ public static function generatePayload($target, $amount = 0) { $target = preg_replace('/[^0-9]/', '', $target); // 1. Format Target if (strlen($target) === 10 && substr($target, 0, 1) === '0') { // เบอร์โทรศัพท์ (แปลง 08x เป็น 00668x) $targetFormatted = '0066' . substr($target, 1); $targetType = '01'; // Phone } else { // เลขบัตรประชาชน หรือ TAX ID $targetFormatted = $target; $targetType = '02'; // Card / Tax ID } // Tag 29 - Merchant Account Information (PromptPay) $sub29_aid = self::tlv('00', 'A000000677010111'); $sub29_target = self::tlv($targetType, $targetFormatted); $merchantInfo = self::tlv('29', $sub29_aid . $sub29_target); // Standard Payload Components $payload = self::tlv('00', '01'); // Payload Format Indicator $payload .= self::tlv('01', $amount > 0 ? '12' : '11'); // 12 Dynamic (มีจำนวนเงิน), 11 Static $payload .= $merchantInfo; $payload .= self::tlv('53', '764'); // Country Currency Code 764 = THB if ($amount > 0) { $payload .= self::tlv('54', number_format($amount, 2, '.', '')); // Amount } $payload .= self::tlv('58', 'TH'); // Country Code // Tag 63 - CRC16 Checksum $payloadForCrc = $payload . '6304'; $checksum = self::crc16($payloadForCrc); return $payloadForCrc . $checksum; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.4 |
proxy
|
phpinfo
|
Settings