Skip to content

Commit

Permalink
Refactors and updates documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
regisbamba committed Apr 14, 2019
1 parent d995432 commit c065a7d
Show file tree
Hide file tree
Showing 18 changed files with 728 additions and 418 deletions.
275 changes: 160 additions & 115 deletions README.md

Large diffs are not rendered by default.

31 changes: 17 additions & 14 deletions src/main/java/ci/bamba/regis/Collections.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,23 @@
import ci.bamba.regis.exceptions.RequestException;
import ci.bamba.regis.models.AccountBalance;
import ci.bamba.regis.models.AccountStatus;
import ci.bamba.regis.models.CollectionsRequestToPayBodyRequest;
import ci.bamba.regis.models.CollectionsRequestToPay;
import ci.bamba.regis.models.CollectionsRequestToPayBodyRequest;
import ci.bamba.regis.models.Token;
import io.reactivex.Observable;

public class Collections extends Product {

Collections(String baseUrl, Environment environment, String subscriptionKey) {
super(baseUrl, environment, subscriptionKey);
}

/**
* @param apiUser an Api user
* @param apiKey an Api key
* @return a token observable that you can use to authenticate your requests.
*/
public Observable<Token> createToken(String apiUser, String apiKey) {
return super.createToken("collection", apiUser, apiKey);
Collections(String baseUrl, Environment environment, String subscriptionKey, String apiUser, String apiKey) {
super(baseUrl, environment, subscriptionKey, apiUser, apiKey);
}

public Observable<Token> createToken() {
return super.createToken("collection");
}

public Observable<Token> createToken(String providerCallbackHost) {
return super.createToken(providerCallbackHost, "collection");
public Observable<String> requestToPay(float amount, String currency, String externalId, String payerPartyId, String payerMessage, String payeeNote) {
return createToken().flatMap(token -> requestToPay(token.getAccessToken(), amount, currency, externalId, payerPartyId, payerMessage, payeeNote));
}

public Observable<String> requestToPay(String token, float amount, String currency, String externalId, String payerPartyId, String payerMessage, String payeeNote) {
Expand All @@ -49,6 +40,10 @@ public Observable<String> requestToPay(String token, float amount, String curren
});
}

public Observable<CollectionsRequestToPay> getRequestToPay(String referenceId) {
return createToken().flatMap(token -> getRequestToPay(token.getAccessToken(), referenceId));
}

public Observable<CollectionsRequestToPay> getRequestToPay(String token, String referenceId) {
String authorization = String.format("Bearer %s", token);
return RestClient
Expand All @@ -63,6 +58,10 @@ public Observable<CollectionsRequestToPay> getRequestToPay(String token, String
});
}

public Observable<AccountBalance> getAccountBalance() {
return createToken().flatMap(token -> getAccountBalance(token.getAccessToken()));
}

public Observable<AccountBalance> getAccountBalance(String token) {
String authorization = String.format("Bearer %s", token);
return RestClient.getService(getBaseUrl())
Expand All @@ -76,6 +75,10 @@ public Observable<AccountBalance> getAccountBalance(String token) {
});
}

public Observable<AccountStatus> getAccountStatus(String msisdn) {
return createToken().flatMap(token -> getAccountStatus(token.getAccessToken(), msisdn));
}

public Observable<AccountStatus> getAccountStatus(String token, String msisdn) {
String authorization = String.format("Bearer %s", token);
return RestClient.getService(getBaseUrl())
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/ci/bamba/regis/MoMo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public MoMo(Environment environment) {
this.environment = environment;
}

public Collections subscribeToCollections(String subscriptionKey) {
return new Collections(getBaseUrl(), getEnvironment(), subscriptionKey);
public Provisioning createProvisioning(String subscriptionKey) {
return new Provisioning(subscriptionKey, getBaseUrl());
}

public Collections createCollections(String subscriptionKey, String apiUser, String apiKey) {
return new Collections(getBaseUrl(), getEnvironment(), subscriptionKey, apiUser, apiKey);
}

public Environment getEnvironment() {
Expand All @@ -23,4 +27,5 @@ private String getBaseUrl() {
return this.environment == Environment.SANDBOX ? BASE_URL_SANDBOX : BASE_URL_PRODUCTION;
}


}
82 changes: 13 additions & 69 deletions src/main/java/ci/bamba/regis/Product.java
Original file line number Diff line number Diff line change
@@ -1,112 +1,56 @@
package ci.bamba.regis;

import java.util.Base64;
import java.util.HashMap;
import java.util.UUID;

import ci.bamba.regis.exceptions.RequestException;
import ci.bamba.regis.models.ApiCredentials;
import ci.bamba.regis.models.ApiUser;
import ci.bamba.regis.models.Token;
import io.reactivex.Observable;

public class Product {

private ApiCredentials apiCredentials;
private String baseUrl;
private String subscriptionKey;
private Environment environment;

Product(String baseUrl, Environment environment, String subscriptionKey) {

Product(String baseUrl, Environment environment, String subscriptionKey, String apiUser, String apiKey) {
this.baseUrl = baseUrl;
this.environment = environment;
this.subscriptionKey = subscriptionKey;
this.apiCredentials = new ApiCredentials(apiUser, apiKey);
}

public ApiCredentials getApiCredentials() {
return apiCredentials;
}

public String getSubscriptionKey() {
return subscriptionKey;
}

public Environment getEnvironment() {
return environment;
}

public String getBaseUrl() {
return this.baseUrl;
}

public Observable<String> createApiUser() {
return createApiUser("");
}

public Observable<String> createApiUser(String providerCallbackHost) {
String referenceId = UUID.randomUUID().toString();
HashMap<String, String> body = new HashMap<>();
body.put("providerCallbackHost", providerCallbackHost);
return RestClient
.getService(getBaseUrl())
.createApiUser(referenceId, getSubscriptionKey(), body)
.map(response -> {
if (response.code() == 201) {
return referenceId;
} else {
throw new RequestException(response.code(), response.message());
}
});
}

public Observable<ApiUser> getApiUser(String referenceId) {
return RestClient.getService(getBaseUrl())
.getApiUser(getSubscriptionKey(), referenceId)
.map(response -> {
if (response.code() == 200) {
return response.body();
} else {
throw new RequestException(response.code(), response.message());
}
});
}

public Observable<ApiCredentials> createApiKey(String referenceId) {
return RestClient
.getService(getBaseUrl())
.createApiKey(getSubscriptionKey(), referenceId)
.map(response -> {
if (response.code() == 201) {
if (response.body() != null) {
return new ApiCredentials(referenceId, response.body().getApiKey());
} else {
throw new RequestException(response.code(), response.message());
}
} else {
throw new RequestException(response.code(), response.message());
}
});
}

Observable<Token> createToken(String type) {
return createApiUser("")
.flatMap(this::createApiKey)
.flatMap(apiCredentials -> createToken(type, apiCredentials.getApiUser(), apiCredentials.getApiKey()));
}

Observable<Token> createToken(String providerCallbackHost, String type) {
return createApiUser(providerCallbackHost)
.flatMap(this::createApiKey)
.flatMap(apiCredentials -> createToken(type, apiCredentials.getApiUser(), apiCredentials.getApiKey()));
return createToken(type, this.getApiCredentials().getUser(), this.getApiCredentials().getKey());
}

Observable<Token> createToken(String type, String apiUser, String apiKey) {
private Observable<Token> createToken(String type, String apiUser, String apiKey) {
byte[] encodedBytes = Base64.getEncoder().encode((apiUser + ":" + apiKey).getBytes());
String authorization = "Basic " + new String(encodedBytes);
return RestClient
.getService(getBaseUrl())
.createToken(authorization, getSubscriptionKey(), type)
.map(response -> {
if (response.code() == 200) {
Token token = response.body();
if (token != null) {
token.setApiKey(apiKey);
token.setApiUser(apiUser);
}
return token;
return response.body();
} else {
throw new RequestException(response.code(), response.message());
}
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/ci/bamba/regis/Provisioning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ci.bamba.regis;

import java.util.HashMap;
import java.util.UUID;

import ci.bamba.regis.exceptions.RequestException;
import ci.bamba.regis.models.ApiCredentials;
import ci.bamba.regis.models.ApiUser;
import io.reactivex.Observable;

public class Provisioning {

private String subscriptionKey;
private String baseUrl;
private Environment environment;

Provisioning(String subscriptionKey, String baseUrl) {
this.subscriptionKey = subscriptionKey;
this.baseUrl = baseUrl;
this.environment = Environment.SANDBOX;
}

public Environment getEnvironment() {
return environment;
}

public String getSubscriptionKey() {
return subscriptionKey;
}

public String getBaseUrl() {
return baseUrl;
}

public Observable<String> createApiUser() {
return createApiUser("string");
}

public Observable<String> createApiUser(String providerCallbackHost) {
String referenceId = UUID.randomUUID().toString();
HashMap<String, String> body = new HashMap<>();
body.put("providerCallbackHost", providerCallbackHost);
return RestClient
.getService(getBaseUrl())
.createApiUser(getSubscriptionKey(), referenceId, body)
.map(response -> {
if (response.code() == 201) {
return referenceId;
} else {
throw new RequestException(response.code(), response.message());
}
});
}

public Observable<ApiUser> getApiUser(String referenceId) {
return RestClient.getService(getBaseUrl())
.getApiUser(getSubscriptionKey(), referenceId)
.map(response -> {
if (response.code() == 200) {
return response.body();
} else {
throw new RequestException(response.code(), response.message());
}
});
}

public Observable<ApiCredentials> createApiKey(String referenceId) {
return RestClient
.getService(getBaseUrl())
.createApiKey(getSubscriptionKey(), referenceId)
.map(response -> {
if (response.code() == 201) {
if (response.body() != null) {
return new ApiCredentials(referenceId, response.body().getApiKey());
} else {
throw new RequestException(response.code(), response.message());
}
} else {
throw new RequestException(response.code(), response.message());
}
});
}

}
4 changes: 2 additions & 2 deletions src/main/java/ci/bamba/regis/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public interface Service {

@POST("v1_0/apiuser")
Observable<Response<Void>> createApiUser(
@Header("X-Reference-Id") String xReferenceId,
@Header("Ocp-Apim-Subscription-Key") String subscriptionKey,
@Header("X-Reference-Id") String xReferenceId,
@Body HashMap<String, String> body
);

Expand Down Expand Up @@ -116,7 +116,7 @@ Observable<Response<AccountBalance>> collectionsGetAccountBalance(
@Header("X-Target-Environment") String targetEnvironment
);

@GET("/v1_0/accountholder/{accountHolderIdType}/{accountHolderId}/active ")
@GET("collection/v1_0/accountholder/{accountHolderIdType}/{accountHolderId}/active ")
Observable<Response<AccountStatus>> collectionsGetAccountStatus(
@Header("Authorization") String authorization,
@Header("Ocp-Apim-Subscription-Key") String subscriptionKey,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ci/bamba/regis/models/AccountStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class AccountStatus {

private boolean result;

public boolean isResult() {
public boolean getResult() {
return result;
}

Expand Down
29 changes: 20 additions & 9 deletions src/main/java/ci/bamba/regis/models/ApiCredentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

public class ApiCredentials {

private String apiUser;
private String apiKey;
private String user;
private String key;

public ApiCredentials(String apiUser, String apiKey) {
this.apiUser = apiUser;
this.apiKey = apiKey;
public ApiCredentials(){

}

public ApiCredentials(String user, String key) {
this.user = user;
this.key = key;
}

public String getApiUser() {
return apiUser;
public String getUser() {
return user;
}
public String getApiKey() {
return apiKey;
public String getKey() {
return key;
}

public void setUser(String user) {
this.user = user;
}

public void setKey(String key) {
this.key = key;
}
}
3 changes: 3 additions & 0 deletions src/main/java/ci/bamba/regis/models/ApiKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ public class ApiKey {

private String apiKey;

public ApiKey() {
}

public ApiKey(String apiKey) {
this.apiKey = apiKey;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/ci/bamba/regis/models/ApiUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ public class ApiUser {
private String providerCallbackHost;
private String targetEnvironment;

public ApiUser(){

}

public ApiUser(String providerCallbackHost, String targetEnvironment) {
this.providerCallbackHost = providerCallbackHost;
this.targetEnvironment = targetEnvironment;
Expand Down
Loading

0 comments on commit c065a7d

Please sign in to comment.