From 82486bc022bb640cb8dea98dd7083d1aece9c786 Mon Sep 17 00:00:00 2001 From: dn220602kdv Date: Wed, 15 Nov 2023 14:24:10 +0200 Subject: [PATCH 1/3] lang, button --- liqpay.go | 30 +++++++++++++++++++++++++++++- liqpay_form.html | 3 ++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/liqpay.go b/liqpay.go index cfcbbd7..6d9fe12 100644 --- a/liqpay.go +++ b/liqpay.go @@ -12,11 +12,23 @@ import ( "net/url" ) -const liqpayURL = "https://www.liqpay.ua/api/" +const ( + liqpayURL = "https://www.liqpay.ua/api/" +) + +var ( + allowedLangs = []string{"uk", "ru", "en"} + buttonLabel = map[string]string{ + "uk": "Сплатити", + "en": "Pay", + "ru": "Оплатить", + } +) type formData struct { Data string Signature string + Label string } type Client struct { @@ -44,6 +56,7 @@ func New(pubKey string, privKey string, client *http.Client) *Client { } func (c Client) Send(apiUrl string, req Request) (Response, error) { + req.checkLang() req.addMissingPubKey(string(c.publicKey)) encodedJSON, err := req.Encode() @@ -86,6 +99,7 @@ func (c Client) Send(apiUrl string, req Request) (Response, error) { func (c Client) RenderForm(req Request) (string, error) { req.addMissingPubKey(string(c.publicKey)) + req.checkLang() encodedJSON, err := req.Encode() if err != nil { @@ -102,6 +116,7 @@ func (c Client) RenderForm(req Request) (string, error) { if err := t.Execute(&buf, formData{ Data: encodedJSON, Signature: signature, + Label: buttonLabel[req["language"].(string)], }); err != nil { return "", err } @@ -115,6 +130,19 @@ func (r Request) addMissingPubKey(key string) { r["public_key"] = key } +func (r Request) checkLang() { + lang, contains := r["language"].(string) + if contains { + for _, allowedLang := range allowedLangs { + if lang == allowedLang { + return + } + } + } + + r["language"] = "uk" +} + func (r Request) Encode() (string, error) { obj, err := json.Marshal(r) if err != nil { diff --git a/liqpay_form.html b/liqpay_form.html index c11e1ae..b70b8af 100644 --- a/liqpay_form.html +++ b/liqpay_form.html @@ -1,5 +1,6 @@
- + +
\ No newline at end of file From 07ebd546a9efde7c1710a756403ef444c610d019 Mon Sep 17 00:00:00 2001 From: dn220602kdv Date: Fri, 17 Nov 2023 16:32:31 +0200 Subject: [PATCH 2/3] TestClient_RenderForm fix --- liqpay_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/liqpay_test.go b/liqpay_test.go index 142771d..8d47861 100644 --- a/liqpay_test.go +++ b/liqpay_test.go @@ -93,10 +93,11 @@ func TestClient_RenderForm(t *testing.T) { }, }, want: `
- - - -
`, + + + + +`, wantErr: false, }, } From 942d39dde26a6e69b37c180ed155971a15d731e4 Mon Sep 17 00:00:00 2001 From: dn220602kdv Date: Mon, 20 Nov 2023 12:09:12 +0200 Subject: [PATCH 3/3] lang fix --- liqpay.go | 10 ++++------ liqpay_test.go | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/liqpay.go b/liqpay.go index 6d9fe12..23cbc74 100644 --- a/liqpay.go +++ b/liqpay.go @@ -56,7 +56,6 @@ func New(pubKey string, privKey string, client *http.Client) *Client { } func (c Client) Send(apiUrl string, req Request) (Response, error) { - req.checkLang() req.addMissingPubKey(string(c.publicKey)) encodedJSON, err := req.Encode() @@ -99,7 +98,6 @@ func (c Client) Send(apiUrl string, req Request) (Response, error) { func (c Client) RenderForm(req Request) (string, error) { req.addMissingPubKey(string(c.publicKey)) - req.checkLang() encodedJSON, err := req.Encode() if err != nil { @@ -116,7 +114,7 @@ func (c Client) RenderForm(req Request) (string, error) { if err := t.Execute(&buf, formData{ Data: encodedJSON, Signature: signature, - Label: buttonLabel[req["language"].(string)], + Label: buttonLabel[req.getLang()], }); err != nil { return "", err } @@ -130,17 +128,17 @@ func (r Request) addMissingPubKey(key string) { r["public_key"] = key } -func (r Request) checkLang() { +func (r Request) getLang() string { lang, contains := r["language"].(string) if contains { for _, allowedLang := range allowedLangs { if lang == allowedLang { - return + return lang } } } - r["language"] = "uk" + return "uk" } func (r Request) Encode() (string, error) { diff --git a/liqpay_test.go b/liqpay_test.go index 8d47861..8a4b9e0 100644 --- a/liqpay_test.go +++ b/liqpay_test.go @@ -93,8 +93,8 @@ func TestClient_RenderForm(t *testing.T) { }, }, want: `
- - + +
`,