Пример #1
0
        /// <summary>
        /// Checks whether or not the <see cref="CVC"/> property is valid.
        /// </summary>
        /// <returns><see langword="true"> if valid, <see langword="false"> otherwise</returns>
        public bool ValidateCVC()
        {
            if (string.IsNullOrWhiteSpace(CVC))
            {
                return(false);
            }
            String cvcValue    = CVC.Trim();
            String updatedType = GetBrand();
            bool   validLength =
                (updatedType == null && cvcValue.Length >= 3 && cvcValue.Length <= 4) ||
                (AMERICAN_EXPRESS.Equals(updatedType) && cvcValue.Length == 4) ||
                cvcValue.Length == 3;

            return(StripeTextUtils.IsWholePositiveNumber(cvcValue) && validLength);
        }
Пример #2
0
        //@NonNull
        //@CardBrand
        private static string GetPossibleCardType(string cardNumber, bool shouldNormalize)
        {
            if (string.IsNullOrWhiteSpace(cardNumber))
            {
                return(Card.UNKNOWN);
            }

            var spacelessCardNumber = cardNumber;

            if (shouldNormalize)
            {
                spacelessCardNumber = StripeTextUtils.RemoveSpacesAndHyphens(cardNumber);
            }

            if (StripeTextUtils.HasAnyPrefix(spacelessCardNumber, Card.PREFIXES_AMERICAN_EXPRESS))
            {
                return(Card.AMERICAN_EXPRESS);
            }
            else if (StripeTextUtils.HasAnyPrefix(spacelessCardNumber, Card.PREFIXES_DISCOVER))
            {
                return(Card.DISCOVER);
            }
            else if (StripeTextUtils.HasAnyPrefix(spacelessCardNumber, Card.PREFIXES_JCB))
            {
                return(Card.JCB);
            }
            else if (StripeTextUtils.HasAnyPrefix(spacelessCardNumber, Card.PREFIXES_DINERS_CLUB))
            {
                return(Card.DINERS_CLUB);
            }
            else if (StripeTextUtils.HasAnyPrefix(spacelessCardNumber, Card.PREFIXES_VISA))
            {
                return(Card.VISA);
            }
            else if (StripeTextUtils.HasAnyPrefix(spacelessCardNumber, Card.PREFIXES_MASTERCARD))
            {
                return(Card.MASTERCARD);
            }
            else
            {
                return(Card.UNKNOWN);
            }
        }
Пример #3
0
        /// <summary>
        /// Card constructor with all available fields.
        /// </summary>
        /// <param name="number">the card number</param>
        /// <param name="expMonth">the expiry month</param>
        /// <param name="expYear">the expiry year</param>
        /// <param name="cvc">the CVC code</param>
        /// <param name="name">the cardholder name</param>
        /// <param name="addressLine1">the first line of the billing address</param>
        /// <param name="addressLine2">the second line of the billing address</param>
        /// <param name="addressCity">the city of the billing address</param>
        /// <param name="addressState">the state of the billing address</param>
        /// <param name="addressZip">the zip code of the billing address</param>
        /// <param name="addressCountry">the country of the billing address</param>
        /// <param name="brand">brand of this card</param>
        /// <param name="last4">last 4 digits of the card</param>
        /// <param name="fingerprint">the card fingerprint</param>
        /// <param name="funding">the funding type of the card</param>
        /// <param name="country">ISO country code of the card itself</param>
        /// <param name="currency">the currency of the card</param>
        /// <param name="id">the cardId</param>
        public Card(
            String number,
            int expMonth,
            int expYear,
            string cvc,
            string name,
            string addressLine1,
            string addressLine2,
            string addressCity,
            string addressState,
            string addressZip,
            string addressCountry,
            string brand,
            string last4,
            string fingerprint,
            string funding,
            string country,
            string currency,
            string id)
        {
            Number         = StripeTextUtils.NullIfBlank(NormalizeCardNumber(number));
            ExpiryMonth    = expMonth;
            ExpiryYear     = expYear;
            CVC            = StripeTextUtils.NullIfBlank(cvc);
            Name           = StripeTextUtils.NullIfBlank(name);
            AddressLine1   = StripeTextUtils.NullIfBlank(addressLine1);
            AddressLine2   = StripeTextUtils.NullIfBlank(addressLine2);
            AddressCity    = StripeTextUtils.NullIfBlank(addressCity);
            AddressState   = StripeTextUtils.NullIfBlank(addressState);
            AddressZip     = StripeTextUtils.NullIfBlank(addressZip);
            AddressCountry = StripeTextUtils.NullIfBlank(addressCountry);
            Brand          = StripeTextUtils.AsCardBrand(brand) == null?GetBrand() : brand;

            Last4 = StripeTextUtils.NullIfBlank(last4) == null?GetLast4() : last4;

            Fingerprint = StripeTextUtils.NullIfBlank(fingerprint);
            Funding     = StripeTextUtils.AsFundingType(funding);
            Country     = StripeTextUtils.NullIfBlank(country);
            Currency    = StripeTextUtils.NullIfBlank(currency);
            Id          = StripeTextUtils.NullIfBlank(id);
        }
Пример #4
0
        /// <summary>
        /// Gets the <see cref="Brand"/> of this card. Updates the value if none has yet been set, or
        /// if the <see cref="Number"/> has been changed.
        /// </summary>
        /// <returns>the <see cref="Brand"/> of this card</returns>
        public string GetBrand()
        {
            if (string.IsNullOrWhiteSpace(Brand) && !string.IsNullOrWhiteSpace(Number))
            {
                /*@CardBrand*/
                String evaluatedType;
                if (StripeTextUtils.HasAnyPrefix(Number, PREFIXES_AMERICAN_EXPRESS))
                {
                    evaluatedType = AMERICAN_EXPRESS;
                }
                else if (StripeTextUtils.HasAnyPrefix(Number, PREFIXES_DISCOVER))
                {
                    evaluatedType = DISCOVER;
                }
                else if (StripeTextUtils.HasAnyPrefix(Number, PREFIXES_JCB))
                {
                    evaluatedType = JCB;
                }
                else if (StripeTextUtils.HasAnyPrefix(Number, PREFIXES_DINERS_CLUB))
                {
                    evaluatedType = DINERS_CLUB;
                }
                else if (StripeTextUtils.HasAnyPrefix(Number, PREFIXES_VISA))
                {
                    evaluatedType = VISA;
                }
                else if (StripeTextUtils.HasAnyPrefix(Number, PREFIXES_MASTERCARD))
                {
                    evaluatedType = MASTERCARD;
                }
                else
                {
                    evaluatedType = UNKNOWN;
                }

                Brand = evaluatedType;
            }

            return(Brand);
        }
Пример #5
0
        /// <summary>
        /// A utility function to map the fields of a <see cref="Card"/> object into a <see langword="Dictionary"/> we
        /// can use in network communications.
        /// </summary>
        /// <param name="card">the <see cref="Card"/> to be read</param>
        /// <returns>a <see langword="Dictionary"/> containing the appropriate values read from the card</returns>
        public static Dictionary <string, string> HashMapFromCard(Card card)
        {
            var cardParams = new Dictionary <string, string>();

            cardParams["card[number]"]          = StripeTextUtils.NullIfBlank(card.Number);
            cardParams["card[cvc]"]             = StripeTextUtils.NullIfBlank(card.CVC);
            cardParams["card[exp_month]"]       = card.ExpiryMonth.ToString();
            cardParams["card[exp_year]"]        = card.ExpiryYear.ToString();
            cardParams["card[name]"]            = StripeTextUtils.NullIfBlank(card.Name);
            cardParams["card[currency]"]        = StripeTextUtils.NullIfBlank(card.Currency);
            cardParams["card[address_line1]"]   = StripeTextUtils.NullIfBlank(card.AddressLine1);
            cardParams["card[address_line2]"]   = StripeTextUtils.NullIfBlank(card.AddressLine2);
            cardParams["card[address_city]"]    = StripeTextUtils.NullIfBlank(card.AddressCity);
            cardParams["card[address_zip]"]     = StripeTextUtils.NullIfBlank(card.AddressZip);
            cardParams["card[address_state]"]   = StripeTextUtils.NullIfBlank(card.AddressState);
            cardParams["card[address_country]"] = StripeTextUtils.NullIfBlank(card.AddressCountry);

            // Remove all null values; they cause validation errors
            RemoveNullParams(cardParams);

            return(cardParams);
        }
Пример #6
0
        public static bool IsValidCardNumber(string cardNumber)
        {
            var normalizedNumber = StripeTextUtils.RemoveSpacesAndHyphens(cardNumber);

            return(IsValidLuhnNumber(normalizedNumber) && IsValidCardLength(normalizedNumber));
        }