-
Notifications
You must be signed in to change notification settings - Fork 87
/
index.js
90 lines (77 loc) · 2.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*!
* promptpay-qr
* JavaScript library to generate PromptPay QR code
* <https://github.com/dtinth/promptpay-qr>
*
* Refs:
* - https://www.blognone.com/node/95133
* - https://www.emvco.com/emv-technologies/qrcodes/
*
* @license MIT
*/
var crc = require('crc')
var ID_PAYLOAD_FORMAT = '00'
var ID_POI_METHOD = '01'
var ID_MERCHANT_INFORMATION_BOT = '29'
var ID_TRANSACTION_CURRENCY = '53'
var ID_TRANSACTION_AMOUNT = '54'
var ID_COUNTRY_CODE = '58'
var ID_CRC = '63'
var PAYLOAD_FORMAT_EMV_QRCPS_MERCHANT_PRESENTED_MODE = '01'
var POI_METHOD_STATIC = '11'
var POI_METHOD_DYNAMIC = '12'
var MERCHANT_INFORMATION_TEMPLATE_ID_GUID = '00'
var BOT_ID_MERCHANT_PHONE_NUMBER = '01'
var BOT_ID_MERCHANT_TAX_ID = '02'
var BOT_ID_MERCHANT_EWALLET_ID = '03'
var GUID_PROMPTPAY = 'A000000677010111'
var TRANSACTION_CURRENCY_THB = '764'
var COUNTRY_CODE_TH = 'TH'
function generatePayload (target, options) {
target = sanitizeTarget(target)
var amount = options.amount
var targetType = (
target.length >= 15 ? (
BOT_ID_MERCHANT_EWALLET_ID
) : target.length >= 13 ? (
BOT_ID_MERCHANT_TAX_ID
) : (
BOT_ID_MERCHANT_PHONE_NUMBER
)
)
var data = [
f(ID_PAYLOAD_FORMAT, PAYLOAD_FORMAT_EMV_QRCPS_MERCHANT_PRESENTED_MODE),
f(ID_POI_METHOD, amount ? POI_METHOD_DYNAMIC : POI_METHOD_STATIC),
f(ID_MERCHANT_INFORMATION_BOT, serialize([
f(MERCHANT_INFORMATION_TEMPLATE_ID_GUID, GUID_PROMPTPAY),
f(targetType, formatTarget(target))
])),
f(ID_COUNTRY_CODE, COUNTRY_CODE_TH),
f(ID_TRANSACTION_CURRENCY, TRANSACTION_CURRENCY_THB),
amount && f(ID_TRANSACTION_AMOUNT, formatAmount(amount))
]
var dataToCrc = serialize(data) + ID_CRC + '04'
data.push(f(ID_CRC, formatCrc(crc.crc16xmodem(dataToCrc, 0xffff))))
return serialize(data)
}
function f (id, value) {
return [ id, ('00' + value.length).slice(-2), value ].join('')
}
function serialize (xs) {
return xs.filter(function (x) { return x }).join('')
}
function sanitizeTarget (id) {
return id.replace(/[^0-9]/g, '')
}
function formatTarget (id) {
const numbers = sanitizeTarget(id)
if (numbers.length >= 13) return numbers
return ('0000000000000' + numbers.replace(/^0/, '66')).slice(-13)
}
function formatAmount (amount) {
return amount.toFixed(2)
}
function formatCrc (crcValue) {
return ('0000' + crcValue.toString(16).toUpperCase()).slice(-4)
}
module.exports = generatePayload