forked from safaricom/mpesa-node-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm-pesa.js
95 lines (84 loc) · 2.08 KB
/
m-pesa.js
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
const {
accountBalance,
b2b,
b2c,
c2bRegister,
c2bSimulate,
lipaNaMpesaOnline,
lipaNaMpesaQuery,
oAuth,
reversal,
transactionStatus
} = require('./endpoints')
const {
request,
security
} = require('./helpers')
/**
* Class representing the Mpesa instance
*/
class Mpesa {
/**
* Introduce Mpesa Configuration
* @constructor
* @param {Object} [config={}] The Configuration to use for mPesa
*/
constructor (config = {}) {
if (!config.consumerKey) throw new Error('Consumer Key is Missing')
if (!config.consumerSecret) throw new Error('Consumer Secret is Missing')
this.configs = { ...config }
this.enviroment = config.environment === 'production' ? 'production' : 'sandbox'
this.request = request.bind(this)
this.security = () => {
return security(this.configs.certPath, this.configs.securityCredential)
}
this.baseURL = `https://${this.enviroment === 'production' ? 'api' : 'sandbox'}.safaricom.co.ke`
}
/**
* AccountBalance via instance
* @borrows AccountBalance as accountBalanceCall
*/
accountBalance () {
return accountBalance.bind(this)(...arguments)
}
/**
* B2B Request via instance
* @name b2bCall
*/
b2b () {
return b2b.bind(this)(...arguments)
}
/**
* B2C Request
* @borrows B2CRequest as b2c
*/
b2c () {
return b2c.bind(this)(...arguments)
}
c2bRegister () {
return c2bRegister.bind(this)(...arguments)
}
c2bSimulate () {
if(this.enviroment === 'production'){
throw new Error('Cannot call C2B simulate in production.')
}
return c2bSimulate.bind(this)(...arguments)
}
lipaNaMpesaOnline () {
return lipaNaMpesaOnline.bind(this)(...arguments)
}
lipaNaMpesaQuery () {
return lipaNaMpesaQuery.bind(this)(...arguments)
}
oAuth () {
const { consumerKey, consumerSecret } = this.configs
return oAuth.bind(this)(consumerKey, consumerSecret)
}
reversal () {
return reversal.bind(this)(...arguments)
}
transactionStatus () {
return transactionStatus.bind(this)(...arguments)
}
}
module.exports = Mpesa