|
| 1 | +package com.riskified.models; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Date; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Locale; |
| 8 | +import java.util.function.Predicate; |
| 9 | + |
| 10 | +import com.riskified.validations.*; |
| 11 | + |
| 12 | +public class WalletPaymentDetails implements IPaymentDetails { |
| 13 | + |
| 14 | + private static final List<PaymentType> WALLET_PAYMENT_TYPES = Arrays.asList( |
| 15 | + PaymentType.APPLE_PAY, |
| 16 | + PaymentType.GOOGLE_PAY, |
| 17 | + PaymentType.SAMSUNG_PAY, |
| 18 | + PaymentType.WECHAT_PAY, |
| 19 | + PaymentType.AMAZON_PAY, |
| 20 | + PaymentType.ALIPAY); |
| 21 | + |
| 22 | + private static final List<String> VALID_ACQUIRER_REGIONS = Arrays.asList("EU", "NONEU"); |
| 23 | + |
| 24 | + // A rule = a check plus the message to raise if it fails. |
| 25 | + // Add new rules here instead of adding branches in validate(). |
| 26 | + private static final List<Rule> RULES = Arrays.asList( |
| 27 | + new Rule(p -> p.paymentType != null, "Payment Type can't be null."), |
| 28 | + new Rule(p -> p.paymentType == null || WALLET_PAYMENT_TYPES.contains(p.paymentType), |
| 29 | + "Payment Type must be one of: " + supportedPaymentTypes()), |
| 30 | + new Rule(p -> p.authorizationId != null && !p.authorizationId.isEmpty(), |
| 31 | + "Authorization Id can't be null or empty."), |
| 32 | + new Rule(p -> p.avsResultCode != null && !p.avsResultCode.isEmpty(), |
| 33 | + "AVS Result Code can't be null or empty."), |
| 34 | + new Rule(p -> p.creditCardCountry == null || isValidCountryCode(p.creditCardCountry), |
| 35 | + "Credit Card Country is not a valid ISO country code."), |
| 36 | + new Rule(p -> p.acquirerRegion == null || VALID_ACQUIRER_REGIONS.contains(p.acquirerRegion), |
| 37 | + "Acquirer Region must be one of: " + VALID_ACQUIRER_REGIONS), |
| 38 | + new Rule(p -> p.expiryMonth == null || (p.expiryMonth >= 1 && p.expiryMonth <= 12), |
| 39 | + "Expiry Month must be between 01 and 12"), |
| 40 | + new Rule(p -> p.expiryYear == null || (p.expiryYear >= 1900 && p.expiryYear <= 9999), |
| 41 | + "Expiry Year must be a 4-digit integer formatted as YYYY")); |
| 42 | + |
| 43 | + private PaymentType paymentType; |
| 44 | + private String avsResultCode; |
| 45 | + private String creditCardCompany; |
| 46 | + private String creditCardCountry; |
| 47 | + private String creditCardToken; |
| 48 | + private String cardholderName; |
| 49 | + private String authorizationId; |
| 50 | + private String mid; |
| 51 | + private String id; |
| 52 | + private Date storedPaymentCreatedAt; |
| 53 | + private Date storedPaymentUpdatedAt; |
| 54 | + private Integer installments; |
| 55 | + private String acquirerBin; |
| 56 | + private String acquirerRegion; |
| 57 | + private AuthorizationType authorizationType; |
| 58 | + private Integer expiryMonth; |
| 59 | + private Integer expiryYear; |
| 60 | + private Double initialPaymentAmount; |
| 61 | + private Integer paymentFrequency; |
| 62 | + private String billingAddressId; |
| 63 | + private AuthenticationResult authenticationResult; |
| 64 | + |
| 65 | + public WalletPaymentDetails(PaymentType paymentType, String authorizationId, String avsResultCode) { |
| 66 | + this.paymentType = paymentType; |
| 67 | + this.authorizationId = authorizationId; |
| 68 | + this.avsResultCode = avsResultCode; |
| 69 | + } |
| 70 | + |
| 71 | + public void validate(Validation validationType) throws FieldBadFormatException { |
| 72 | + if (validationType != Validation.ALL) { |
| 73 | + return; |
| 74 | + } |
| 75 | + for (Rule rule : RULES) { |
| 76 | + if (!rule.passes(this)) { |
| 77 | + throw new FieldBadFormatException(this, rule.message()); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static String supportedPaymentTypes() { |
| 83 | + List<String> names = new ArrayList<String>(WALLET_PAYMENT_TYPES.size()); |
| 84 | + for (PaymentType type : WALLET_PAYMENT_TYPES) { |
| 85 | + names.add(type.name().toLowerCase()); |
| 86 | + } |
| 87 | + return names.toString(); |
| 88 | + } |
| 89 | + |
| 90 | + private static boolean isValidCountryCode(String countryCode) { |
| 91 | + return Arrays.asList(Locale.getISOCountries()).contains(countryCode); |
| 92 | + } |
| 93 | + |
| 94 | + private static final class Rule { |
| 95 | + private final Predicate<WalletPaymentDetails> check; |
| 96 | + private final String message; |
| 97 | + |
| 98 | + Rule(Predicate<WalletPaymentDetails> check, String message) { |
| 99 | + this.check = check; |
| 100 | + this.message = message; |
| 101 | + } |
| 102 | + |
| 103 | + boolean passes(WalletPaymentDetails details) { |
| 104 | + return check.test(details); |
| 105 | + } |
| 106 | + |
| 107 | + String message() { |
| 108 | + return message; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public PaymentType getPaymentType() { |
| 113 | + return paymentType; |
| 114 | + } |
| 115 | + |
| 116 | + public void setPaymentType(PaymentType paymentType) { |
| 117 | + this.paymentType = paymentType; |
| 118 | + } |
| 119 | + |
| 120 | + public String getAvsResultCode() { |
| 121 | + return avsResultCode; |
| 122 | + } |
| 123 | + |
| 124 | + public void setAvsResultCode(String avsResultCode) { |
| 125 | + this.avsResultCode = avsResultCode; |
| 126 | + } |
| 127 | + |
| 128 | + public String getCreditCardCompany() { |
| 129 | + return creditCardCompany; |
| 130 | + } |
| 131 | + |
| 132 | + public void setCreditCardCompany(String creditCardCompany) { |
| 133 | + this.creditCardCompany = creditCardCompany; |
| 134 | + } |
| 135 | + |
| 136 | + public String getCreditCardCountry() { |
| 137 | + return creditCardCountry; |
| 138 | + } |
| 139 | + |
| 140 | + public void setCreditCardCountry(String creditCardCountry) { |
| 141 | + this.creditCardCountry = creditCardCountry; |
| 142 | + } |
| 143 | + |
| 144 | + public String getCreditCardToken() { |
| 145 | + return creditCardToken; |
| 146 | + } |
| 147 | + |
| 148 | + public void setCreditCardToken(String creditCardToken) { |
| 149 | + this.creditCardToken = creditCardToken; |
| 150 | + } |
| 151 | + |
| 152 | + public String getCardholderName() { |
| 153 | + return cardholderName; |
| 154 | + } |
| 155 | + |
| 156 | + public void setCardholderName(String cardholderName) { |
| 157 | + this.cardholderName = cardholderName; |
| 158 | + } |
| 159 | + |
| 160 | + public String getAuthorizationId() { |
| 161 | + return authorizationId; |
| 162 | + } |
| 163 | + |
| 164 | + public void setAuthorizationId(String authorizationId) { |
| 165 | + this.authorizationId = authorizationId; |
| 166 | + } |
| 167 | + |
| 168 | + public String getMid() { |
| 169 | + return mid; |
| 170 | + } |
| 171 | + |
| 172 | + public void setMid(String mid) { |
| 173 | + this.mid = mid; |
| 174 | + } |
| 175 | + |
| 176 | + public String getId() { |
| 177 | + return id; |
| 178 | + } |
| 179 | + |
| 180 | + public void setId(String id) { |
| 181 | + this.id = id; |
| 182 | + } |
| 183 | + |
| 184 | + public Date getStoredPaymentCreatedAt() { |
| 185 | + return storedPaymentCreatedAt; |
| 186 | + } |
| 187 | + |
| 188 | + public void setStoredPaymentCreatedAt(Date storedPaymentCreatedAt) { |
| 189 | + this.storedPaymentCreatedAt = storedPaymentCreatedAt; |
| 190 | + } |
| 191 | + |
| 192 | + public Date getStoredPaymentUpdatedAt() { |
| 193 | + return storedPaymentUpdatedAt; |
| 194 | + } |
| 195 | + |
| 196 | + public void setStoredPaymentUpdatedAt(Date storedPaymentUpdatedAt) { |
| 197 | + this.storedPaymentUpdatedAt = storedPaymentUpdatedAt; |
| 198 | + } |
| 199 | + |
| 200 | + public Integer getInstallments() { |
| 201 | + return installments; |
| 202 | + } |
| 203 | + |
| 204 | + public void setInstallments(Integer installments) { |
| 205 | + this.installments = installments; |
| 206 | + } |
| 207 | + |
| 208 | + public String getAcquirerBin() { |
| 209 | + return acquirerBin; |
| 210 | + } |
| 211 | + |
| 212 | + public void setAcquirerBin(String acquirerBin) { |
| 213 | + this.acquirerBin = acquirerBin; |
| 214 | + } |
| 215 | + |
| 216 | + public String getAcquirerRegion() { |
| 217 | + return acquirerRegion; |
| 218 | + } |
| 219 | + |
| 220 | + public void setAcquirerRegion(String acquirerRegion) { |
| 221 | + this.acquirerRegion = acquirerRegion; |
| 222 | + } |
| 223 | + |
| 224 | + public AuthorizationType getAuthorizationType() { |
| 225 | + return authorizationType; |
| 226 | + } |
| 227 | + |
| 228 | + public void setAuthorizationType(AuthorizationType authorizationType) { |
| 229 | + this.authorizationType = authorizationType; |
| 230 | + } |
| 231 | + |
| 232 | + public Integer getExpiryMonth() { |
| 233 | + return expiryMonth; |
| 234 | + } |
| 235 | + |
| 236 | + public void setExpiryMonth(Integer expiryMonth) { |
| 237 | + this.expiryMonth = expiryMonth; |
| 238 | + } |
| 239 | + |
| 240 | + public Integer getExpiryYear() { |
| 241 | + return expiryYear; |
| 242 | + } |
| 243 | + |
| 244 | + public void setExpiryYear(Integer expiryYear) { |
| 245 | + this.expiryYear = expiryYear; |
| 246 | + } |
| 247 | + |
| 248 | + public Double getInitialPaymentAmount() { |
| 249 | + return initialPaymentAmount; |
| 250 | + } |
| 251 | + |
| 252 | + public void setInitialPaymentAmount(Double initialPaymentAmount) { |
| 253 | + this.initialPaymentAmount = initialPaymentAmount; |
| 254 | + } |
| 255 | + |
| 256 | + public Integer getPaymentFrequency() { |
| 257 | + return paymentFrequency; |
| 258 | + } |
| 259 | + |
| 260 | + public void setPaymentFrequency(Integer paymentFrequency) { |
| 261 | + this.paymentFrequency = paymentFrequency; |
| 262 | + } |
| 263 | + |
| 264 | + public String getBillingAddressId() { |
| 265 | + return billingAddressId; |
| 266 | + } |
| 267 | + |
| 268 | + public void setBillingAddressId(String billingAddressId) { |
| 269 | + this.billingAddressId = billingAddressId; |
| 270 | + } |
| 271 | + |
| 272 | + public AuthenticationResult getAuthenticationResult() { |
| 273 | + return authenticationResult; |
| 274 | + } |
| 275 | + |
| 276 | + public void setAuthenticationResult(AuthenticationResult authenticationResult) { |
| 277 | + this.authenticationResult = authenticationResult; |
| 278 | + } |
| 279 | + |
| 280 | +} |
0 commit comments