1
1
package ug .sparkpl .momoapi .network .collections ;
2
2
3
+ import java .io .IOException ;
4
+ import java .util .HashMap ;
5
+ import java .util .UUID ;
6
+ import java .util .concurrent .TimeUnit ;
7
+
8
+ import ug .sparkpl .momoapi .models .AccessToken ;
9
+ import ug .sparkpl .momoapi .models .Balance ;
10
+ import ug .sparkpl .momoapi .models .RequestToPay ;
11
+ import ug .sparkpl .momoapi .models .Transaction ;
12
+ import ug .sparkpl .momoapi .network .RequestOptions ;
13
+ import ug .sparkpl .momoapi .utils .DateTimeTypeConverter ;
14
+
15
+ import org .joda .time .DateTime ;
16
+
3
17
import com .google .gson .FieldNamingPolicy ;
4
18
import com .google .gson .Gson ;
5
19
import com .google .gson .GsonBuilder ;
20
+
6
21
import okhttp3 .Credentials ;
7
22
import okhttp3 .OkHttpClient ;
8
23
import okhttp3 .logging .HttpLoggingInterceptor ;
9
- import org .joda .time .DateTime ;
10
24
import retrofit2 .Response ;
11
25
import retrofit2 .Retrofit ;
12
26
import retrofit2 .converter .gson .GsonConverterFactory ;
13
27
import retrofit2 .converter .scalars .ScalarsConverterFactory ;
14
- import ug .sparkpl .momoapi .models .AccessToken ;
15
- import ug .sparkpl .momoapi .models .Balance ;
16
- import ug .sparkpl .momoapi .models .RequestToPay ;
17
- import ug .sparkpl .momoapi .models .Transaction ;
18
- import ug .sparkpl .momoapi .network .RequestOptions ;
19
- import ug .sparkpl .momoapi .utils .DateTimeTypeConverter ;
20
-
21
- import java .io .IOException ;
22
- import java .util .HashMap ;
23
- import java .util .UUID ;
24
- import java .util .concurrent .TimeUnit ;
25
28
26
29
public class CollectionsClient {
27
30
@@ -35,12 +38,17 @@ public class CollectionsClient {
35
38
private Retrofit client ;
36
39
37
40
41
+ /**
42
+ * CollectionsClient.
43
+ *
44
+ * @param opts RequestOptions
45
+ */
38
46
public CollectionsClient (RequestOptions opts ) {
39
47
this .opts = opts ;
40
48
this .gson = new GsonBuilder ()
41
- .setFieldNamingPolicy (FieldNamingPolicy .LOWER_CASE_WITH_UNDERSCORES )
42
- .registerTypeAdapter (DateTime .class , new DateTimeTypeConverter ())
43
- .create ();
49
+ .setFieldNamingPolicy (FieldNamingPolicy .LOWER_CASE_WITH_UNDERSCORES )
50
+ .registerTypeAdapter (DateTime .class , new DateTimeTypeConverter ())
51
+ .create ();
44
52
45
53
this .session = new CollectionSession ();
46
54
@@ -64,58 +72,101 @@ public CollectionsClient(RequestOptions opts) {
64
72
65
73
66
74
this .httpClient = okhttpbuilder
67
- .build ();
75
+ .build ();
68
76
69
77
70
78
this .retrofitClient = new Retrofit .Builder ()
71
- .client (this .httpClient )
72
- .baseUrl (opts .getBaseUrl ())
73
- .addConverterFactory (GsonConverterFactory .create (gson ))
74
- .addConverterFactory (ScalarsConverterFactory .create ())
75
- .build ();
79
+ .client (this .httpClient )
80
+ .baseUrl (opts .getBaseUrl ())
81
+ .addConverterFactory (GsonConverterFactory .create (gson ))
82
+ .addConverterFactory (ScalarsConverterFactory .create ())
83
+ .build ();
76
84
77
85
this .apiService = this .retrofitClient .create (CollectionsApiService .class );
78
86
79
87
80
88
}
81
89
82
90
91
+ /**
92
+ * get access Token.
93
+ *
94
+ * @return AccessToken
95
+ * @throws IOException when there is a network error
96
+ */
83
97
public AccessToken getToken () throws IOException {
84
- String credentials = Credentials .basic (this .opts .getCollectionUserId (), this .opts .getCollectionApiSecret ());
98
+ String credentials = Credentials .basic (this .opts .getCollectionUserId (),
99
+ this .opts .getCollectionApiSecret ());
85
100
Response <AccessToken > token = this .apiService
86
- .getToken (credentials , this .opts .getCollectionPrimaryKey ()).execute ();
101
+ .getToken (credentials , this .opts .getCollectionPrimaryKey ()).execute ();
87
102
88
103
return token .body ();
89
104
}
90
105
91
106
107
+ /**
108
+ * get Account Balance.
109
+ *
110
+ * @return Balance
111
+ * @throws IOException when there is a network error
112
+ */
92
113
public Balance getBalance () throws IOException {
93
114
Response <Balance > balance = this .apiService
94
- .getBalance ().execute ();
115
+ .getBalance ().execute ();
95
116
return balance .body ();
96
117
97
118
}
98
119
120
+ /**
121
+ * get Transaction.
122
+ *
123
+ * @param ref String
124
+ * @return Transaction
125
+ * @throws IOException when there is a network error
126
+ */
99
127
public Transaction getTransaction (String ref ) throws IOException {
100
128
Response <Transaction > transaction = this .apiService
101
- .getTransactionStatus (ref ).execute ();
129
+ .getTransactionStatus (ref ).execute ();
102
130
return transaction .body ();
103
131
104
132
}
105
133
106
134
107
- public String requestToPay (String mobile , String amount , String external_id , String payee_note , String payer_message , String currency ) throws IOException {
108
- RequestToPay rBody = new RequestToPay (mobile , amount , external_id , payee_note , payer_message , currency );
135
+ /**
136
+ * Request To Pay.
137
+ *
138
+ * @param mobile String
139
+ * @param amount String
140
+ * @param externalId String
141
+ * @param payeeNote String
142
+ * @param payerMessage String
143
+ * @param currency String
144
+ * @return String
145
+ * @throws IOException when there is a network error
146
+ */
147
+ public String requestToPay (String mobile , String amount , String externalId , String payeeNote ,
148
+ String payerMessage , String currency ) throws IOException {
149
+ RequestToPay rbody = new RequestToPay (mobile , amount , externalId ,
150
+ payeeNote , payerMessage , currency );
109
151
String ref = UUID .randomUUID ().toString ();
110
- this .apiService .requestToPay (rBody , ref ).execute ();
152
+ this .apiService .requestToPay (rbody , ref ).execute ();
111
153
return ref ;
112
154
113
155
}
114
156
157
+ /**
158
+ * Request To Pay.
159
+ *
160
+ * @param opts HashMap
161
+ * @return String
162
+ * @throws IOException when there is a network error
163
+ */
115
164
public String requestToPay (HashMap <String , String > opts ) throws IOException {
116
- RequestToPay rBody = new RequestToPay (opts .get ("mobile" ), opts .get ("amount" ), opts .get ("externalId" ), opts .get ("payeeNote" ), opts .get ("payerMessage" ), this .opts .getCurrency ());
165
+ RequestToPay rbody = new RequestToPay (opts .get ("mobile" ), opts .get ("amount" ),
166
+ opts .get ("externalId" ), opts .get ("payeeNote" ), opts .get ("payerMessage" ),
167
+ this .opts .getCurrency ());
117
168
String ref = UUID .randomUUID ().toString ();
118
- this .apiService .requestToPay (rBody , ref ).execute ();
169
+ this .apiService .requestToPay (rbody , ref ).execute ();
119
170
return ref ;
120
171
121
172
}
0 commit comments