-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
95 lines (80 loc) · 2.32 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
#!/usr/bin/env node
const { program } = require('commander');
const axios = require('axios');
const { v4: uuidv4 } = require('uuid');
class MomoSandbox {
constructor(callbackHost, primaryKey) {
this.callbackHost = callbackHost;
this.primaryKey = primaryKey;
this.userId = null;
this.userApiKey = null;
}
generateUUID() {
return uuidv4();
}
createApiUser(UUID) {
const options = {
method: 'POST',
url: 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser',
headers: {
'X-Reference-Id': UUID,
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': this.primaryKey,
},
data: {
providerCallbackHost: this.callbackHost,
},
};
return axios(options);
}
confirmApiUser(UUID) {
const options = {
method: 'GET',
url: `https://sandbox.momodeveloper.mtn.com/v1_0/apiuser/${UUID}`,
headers: {
'X-Reference-Id': UUID,
'Ocp-Apim-Subscription-Key': this.primaryKey,
},
};
return axios(options);
}
createUserApiKey(UUID) {
const options = {
method: 'POST',
url: `https://sandbox.momodeveloper.mtn.com/v1_0/apiuser/${UUID}/apikey`,
headers: {
'X-Reference-Id': UUID,
'Ocp-Apim-Subscription-Key': this.primaryKey,
},
};
return axios(options)
.then(response => response.data.apiKey);
}
async generateUserCredentials() {
try {
const UUID = this.generateUUID();
await this.createApiUser(UUID);
await this.confirmApiUser(UUID);
this.userApiKey = await this.createUserApiKey(UUID);
// Display the results
this.userId = UUID;
console.log('userId:', this.userId);
console.log('userApiKey:', this.userApiKey);
console.log('callbackHost:', this.callbackHost);
} catch (error) {
console.error('An error occurred:', error);
}
}
}
program
.option('--callback-host <callbackHost>', 'The callback host')
.option('--primary-key <key>', 'The primary key')
.parse(process.argv);
const { callbackHost, primaryKey } = program.opts();
if (!callbackHost || !primaryKey) {
console.error('Please provide the callback host and primary key.');
program.help();
process.exit(1);
}
const momoSandbox = new MomoSandbox(callbackHost, primaryKey);
momoSandbox.generateUserCredentials();