Skip to content

Commit 3281efb

Browse files
committed
Create methods needed
1 parent 29a0530 commit 3281efb

25 files changed

+857
-171
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
1616
$path
17-
.idea
17+
.idea
18+
.env

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ import "github.com/NdoleStudio/neero-go"
2828

2929
## Implemented
3030

31-
- [Status Codes](#status-codes)
32-
- `GET /200`: OK
31+
- Payment Methods
32+
- `POST /api/v1/payment-methods`: Create Payment Method
33+
- `POST /api/v1/payment-methods/resolve-details`: Resolve Payment Method Details
34+
- Balances
35+
- `GET /api/v1/balances/payment-method/{paymentMethodId}`: Get Balance for a Payment Method
36+
- Transaction Intents
37+
- `POST /api/v1/transaction-intents/cash-in`: Create Cash In Payment Intent
38+
- `POST /api/v1/transaction-intents/cash-out`: Create Cash Out Payment Intent
39+
- `GET /api/v1/transaction-intents/{transactionId}`: Find Transaction Intent By Id
3340

3441
## Usage
3542

@@ -45,7 +52,7 @@ import (
4552
)
4653

4754
func main() {
48-
statusClient := client.New(client.WithDelay(200))
55+
client := neero.New(neero.WithSecretKey("" /*Your Neero merchant account secret key*/))
4956
}
5057
```
5158

balance.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package neero
2+
3+
// BalanceResponse represents the account balance information.
4+
type BalanceResponse struct {
5+
Balance float64 `json:"balance"`
6+
Currency string `json:"currency"`
7+
}

balance_service.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package neero
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
)
8+
9+
// balanceService is the API client for the `/payment-gateway/api/v1/balances/` endpoint
10+
type balanceService service
11+
12+
// Get balance By Payment Method Id
13+
//
14+
// API Docs: https://app.theneo.io/neero/neero-payment-gateway/balances/get-balance-by-payment-method-id
15+
func (service *balanceService) Get(ctx context.Context, paymentMethodID string) (*BalanceResponse, *Response, error) {
16+
request, err := service.client.newRequest(ctx, http.MethodGet, "/payment-gateway/api/v1/balances/payment-method/"+paymentMethodID, nil)
17+
if err != nil {
18+
return nil, nil, err
19+
}
20+
21+
response, err := service.client.do(request)
22+
if err != nil {
23+
return nil, response, err
24+
}
25+
26+
balance := new(BalanceResponse)
27+
if err = json.Unmarshal(*response.Body, balance); err != nil {
28+
return nil, response, err
29+
}
30+
31+
return balance, response, nil
32+
}

client.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
package client
1+
package neero
22

33
import (
44
"bytes"
55
"context"
66
"encoding/json"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"net/http"
11-
"strconv"
1210
)
1311

1412
type service struct {
1513
client *Client
1614
}
1715

18-
// Client is the campay API client.
16+
// Client is the neero API client.
1917
// Do not instantiate this client with Client{}. Use the New method instead.
2018
type Client struct {
21-
httpClient *http.Client
22-
common service
23-
baseURL string
24-
delay int
25-
26-
Status *statusService
19+
common service
20+
httpClient *http.Client
21+
baseURL string
22+
secretKey string
23+
PaymentMethod *paymentMethodService
24+
Balance *balanceService
25+
TransactionIntent *transactionIntentService
2726
}
2827

29-
// New creates and returns a new campay.Client from a slice of campay.ClientOption.
28+
// New creates and returns a new neero.Client from a slice of neero.ClientOption.
3029
func New(options ...Option) *Client {
3130
config := defaultClientConfig()
3231

@@ -36,19 +35,21 @@ func New(options ...Option) *Client {
3635

3736
client := &Client{
3837
httpClient: config.httpClient,
39-
delay: config.delay,
38+
secretKey: config.secretKey,
4039
baseURL: config.baseURL,
4140
}
4241

4342
client.common.client = client
44-
client.Status = (*statusService)(&client.common)
43+
client.PaymentMethod = (*paymentMethodService)(&client.common)
44+
client.Balance = (*balanceService)(&client.common)
45+
client.TransactionIntent = (*transactionIntentService)(&client.common)
4546
return client
4647
}
4748

4849
// newRequest creates an API request. A relative URL can be provided in uri,
4950
// in which case it is resolved relative to the BaseURL of the Client.
5051
// URI's should always be specified without a preceding slash.
51-
func (client *Client) newRequest(ctx context.Context, method, uri string, body interface{}) (*http.Request, error) {
52+
func (client *Client) newRequest(ctx context.Context, method, uri string, body any) (*http.Request, error) {
5253
var buf io.ReadWriter
5354
if body != nil {
5455
buf = &bytes.Buffer{}
@@ -68,9 +69,7 @@ func (client *Client) newRequest(ctx context.Context, method, uri string, body i
6869
req.Header.Set("Content-Type", "application/json")
6970
req.Header.Set("Accept", "application/json")
7071

71-
if client.delay > 0 {
72-
client.addURLParams(req, map[string]string{"sleep": strconv.Itoa(client.delay)})
73-
}
72+
req.SetBasicAuth(client.secretKey, "")
7473

7574
return req, nil
7675
}
@@ -103,7 +102,7 @@ func (client *Client) do(req *http.Request) (*Response, error) {
103102
return resp, err
104103
}
105104

106-
_, err = io.Copy(ioutil.Discard, httpResponse.Body)
105+
_, err = io.Copy(io.Discard, httpResponse.Body)
107106
if err != nil {
108107
return resp, err
109108
}
@@ -120,7 +119,7 @@ func (client *Client) newResponse(httpResponse *http.Response) (*Response, error
120119
resp := new(Response)
121120
resp.HTTPResponse = httpResponse
122121

123-
buf, err := ioutil.ReadAll(resp.HTTPResponse.Body)
122+
buf, err := io.ReadAll(resp.HTTPResponse.Body)
124123
if err != nil {
125124
return nil, err
126125
}

client_config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package client
1+
package neero
22

33
import "net/http"
44

55
type clientConfig struct {
66
httpClient *http.Client
7-
delay int
7+
secretKey string
88
baseURL string
99
}
1010

1111
func defaultClientConfig() *clientConfig {
1212
return &clientConfig{
1313
httpClient: http.DefaultClient,
14-
delay: 0,
15-
baseURL: "https://httpstat.us",
14+
secretKey: "",
15+
baseURL: "https://api.neero.tech",
1616
}
1717
}

client_option.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package neero
22

33
import (
44
"net/http"
@@ -35,12 +35,9 @@ func WithBaseURL(baseURL string) Option {
3535
})
3636
}
3737

38-
// WithDelay sets the delay in milliseconds before a response is gotten.
39-
// The delay must be > 0 for it to be used.
40-
func WithDelay(delay int) Option {
38+
// WithSecretKey sets the secret key to be used for API requests.
39+
func WithSecretKey(secretKey string) Option {
4140
return clientOptionFunc(func(config *clientConfig) {
42-
if delay > 0 {
43-
config.delay = delay
44-
}
41+
config.secretKey = secretKey
4542
})
4643
}

client_option_test.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package neero
22

33
import (
44
"net/http"
@@ -71,34 +71,19 @@ func TestWithBaseURL(t *testing.T) {
7171
})
7272
}
7373

74-
func TestWithDelay(t *testing.T) {
75-
t.Run("delay is set successfully", func(t *testing.T) {
74+
func TestWithSecretKey(t *testing.T) {
75+
t.Run("secret key is set successfully", func(t *testing.T) {
7676
// Setup
7777
t.Parallel()
7878

7979
// Arrange
8080
config := defaultClientConfig()
81-
delay := 1
81+
key := "sk_test_1234567890"
8282

8383
// Act
84-
WithDelay(delay).apply(config)
84+
WithSecretKey(key).apply(config)
8585

8686
// Assert
87-
assert.Equal(t, delay, config.delay)
88-
})
89-
90-
t.Run("delay is not set when value < 0", func(t *testing.T) {
91-
// Setup
92-
t.Parallel()
93-
94-
// Arrange
95-
config := defaultClientConfig()
96-
delay := -1
97-
98-
// Act
99-
WithDelay(delay).apply(config)
100-
101-
// Assert
102-
assert.Equal(t, 0, config.delay)
87+
assert.Equal(t, key, config.secretKey)
10388
})
10489
}

e2e/balance_service_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package e2e
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"os"
7+
"testing"
8+
9+
"github.com/NdoleStudio/neero-go"
10+
"github.com/davecgh/go-spew/spew"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestBalanceService_Get(t *testing.T) {
15+
// Arrange
16+
request := &neero.CreatePaymentMethodRequestNeroMerchantDetails{
17+
MerchantKey: os.Getenv("NEERO_MERCHANT_KEY"),
18+
StoreID: os.Getenv("NEERO_STORE_ID"),
19+
BalanceID: os.Getenv("NEERO_BALANCE_ID"),
20+
OperatorID: os.Getenv("NEERO_OPERATOR_ID"),
21+
}
22+
23+
paymentMethod, _, _ := client.PaymentMethod.Create(context.Background(), request)
24+
spew.Dump(paymentMethod.ID)
25+
26+
// Act
27+
balance, response, err := client.Balance.Get(context.Background(), paymentMethod.ID)
28+
29+
// Assert
30+
assert.Nil(t, err)
31+
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
32+
spew.Dump(balance)
33+
}

e2e/client.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package e2e
2+
3+
import (
4+
"os"
5+
6+
"github.com/NdoleStudio/neero-go"
7+
// Auto load .env file
8+
_ "github.com/joho/godotenv/autoload"
9+
)
10+
11+
var client = neero.New(neero.WithSecretKey(os.Getenv("NEERO_SECRET_KEY")))

0 commit comments

Comments
 (0)