mirror of
https://github.com/valitydev/adapter-common-lib.git
synced 2024-11-06 02:05:18 +00:00
add init card model with optional exp date (#34)
* add init card model with optional exp date * bump Co-authored-by: ggmaleva <ggmaleva@yandex.ru>
This commit is contained in:
parent
48a84735ab
commit
cde1b30665
2
pom.xml
2
pom.xml
@ -12,7 +12,7 @@
|
||||
</parent>
|
||||
|
||||
<artifactId>adapter-common-lib</artifactId>
|
||||
<version>1.1.3</version>
|
||||
<version>1.1.4</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>adapter-common-lib</name>
|
||||
|
@ -9,6 +9,7 @@ import dev.vality.damsel.domain.BankCard;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@ -26,23 +27,10 @@ public class BankCardExtractor {
|
||||
public static CardDataProxyModel initCardDataProxyModel(BankCard bankCard,
|
||||
CardData cardData,
|
||||
List<String> cardHoldersNames) {
|
||||
String cardHolder;
|
||||
if (bankCard.isSetCardholderName()) {
|
||||
cardHolder = bankCard.getCardholderName();
|
||||
} else if (cardData.isSetCardholderName()) {
|
||||
cardHolder = cardData.getCardholderName();
|
||||
} else if (cardHoldersNames != null && cardHoldersNames.size() > 0) {
|
||||
cardHolder = getCardHolderFromList(cardHoldersNames, bankCard.getToken());
|
||||
} else {
|
||||
cardHolder = (FAKER_NAME.firstName() + StringUtils.SPACE + FAKER_NAME.lastName())
|
||||
.replaceAll(NAME_REGEXP, StringUtils.EMPTY)
|
||||
.toUpperCase();
|
||||
}
|
||||
|
||||
String cardHolder = extractCardHolder(bankCard, cardData, cardHoldersNames);
|
||||
if (!bankCard.isSetExpDate() && !cardData.isSetExpDate()) {
|
||||
throw new CdsStorageExpDateException("Expiration date not found");
|
||||
}
|
||||
|
||||
return CardDataProxyModel.builder()
|
||||
.cardholderName(cardHolder)
|
||||
.pan(cardData.getPan())
|
||||
@ -51,7 +39,45 @@ public class BankCardExtractor {
|
||||
.build();
|
||||
}
|
||||
|
||||
private static String extractCardHolder(BankCard bankCard, CardData cardData, List<String> cardHoldersNames) {
|
||||
if (bankCard.isSetCardholderName()) {
|
||||
return bankCard.getCardholderName();
|
||||
} else if (cardData.isSetCardholderName()) {
|
||||
return cardData.getCardholderName();
|
||||
} else if (!CollectionUtils.isEmpty(cardHoldersNames)) {
|
||||
return getCardHolderFromList(cardHoldersNames, bankCard.getToken());
|
||||
} else {
|
||||
return (FAKER_NAME.firstName() + StringUtils.SPACE + FAKER_NAME.lastName())
|
||||
.replaceAll(NAME_REGEXP, StringUtils.EMPTY)
|
||||
.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
private static String getCardHolderFromList(List<String> cardHoldersNames, String cardToken) {
|
||||
return cardHoldersNames.get(Math.abs(cardToken.hashCode() % cardHoldersNames.size()));
|
||||
}
|
||||
|
||||
public static CardDataProxyModel initCardDataProxyModelWithOptionalExpDate(BankCard bankCard, CardData cardData) {
|
||||
String cardHolder = extractCardHolder(bankCard, cardData, null);
|
||||
return CardDataProxyModel.builder()
|
||||
.cardholderName(cardHolder)
|
||||
.pan(cardData.getPan())
|
||||
.expMonth(getExpMonth(bankCard, cardData))
|
||||
.expYear(getExpYear(bankCard, cardData))
|
||||
.build();
|
||||
}
|
||||
|
||||
private static byte getExpMonth(BankCard bankCard, CardData cardData) {
|
||||
if (bankCard.isSetExpDate()) {
|
||||
return bankCard.getExpDate().getMonth();
|
||||
}
|
||||
return cardData.isSetExpDate() ? cardData.getExpDate().getMonth() : 0;
|
||||
}
|
||||
|
||||
private static short getExpYear(BankCard bankCard, CardData cardData) {
|
||||
if (bankCard.isSetExpDate()) {
|
||||
return bankCard.getExpDate().getYear();
|
||||
}
|
||||
return cardData.isSetExpDate() ? cardData.getExpDate().getYear() : 0;
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,23 @@ class BankCardExtractorTest {
|
||||
new CardData(),
|
||||
List.of(TEST_1, TEST_2));
|
||||
|
||||
assertEquals(cardDataProxyModel.getCardholderName(), TEST_1);
|
||||
assertEquals(TEST_1, cardDataProxyModel.getCardholderName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void initCardDataProxyModelWithOptionalExpDate() {
|
||||
String pan = "pan";
|
||||
CardDataProxyModel cardDataProxyModel =
|
||||
BankCardExtractor.initCardDataProxyModelWithOptionalExpDate(
|
||||
new BankCard()
|
||||
.setToken("test")
|
||||
.setCardholderName(TEST_1),
|
||||
new CardData()
|
||||
.setPan(pan));
|
||||
|
||||
assertEquals(TEST_1, cardDataProxyModel.getCardholderName());
|
||||
assertEquals(pan, cardDataProxyModel.getPan());
|
||||
assertEquals(0, cardDataProxyModel.getExpMonth());
|
||||
assertEquals(0, cardDataProxyModel.getExpYear());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user