Skip to content

Commit e649265

Browse files
Regis Bamba [ MTN Cote d'Ivoire ]Regis Bamba [ MTN Cote d'Ivoire ]
authored andcommitted
Refactors and updates documentation
1 parent c97dc03 commit e649265

16 files changed

Lines changed: 413 additions & 459 deletions

README.md

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# MoMo API Java Client
2+
23
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/regisbamba/momoapi-java/blob/master/License.md) [![Release](https://jitpack.io/v/regisbamba/momoapi-java.svg)](https://jitpack.io/#regisbamba/momoapi-java) [![Build Status](https://travis-ci.org/regisbamba/momoapi-java.svg?branch=master)](https://travis-ci.org/regisbamba/momoapi-java) [![codecov](https://codecov.io/gh/regisbamba/momoapi-java/branch/master/graph/badge.svg)](https://codecov.io/gh/regisbamba/momoapi-java)
34

45
This library helps you consume the new MTN Mobile Money API ([MoMo API](https://momodeveloper.mtn.com)).
56

67
Features :
8+
79
- Support for Collections, Disbursements, Remittances
810
- Support for Sandbox User provisioning
911
- Automatically generate tokens for your requests, eg: collections.getAccountBalance() // no need to get token
1012

1113
## 1. Installation
1214

1315
To get started, you first have to add the JitPack repository to your root build.gradle file at the end of repositories section.
16+
1417
```
1518
allprojects {
1619
repositories {
@@ -21,6 +24,7 @@ allprojects {
2124
```
2225

2326
Then, add the dependency to your dependencies section
27+
2428
```
2529
dependencies {
2630
implementation 'com.github.regisbamba:momoapi-java:{latest-version}'
@@ -30,15 +34,18 @@ dependencies {
3034
For other build systems, please see instructions on [Jitpack's website](https://jitpack.io/#regisbamba/momoapi-java).
3135

3236
## 2. Prerequesite
37+
3338
Before everything else, make sure you open an account on the ([MoMo API](https://momodeveloper.mtn.com)) portal.
3439
You will need to subscribe to products on the portal before you can use them through the API and this client.
3540

3641
## 3. Consuming resources from the API
42+
3743
This library uses Reactive Programming via [RxJava](https://github.com/ReactiveX/RxJava). All API resources are provided via **Observable** streams.
3844

3945
When you make a request, you can get the results by subscribing to the Observable and check whether the request was successful or not.
4046

4147
Eg: Getting the balance for your account.
48+
4249
```java
4350
collections.getAccountBalance().subscribe(
4451
new Consumer<AccountBalance>() {
@@ -49,7 +56,9 @@ collections.getAccountBalance().subscribe(
4956
}
5057
);
5158
```
52-
You can also consume error events in case of the API request failed.
59+
60+
You can also consume error events in case the API request failed.
61+
5362
```java
5463
collections.getAccountBalance().subscribe(
5564
new Consumer<AccountBalance>() {
@@ -70,6 +79,7 @@ collections.getAccountBalance().subscribe(
7079
```
7180

7281
If you use Java 8, you can use lambda functions for more clarity.
82+
7383
```java
7484
collections.getAccountBalance().subscribe(accountBalance -> { // This function executes in case of success.
7585
System.out.println(accountBalance.getAvailableBalance()); // 900
@@ -92,15 +102,19 @@ collections.getAccountBalance().subscribe(
92102
## 4. Reference
93103

94104
### 4.1 Create a MoMo client
105+
95106
Create a new MoMo client by specifying the environment (either SANDBOX OR PRODUCTION).
107+
96108
```java
97109
MoMo momo = new MoMo(Environment.SANDBOX);
98110
```
99111

100112
### 4.2 Authenticate your requests
113+
101114
According to [documentation](https://momodeveloper.mtn.com/api-documentation/api-description/), the credentials to be used are :
102-
- Subscription Key
103-
- API User and API Key to generate a Bearer Token for Oauth 2.0
115+
116+
- Subscription Key
117+
- API User and API Key to generate a Bearer Token for Oauth 2.0
104118

105119
The Subscription Key is available when you subscribe to a product via the portal.
106120

@@ -111,63 +125,72 @@ The API User and API Key are used to grant access to the wallet system in a spec
111125

112126
In simple terms, if you are in production you should copy and paste API User and API Key from the portal and store them as variables in your code.
113127

114-
If you are in sandbox, use the Provisioning class to generate API User and API Key as explained below.
128+
If you are in sandbox, use the SandboxProvisioning class to generate API User and API Key as explained below.
115129

116130
First get a provisioning instance :
131+
117132
```java
118-
Provisioning provisioning = momo.createProvisioning(subscriptionKey);
119-
```
133+
SandboxProvisioning sandboxProvisioning = momo.createSandboxProvisioning(subscriptionKey);
134+
```
120135

121136
#### 4.2.1 Create an API User (Sandbox Only)
137+
122138
Create an API User and get the referenceId back.
123139

124140
Documentation: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/post-v1_0-apiuser
125141

126142
```java
127-
provisioning.createApiUser().subscribe(referenceId -> {
143+
sandboxProvisioning.createApiUser().subscribe(referenceId -> {
128144
System.out.println(referenceId); // db0fc432-c940-4116-bbd1-887ab663e2a3
129145
});
130146
```
131147

132148
You can also specify a providerCallbackHost parameter.
149+
133150
```java
134-
provisioning.createApiUser("www.myapp.com").subscribe(referenceId -> {
151+
sandboxProvisioning.createApiUser("www.myapp.com").subscribe(referenceId -> {
135152
System.out.println(referenceId); // 0812e642-5692-463b-8dce-370af19802c8
136153
});
137154
```
138155

139156
#### 4.2.2 Create an API Key (Sandbox Only)
157+
140158
Create an API Key using the referenceId from API User.
141159

142160
Documentation: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/post-v1_0-apiuser-apikey?
143161

144162
```java
145-
provisioning.createApiKey(referenceId).subscribe(apiCredentials -> {
163+
sandboxProvisioning.createApiKey(referenceId).subscribe(apiCredentials -> {
146164
System.out.print(apiCredentials.getUser()); // 822b8ea9-cc34-47b8-adcc-23a9a468b0df
147165
System.out.print(apiCredentials.getKey()); // 06796ba6ab714c4990b068dcfac66d88
148166
});
149167
```
150168

151-
152169
#### 4.2.3 Get an API User (Sandbox Only)
170+
153171
Get an API User record.
154172

155173
Documentation: https://momodeveloper.mtn.com/docs/services/sandbox-provisioning-api/operations/get-v1_0-apiuser?
174+
156175
```java
157-
provisioning.getApiUser(referenceId).subscribe(apiUser -> {
176+
sandboxProvisioning.getApiUser(referenceId).subscribe(apiUser -> {
158177
System.out.println(apiUser.getProviderCallbackHost()); // www.myapp.com
159178
System.out.println(apiUser.getTargetEnvironment()); // sandbox
160179
});
161180
```
162181

163182
### 4.3 Get a product instance
183+
164184
To make a request for a particular product, you need to create an instance of that product.
185+
165186
```java
166187
Collections collections = momo.createCollections(subscriptionKey, apiUser, apiKey);
167-
```
188+
```
189+
168190
You can also do so for Disbursements and Remittances.
169191

170192
### 4.4 Collections
193+
171194
The Collections product enable remote collection of bills, fees or taxes.
172195

173196
#### 4.4.1 Create a Token
@@ -178,7 +201,7 @@ Documentation: https://momodeveloper.mtn.com/docs/services/collection/operations
178201

179202
```java
180203
collections.createToken().subscribe(
181-
token -> {
204+
token -> {
182205
System.out.println(token.getAccessToken()); // eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0....
183206
System.out.println(token.getExpiresIn()); // 3600
184207
System.out.println(token.getTokenType()); // acess_token
@@ -190,12 +213,13 @@ collections.createToken().subscribe(
190213

191214
**Please note**: For all requests, you can either create your own token as explained above and pass it to your requests or you can let the client automatically generate it for you.
192215

193-
194216
#### 4.4.2 Request to pay
217+
195218
Request a payment from a consumer (Payer).
196219

197220
Documentation:
198221
https://momodeveloper.mtn.com/docs/services/collection/operations/requesttopay-POST?
222+
199223
```java
200224
float amount = 900;
201225
String currency = "EUR"; // In Sandbox, this should be EUR.
@@ -212,20 +236,22 @@ collections.requestToPay(amount, currency, externalId, payerPartyId, payerMessag
212236
```
213237

214238
#### 4.4.3 Get a status of a request to pay
239+
215240
Get the status of a request to pay.
216241

217242
Documentation: https://momodeveloper.mtn.com/docs/services/collection/operations/requesttopay-referenceId-GET?
218243

219244
```java
220245
collections.getRequestToPay(referenceId).subscribe(
221246
requestToPay -> {
222-
System.out.println(requestToPay.getFinancialTransactionId()); // 521734614
223-
System.out.println(requestToPay.getStatus()); // SUCCESSFUL
247+
System.out.println(requestToPay.getFinancialTransactionId()); // 521734614
248+
System.out.println(requestToPay.getStatus()); // SUCCESSFUL
224249
}
225250
);
226251
```
227252

228253
#### 4.4.4 Get the balance of the account.
254+
229255
Get the balance of the account.
230256

231257
Documentation: https://momodeveloper.mtn.com/docs/services/collection/operations/get-v1_0-account-balance?
@@ -238,6 +264,7 @@ collections.getAccountBalance().subscribe(accountBalance -> {
238264
```
239265

240266
#### 4.4.5 Get the status of an account holder
267+
241268
Check if an account holder is registered and active in the system.
242269

243270
Documentation: https://momodeveloper.mtn.com/docs/services/collection/operations/get-v1_0-accountholder-accountholderidtype-accountholderid-active?
@@ -247,8 +274,9 @@ collections.getAccountStatus("46733123453").subscribe(accountStatus -> {
247274
System.out.println(accountStatus.getResult()); // true
248275
});
249276
```
250-
277+
251278
### 4.5 Disbursements
279+
252280
The Disbursement product lets you automatically deposit funds to multiple users in one transaction.
253281

254282
#### 4.5.1 Create a Token
@@ -259,7 +287,7 @@ Documentation: https://momodeveloper.mtn.com/docs/services/disbursement/operatio
259287

260288
```java
261289
disbursements.createToken().subscribe(
262-
token -> {
290+
token -> {
263291
System.out.println(token.getAccessToken()); // eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0....
264292
System.out.println(token.getExpiresIn()); // 3600
265293
System.out.println(token.getTokenType()); // acess_token
@@ -271,12 +299,13 @@ disbursements.createToken().subscribe(
271299

272300
**Please note**: For all requests, you can either create your own token as explained above and pass it to your requests or you can let the client automatically generate it for you.
273301

274-
275302
#### 4.5.2 Transfer
303+
276304
Transfer an amount to a payee account.
277305

278306
Documentation:
279307
https://momodeveloper.mtn.com/docs/services/disbursement/operations/transfer-POST?
308+
280309
```java
281310
float amount = 900;
282311
String currency = "EUR"; // In Sandbox, this should be EUR.
@@ -293,20 +322,22 @@ disbursements.transfer(amount, currency, externalId, payeePartyId, payerMessage,
293322
```
294323

295324
#### 4.5.3 Get a status of a transfer
325+
296326
Get the status of a transfer.
297327

298328
Documentation: https://momodeveloper.mtn.com/docs/services/disbursement/operations/transfer-referenceId-GET?
299329

300330
```java
301331
disbursements.getTransfer(referenceId).subscribe(
302332
transfer -> {
303-
System.out.println(transfer.getFinancialTransactionId()); // 521734614
304-
System.out.println(transfer.getStatus()); // SUCCESSFUL
333+
System.out.println(transfer.getFinancialTransactionId()); // 521734614
334+
System.out.println(transfer.getStatus()); // SUCCESSFUL
305335
}
306336
);
307337
```
308338

309339
#### 4.5.4 Get the balance of the account.
340+
310341
Get the balance of the account.
311342

312343
Documentation: https://momodeveloper.mtn.com/docs/services/disbursement/operations/get-v1_0-account-balance?
@@ -319,6 +350,7 @@ disbursements.getAccountBalance().subscribe(accountBalance -> {
319350
```
320351

321352
#### 4.5.5 Get the status of an account holder
353+
322354
Check if an account holder is registered and active in the system.
323355

324356
Documentation: https://momodeveloper.mtn.com/docs/services/disbursement/operations/get-v1_0-accountholder-accountholderidtype-accountholderid-active?
@@ -330,6 +362,7 @@ disbursements.getAccountStatus("46733123453").subscribe(accountStatus -> {
330362
```
331363

332364
### 4.6 Remittances
365+
333366
The Remittance product lets you automatically deposit funds to multiple users in one transaction.
334367

335368
#### 4.6.1 Create a Token
@@ -340,7 +373,7 @@ Documentation: https://momodeveloper.mtn.com/docs/services/remittance/operations
340373

341374
```java
342375
remittances.createToken().subscribe(
343-
token -> {
376+
token -> {
344377
System.out.println(token.getAccessToken()); // eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0....
345378
System.out.println(token.getExpiresIn()); // 3600
346379
System.out.println(token.getTokenType()); // acess_token
@@ -352,12 +385,13 @@ remittances.createToken().subscribe(
352385

353386
**Please note**: For all requests, you can either create your own token as explained above and pass it to your requests or you can let the client automatically generate it for you.
354387

355-
356388
#### 4.6.2 Transfer
389+
357390
Transfer an amount to a payee account.
358391

359392
Documentation:
360393
https://momodeveloper.mtn.com/docs/services/remittance/operations/transfer-POST?
394+
361395
```java
362396
float amount = 900;
363397
String currency = "EUR"; // In Sandbox, this should be EUR.
@@ -374,20 +408,22 @@ remittances.transfer(amount, currency, externalId, payeePartyId, payerMessage, p
374408
```
375409

376410
#### 4.6.3 Get a status of a transfer
411+
377412
Get the status of a transfer.
378413

379414
Documentation: https://momodeveloper.mtn.com/docs/services/remittance/operations/transfer-referenceId-GET?
380415

381416
```java
382417
remittances.getTransfer(referenceId).subscribe(
383418
transfer -> {
384-
System.out.println(transfer.getFinancialTransactionId()); // 521734614
385-
System.out.println(transfer.getStatus()); // SUCCESSFUL
419+
System.out.println(transfer.getFinancialTransactionId()); // 521734614
420+
System.out.println(transfer.getStatus()); // SUCCESSFUL
386421
}
387422
);
388423
```
389424

390425
#### 4.6.4 Get the balance of the account.
426+
391427
Get the balance of the account.
392428

393429
Documentation: https://momodeveloper.mtn.com/docs/services/remittance/operations/get-v1_0-account-balance?
@@ -400,6 +436,7 @@ remittances.getAccountBalance().subscribe(accountBalance -> {
400436
```
401437

402438
#### 4.6.5 Get the status of an account holder
439+
403440
Check if an account holder is registered and active in the system.
404441

405442
Documentation: https://momodeveloper.mtn.com/docs/services/remittance/operations/get-v1_0-accountholder-accountholderidtype-accountholderid-active?
@@ -409,6 +446,3 @@ remittances.getAccountStatus("46733123453").subscribe(accountStatus -> {
409446
System.out.println(accountStatus.getResult()); // true
410447
});
411448
```
412-
413-
414-

0 commit comments

Comments
 (0)