-
Notifications
You must be signed in to change notification settings - Fork 2
/
mpesa.go
335 lines (256 loc) · 8.23 KB
/
mpesa.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package yuri
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"reflect"
"time"
)
type Mpesa struct {
Live bool
ConsumerKey string
ConsumerSecret string
}
func NewMpesa(ConsumerKey, ConsumerSecret string, live bool) Mpesa {
return Mpesa{
Live: live,
ConsumerKey: ConsumerKey,
ConsumerSecret: ConsumerSecret,
}
}
func (m *Mpesa) SetMode(mode bool) {
m.Live = mode
}
func (m *Mpesa) StkPushRequest(body StKPushRequestBody, passKey string) (*StkPushResult, error) {
if body.Timestamp == "" {
t := time.Now()
fTime := t.Format("20060102150405")
body.Timestamp = fTime
body.Password = GeneratePassword(body.BusinessShortCode, passKey, fTime)
}
body.TransactionType = CustomerPayBillOnline
var stkPushResult StkPushResult
err := m.sendAndProcessStkPushRequest(m.getStkPush(), body, &stkPushResult, nil)
return &stkPushResult, err
}
func (m *Mpesa) StkPushQuery(body StkPushQueryRequestBody, passKey string) (*StkPushQueryResponseBody, error) {
if body.Timestamp == "" {
t := time.Now()
fTime := t.Format("20060102150405")
body.Timestamp = fTime
body.Password = GeneratePassword(body.BusinessShortCode, passKey, fTime)
}
var stkPushResult StkPushQueryResponseBody
err := m.sendAndProcessStkPushRequest(m.getStkPushQuery(), body, &stkPushResult, nil)
return &stkPushResult, err
}
// B2CRequest Sends Money from a business to the Customer
func (m *Mpesa) B2CRequest(b2c B2CRequestBody) (*MpesaResult, error) {
if b2c.CommandID == "" {
b2c.CommandID = BusinessPayment
}
return m.sendAndProcessMpesaRequest("https://api.safaricom.co.ke/mpesa/b2c/v1/paymentrequest", b2c, nil)
}
// B2BRequest Sends Money from a business to the business
// I.e from onePayBill to another
func (m *Mpesa) B2BRequest(b2b B2BRequestBody) (*MpesaResult, error) {
return m.sendAndProcessMpesaRequest(m.getB2BUrl(), b2b, nil)
}
//C2BRequest Sends Money from a business to the Customer
///func (m *Mpesa) C2BRequest(c2 B2BRequestBody) (*MpesaResult, error) {
///return nil, nil
///}
// use AccountBalance as the CommandID
// use AccountBalanceRequestBody.indentifier as 4 if its shortcode
//
func (m *Mpesa) AccountBalanceRequest(balance AccountBalanceRequestBody) (*MpesaResult, error) {
balance.CommandID = AccountBalance
//
return m.sendAndProcessMpesaRequest(m.getBalanceUrl(), balance, nil)
}
func (m *Mpesa) TransactionStatusRequest(transactionStatusRequestBody TransactionStatusRequestBody) (*MpesaResult, error) {
////1 for user
///4 for organization (indentifiertype)
transactionStatusRequestBody.CommandID = TransactionStatusQuery
return m.sendAndProcessMpesaRequest(m.getTransactionStatusUrl(), transactionStatusRequestBody, nil)
}
func (m *Mpesa) RegisterC2BUrls(C2BURLRequestBody RegisterC2BURLRequestBody) (*MpesaResult, error) {
return m.sendAndProcessMpesaRequest(m.getC2BRegisterUrl(), C2BURLRequestBody, nil)
}
func (m *Mpesa) SimulateC2BTransaction(c2BSimulationRequestBody C2BSimulationRequestBody) (*MpesaResult, error) {
return m.sendAndProcessMpesaRequest(m.getC2BSimulationUrl(), c2BSimulationRequestBody, nil)
}
func (m *Mpesa) sendAndProcessMpesaRequest(url string, data interface{}, extraHeader map[string]string) (*MpesaResult, error) {
token, err := m.GetAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Authorization"] = "Bearer " + token.AccessToken
///add the extra headers
//Get union of the headers
for k, v := range extraHeader {
headers[k] = v
}
resp, err := postRequest(url, data, headers)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) {
b, _ := ioutil.ReadAll(resp.Body)
return nil, &RequestError{Message: string(b), StatusCode: resp.StatusCode}
}
var response MpesaResult
///var respe map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, errors.New("error converting from json")
}
return &response, nil
}
func (m *Mpesa) sendAndProcessStkPushRequest(url string, data interface{}, respItem interface{}, extraHeader map[string]string) error {
if reflect.ValueOf(respItem).Kind() != reflect.Ptr {
log.Println("not a pointer")
return errors.New("response should be a pointer")
}
token, err := m.GetAccessToken()
if err != nil {
return err
}
log.Println(token.AccessToken)
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
///auth := "Bearer " + token.AccessToken
//headers["authorization"]= auth
headers["Authorization"] = "Bearer " + token.AccessToken
///add the extra headers
//Get union of the headers
for k, v := range extraHeader {
headers[k] = v
}
resp, err := postRequest(url, data, headers)
if err != nil {
return err
}
defer resp.Body.Close()
if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) {
b, _ := ioutil.ReadAll(resp.Body)
return &RequestError{Message: string(b), StatusCode: resp.StatusCode}
}
///var respe map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(respItem); err != nil {
PrintStruct(err)
return errors.New("error converting from json")
}
return nil
}
func getRequest(url string, headers map[string]string, queryParameters map[string]string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
for key, value := range queryParameters {
q := req.URL.Query()
q.Add(key, value)
req.URL.RawQuery = q.Encode()
}
for key, value := range headers {
req.Header.Set(key, value)
}
client := &http.Client{
//Timeout: 20 * time.Second
}
return client.Do(req)
}
func postRequest(url string, data interface{}, headers map[string]string) (*http.Response, error) {
///requestBody, err := json.Marshal(data)
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(b))
if err != nil {
return nil, err
}
for key, value := range headers {
req.Header.Set(key, value)
}
client := &http.Client{
//Timeout: 15 * time.Second
}
return client.Do(req)
}
//GetAccessToken will get the token to be used to query data
func (m *Mpesa) GetAccessToken() (*AccessTokenResponse, error) {
req, err := http.NewRequest(http.MethodGet, m.getAccessTokenUrl(), nil)
if err != nil {
return nil, err
}
log.Println(req.URL.String())
req.SetBasicAuth(m.ConsumerKey, m.ConsumerSecret)
req.Header.Add("Accept", "application/json")
req.Header.Set("Accept", "application/json")
client := &http.Client{
///Timeout: 15 * time.Second
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if !(resp.StatusCode >= 200 && resp.StatusCode <= 299) {
b, _ := ioutil.ReadAll(resp.Body)
if string(b) == "" {
return nil, &RequestError{Message: "Error getting token", StatusCode: resp.StatusCode}
}
return nil, &RequestError{Message: string(b), StatusCode: resp.StatusCode}
}
var token AccessTokenResponse
if err := json.NewDecoder(resp.Body).Decode(&token); err != nil {
return nil, errors.New("error converting from json")
}
return &token, nil
}
func (m *Mpesa) GetSecurityCredential(initiatorPassword string) (string, error) {
var fileName string
if m.Live {
fileName = "https://developer.safaricom.co.ke/sites/default/files/cert/cert_prod/cert.cer"
} else {
fileName = "https://developer.safaricom.co.ke/sites/default/files/cert/cert_sandbox/cert.cer"
}
resp, err := getRequest(fileName, nil, nil)
if err != nil {
return "", err
}
defer resp.Body.Close()
var key []byte
key, err = ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
block, _ := pem.Decode(key)
var cert *x509.Certificate
cert, _ = x509.ParseCertificate(block.Bytes)
rsaPublicKey := cert.PublicKey.(*rsa.PublicKey)
rng := rand.Reader
encrypted, err := rsa.EncryptPKCS1v15(rng, rsaPublicKey, []byte(initiatorPassword))
if err != nil {
return "", err
}
enc := base64.StdEncoding.EncodeToString(encrypted)
println(enc)
return enc, nil
}
func GeneratePassword(shortCode, passkey, time string) string {
password := fmt.Sprintf("%s%s%s", shortCode, passkey, time)
return base64.StdEncoding.EncodeToString([]byte(password))
}