Created
January 25, 2013 13:52
-
-
Save Fodsuk/4634607 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CardValidationService : ICardValidationService | |
{ | |
private readonly CallValidationConfiguration _config; | |
private readonly ICallValidateDataService _callValidateDataService; | |
private readonly ILogger _logger = LoggerProvider.GetLogger<CardValidationService>(); | |
public CardValidationService(CallValidationConfiguration config, ICallValidateDataService callValidateDataService) | |
{ | |
_config = config; | |
_callValidateDataService = callValidateDataService; | |
} | |
public CallValidateResult ValidateWithCallValidate(CallValidateDetails data) | |
{ | |
if (_config.ResultOverride == CallValidateResultOverride.Valid) | |
return GetValidResult(data.ApplicationId); | |
if ((_config.ResultOverride == CallValidateResultOverride.Invalid || | |
_config.ResultOverride == CallValidateResultOverride.Referred) && | |
data.AttemptNumber >= _config.MaximumAttempts) | |
return GetMaxAttemptsResult(data.ApplicationId); | |
if (_config.ResultOverride == CallValidateResultOverride.Invalid) | |
return GetInvalidResult(data.ApplicationId); | |
if (_config.ResultOverride == CallValidateResultOverride.Unavailable) | |
return GetUnavailableResult(data.ApplicationId); | |
if (_config.ResultOverride == CallValidateResultOverride.Referred) | |
return GetReferredResult(data.ApplicationId); | |
var indDetails = new IndividualDetails | |
{ | |
Title = data.Title, | |
FirstName = data.FirstName, | |
SurName = data.LastName, | |
DateOfBirth = data.DateOfBirth.ToString("dd/MM/yyyy"), | |
EmailAddress = data.EmailAddress, | |
}; | |
var addDetails = new AddressDetails | |
{ | |
AbodeNumber = data.FlatNumber, | |
Address1 = data.Street, | |
Town = data.City, | |
Postcode = data.Postcode, | |
BuildingNumber = data.BuildingNumber, | |
BuildingName = data.BuildingName | |
}; | |
var bankInfo = new BankInformation | |
{ | |
BankAccountNumber = data.BankAccountNumber, | |
BankSortCode = data.BankSortCode | |
}; | |
var cardInfo = new CardInformation | |
{ | |
CardCV2 = data.CardCvvNumber, | |
CardExpiryDate = data.CardExpiryDate, | |
CardNumber = data.CardNumber, | |
CardStartDate = data.CardStartDate, | |
}; | |
var perInfo = new PersonalInformation | |
{ | |
IndividualDetails = indDetails, | |
AddressDetails = addDetails | |
}; | |
var checksRequired = new ChecksRequired | |
{ | |
BankStandard = _config.CheckBankStandard, | |
BankEnhanced = _config.CheckBankEnhanced, | |
CardLive = _config.CheckCardLive, | |
CardEnhanced = _config.CheckCardEnhanced, | |
IDEnhanced = _config.CheckIDEnhanced, | |
DeliveryFraud = _config.CheckDeliveryFraud, | |
EmailValidate = _config.CheckEmailValidate, | |
CreditScore = _config.ReturnCreditScore, | |
Zodiac = _config.CheckZodiac, | |
IPAddress = _config.CheckIPAddress | |
}; | |
var auth = new Authentication | |
{ | |
Company = _config.Company, | |
Username = _config.Username, | |
Password = _config.Password | |
}; | |
var sessions = new List<Session> | |
{ | |
new Session | |
{ | |
Data = new Data | |
{ | |
BankInformation = bankInfo, | |
CardInformation = cardInfo, | |
ChecksRequired = checksRequired, | |
PersonalInformation = perInfo | |
}, | |
RID = _config.RID | |
}, | |
}; | |
var details = new CallValidateSendResult | |
{ | |
Authentication = auth, | |
Application = _config.Mode, | |
Sessions = sessions | |
}; | |
CallValidateResult result; | |
try | |
{ | |
var response = _callValidateDataService.ValidateAndSaveResult(data.ApplicationId, details); | |
result = new CallValidateResult | |
{ | |
BankCheckPassed = response.BankCheckPassed, | |
BankCheckScore = response.BankCheckScore, | |
CardCheckPassed = response.CardCheckPassed, | |
CardCheckScore = response.CardCheckScore, | |
CardType = response.CardType, | |
DeceasedCheckPassed = response.DeceasedCheckPassed, | |
PepCheckPassed = response.PepCheckPassed, | |
SanctionsCheckPassed = response.SanctionsCheckPassed, | |
SdnCheckPassed = response.SdnCheckPassed, | |
CifaCheckPassed = response.CifaCheckPassed, | |
ValidationResult = ValidateCardDetailsResult.Invalid | |
}; | |
if (response.BankCheckPassed && response.CardCheckPassed && response.DeceasedCheckPassed && | |
response.PepCheckPassed | |
&& response.SanctionsCheckPassed && response.SdnCheckPassed && response.CifaCheckPassed && | |
response.CardTypeCheckPassed) | |
result.ValidationResult = ValidateCardDetailsResult.Valid; | |
if (response.PepCheckPassed == false || response.CifaCheckPassed == false) | |
result.ValidationResult = ValidateCardDetailsResult.Referral; | |
} | |
catch (Exception e) | |
{ | |
_logger.Write("Error validating card details", LogSeverity.Error, e); | |
result = new CallValidateResult | |
{ | |
ValidationResult = ValidateCardDetailsResult.CardValidatorUnavailable | |
}; | |
} | |
result.ApplicantId = data.ApplicationId; | |
if ((result.ValidationResult == ValidateCardDetailsResult.Invalid || result.ValidationResult == ValidateCardDetailsResult.Referral) && data.AttemptNumber >= _config.MaximumAttempts) | |
result.ValidationResult = ValidateCardDetailsResult.MaximumAttemptsReached; | |
return result; | |
} | |
public bool ValidateExpiryDate(string cardExpiryDate, DateTime lastPaymentDate) | |
{ | |
if (cardExpiryDate == null || !CardDateUtil.FormatIsValid(cardExpiryDate)) return false; | |
DateTime expiryDate = CardDateUtil.ConvertToDateTime(cardExpiryDate, SetDayOfMonth.LastDayOfMonth); | |
var invalidExpiryDate = lastPaymentDate.AddMonths(3); //TODO: pass as variable | |
var inDate = expiryDate >= invalidExpiryDate; | |
return inDate; | |
} | |
private CallValidateResult GetReferredResult(int applicationId) | |
{ | |
var result = GetInvalidResult(applicationId); | |
result.ValidationResult = ValidateCardDetailsResult.Referral; | |
return result; | |
} | |
private CallValidateResult GetUnavailableResult(int applicationId) | |
{ | |
var result = GetInvalidResult(applicationId); | |
result.ValidationResult = ValidateCardDetailsResult.CardValidatorUnavailable; | |
return result; | |
} | |
private CallValidateResult GetInvalidResult(int applicationId) | |
{ | |
var result = new CallValidateResult | |
{ | |
ApplicantId = applicationId, | |
BankCheckPassed = false, | |
BankCheckScore = "0", | |
CardCheckPassed = false, | |
CardCheckScore = "0", | |
CardType = "Visa", | |
CifaCheckPassed = false, | |
DeceasedCheckPassed = false, | |
PepCheckPassed = false, | |
SanctionsCheckPassed = false, | |
SdnCheckPassed = false, | |
ValidationResult = ValidateCardDetailsResult.Invalid | |
}; | |
return result; | |
} | |
private CallValidateResult GetMaxAttemptsResult(int applicationId) | |
{ | |
var result = GetInvalidResult(applicationId); | |
result.ValidationResult = ValidateCardDetailsResult.MaximumAttemptsReached; | |
return result; | |
} | |
private CallValidateResult GetValidResult(int applicationId) | |
{ | |
var result = new CallValidateResult | |
{ | |
ApplicantId = applicationId, | |
BankCheckPassed = true, | |
BankCheckScore = "50", | |
CardCheckPassed = true, | |
CardCheckScore = "50", | |
CardType = "Visa", | |
CifaCheckPassed = true, | |
DeceasedCheckPassed = true, | |
PepCheckPassed = true, | |
SanctionsCheckPassed = true, | |
SdnCheckPassed = true, | |
ValidationResult = ValidateCardDetailsResult.Valid | |
}; | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment