Added validateCreditMethod standalone method
This commit is contained in:
parent
f264bb43bc
commit
79cbf76761
4 changed files with 65 additions and 43 deletions
|
@ -24,7 +24,7 @@ This project provides a collection of useful template renderers.
|
|||
## Encoding Renderers
|
||||
|
||||
| Renderer | Description |
|
||||
|:--------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------|
|
||||
|:------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------|
|
||||
| [rife.render.EncodeBase64](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeBase64) | Encodes a template value to Base64 |
|
||||
| [rife.render.EncodeHtml](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeHtml) | Encodes a template value to HTML |
|
||||
| [rife.render.EncodeHtmlEntities](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.EncodeHtmlEntities) | Encodes a template value to HTML decimal entities |
|
||||
|
|
|
@ -13,7 +13,7 @@ plugins {
|
|||
id("com.github.ben-manes.versions") version "0.46.0"
|
||||
}
|
||||
|
||||
val rifeVersion by rootProject.extra { "1.5.4" }
|
||||
val rifeVersion by rootProject.extra { "1.5.5" }
|
||||
|
||||
group = "com.uwyn.rife2"
|
||||
version = "1.0.1-SNAPSHOT"
|
||||
|
|
|
@ -192,39 +192,14 @@ public final class RenderUtils {
|
|||
return src;
|
||||
}
|
||||
|
||||
try {
|
||||
var cc = src.replaceAll("[^0-9]", "");
|
||||
|
||||
var len = cc.length();
|
||||
if (len >= 4) {
|
||||
// Luhn algorithm
|
||||
var sum = 0;
|
||||
boolean isSecond = false;
|
||||
int digit;
|
||||
char c;
|
||||
for (int i = len - 1; i >= 0; i--) {
|
||||
c = cc.charAt(i);
|
||||
if (c >= '0' && c <= '9') {
|
||||
digit = cc.charAt(i) - '0';
|
||||
if (isSecond) {
|
||||
digit = digit * 2;
|
||||
}
|
||||
sum += digit / 10;
|
||||
sum += digit % 10;
|
||||
}
|
||||
|
||||
isSecond = !isSecond;
|
||||
}
|
||||
if (sum % 10 == 0) {
|
||||
return cc.substring(len - 4);
|
||||
}
|
||||
}
|
||||
} catch (NumberFormatException ignore) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
if (validateCreditCard(cc)) {
|
||||
return cc.substring(cc.length() - 4);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a text String to HTML decimal entities.
|
||||
|
@ -520,4 +495,42 @@ public final class RenderUtils {
|
|||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a credit card number using the Luhn algorithm.
|
||||
*
|
||||
* @param cc the credit card number
|
||||
* @return {@code trude} if the credit card number is valid
|
||||
*/
|
||||
public static boolean validateCreditCard(String cc) {
|
||||
try {
|
||||
var len = cc.length();
|
||||
if (len >= 8 && len <= 19) {
|
||||
// Luhn algorithm
|
||||
var sum = 0;
|
||||
boolean second = false;
|
||||
int digit;
|
||||
char c;
|
||||
for (int i = len - 1; i >= 0; i--) {
|
||||
c = cc.charAt(i);
|
||||
if (c >= '0' && c <= '9') {
|
||||
digit = cc.charAt(i) - '0';
|
||||
if (second) {
|
||||
digit = digit * 2;
|
||||
}
|
||||
sum += digit / 10;
|
||||
sum += digit % 10;
|
||||
|
||||
second = !second;
|
||||
}
|
||||
}
|
||||
if (sum % 10 == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (NumberFormatException ignore) {
|
||||
// do nothing
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -83,4 +83,13 @@ class TestRenderUtils {
|
|||
assertThat(RenderUtils.uncapitalize("A")).isEqualTo("a");
|
||||
assertThat(RenderUtils.uncapitalize("")).as("empty").isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testValidateCreditCard() {
|
||||
assertThat(RenderUtils.validateCreditCard("4505 4672 3366 6430")).as("visa").isTrue();
|
||||
assertThat(RenderUtils.validateCreditCard("5189-5923-3915-0425")).as("mastercard").isTrue();
|
||||
assertThat(RenderUtils.validateCreditCard("3433634926643302")).as("amex").isTrue();
|
||||
assertThat(RenderUtils.validateCreditCard("6011 1076-8252 0629")).as("discover").isTrue();
|
||||
assertThat(RenderUtils.validateCreditCard("0123456789012345")).as("invalid").isFalse();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue