From c0f532aa74cf1e9d68f45e77c4688dbc23024663 Mon Sep 17 00:00:00 2001 From: Cyrill Schumacher Date: Wed, 19 May 2021 16:03:56 +0200 Subject: [PATCH] Add merchant specific opaque data container does not get used in requests towards datatrans but Data map can contain merchant specific configurations. --- client.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ client_test.go | 34 +++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/client.go b/client.go index f123ba6..a71a4c8 100644 --- a/client.go +++ b/client.go @@ -11,6 +11,7 @@ import ( "io" "io/ioutil" "net/http" + "strconv" "time" ) @@ -50,6 +51,9 @@ type OptionMerchant struct { DisableRawJSONBody bool MerchantID string // basic auth user Password string // basic auth pw + // Data contains merchant specific other IDs or configurations. Keys/Values + // from this map are not getting used in requests towards datatrans. + Data map[string]interface{} } func (m OptionMerchant) apply(c *Client) error { @@ -536,3 +540,54 @@ func (c *Client) ReconciliationsSalesBulk(ctx context.Context, sales RequestReco } return &rrs, nil } + +// GetDataInt returns the int value from the data map or false if not found or failed to convert. +func (c *Client) GetDataInt(key string) (int, bool) { + internalID := c.currentInternalID + if !c.internalIDFound { + return 0, false + } + raw, ok := c.merchants[internalID].Data[key] + if !ok { + return 0, false + } + switch t := raw.(type) { + case string: + i, err := strconv.Atoi(t) + return i, err == nil + case int: + return t, true + case int64: + return int(t), true + } + return 0, false +} + +// GetDataString returns the string value from the data map or false if not found or failed to convert. +func (c *Client) GetDataString(key string) (string, bool) { + internalID := c.currentInternalID + if !c.internalIDFound { + return "", false + } + raw, ok := c.merchants[internalID].Data[key] + if !ok { + return "", false + } + switch t := raw.(type) { + case string: + return t, true + case []byte: + return string(t), true + default: + return fmt.Sprintf("%v", t), true + } +} + +func (c *Client) GetDataRaw(key string) (interface{}, bool) { + internalID := c.currentInternalID + if !c.internalIDFound { + return nil, false + } + raw, ok := c.merchants[internalID].Data[key] + return raw, ok +} diff --git a/client_test.go b/client_test.go index 9d915b6..1fb0917 100644 --- a/client_test.go +++ b/client_test.go @@ -193,3 +193,37 @@ func TestClient_AliasDelete_Error(t *testing.T) { t.Error("errors not equal") } } + +func TestClient_GetData(t *testing.T) { + c, err := datatrans.MakeClient( + datatrans.OptionMerchant{ + InternalID: "", // default + MerchantID: "A", + Data: map[string]interface{}{ + "k1": "1", + }, + }, + datatrans.OptionMerchant{ + InternalID: "B", + MerchantID: "B", + Data: map[string]interface{}{ + "k2": "2", + }, + }, + ) + must(t, err) + if v, ok := c.GetDataString("k1"); !ok || v != "1" { + t.Errorf("wrong value for k1: %#v", v) + } + if v, ok := c.GetDataInt("k1"); !ok || v != 1 { + t.Errorf("wrong value for k1: %#v", v) + } + + c2 := c.WithMerchant("B") + if v, ok := c2.GetDataString("k2"); !ok || v != "2" { + t.Errorf("wrong value for k2: %#v", v) + } + if v, ok := c2.GetDataInt("k2"); !ok || v != 2 { + t.Errorf("wrong value for k2: %#v", v) + } +}