-
Notifications
You must be signed in to change notification settings - Fork 5
/
mpesa.go
584 lines (464 loc) · 17.2 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package mpesa
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"embed"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"image/png"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
// Environment indicates the current mode the application is running on. Either EnvironmentSandbox or EnvironmentProduction.
type Environment uint8
// cache stores the AuthorizationResponse for the specified accessTokenTTL
type cache map[string]AuthorizationResponse
const (
EnvironmentSandbox Environment = iota
EnvironmentProduction
)
type ResponseType string
const (
ResponseTypeCanceled ResponseType = "Canceled"
ResponseTypeComplete ResponseType = "Completed"
)
var accessTokenTTL = 55 * time.Minute
// requiredURLScheme present the required scheme for the callbacks
const requiredURLScheme = "https"
const (
sandboxBaseURL = "https://sandbox.safaricom.co.ke"
productionBaseURL = "https://api.safaricom.co.ke"
)
// IsProduction returns true if the current env is set to production.
func (e Environment) IsProduction() bool {
return e == EnvironmentProduction
}
// BaseURL returns the base url for the current Environment
func (e Environment) BaseURL() string {
if !e.IsProduction() {
return sandboxBaseURL
}
return productionBaseURL
}
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
//go:embed certs
var certFS embed.FS
// Mpesa is an app to make a transaction
type Mpesa struct {
client HttpClient
environment Environment
mu sync.Mutex
cache cache
consumerKey string
consumerSecret string
}
var (
// ErrInvalidPasskey indicates that no passkey was provided.
ErrInvalidPasskey = errors.New("mpesa: passkey cannot be empty")
// ErrInvalidInitiatorPassword indicates that no initiator password was provided.
ErrInvalidInitiatorPassword = errors.New("mpesa: initiator password cannot be empty")
)
// validateURL checks if the provided URL is valid and is being server via https
func validateURL(rawURL string) error {
u, err := url.ParseRequestURI(rawURL)
if err != nil {
return fmt.Errorf("mpesa: %v", err)
}
if u.Scheme != requiredURLScheme {
return fmt.Errorf("mpesa: %q must use %q", rawURL, requiredURLScheme)
}
return nil
}
// NewApp initializes a new Mpesa app that will be used to perform C2B or B2C transactions.
func NewApp(c HttpClient, consumerKey, consumerSecret string, env Environment) *Mpesa {
if c == nil {
c = &http.Client{
Timeout: 10 * time.Second,
}
}
return &Mpesa{
client: c,
environment: env,
cache: make(cache),
consumerKey: consumerKey,
consumerSecret: consumerSecret,
}
}
// endpointAuth returns the auth endpoint prefixed with the current Environment base URL
func (m *Mpesa) endpointAuth() string {
return m.Environment().BaseURL() + `/oauth/v1/generate?grant_type=client_credentials`
}
// endpointB2C returns the account balance endpoint prefixed with the current Environment base URL
func (m *Mpesa) endpointAccountBalance() string {
return m.Environment().BaseURL() + `/mpesa/accountbalance/v1/query`
}
// endpointB2C returns the B2C endpoint prefixed with the current Environment base URL
func (m *Mpesa) endpointB2C() string {
return m.Environment().BaseURL() + `/mpesa/b2c/v1/paymentrequest`
}
// endpointBusinessPayBill returns the Business Pay Bill endpoint prefixed with the current Environment base URL
func (m *Mpesa) endpointBusinessPayBill() string {
return m.Environment().BaseURL() + `/mpesa/b2b/v1/paymentrequest`
}
// endpointB2C returns the endpoint to register C2B callbacks prefixed with the current Environment base URL
func (m *Mpesa) endpointC2BRegister() string {
return m.Environment().BaseURL() + `/mpesa/c2b/v1/registerurl`
}
// endpointB2C returns the endpoint to generate dunamic QR code prefixed with the current Environment base URL
func (m *Mpesa) endpointDynamicQR() string {
return m.Environment().BaseURL() + `/mpesa/qrcode/v1/generate`
}
// endpointSTK returns the endpoint to generate an STK push prefixed with the current Environment base URL
func (m *Mpesa) endpointSTK() string {
return m.Environment().BaseURL() + `/mpesa/stkpush/v1/processrequest`
}
// endpointSTK returns the endpoint to query the status of an STK request prefixed with the current Environment base URL
func (m *Mpesa) endpointSTKQuery() string {
return m.Environment().BaseURL() + `/mpesa/stkpushquery/v1/query`
}
// endpointSTK returns the endpoint to query the status of a transaction prefixed with the current Environment base URL
func (m *Mpesa) endpointTransactionStatus() string {
return m.Environment().BaseURL() + `/mpesa/transactionstatus/v1/query`
}
// generateTimestampAndPassword returns the current timestamp in the format YYYYMMDDHHmmss and a base64 encoded
// password in the format shortcode+passkey+timestamp
func generateTimestampAndPassword(shortcode uint, passkey string) (string, string) {
timestamp := time.Now().Format("20060102150405")
password := fmt.Sprintf("%d%s%s", shortcode, passkey, timestamp)
return timestamp, base64.StdEncoding.EncodeToString([]byte(password))
}
// makeHttpRequestWithToken makes an API call to the provided url using the provided http method.
func (m *Mpesa) makeHttpRequestWithToken(
ctx context.Context, method, url string, body interface{},
) (*http.Response, error) {
reqBody, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("mpesa: marshal request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(reqBody))
if err != nil {
return nil, fmt.Errorf("mpesa: create request: %v", err)
}
accessToken, err := m.GenerateAccessToken(ctx)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", `Bearer `+accessToken)
res, err := m.client.Do(req)
if err != nil {
return nil, fmt.Errorf("mpesa: make request: %v", err)
}
return res, nil
}
// Environment returns the current environment the app is running on.
func (m *Mpesa) Environment() Environment {
return m.environment
}
// GenerateAccessToken returns a time bound access token to call allowed APIs.
// This token should be used in all other subsequent responses to the APIs
// GenerateAccessToken will also cache the access token for the specified refresh after period
func (m *Mpesa) GenerateAccessToken(ctx context.Context) (string, error) {
m.mu.Lock()
defer m.mu.Unlock()
if cachedData, ok := m.cache[m.consumerKey]; ok {
if cachedData.setAt.Add(accessTokenTTL).After(time.Now()) {
return cachedData.AccessToken, nil
}
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.endpointAuth(), nil)
if err != nil {
return "", fmt.Errorf("mpesa: create auth request: %v", err)
}
req.SetBasicAuth(m.consumerKey, m.consumerSecret)
res, err := m.client.Do(req)
if err != nil {
return "", fmt.Errorf("mpesa: make auth request: %v", err)
}
//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("mpesa: auth failed with status: %v", res.Status)
}
var response AuthorizationResponse
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return "", fmt.Errorf("mpesa: decode auth response: %v", err)
}
response.setAt = time.Now()
m.cache[m.consumerKey] = response
return m.cache[m.consumerKey].AccessToken, nil
}
// STKPush initiates online payment on behalf of a customer using STKPush.
func (m *Mpesa) STKPush(ctx context.Context, passkey string, req STKPushRequest) (*Response, error) {
if passkey == "" {
return nil, ErrInvalidPasskey
}
req.Timestamp, req.Password = generateTimestampAndPassword(req.BusinessShortCode, passkey)
res, err := m.makeHttpRequestWithToken(ctx, http.MethodPost, m.endpointSTK(), req)
if err != nil {
return nil, err
}
//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()
return decodeResponse(res)
}
// UnmarshalSTKPushCallback decodes the provided value to STKPushCallback.
func UnmarshalSTKPushCallback(r io.Reader) (*STKPushCallback, error) {
var callback STKPushCallback
if err := json.NewDecoder(r).Decode(&callback); err != nil {
return nil, fmt.Errorf("mpesa: decode: %v", err)
}
return &callback, nil
}
func (m *Mpesa) generateSecurityCredentials(initiatorPwd string) (string, error) {
certPath := "certs/sandbox.cer"
if m.Environment().IsProduction() {
certPath = "certs/production.cer"
}
publicKey, err := certFS.ReadFile(certPath)
if err != nil {
return "", fmt.Errorf("mpesa: read cert: %v", err)
}
block, _ := pem.Decode(publicKey)
var cert *x509.Certificate
cert, err = x509.ParseCertificate(block.Bytes)
if err != nil {
return "", fmt.Errorf("mpesa:parse cert: %v", err)
}
rsaPublicKey := cert.PublicKey.(*rsa.PublicKey)
reader := rand.Reader
signature, err := rsa.EncryptPKCS1v15(reader, rsaPublicKey, []byte(initiatorPwd))
if err != nil {
return "", fmt.Errorf("mpesa: encrypt password: %v", err)
}
return base64.StdEncoding.EncodeToString(signature), nil
}
// B2C transacts between an M-Pesa short code to a phone number registered on M-Pesa
func (m *Mpesa) B2C(ctx context.Context, initiatorPwd string, req B2CRequest) (*Response, error) {
if initiatorPwd == "" {
return nil, ErrInvalidInitiatorPassword
}
securityCredential, err := m.generateSecurityCredentials(initiatorPwd)
if err != nil {
return nil, err
}
req.SecurityCredential = securityCredential
res, err := m.makeHttpRequestWithToken(ctx, http.MethodPost, m.endpointB2C(), req)
if err != nil {
return nil, err
}
//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()
return decodeResponse(res)
}
// UnmarshalCallback decodes the provided value to Callback
func UnmarshalCallback(r io.Reader) (*Callback, error) {
var callback Callback
if err := json.NewDecoder(r).Decode(&callback); err != nil {
return nil, fmt.Errorf("mpesa: decode: %v", err)
}
return &callback, nil
}
// STKQuery checks the status of an STKPush payment.
func (m *Mpesa) STKQuery(ctx context.Context, passkey string, req STKQueryRequest) (*Response, error) {
if passkey == "" {
return nil, ErrInvalidPasskey
}
req.Timestamp, req.Password = generateTimestampAndPassword(req.BusinessShortCode, passkey)
res, err := m.makeHttpRequestWithToken(ctx, http.MethodPost, m.endpointSTKQuery(), req)
if err != nil {
return nil, err
}
//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()
return decodeResponse(res)
}
// RegisterC2BURL API works hand in hand with Customer to Business (C2B) APIs and allows receiving payment notifications to your paybill.
// This API enables you to register the callback URLs via which you shall receive notifications for payments to your pay bill/till number.
// There are two URLs required for Register URL API: Validation URL and Confirmation URL.
// Validation URL: This is the URL that is only used when a Merchant (Partner) requires to validate the details of the payment before accepting.
// For example, a bank would want to verify if an account number exists in their platform before accepting a payment from the customer.
// Confirmation URL: This is the URL that receives payment notification once payment has been completed successfully on M-PESA.
func (m *Mpesa) RegisterC2BURL(ctx context.Context, req RegisterC2BURLRequest) (*Response, error) {
switch req.ResponseType {
case ResponseTypeComplete, ResponseTypeCanceled:
response, err := m.makeHttpRequestWithToken(ctx, http.MethodPost, m.endpointC2BRegister(), req)
if err != nil {
return nil, err
}
defer func(body io.ReadCloser) {
_ = body.Close()
}(response.Body)
return decodeResponse(response)
default:
return nil, fmt.Errorf("mpesa: the provided ResponseType [%s] is not valid", req.ResponseType)
}
}
// DynamicQR API is used to generate a Dynamic QR which enables Safaricom M-PESA customers who have My Safaricom App or
// M-PESA app, to scan a QR (Quick Response) code, to capture till number and amount then authorize to pay for goods and
// services at select LIPA NA M-PESA (LNM) merchant outlets. If the decodeImage parameter is set to true, the QR code
// will be decoded and a base url is set on the ImagePath field
func (m *Mpesa) DynamicQR(
ctx context.Context, req DynamicQRRequest, transactionType DynamicQRTransactionType, decodeImage bool,
) (*DynamicQRResponse, error) {
req.TransactionType = transactionType
res, err := m.makeHttpRequestWithToken(ctx, http.MethodPost, m.endpointDynamicQR(), req)
if err != nil {
return nil, err
}
//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()
var resp *DynamicQRResponse
if err = json.NewDecoder(res.Body).Decode(&resp); err != nil {
return nil, fmt.Errorf("mpesa: decode response: %v", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"mpesa: request %v failed with code %v: %v", resp.RequestID, resp.ErrorCode, resp.ErrorMessage,
)
}
if !decodeImage {
return resp, nil
}
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(resp.QRCode))
image, err := png.Decode(reader)
if err != nil {
return nil, fmt.Errorf("mpesa: decode png: %v", err)
}
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("mpesa: wd: %v", err)
}
imagesDir := filepath.Join(wd, "storage", "images")
if _, err := os.Stat(imagesDir); os.IsNotExist(err) {
if err = os.Mkdir(imagesDir, os.ModePerm); err != nil {
return nil, fmt.Errorf("mpesa: create images dir: %v", err)
}
}
amountStr := strconv.Itoa(int(req.Amount))
filename := req.MerchantName + "_" + amountStr + "_" + req.CreditPartyIdentifier + ".png"
filename = imagesDir + "/" + strings.ReplaceAll(filename, " ", "_")
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return nil, fmt.Errorf("mpesa: open png: %v", err)
}
if err = png.Encode(f, image); err != nil {
return nil, fmt.Errorf("mpesa: encode png: %v", err)
}
resp.ImagePath = filename
return resp, nil
}
// GetTransactionStatus checks the status of a transaction
func (m *Mpesa) GetTransactionStatus(
ctx context.Context, initiatorPwd string, req TransactionStatusRequest,
) (*Response, error) {
if initiatorPwd == "" {
return nil, ErrInvalidInitiatorPassword
}
if err := validateURL(req.QueueTimeOutURL); err != nil {
return nil, err
}
if err := validateURL(req.ResultURL); err != nil {
return nil, err
}
securityCredential, err := m.generateSecurityCredentials(initiatorPwd)
if err != nil {
return nil, err
}
req.SecurityCredential = securityCredential
req.CommandID = TransactionStatusQueryCommandID
req.IdentifierType = ShortcodeIdentifierType
res, err := m.makeHttpRequestWithToken(ctx, http.MethodPost, m.endpointTransactionStatus(), req)
if err != nil {
return nil, err
}
//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()
return decodeResponse(res)
}
// GetAccountBalance fetches the account balance of a short code. This can be used for both B2C, buy goods and pay bill
// accounts.
func (m *Mpesa) GetAccountBalance(
ctx context.Context, initiatorPwd string, req AccountBalanceRequest,
) (*Response, error) {
if initiatorPwd == "" {
return nil, ErrInvalidInitiatorPassword
}
if err := validateURL(req.QueueTimeOutURL); err != nil {
return nil, err
}
if err := validateURL(req.ResultURL); err != nil {
return nil, err
}
securityCredential, err := m.generateSecurityCredentials(initiatorPwd)
if err != nil {
return nil, err
}
req.SecurityCredential = securityCredential
req.CommandID = AccountBalanceCommandID
req.IdentifierType = ShortcodeIdentifierType
res, err := m.makeHttpRequestWithToken(ctx, http.MethodPost, m.endpointAccountBalance(), req)
if err != nil {
return nil, err
}
//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()
return decodeResponse(res)
}
// BusinessPayBill API enables you to pay bills directly from your business account to a pay bill number, or a paybill
// store. You can use this API to pay on behalf of a consumer/requester.
//
// The transaction moves money from your MMF/Working account to the recipient’s utility account.
func (m *Mpesa) BusinessPayBill(ctx context.Context, initiatorPwd string, req BusinessPayBillRequest) (*Response, error) {
if initiatorPwd == "" {
return nil, ErrInvalidInitiatorPassword
}
if err := validateURL(req.QueueTimeOutURL); err != nil {
return nil, err
}
if err := validateURL(req.ResultURL); err != nil {
return nil, err
}
securityCredential, err := m.generateSecurityCredentials(initiatorPwd)
if err != nil {
return nil, err
}
req.SecurityCredential = securityCredential
req.CommandID = BusinessPayBillCommandID
req.RecieverIdentifierType = ShortcodeIdentifierType
req.SenderIdentifierType = ShortcodeIdentifierType
res, err := m.makeHttpRequestWithToken(ctx, http.MethodPost, m.endpointBusinessPayBill(), req)
if err != nil {
return nil, err
}
//goland:noinspection GoUnhandledErrorResult
defer res.Body.Close()
return decodeResponse(res)
}
func decodeResponse(res *http.Response) (*Response, error) {
var resp Response
if err := json.NewDecoder(res.Body).Decode(&resp); err != nil {
return nil, fmt.Errorf("mpesa: decode response: %v", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"mpesa: request %v failed with code %v: %v", resp.RequestID, resp.ErrorCode, resp.ErrorMessage,
)
}
return &resp, nil
}