Skip to content

Commit

Permalink
feat: Add comment and move file
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwan2546 committed May 5, 2024
1 parent ca76366 commit 9c98e99
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 82 deletions.
51 changes: 43 additions & 8 deletions generator/billpayment.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
package generator

import "github.com/mrwan200/promptparse-go/lib"
import (
"fmt"

func BOTBarcode(billerId string, ref1 string, ref2 string, amount float64) string {
barcode := lib.BOTBarcode{
BillerID: billerId,
Ref1: ref1,
Ref2: ref2,
Amount: amount,
"github.com/mrwan200/promptparse-go/lib"
)

// Generate PromptPay Bill Payment (Tag 30) QR Code
func BillPayment(billerID string, amount float64, ref1 string, ref2 string, ref3 string) string {
tag30 := []lib.TLVTag{
lib.Tag("00", "A000000677010112"),
lib.Tag("01", billerID),
lib.Tag("02", ref1),
}

if ref2 != "" {
result := append(tag30, lib.Tag("03", ref2))
tag30 = result
}

payload := []lib.TLVTag{
lib.Tag("00", "01"),
lib.Tag("01", "11"),
lib.Tag("30", lib.Encode(tag30)),
lib.Tag("53", "764"),
lib.Tag("58", "TH"),
}

if amount != 0 {
payload[1] = lib.Tag("01", "12")
// Append data
result := append(payload, lib.Tag("54", fmt.Sprintf("%.2f", float64(amount*100)/100)))
payload = result
}

if ref3 != "" {
// Append data
result := append(payload, lib.Tag("62", ref3))
payload = result
}

tag, err := lib.WithCRCTag(lib.Encode(payload), "63")
if err != nil {
return ""
}

return barcode.ToString()
return tag
}
14 changes: 14 additions & 0 deletions generator/botbarcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package generator

import "github.com/mrwan200/promptparse-go/lib"

func BOTBarcode(billerId string, ref1 string, ref2 string, amount float64) string {
barcode := lib.BOTBarcode{
BillerID: billerId,
Ref1: ref1,
Ref2: ref2,
Amount: amount,
}

return barcode.ToString()
}
80 changes: 8 additions & 72 deletions generator/promptpay.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,17 @@ import (
)

const (
MSIDN = "01"
NATID = "02"
/** Mobile number */
MSIDN = "01"
/** National ID or Tax ID */
NATID = "02"
/** E-Wallet ID */
EWALLETID = "03"
BANKACC = "04"
/** Bank Account (Reserved) */
BANKACC = "04"
)

func SlipVerify(sendingBank string, transRef string) string {
payload := []lib.TLVTag{
lib.Tag("00", lib.Encode([]lib.TLVTag{
{
ID: "00",
Value: "000001",
},
{
ID: "01",
Value: sendingBank,
},
{
ID: "02",
Value: transRef,
},
})),
lib.Tag("51", "TH"),
}

tag, err := lib.WithCRCTag(lib.Encode(payload), "91")
if err != nil {
return ""
}

return tag
}

// Generate PromptPay AnyID (Tag 29) QR Code
func AnyID(types string, target string, amount float64) string {
if types == MSIDN {
msidn := target
Expand Down Expand Up @@ -77,45 +55,3 @@ func AnyID(types string, target string, amount float64) string {

return tag
}

func BillPayment(billerID string, amount float64, ref1 string, ref2 string, ref3 string) string {

tag30 := []lib.TLVTag{
lib.Tag("00", "A000000677010112"),
lib.Tag("01", billerID),
lib.Tag("02", ref1),
}

if ref2 != "" {
result := append(tag30, lib.Tag("03", ref2))
tag30 = result
}

payload := []lib.TLVTag{
lib.Tag("00", "01"),
lib.Tag("01", "11"),
lib.Tag("30", lib.Encode(tag30)),
lib.Tag("53", "764"),
lib.Tag("58", "TH"),
}

if amount != 0 {
payload[1] = lib.Tag("01", "12")
// Append data
result := append(payload, lib.Tag("54", fmt.Sprintf("%.2f", float64(amount*100)/100)))
payload = result
}

if ref3 != "" {
// Append data
result := append(payload, lib.Tag("62", ref3))
payload = result
}

tag, err := lib.WithCRCTag(lib.Encode(payload), "63")
if err != nil {
return ""
}

return tag
}
33 changes: 33 additions & 0 deletions generator/slipverify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package generator

import "github.com/mrwan200/promptparse-go/lib"

// Generate Slip Verify QR Code
//
// This also called "Mini-QR" that embedded in slip used for verify transactions
func SlipVerify(sendingBank string, transRef string) string {
payload := []lib.TLVTag{
lib.Tag("00", lib.Encode([]lib.TLVTag{
{
ID: "00",
Value: "000001",
},
{
ID: "01",
Value: sendingBank,
},
{
ID: "02",
Value: transRef,
},
})),
lib.Tag("51", "TH"),
}

tag, err := lib.WithCRCTag(lib.Encode(payload), "91")
if err != nil {
return ""
}

return tag
}
5 changes: 5 additions & 0 deletions generator/truemoney.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/mrwan200/promptparse-go/utils"
)

// Generate QR Code for TrueMoney Wallet
//
// This QR Code can also be scanned with other apps,
// just like a regular e-Wallet PromptPay QR
// but `Personal Message (Tag 81)` will be ignored.
func Truemoney(mobileNo string, amount float64, message string) string {
tag29 := []lib.TLVTag{
lib.Tag("00", "A000000677010111"),
Expand Down
3 changes: 3 additions & 0 deletions lib/BOTBarcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func (bc *BOTBarcode) ToString() string {
return fmt.Sprintf("|%s\r%s\r%s\r%s", bc.BillerID, bc.Ref1, bc.Ref2, fmt.Sprintf("%.2f", bc.Amount))
}

// Converts BOT Barcode to PromptPay QR Tag 30 (Bill Payment)
//
// This method works for some biller, depends on destination bank
func (bc *BOTBarcode) ToQRTag30() string {
tag30 := []TLVTag{
Tag("00", "A000000677010112"),
Expand Down
6 changes: 6 additions & 0 deletions lib/tlv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type TLVTag struct {
Length int
}

// Decode TLV string into array of TLV Tags
func Decode(payload string) (*[]TLVTag, error) {
var tags []TLVTag

Expand Down Expand Up @@ -44,6 +45,7 @@ func Decode(payload string) (*[]TLVTag, error) {
return &tags, nil
}

// Encode TLV Tags array into TLV string
func Encode(tags []TLVTag) string {
var payload string

Expand All @@ -63,6 +65,7 @@ func Encode(tags []TLVTag) string {
return payload
}

// Generate CRC Checksum for provided string
func Checksum(payload string) (string, error) {
sum, err := utils.CRC16XModem(payload, 0xffff)
if err != nil {
Expand All @@ -72,6 +75,7 @@ func Checksum(payload string) (string, error) {
return result, nil
}

// Get TLV string combined with CRC Tag
func WithCRCTag(payload string, crcTagId string) (string, error) {
payload += fmt.Sprintf("%02s", crcTagId)
payload += "04"
Expand All @@ -86,6 +90,7 @@ func WithCRCTag(payload string, crcTagId string) (string, error) {

}

// Get Tag or Sub-tag by Tag ID in array of TLV Tags
func Get(tlvTags []TLVTag, tagId string, subTagId string) TLVTag {
var tag TLVTag

Expand All @@ -108,6 +113,7 @@ func Get(tlvTags []TLVTag, tagId string, subTagId string) TLVTag {
return tag
}

// Create new TLV Tag
func Tag(tagId string, value string) TLVTag {
return TLVTag{
ID: tagId,
Expand Down
2 changes: 2 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/mrwan200/promptparse-go/lib"
)

// Parse any EMVCo-compatible QR Code data string
func Parse(payload string, strict bool, subTags bool) *lib.EMVCoQRStruct {
reg, err := regexp.Compile(`^\d{4}.+`)
if err != nil {
Expand Down Expand Up @@ -63,6 +64,7 @@ func Parse(payload string, strict bool, subTags bool) *lib.EMVCoQRStruct {
return &emv
}

// Parse barcode data string (BOT Barcode Standard)
func ParseBarcode(payload string) lib.BOTBarcode {
barcode := lib.BOTBarcode{}
barcode.FromString(payload)
Expand Down
3 changes: 1 addition & 2 deletions utils/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"strings"
)

// Generate an `UCS-2`-like? Hex string for Tag 81
func EncodeTag81(message string) string {
var msg []string
for _, val := range message {
// hex := strconv.FormatInt(int64(val), 16)

result := append(msg, fmt.Sprintf("%04X", int(val)))
msg = result
}
Expand Down

0 comments on commit 9c98e99

Please sign in to comment.