-
Notifications
You must be signed in to change notification settings - Fork 10
/
jpqr.go
83 lines (71 loc) · 2.05 KB
/
jpqr.go
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
/*
Package jpqr implements encoding and decoding of JPQR as defined in JPQR MPM Guideline.
*/
package jpqr
import (
"fmt"
"go.mercari.io/go-emv-code/mpm"
)
// Decode decodes payload and validates as JPQR.
func Decode(payload []byte) (*mpm.Code, error) {
c, err := mpm.Decode(payload, []mpm.ValidatorFunc{
validateID,
validateCountryCodeIsJP,
validateTransactionCurrency,
validatePostalCode,
validateMerchantInformation,
}...)
if err != nil {
return nil, err
}
return c, nil
}
// Encode encodes to EMV Payment Code payload.
func Encode(c *mpm.Code) ([]byte, error) {
return mpm.Encode(c, []mpm.ValidatorFunc{
validateID,
validateCountryCodeIsJP,
validateTransactionCurrency,
validatePostalCode,
validateMerchantInformation,
}...)
}
func validateID(c *mpm.Code) error {
if _, err := ParseID(c); err != nil {
return mpm.NewInvalidFormat(fmt.Sprintf("jpqr: %s", err))
}
return nil
}
const countryCode = "JP"
func validateCountryCodeIsJP(c *mpm.Code) error {
if c.CountryCode == countryCode {
return nil
}
return mpm.NewInvalidFormat(fmt.Sprintf("jpqr: CountryCode should be %s", countryCode))
}
const transactionCurrency = "392"
func validateTransactionCurrency(c *mpm.Code) error {
if c.TransactionCurrency == transactionCurrency {
return nil
}
return mpm.NewInvalidFormat(fmt.Sprintf("jpqr: TransactionCurrency should be %s", transactionCurrency))
}
func validatePostalCode(c *mpm.Code) error {
if c.PostalCode == "" {
return mpm.NewInvalidFormat("jpqr: PostalCode should be represented")
}
return nil
}
const languagePreference = "JA"
func validateMerchantInformation(c *mpm.Code) error {
if !c.MerchantInformation.Valid {
return mpm.NewInvalidFormat("jpqr: MerchantInformation should be represented")
}
if c.MerchantInformation.LanguagePreference != languagePreference {
return mpm.NewInvalidFormat(fmt.Sprintf("jpqr: MerchantInformation.LanguagePreference should be %s", languagePreference))
}
if c.MerchantInformation.City != "" {
return mpm.NewInvalidFormat("jpqr: MerchantInformation.City is not necessary")
}
return nil
}