diff --git a/client.go b/client.go index 9525879..e3c5823 100644 --- a/client.go +++ b/client.go @@ -27,6 +27,7 @@ type Client interface { TestClient RateClient StatementClient + FxClient } var _ Client = (*client)(nil) diff --git a/fx.go b/fx.go new file mode 100644 index 0000000..72e0356 --- /dev/null +++ b/fx.go @@ -0,0 +1,60 @@ +package clearbank + +import ( + "context" + "fmt" + "net/http" +) + +type FxClient interface { + InitiateFxOrder(context.Context, FXPayload) error +} + +type FXAttestation string + +const ( + FXAttestationSameOwner FXAttestation = "Y" + FXAttestationDifferentOwner FXAttestation = "N" +) + +type FXPayload struct { + CustomerInformation struct { + SellAccount struct { + Owner string `json:"owner"` + IBAN string `json:"iban"` + } `json:"sellAccount"` + BuyAccount struct { + Owner string `json:"owner"` + IBAN string `json:"iban"` + } `json:"buyAccount"` + Attestation FXAttestation `json:"attestation"` + } `json:"customerInformation"` + TradeInformation struct { + ValueDate string `json:"valueDate,omitempty"` + Details struct { + InstructedAmount float64 `json:"instructedAmount"` + FixedSide FixedSide `json:"fixedSide"` + SellCurrency string `json:"sellCurrency"` + BuyCurrency string `json:"buyCurrency"` + } `json:"details"` + Margin struct { + Amount float64 `json:"amount"` + Account struct { + Owner string `json:"owner"` + IBAN string `json:"iban"` + } `json:"account"` + } `json:"margin,omitempty"` + EndToEndID string `json:"endToEndId"` + UnstructuredInformation string `json:"unstructuredInformation,omitempty"` + } `json:"tradeInformation"` +} + +func (c *client) InitiateFxOrder(ctx context.Context, payload FXPayload) error { + req, err := c.newRequest(ctx, http.MethodPost, "/fx/v1/order", payload) + if err != nil { + return fmt.Errorf("failed to create request: %w", err) + } + + req.ExpectStatus(http.StatusAccepted) + return c.do(ctx, req) +} diff --git a/fx_test.go b/fx_test.go new file mode 100644 index 0000000..06ec150 --- /dev/null +++ b/fx_test.go @@ -0,0 +1,28 @@ +package clearbank_test + +import ( + "bytes" + "context" + "io" + "net/http" + "testing" + + "github.com/brokeyourbike/clearbank-api-client-go" + "github.com/brokeyourbike/clearbank-api-client-go/signature" + "github.com/stretchr/testify/assert" + mock "github.com/stretchr/testify/mock" +) + +func TestInitiateFxOrder(t *testing.T) { + mockSigner := signature.NewMockSigner(t) + mockHttpClient := clearbank.NewMockHttpClient(t) + client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient)) + + ctx := clearbank.RequestIdContext(context.TODO(), "123") + mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once() + + resp := &http.Response{StatusCode: http.StatusAccepted, Body: io.NopCloser(bytes.NewReader(nil))} + mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once() + + assert.NoError(t, client.InitiateFxOrder(ctx, clearbank.FXPayload{})) +}