-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.go
211 lines (179 loc) · 5.58 KB
/
client.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package dtone
import (
"bytes"
"fmt"
"io"
"net/http"
"strings"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/jsonx"
"github.com/shopspring/decimal"
)
type StatusCID int
const (
apiURL = "https://dvs-api.dtone.com/v1/"
// see https://dvs-api-doc.dtone.com/#section/Overview/Transactions
StatusCIDCreated StatusCID = 1
StatusCIDConfirmed StatusCID = 2
StatusCIDRejected StatusCID = 3
StatusCIDCancelled StatusCID = 4
StatusCIDSubmitted StatusCID = 5
StatusCIDCompleted StatusCID = 7
StatusCIDReversed StatusCID = 8
StatusCIDDeclined StatusCID = 9
)
// Client is a DTOne client, see https://dvs-api-doc.dtone.com/ for API docs
type Client struct {
httpClient *http.Client
httpRetries *httpx.RetryConfig
key string
secret string
}
// NewClient creates a new DT One client
func NewClient(httpClient *http.Client, httpRetries *httpx.RetryConfig, key, secret string) *Client {
return &Client{httpClient: httpClient, httpRetries: httpRetries, key: key, secret: secret}
}
// error response contains errors when a request fails
type errorResponse struct {
Errors []struct {
Code int `json:"code"`
Message string `json:"message"`
} `json:"errors"`
}
func (e *errorResponse) Error() string {
msgs := make([]string, len(e.Errors))
for i := range e.Errors {
msgs[i] = e.Errors[i].Message
}
return strings.Join(msgs, ", ")
}
// Operator is a mobile operator
type Operator struct {
ID int `json:"id"`
Name string `json:"name"`
Country struct {
Name string `json:"name"`
ISOCode string `json:"iso_code"`
Regions []struct {
Name string `json:"name"`
Code string `json:"code"`
} `json:"regions"`
} `json:"country"`
Identified bool `json:"identified"`
}
// LookupMobileNumber see https://dvs-api-doc.dtone.com/#tag/Mobile-Number
func (c *Client) LookupMobileNumber(tel string) ([]*Operator, *httpx.Trace, error) {
var response []*Operator
trace, err := c.request("GET", fmt.Sprintf("lookup/mobile-number/%s", tel), nil, &response)
if err != nil {
return nil, trace, err
}
return response, trace, nil
}
// Product is an available digital services product
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Service struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"service"`
Operator struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"operator"`
Type string `json:"type"`
Source struct {
Amount decimal.Decimal `json:"amount"`
Unit string `json:"unit"`
UnitType string `json:"unit_type"`
} `json:"source"`
Destination struct {
Amount decimal.Decimal `json:"amount"`
Unit string `json:"unit"`
UnitType string `json:"unit_type"`
} `json:"destination"`
}
// Products see https://dvs-api-doc.dtone.com/#tag/Products
func (c *Client) Products(_type string, operatorID int) ([]*Product, *httpx.Trace, error) {
var response []*Product
// TODO endpoint could return more than 100 products in which case we need to page
trace, err := c.request("GET", fmt.Sprintf("products?type=%s&operator_id=%d&per_page=100", _type, operatorID), nil, &response)
if err != nil {
return nil, trace, err
}
return response, trace, nil
}
// Transaction is a product sent to a beneficiary
type Transaction struct {
ID int64 `json:"id"`
ExternalID string `json:"external_id"`
CreationDate string `json:"creation_date"`
ConfirmationExpirationDate string `json:"confirmation_expiration_date"`
ConfirmationDate string `json:"confirmation_date"`
Status struct {
ID int `json:"id"`
Message string `json:"message"`
Class struct {
ID StatusCID `json:"id"`
Message string `json:"message"`
}
} `json:"status"`
}
// TransactionSync see https://dvs-api-doc.dtone.com/#tag/Transactions
func (c *Client) TransactionSync(externalID string, productID int, mobileNumber string) (*Transaction, *httpx.Trace, error) {
var response *Transaction
type creditPartyIdentifier struct {
MobileNumber string `json:"mobile_number"`
}
payload := &struct {
ExternalID string `json:"external_id"`
ProductID int `json:"product_id"`
AutoConfirm bool `json:"auto_confirm"`
CreditPartyIdentifier creditPartyIdentifier `json:"credit_party_identifier"`
}{
ExternalID: externalID,
ProductID: productID,
AutoConfirm: true,
CreditPartyIdentifier: creditPartyIdentifier{
MobileNumber: mobileNumber,
},
}
trace, err := c.request("POST", "sync/transactions", payload, &response)
if err != nil {
return nil, trace, err
}
return response, trace, nil
}
func (c *Client) request(method, endpoint string, payload any, response any) (*httpx.Trace, error) {
url := apiURL + endpoint
headers := map[string]string{}
var body io.Reader
if payload != nil {
data, err := jsonx.Marshal(payload)
if err != nil {
return nil, err
}
body = bytes.NewReader(data)
headers["Content-Type"] = "application/json"
}
req, err := httpx.NewRequest(method, url, body, headers)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.key, c.secret)
trace, err := httpx.DoTrace(c.httpClient, req, c.httpRetries, nil, -1)
if err != nil {
return trace, err
}
if trace.Response.StatusCode >= 400 {
response := &errorResponse{}
jsonx.Unmarshal(trace.ResponseBody, response)
return trace, response
}
if response != nil {
return trace, jsonx.Unmarshal(trace.ResponseBody, response)
}
return trace, nil
}