Skip to content

Commit 67577dd

Browse files
authored
fix(payments): connector config compatibility (#1754)
2 parents d772032 + 88f3592 commit 67577dd

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

components/payments/cmd/connectors/internal/api/connector.go

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import (
1313
"github.com/formancehq/payments/internal/otel"
1414
"github.com/formancehq/stack/libs/go-libs/api"
1515
"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"
16+
"github.com/formancehq/stack/libs/go-libs/logging"
1617
"github.com/formancehq/stack/libs/go-libs/pointer"
1718
"github.com/google/uuid"
1819
"github.com/gorilla/mux"
1920
"go.opentelemetry.io/otel/attribute"
2021
"go.opentelemetry.io/otel/trace"
22+
"golang.org/x/text/cases"
23+
"golang.org/x/text/language"
2124
)
2225

2326
type APIVersion int
@@ -110,14 +113,65 @@ func readConfig[Config models.ConnectorConfigObject](
110113
return
111114
}
112115

113-
err = json.NewEncoder(w).Encode(api.BaseResponse[Config]{
114-
Data: &config,
115-
})
116+
b, err := config.Marshal()
116117
if err != nil {
117118
otel.RecordError(span, err)
118-
api.InternalServerError(w, r, err)
119+
handleConnectorsManagerErrors(w, r, err)
120+
return
121+
}
122+
123+
var m map[string]interface{}
124+
err = json.Unmarshal(b, &m)
125+
if err != nil {
126+
otel.RecordError(span, err)
127+
handleConnectorsManagerErrors(w, r, err)
128+
return
129+
}
130+
131+
// inject provider into config json so SDK can distinguish between config types
132+
caser := cases.Title(language.English)
133+
m["provider"] = caser.String(connectorID.Provider.String())
134+
135+
// rewrite pollingDuration struct as a string to match API spec
136+
pollingPeriod, ok := m["pollingPeriod"].(map[string]interface{})
137+
if ok {
138+
duration, found := pollingPeriod["duration"]
139+
if found {
140+
var ns int64
141+
switch v := duration.(type) {
142+
case int64:
143+
ns = v
144+
case float64:
145+
ns = int64(v)
146+
default:
147+
logging.FromContext(ctx).Debugf("pollingPeriod.Duration was of an unexpected type: %T", v)
148+
}
149+
m["pollingPeriod"] = time.Duration(ns).String()
150+
}
151+
}
152+
153+
result, err := json.Marshal(m)
154+
if err != nil {
155+
otel.RecordError(span, err)
156+
handleConnectorsManagerErrors(w, r, err)
119157
return
120158
}
159+
160+
rawConfig := json.RawMessage(result)
161+
writeConfig(w, r, rawConfig, span)
162+
}
163+
}
164+
165+
func writeConfig(w http.ResponseWriter, r *http.Request, v any, span trace.Span) {
166+
resBody := api.BaseResponse[any]{
167+
Data: &v,
168+
}
169+
w.Header().Set("Content-Type", "application/json")
170+
if v != nil {
171+
if err := json.NewEncoder(w).Encode(resBody); err != nil {
172+
otel.RecordError(span, err)
173+
api.InternalServerError(w, r, err)
174+
}
121175
}
122176
}
123177

components/payments/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ require (
4343
go.uber.org/mock v0.3.0
4444
golang.org/x/oauth2 v0.16.0
4545
golang.org/x/sync v0.5.0
46+
golang.org/x/text v0.14.0
4647
)
4748

4849
require (
@@ -194,7 +195,6 @@ require (
194195
golang.org/x/mod v0.11.0 // indirect
195196
golang.org/x/net v0.21.0 // indirect
196197
golang.org/x/sys v0.18.0 // indirect
197-
golang.org/x/text v0.14.0 // indirect
198198
golang.org/x/tools v0.7.0 // indirect
199199
google.golang.org/appengine v1.6.8 // indirect
200200
google.golang.org/genproto/googleapis/api v0.0.0-20240125205218-1f4bbc51befe // indirect

0 commit comments

Comments
 (0)