-
Notifications
You must be signed in to change notification settings - Fork 1
/
paystack.go
81 lines (67 loc) · 1.74 KB
/
paystack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package paystack
import (
"fmt"
"log/slog"
"net/http"
"net/url"
"os"
"strings"
"time"
)
type Client struct {
APIKey string
HttpClient *http.Client
log *slog.Logger
Transaction *Transaction
TransactionSplit *TransactionSplit
Plan *Plans
Subscription *Subscription
BaseUrl *url.URL
}
const BASE_URL = "https://api.paystack.co"
// Response represents arbitrary response data
type APIResponse map[string]interface{}
type GenericResponse map[string]interface{}
type Metadata map[string]interface{}
type QueryType struct {
Key string
Value string
}
// PaginationMeta is pagination metadata for paginated responses from the Paystack API
type PaginationMeta struct {
Total int `json:"total"`
Skipped int `json:"skipped"`
PerPage int `json:"perPage"`
Page int `json:"page"`
PageCount int `json:"pageCount"`
}
// NewClient creates a new Paystack API client with the given API key.
func NewClient(apiKey string) *Client {
httpClient := &http.Client{
Timeout: 5 * time.Second,
}
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
parsedUrl, _ := url.Parse(BASE_URL)
c := &Client{APIKey: apiKey, HttpClient: httpClient, log: logger, BaseUrl: parsedUrl}
c.Transaction = newTransaction(c)
c.TransactionSplit = newTransactionSplit(c)
c.Plan = newPlans(c)
c.Subscription = newSubscription(c)
return c
}
func Query(key, value string) QueryType {
return QueryType{
Key: key,
Value: value,
}
}
func addQueryToUrl(url string, queries ...QueryType) string {
for _, query := range queries {
if strings.Contains(url, "?") {
url += fmt.Sprintf("&%s=%s", query.Key, query.Value)
} else {
url += fmt.Sprintf("?%s=%s", query.Key, query.Value)
}
}
return url
}