using gehGassi.Dto.Common;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
using gehGassi.Domain.Common;
namespace gehGassi.Domain.Payment
{
///
/// Objekt für das Anlegen oder Aktualisieren einer Bankverbindung eines AppUsers
///
public class BankAccount
{
///
/// ID des AppUsers
///
[Required]
[MaxLength(128)]
public string AppUserId { get; set; }
///
/// ID des AppUsers bei MangoPay
///
[MaxLength(128)]
public string PaymentId { get; set; }
///
/// ID der Bankverbindung bei MangoPay
///
[MaxLength(128)]
public string BankId { get; set; }
///
/// Name des Inhabers der Bankverbindung
///
[Required]
[MaxLength(255)]
public string OwnerName { get; set; }
///
/// Adresse des Inhabers der Bankverbindung
///
[Required]
public Address Address { get; set; }
///
/// Iban der Bankverbindung
///
[Required]
[MaxLength(255)]
public string Iban { get; set; }
///
/// Bic der Bankverbindung
///
[MaxLength(255)]
public string Bic { get; set; }
///
/// Gibt zurück ob der Iban gültig ist
///
/// true wenn gültig, false sonst
public bool IsIbanValid()
{
if (string.IsNullOrEmpty(Iban))
return false;
var iban = Iban.ToUpper().Replace(" ", "").Replace("-", "");
if (!Regex.IsMatch(iban, @"^[A-Z]{2}[0-9]{2}[A-Z0-9]{4}[0-9]{7}([A-Z0-9]?){0,16}$"))
return false;
var ibanWithoutCountryAndChecksum = iban.Substring(4) + iban.Substring(0, 4);
var ibanWithoutCountryAndChecksumAsNumber = string.Concat(ibanWithoutCountryAndChecksum.Select(c => char.IsLetter(c) ? c - 55 : c - '0'));
var checksum = BigInteger.Parse(ibanWithoutCountryAndChecksumAsNumber) % 97;
return checksum == 1;
}
///
/// Gibt zurück ob der Bic gültig ist.
/// Der BIC ist optional und wird nur geprüft wenn er nicht leer ist
///
/// true wenn gültig, false sonst
public bool IsBicValid()
{
if (string.IsNullOrWhiteSpace(Bic))
return true;
var bic = Bic.ToUpper().Replace(" ", "").Replace("-", "");
if (!Regex.IsMatch(bic, @"^[A-Z]{6}[A-Z2-9][A-NP-Z0-9]([A-Z0-9]{3})?$"))
return false;
return true;
}
///
/// Gibt zurück ob die Bankverbindung geändert wurde
///
/// Bankverbindung für den Vergleich
/// true wenn geändert, false sonst
public bool HasChanged(BankAccount bankAccount)
{
if (bankAccount == null)
return true;
if(OwnerName != bankAccount.OwnerName)
return true;
if(Iban.ToUpper().Replace(" ", "").Replace("-", "") != bankAccount.Iban.ToUpper().Replace(" ", "").Replace("-", ""))
return true;
if(Bic.ToUpper().Replace(" ", "").Replace("-", "") != bankAccount.Bic.ToUpper().Replace(" ", "").Replace("-", ""))
return true;
if(Address.AddressLine1 != bankAccount.Address.AddressLine1)
return true;
if(Address.AddressLine2 != bankAccount.Address.AddressLine2)
return true;
if(Address.City != bankAccount.Address.City)
return true;
if(Address.CountryCode != bankAccount.Address.CountryCode)
return true;
if(Address.Zip != bankAccount.Address.Zip)
return true;
if(Address.State != bankAccount.Address.State)
return true;
return false;
}
}
}