Skip to content

Commit

Permalink
fix: Remove unnecessary pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwan2546 committed May 5, 2024
1 parent 4c11c0f commit 522ad00
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 29 deletions.
18 changes: 9 additions & 9 deletions generator/promptpay.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (

func SlipVerify(sendingBank string, transRef string) string {
payload := []lib.TLVTag{
lib.Tag("00", *lib.Encode([]lib.TLVTag{
lib.Tag("00", lib.Encode([]lib.TLVTag{
{
ID: "00",
Value: "000001",
Expand All @@ -33,12 +33,12 @@ func SlipVerify(sendingBank string, transRef string) string {
lib.Tag("51", "TH"),
}

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

return *tag
return tag
}

func AnyID(types string, target string, amount float64) string {
Expand All @@ -58,7 +58,7 @@ func AnyID(types string, target string, amount float64) string {
payload := []lib.TLVTag{
lib.Tag("00", "01"),
lib.Tag("01", "11"),
lib.Tag("29", *lib.Encode(tag29)),
lib.Tag("29", lib.Encode(tag29)),
lib.Tag("53", "764"),
lib.Tag("58", "TH"),
}
Expand All @@ -70,12 +70,12 @@ func AnyID(types string, target string, amount float64) string {
payload = result
}

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

return *tag
return tag
}

func BillPayment(billerID string, amount float64, ref1 string, ref2 string, ref3 string) string {
Expand All @@ -94,7 +94,7 @@ func BillPayment(billerID string, amount float64, ref1 string, ref2 string, ref3
payload := []lib.TLVTag{
lib.Tag("00", "01"),
lib.Tag("01", "11"),
lib.Tag("30", *lib.Encode(tag30)),
lib.Tag("30", lib.Encode(tag30)),
lib.Tag("53", "764"),
lib.Tag("58", "TH"),
}
Expand All @@ -112,10 +112,10 @@ func BillPayment(billerID string, amount float64, ref1 string, ref2 string, ref3
payload = result
}

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

return *tag
return tag
}
6 changes: 3 additions & 3 deletions generator/truemoney.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Truemoney(mobileNo string, amount float64, message string) string {
payload := []lib.TLVTag{
lib.Tag("00", "01"),
lib.Tag("01", "11"),
lib.Tag("29", *lib.Encode(tag29)),
lib.Tag("29", lib.Encode(tag29)),
lib.Tag("53", "764"),
lib.Tag("58", "TH"),
}
Expand All @@ -34,10 +34,10 @@ func Truemoney(mobileNo string, amount float64, message string) string {
payload = result
}

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

return *tag
return tag
}
1 change: 0 additions & 1 deletion lib/BOTBarcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func (bc *BOTBarcode) FromString(payload string) {
} else {
bc.Amount = 0
}

}

func (bc *BOTBarcode) ToString() string {
Expand Down
6 changes: 3 additions & 3 deletions lib/EMVCoQR.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (emv *EMVCoQRStruct) GetPayload() string {
return emv.Payload
}

func (emv *EMVCoQRStruct) Validate(crcTagId string) (*string, error) {
func (emv *EMVCoQRStruct) Validate(crcTagId string) (string, error) {
var tlvTags []TLVTag

for _, tags := range emv.Tags {
Expand All @@ -33,9 +33,9 @@ func (emv *EMVCoQRStruct) Validate(crcTagId string) (*string, error) {
// Encode tag
encoded := Encode(tlvTags)

expected, err := WithCRCTag(*encoded, crcTagId)
expected, err := WithCRCTag(encoded, crcTagId)
if err != nil {
return nil, err
return "", err
}

return expected, nil
Expand Down
20 changes: 10 additions & 10 deletions lib/tlv.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Decode(payload string) (*[]TLVTag, error) {
return &tags, nil
}

func Encode(tags []TLVTag) *string {
func Encode(tags []TLVTag) string {
var payload string

for _, tag := range tags {
Expand All @@ -55,34 +55,34 @@ func Encode(tags []TLVTag) *string {
if len(tag.SubTags) > 0 {
result := Encode(tag.SubTags)
// Append it
payload += *result
payload += result
}
payload += tag.Value
}

return &payload
return payload
}

func Checksum(payload string) (*string, error) {
func Checksum(payload string) (string, error) {
sum, err := utils.CRC16XModem(payload, 0xffff)
if err != nil {
return nil, err
return "", err
}
result := strings.ToUpper(fmt.Sprintf("%04x", sum))
return &result, nil
return result, nil
}

func WithCRCTag(payload string, crcTagId string) (*string, error) {
func WithCRCTag(payload string, crcTagId string) (string, error) {
payload += fmt.Sprintf("%02s", crcTagId)
payload += "04"
// Checksum
crc, err := Checksum(payload)
if err != nil {
return nil, err
return "", err
}
payload += *crc
payload += crc

return &payload, nil
return payload, nil

}

Expand Down
12 changes: 9 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ import (
)

func Parse(payload string, strict bool, subTags bool) *lib.EMVCoQRStruct {
match, _ := regexp.MatchString("^\\d{4}.+", payload)
reg, err := regexp.Compile(`^\d{4}.+`)
if err != nil {
return nil
}

match := reg.MatchString(payload)
if !match {
return nil
}

if strict {
excepted := payload[len(payload)-4:]
calculated, _ := lib.Checksum(payload[:len(payload)-4])
if excepted != *calculated {
if excepted != calculated {
return nil
}
}
Expand All @@ -27,9 +32,10 @@ func Parse(payload string, strict bool, subTags bool) *lib.EMVCoQRStruct {
}

if subTags {

for idx, tag := range *tags {
// Check if invalid
match, _ := regexp.MatchString("^\\d{4}.+", tag.Value)
match := reg.MatchString(tag.Value)
if !match {
continue
}
Expand Down
60 changes: 60 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package promptparsego_test

import (
"testing"

promptparse "github.com/mrwan200/promptparse-go"
)

func TestInvalidStringPassedToParser(t *testing.T) {
parsed := promptparse.Parse("AAAA0000", false, true)
if parsed != nil {
t.Fatalf("Incorrect test. (TestInvalidStringPassedToParser)")
}
}

func TestParseTLVAndGetTagCount(t *testing.T) {
parsed := promptparse.Parse("000411110104222202043333", false, true)
if parsed == nil {
t.Fatalf("Invalid payload. (TestParseTLVAndGetTagCount)")
}

// Get tag
tag := parsed.GetTag("01", "")
if len(tag.Value) < 3 {
t.Fatalf("Incorrect test. (TestParseTLVAndGetTagCount)")
}
}

func TestParseTLVAndGetOneTag(t *testing.T) {
parsed := promptparse.Parse("000411110104222202043333", false, true)
if parsed == nil {
t.Fatalf("Invalid payload. (TestParseTLVAndGetOneTag)")
}

// Get tag
tag := parsed.GetTag("01", "")
if tag.Value != "2222" {
t.Fatalf("Incorrect test. (TestParseTLVAndGetOneTag)")
}
}

func TestParserPayloadStrictWithInvalidChecksum(t *testing.T) {
parsed := promptparse.Parse("00020101021229370016A0000006770101110113006680111111153037645802TH540520.156304FFFF", true, true)
if parsed != nil {
t.Fatalf("Incorrect test. (TestParserPayloadStrictWithInvalidChecksum)")
}
}

func TestParserPayloadStrictWithValidChecksumAndGetTagValue(t *testing.T) {
parsed := promptparse.Parse("00020101021229370016A0000006770101110113006680111111153037645802TH540520.15630442BE", true, true)
if parsed == nil {
t.Fatalf("Invalid payload. (TestParserPayloadStrictWithValidChecksumAndGetTagValue)")
}

// Get tag
value := parsed.GetTagValue("29", "01")
if value != "0066801111111" {
t.Fatalf("Incorrect test. (TestParserPayloadStrictWithValidChecksumAndGetTagValue)")
}
}

0 comments on commit 522ad00

Please sign in to comment.