Skip to content

Commit 1a50edb

Browse files
committed
Merge branch 'jeremiahVaris-Add_card_number_validation'
2 parents 7dded77 + bf9ca77 commit 1a50edb

File tree

2 files changed

+141
-111
lines changed

2 files changed

+141
-111
lines changed

raveandroid/src/main/java/com/flutterwave/raveandroid/Utils.java

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static String getDeviceImei(Context c) {
6060
return ip;
6161
}
6262

63-
public static boolean wasTxSuccessful(RavePayInitializer ravePayInitializer, String responseAsJSONString){
63+
public static boolean wasTxSuccessful(RavePayInitializer ravePayInitializer, String responseAsJSONString) {
6464

6565
String amount = ravePayInitializer.getAmount() + "";
6666
String currency = ravePayInitializer.getCurrency();
@@ -76,13 +76,12 @@ public static boolean wasTxSuccessful(RavePayInitializer ravePayInitializer, Str
7676
if (areAmountsSame(amount, txAmount) &&
7777
chargeResponse.equalsIgnoreCase("00") &&
7878
(status.contains("success") |
79-
status.contains("pending-capture")) &&
79+
status.contains("pending-capture")) &&
8080
currency.equalsIgnoreCase(txCurrency)) {
8181
Log.d("RAVE TX V", "true");
8282
return true;
8383
}
84-
}
85-
catch (JSONException e) {
84+
} catch (JSONException e) {
8685
e.printStackTrace();
8786
Log.d("RAVE TX V", "false");
8887
return false;
@@ -126,7 +125,8 @@ public static boolean isEmailValid(String email) {
126125
public static String convertChargeRequestPayloadToJson(Payload body) {
127126

128127
Gson gson = new Gson();
129-
Type type = new TypeToken<Payload>() {}.getType();
128+
Type type = new TypeToken<Payload>() {
129+
}.getType();
130130
return gson.toJson(body, type);
131131
}
132132

@@ -136,8 +136,7 @@ public static List<Meta> pojofyMetaString(String meta) {
136136
Type type = new TypeToken<List<Meta>>() {
137137
}.getType();
138138
return gson.fromJson(meta, type);
139-
}
140-
catch (Exception e) {
139+
} catch (Exception e) {
141140
e.printStackTrace();
142141
return null;
143142
}
@@ -150,8 +149,7 @@ public static List<SubAccount> pojofySubaccountString(String subaccount) {
150149
Type type = new TypeToken<List<SubAccount>>() {
151150
}.getType();
152151
return gson.fromJson(subaccount, type);
153-
}
154-
catch (Exception e) {
152+
} catch (Exception e) {
155153
e.printStackTrace();
156154
return null;
157155
}
@@ -160,17 +158,19 @@ public static List<SubAccount> pojofySubaccountString(String subaccount) {
160158

161159
public static String stringifyMeta(List<Meta> meta) {
162160
Gson gson = new Gson();
163-
Type type = new TypeToken<List<Meta>>() {}.getType();
161+
Type type = new TypeToken<List<Meta>>() {
162+
}.getType();
164163
return gson.toJson(meta, type);
165164
}
166165

167166
public static String stringifySubaccounts(List<SubAccount> subAccounts) {
168167
Gson gson = new Gson();
169-
Type type = new TypeToken<List<SubAccount>>() {}.getType();
168+
Type type = new TypeToken<List<SubAccount>>() {
169+
}.getType();
170170
return gson.toJson(subAccounts, type);
171171
}
172172

173-
public static byte[] RSAEncrypt(String plaintext){
173+
public static byte[] RSAEncrypt(String plaintext) {
174174
PublicKey key = getKey("baA/RgjURU3I0uqH3iRos3NbE8fT+lP8SDXKymsnfdPrMQAEoMBuXtoaQiJ1i5tuBG9EgSEOH1LAZEaAsvwClw==");
175175
byte[] ciphertext = null;
176176
try {
@@ -183,15 +183,14 @@ public static byte[] RSAEncrypt(String plaintext){
183183
return ciphertext;
184184
}
185185

186-
public static PublicKey getKey(String key){
187-
try{
186+
public static PublicKey getKey(String key) {
187+
try {
188188
byte[] byteKey = Base64.decode(key.getBytes(Charset.forName("UTF-16")), Base64.DEFAULT);
189189
X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
190190
KeyFactory kf = KeyFactory.getInstance("RSA");
191191

192192
return kf.generatePublic(X509publicKey);
193-
}
194-
catch(Exception e){
193+
} catch (Exception e) {
195194
e.printStackTrace();
196195
}
197196

@@ -201,7 +200,7 @@ public static PublicKey getKey(String key){
201200
public static String getEncryptedData(String unEncryptedString, String encryptionKey) {
202201
try {
203202
return encrypt(unEncryptedString, encryptionKey);
204-
}catch (Exception e){
203+
} catch (Exception e) {
205204
e.printStackTrace();
206205
}
207206
return null;
@@ -210,15 +209,15 @@ public static String getEncryptedData(String unEncryptedString, String encryptio
210209
public static String encryptRef(String key, String ref) {
211210
try {
212211
return AESCrypt.encrypt(key, ref);
213-
}catch (GeneralSecurityException e){
212+
} catch (GeneralSecurityException e) {
214213
return null;
215214
}
216215
}
217216

218217
public static String decryptRef(String key, String encryptedRef) {
219218
try {
220219
return AESCrypt.decrypt(key, encryptedRef);
221-
}catch (GeneralSecurityException e){
220+
} catch (GeneralSecurityException e) {
222221
return null;
223222
}
224223
}
@@ -250,8 +249,7 @@ public static String obfuscateCardNumber(String first6, String last4) {
250249
int cardNoLength = first6.length() + last4.length();
251250
if (cardNoLength < 10) {
252251
return first6 + last4;
253-
}
254-
else {
252+
} else {
255253

256254
int othersLength = 6;
257255

@@ -270,8 +268,8 @@ public static String spacifyCardNumber(String cardNo) {
270268

271269
int len = cardNo.length();
272270

273-
int nChunks = len/4;
274-
int rem = len%4;
271+
int nChunks = len / 4;
272+
int rem = len % 4;
275273

276274

277275
for (int i = 0; i < nChunks; i++) {
@@ -284,4 +282,43 @@ public static String spacifyCardNumber(String cardNo) {
284282
return spacified;
285283

286284
}
285+
286+
/**
287+
* Checks that a number is valid according to the Luhn algorithm
288+
* https://en.wikipedia.org/wiki/Luhn_algorithm
289+
*
290+
* @param number to be checked
291+
* @return true if valid
292+
*/
293+
public static boolean isValidLuhnNumber(String number) {
294+
295+
try {// Verify that string contains only numbers
296+
Long parsed = Long.parseLong(number);
297+
} catch (Exception e) {
298+
e.printStackTrace();
299+
return false;
300+
}
301+
302+
String reversedNumber = new StringBuffer(number).reverse().toString();
303+
char reversedCharArray[] = reversedNumber.toCharArray();
304+
int luhnSum = 0;
305+
306+
// Double the value of every second digit from the right
307+
for (int index = 1; index < number.length(); index += 2) {
308+
int doubleResult = Character.getNumericValue(reversedCharArray[index]) * 2;
309+
if (doubleResult > 9) {// If result has double digits, sum digits
310+
doubleResult = 1 + (doubleResult - 10);
311+
}
312+
313+
reversedCharArray[index] = Character.forDigit(doubleResult, 10);
314+
}
315+
316+
// Sum all digits
317+
for (int index = 0; index < number.length(); index++) {
318+
luhnSum += Character.getNumericValue(reversedCharArray[index]);
319+
}
320+
321+
// For valiid Luhn number, sum result should be a multiple of 10
322+
return luhnSum % 10 == 0;
323+
}
287324
}

0 commit comments

Comments
 (0)