/// <summary>
        /// בדיקת מס' כרטיס אשראי כולל ישראכארד
        /// http://halemo.net/info/isracard/index.html
        /// </summary>
        /// <param name="creditCard">מס' כרטיס האשראי</param>
        /// <param name="isIsraCard">האם כרטיס אשראי ישראכארד</param>
        /// <param name="notEmpty">האם חייב להיות מלא</param>
        /// <returns>תקין/שגוי</returns>
        public static ErrorStatus CheckCreditCardNumber(string creditCard, CreditCardCompanies company, bool notEmpty)
        {
            ErrorStatus status = ErrorStatus.NONE;

            if (company != CreditCardCompanies.ISRACARD)
            {
                status = CheckCreditCardNumber(creditCard, notEmpty);

                if (status == ErrorStatus.NONE)
                {
                    string cc = JustDigits(creditCard);

                    switch (company)
                    {
                    case CreditCardCompanies.AMERICANEXPRESS:
                    {
                        if (cc.Length != 15 || !(new List <string> {
                                "24", "37"
                            }).Contains(cc.Substring(0, 2)))
                        {
                            status = ErrorStatus.ERROR;
                        }
                        break;
                    }

                    case CreditCardCompanies.DINERS:
                    {
                        if (cc.Length != 14 || !(new List <string> {
                                "30", "36", "38"
                            }).Contains(cc.Substring(0, 2)))
                        {
                            status = ErrorStatus.ERROR;
                        }
                        break;
                    }

                    case CreditCardCompanies.MASTERCARD:
                    {
                        if (cc.Length != 16 || !(new List <string> {
                                "51", "52", "53", "54", "55"
                            }).Contains(cc.Substring(0, 2)))
                        {
                            status = ErrorStatus.ERROR;
                        }
                        break;
                    }

                    case CreditCardCompanies.VISA:
                    case CreditCardCompanies.VISA_LEUMI:
                    {
                        if (cc.Length != 16 || cc[0] != '4')
                        {
                            status = ErrorStatus.ERROR;
                        }
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }
            }
            else
            {
                string cc = JustDigits(creditCard);

                if (cc.Length < 8 || cc.Length > 9)
                {
                    status = ErrorStatus.ERROR;
                }
                else
                {
                    if (cc.Length == 8)
                    {
                        cc = "0" + creditCard;
                    }

                    int multiplier  = 1;
                    int sumOfDigits = 0;

                    for (int i = cc.Length - 1; i >= 0; i--)
                    {
                        sumOfDigits += (cc[i] - '0') * multiplier++;
                    }

                    status = (sumOfDigits % 11 == 0) ? ErrorStatus.NONE : ErrorStatus.ERROR;
                }
            }

            return(status);
        }
Beispiel #2
0
        private void DetermineCompany(string cardNumber)
        {
            var visaRgx = new Regex(VisaPattern, RegexOptions.IgnoreCase);
            var matches = visaRgx.Matches(cardNumber);

            if (matches.Count > 0)
            {
                if (_visaElectronFirstNumbers.Any(x => cardNumber.StartsWith(x)) &&
                    cardNumber.Count() == 16)
                {
                    var id = CreditCardCompanies.Find(x => x.Display == VisaElectron).Id;
                    if (id != null)
                    {
                        CreditCardType = (int)id;
                    }
                }
                else
                {
                    var id = CreditCardCompanies.Find(x => x.Display == Visa).Id;
                    if (id != null)
                    {
                        CreditCardType = (int)id;
                    }
                }
            }
            else
            {
                var masterRgx = new Regex(MasterPattern, RegexOptions.IgnoreCase);
                matches = masterRgx.Matches(cardNumber);
                if (matches.Count > 0)
                {
                    var id = CreditCardCompanies.Find(x => x.Display == MasterCard).Id;
                    if (id != null)
                    {
                        CreditCardType = (int)id;
                    }
                }
                else
                {
                    var amexRgx = new Regex(AmexPattern, RegexOptions.IgnoreCase);
                    matches = amexRgx.Matches(cardNumber);
                    if (matches.Count > 0)
                    {
                        var id = CreditCardCompanies.Find(x => x.Display == Amex).Id;
                        if (id != null)
                        {
                            CreditCardType = (int)id;
                        }
                    }
                    else
                    {
                        var discoverRgx = new Regex(DiscoverPattern, RegexOptions.IgnoreCase);
                        matches = discoverRgx.Matches(cardNumber);
                        if (matches.Count > 0)
                        {
                            var id = CreditCardCompanies.Find(x => x.Display == Discover).Id;
                            if (id != null)
                            {
                                CreditCardType = (int)id;
                            }
                        }
                        else
                        {
                            var i = CreditCardCompanies.Find(x => x.Display == CreditCardGeneric).Id;
                            if (i != null)
                            {
                                CreditCardType = (int)i;
                            }
                        }
                    }
                }
            }
        }