-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Iknite-Space/exchange_rate_service_init_
Implemented Transfers service
- Loading branch information
Showing
6 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package flutterwave | ||
|
||
// ExchangeRateResponse is data returned when querying a transaction. | ||
type TransferRateResponse struct { | ||
Status string `json:"status"` | ||
Message string `json:"message"` | ||
Data struct { | ||
Rate float64 `json:"rate"` | ||
Source Payment `json:"source"` | ||
Destination Payment `json:"destination"` | ||
} | ||
} | ||
|
||
// Payment is data returned for either the source or the destination. | ||
type Payment struct { | ||
Currency string `json:"currency"` | ||
Amount int `json:"amount"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package flutterwave | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
// transfersService is the API client for the `/v3/transfers` endpoint | ||
type transfersService service | ||
|
||
// Estimate the Transfer Rate of a transaction | ||
// | ||
// API Docs: https://developer.flutterwave.com/reference/check-transfer-rates | ||
func (service *transfersService) Rate(ctx context.Context, amount int, destination_currency, source_currency string) (*TransferRateResponse, *Response, error) { | ||
uri := "/v3/transfers/rates" | ||
|
||
request, err := service.client.newRequest(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// Adding the parameters | ||
requestWithParams := service.client.addURLParams(request, map[string]string{ | ||
"amount": strconv.Itoa(amount), | ||
"destination_currency": destination_currency, | ||
"source_currency": source_currency, | ||
}) | ||
|
||
response, err := service.client.do(requestWithParams) | ||
if err != nil { | ||
return nil, response, err | ||
} | ||
|
||
var data TransferRateResponse | ||
if err = json.Unmarshal(*response.Body, &data); err != nil { | ||
return nil, response, err | ||
} | ||
|
||
return &data, response, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package flutterwave | ||
|
||
import ( | ||
"context" | ||
"github.com/NdoleStudio/flutterwave-go/internal/helpers" | ||
"github.com/NdoleStudio/flutterwave-go/internal/stubs" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestTransfersService_Rate(t *testing.T) { | ||
// Setup | ||
t.Parallel() | ||
|
||
// Arrange | ||
server := helpers.MakeTestServer(http.StatusOK, string(stubs.TransferRateResponse())) | ||
client := New(WithBaseURL(server.URL)) | ||
|
||
// Act | ||
rate, response, err := client.Transfers.Rate(context.Background(), 1000, "USD", "NGN") | ||
|
||
// Assert | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) | ||
assert.Equal(t, stubs.TransferRateResponse(), *response.Body) | ||
assert.Equal(t, 624240, rate.Data.Source.Amount) | ||
|
||
// Teardown | ||
server.Close() | ||
} | ||
|
||
func TestTransfersService_Rate_Failure(t *testing.T) { | ||
// Setup | ||
t.Parallel() | ||
|
||
// Arrange | ||
server := helpers.MakeTestServer(http.StatusInternalServerError, `{"error": "internal server error"}`) | ||
client := New(WithBaseURL(server.URL)) | ||
|
||
// Act | ||
rate, response, err := client.Transfers.Rate(context.Background(), 1000, "USD", "NGN") | ||
|
||
// Assert | ||
assert.NotNil(t, err) // Expect an error | ||
assert.Nil(t, rate) // The rate should be nil due to failure | ||
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) | ||
assert.Contains(t, err.Error(), "500") // Ensure error message contains 500 | ||
|
||
// Teardown | ||
server.Close() | ||
} |