-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
119 lines (111 loc) · 3.91 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require('dotenv').config();
const axios = require('axios').default;
const crypto = require('crypto');
const constants = require('constants');
let mpesaConfig;
function _getBearerToken(mpesa_public_key, mpesa_api_key) {
const publicKey = "-----BEGIN PUBLIC KEY-----\n"+mpesa_public_key+"\n"+"-----END PUBLIC KEY-----";
const buffer = Buffer.from(mpesa_api_key);
const encrypted = crypto.publicEncrypt({
'key': publicKey,
'padding': constants.RSA_PKCS1_PADDING,
}, buffer);
return encrypted.toString("base64");
}
function initialize_api_from_dotenv() {
if (!mpesaConfig) {
mpesaConfig = {
baseUrl: process.env.MPESA_API_HOST,
apiKey: process.env.MPESA_API_KEY,
publicKey: process.env.MPESA_PUBLIC_KEY,
origin: process.env.MPESA_ORIGIN,
serviceProviderCode: process.env.MPESA_SERVICE_PROVIDER_CODE
};
validateConfig(mpesaConfig);
console.log("Using M-Pesa environment configuration");
} else {
console.log("Using custom M-Pesa configuration");
}
}
function required_config_arg(argName) {
return "Please provide a valid " + argName + " in the configuration when calling initializeApi()";
}
function validateConfig(configParams) {
if (!configParams.baseUrl) {
throw required_config_arg("baseUrl")
}
if (!configParams.apiKey) {
throw required_config_arg("apiKey")
}
if (!configParams.publicKey) {
throw required_config_arg("publicKey")
}
if (!configParams.origin) {
throw required_config_arg("origin")
}
if (!configParams.serviceProviderCode) {
throw required_config_arg("serviceProviderCode")
}
}
module.exports.initializeApi = function (configParams) {
validateConfig(configParams);
mpesaConfig = configParams;
};
module.exports.initiate_c2b = async function (amount, msisdn, transaction_ref, thirdparty_ref) {
initialize_api_from_dotenv();
try {
let response;
response = await axios({
method: 'post',
url: 'https://' + mpesaConfig.baseUrl + ':18352/ipg/v1x/c2bPayment/singleStage/',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + _getBearerToken(mpesaConfig.publicKey, mpesaConfig.apiKey),
'Origin': mpesaConfig.origin
},
data: {
"input_TransactionReference": transaction_ref,
"input_CustomerMSISDN": msisdn + "",
"input_Amount": amount + "",
"input_ThirdPartyReference": thirdparty_ref,
"input_ServiceProviderCode": mpesaConfig.serviceProviderCode + ""
}
});
return response.data;
} catch (e) {
if (e.response.data) {
throw e.response.data;
} else {
throw e;
}
}
};
module.exports.initiate_b2c = async function (amount, msisdn, transaction_ref, thirdparty_ref) {
initialize_api_from_dotenv();
try {
let response;
response = await axios({
method: 'post',
url: 'https://' + mpesaConfig.baseUrl + ':18345/ipg/v1x/b2cPayment/',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + _getBearerToken(mpesaConfig.publicKey, mpesaConfig.apiKey),
'Origin': mpesaConfig.origin
},
data: {
"input_TransactionReference": transaction_ref,
"input_CustomerMSISDN": msisdn + "",
"input_Amount": amount + "",
"input_ThirdPartyReference": thirdparty_ref,
"input_ServiceProviderCode": mpesaConfig.serviceProviderCode + ""
}
});
return response.data;
} catch (e) {
if (e.response.data) {
throw e.response.data;
} else {
throw e;
}
}
};