예제 #1
0
 /// <summary>
 /// Validate the object.
 /// </summary>
 /// <exception cref="ValidationException">
 /// Thrown if validation fails
 /// </exception>
 public virtual void Validate()
 {
     if (CardNumber == null)
     {
         throw new ValidationException(ValidationRules.CannotBeNull, "CardNumber");
     }
     if (SecurityCode == null)
     {
         throw new ValidationException(ValidationRules.CannotBeNull, "SecurityCode");
     }
     if (CardHolderName == null)
     {
         throw new ValidationException(ValidationRules.CannotBeNull, "CardHolderName");
     }
     if (BillingAddress == null)
     {
         throw new ValidationException(ValidationRules.CannotBeNull, "BillingAddress");
     }
     if (Email == null)
     {
         throw new ValidationException(ValidationRules.CannotBeNull, "Email");
     }
     if (SecurityCode != null)
     {
         if (!System.Text.RegularExpressions.Regex.IsMatch(SecurityCode, "\\d{3,4}"))
         {
             throw new ValidationException(ValidationRules.Pattern, "SecurityCode", "\\d{3,4}");
         }
     }
     if (ExpirationMonth > 12)
     {
         throw new ValidationException(ValidationRules.InclusiveMaximum, "ExpirationMonth", 12);
     }
     if (ExpirationMonth < 1)
     {
         throw new ValidationException(ValidationRules.InclusiveMinimum, "ExpirationMonth", 1);
     }
     if (ExpirationYear > 2038)
     {
         throw new ValidationException(ValidationRules.InclusiveMaximum, "ExpirationYear", 2038);
     }
     if (ExpirationYear < 2018)
     {
         throw new ValidationException(ValidationRules.InclusiveMinimum, "ExpirationYear", 2018);
     }
     if (BillingAddress != null)
     {
         BillingAddress.Validate();
     }
 }
예제 #2
0
 /// <summary>
 /// Validate the object.
 /// </summary>
 /// <exception cref="ValidationException">
 /// Thrown if validation fails
 /// </exception>
 public virtual void Validate()
 {
     if (Token == null)
     {
         throw new ValidationException(ValidationRules.CannotBeNull, "Token");
     }
     if (CardHolderName == null)
     {
         throw new ValidationException(ValidationRules.CannotBeNull, "CardHolderName");
     }
     if (BillingAddress == null)
     {
         throw new ValidationException(ValidationRules.CannotBeNull, "BillingAddress");
     }
     if (Email == null)
     {
         throw new ValidationException(ValidationRules.CannotBeNull, "Email");
     }
     if (ExpirationMonth > 12)
     {
         throw new ValidationException(ValidationRules.InclusiveMaximum, "ExpirationMonth", 12);
     }
     if (ExpirationMonth < 1)
     {
         throw new ValidationException(ValidationRules.InclusiveMinimum, "ExpirationMonth", 1);
     }
     if (ExpirationYear > 2038)
     {
         throw new ValidationException(ValidationRules.InclusiveMaximum, "ExpirationYear", 2038);
     }
     if (ExpirationYear < 2018)
     {
         throw new ValidationException(ValidationRules.InclusiveMinimum, "ExpirationYear", 2018);
     }
     if (BillingAddress != null)
     {
         BillingAddress.Validate();
     }
 }
        public override void Validate(Validations validationType = Validations.Weak)
        {
            base.Validate(validationType);

            // All properties are optional, so we only validated when they're filled.

            if (LineItems != null)
            {
                LineItems.ToList().ForEach(item => item.Validate(validationType));
            }


            if (ShippingAddress != null)
            {
                ShippingLines.ToList().ForEach(item => item.Validate(validationType));
            }

            if (PaymentDetails != null && PaymentDetails.Length > 0)
            {
                PaymentDetails.ToList().ForEach(item => item.Validate(validationType));
            }

            if (NoChargeAmount != null)
            {
                NoChargeAmount.Validate(validationType);
            }

            if (validationType == Validations.Weak)
            {
                if (BillingAddress != null)
                {
                    BillingAddress.Validate(validationType);
                }
                else if (ShippingAddress != null)
                {
                    ShippingAddress.Validate(validationType);
                }
            }
            else
            {
                if (BillingAddress != null)
                {
                    BillingAddress.Validate(validationType);
                }
                if (ShippingAddress != null)
                {
                    ShippingAddress.Validate(validationType);
                }
            }

            if (Customer != null)
            {
                Customer.Validate(validationType);
            }
            if (!string.IsNullOrEmpty(Email))
            {
                InputValidators.ValidateEmail(Email);
            }
            if (!string.IsNullOrEmpty(CustomerBrowserIp))
            {
                InputValidators.ValidateIp(CustomerBrowserIp);
            }
            if (!string.IsNullOrEmpty(Currency))
            {
                InputValidators.ValidateCurrency(Currency);
            }
            if (TotalPrice.HasValue)
            {
                InputValidators.ValidateZeroOrPositiveValue(TotalPrice.Value, "Total Price");
            }

            if (CreatedAt != null)
            {
                InputValidators.ValidateDateNotDefault(CreatedAt.Value, "Created At");
            }
            if (UpdatedAt != null)
            {
                InputValidators.ValidateDateNotDefault(UpdatedAt.Value, "Updated At");
            }

            if (DiscountCodes != null && DiscountCodes.Length > 0)
            {
                DiscountCodes.ToList().ForEach(item => item.Validate(validationType));
            }
            if (TotalPriceUsd.HasValue)
            {
                InputValidators.ValidateZeroOrPositiveValue(TotalPriceUsd.Value, "Total Price USD");
            }
            if (TotalDiscounts.HasValue)
            {
                InputValidators.ValidateZeroOrPositiveValue(TotalDiscounts.Value, "Total Discounts");
            }
            if (ClosedAt.HasValue)
            {
                InputValidators.ValidateDateNotDefault(ClosedAt.Value, "Closed At");
            }

            if (Decision != null)
            {
                Decision.Validate(validationType);
            }
        }
예제 #4
0
파일: Order.cs 프로젝트: aflopes/sdk_net
        /// <summary>
        /// Validates the objects fields content
        /// </summary>
        /// <param name="isWeak">Should use weak validations or strong</param>
        /// <exception cref="OrderFieldBadFormatException">throws an exception if one of the parameters doesn't match the expected format</exception>
        public override void Validate(Validations validationType = Validations.Weak)
        {
            base.Validate(validationType);
            InputValidators.ValidateObjectNotNull(LineItems, "Line Items");
            LineItems.ToList().ForEach(item => item.Validate(validationType));
            InputValidators.ValidateObjectNotNull(ShippingLines, "Shipping Lines");
            ShippingLines.ToList().ForEach(item => item.Validate(validationType));
            if (PaymentDetails == null && NoChargeAmount == null)
            {
                throw new Exceptions.OrderFieldBadFormatException("Both PaymentDetails and NoChargeDetails are missing - at least one should be specified");
            }
            if (PaymentDetails != null)
            {
                PaymentDetails.Validate(validationType);
            }
            else
            {
                NoChargeAmount.Validate(validationType);
            }

            if (validationType == Validations.Weak)
            {
                if (BillingAddress == null && ShippingAddress == null)
                {
                    throw new Exceptions.OrderFieldBadFormatException("Both shipping and billing addresses are missing - at least one should be specified");
                }

                if (BillingAddress != null)
                {
                    BillingAddress.Validate(validationType);
                }
                else
                {
                    ShippingAddress.Validate(validationType);
                }
            }
            else
            {
                InputValidators.ValidateObjectNotNull(BillingAddress, "Billing Address");
                BillingAddress.Validate(validationType);
                InputValidators.ValidateObjectNotNull(ShippingAddress, "Shipping Address");
                ShippingAddress.Validate(validationType);
            }

            InputValidators.ValidateObjectNotNull(Customer, "Customer");
            Customer.Validate(validationType);
            InputValidators.ValidateEmail(Email);
            InputValidators.ValidateIp(CustomerBrowserIp);
            InputValidators.ValidateCurrency(Currency);
            InputValidators.ValidateZeroOrPositiveValue(TotalPrice.Value, "Total Price");
            InputValidators.ValidateValuedString(Gateway, "Gateway");
            InputValidators.ValidateDateNotDefault(CreatedAt.Value, "Created At");
            InputValidators.ValidateDateNotDefault(UpdatedAt.Value, "Updated At");

            // optional fields validations
            if (DiscountCodes != null && DiscountCodes.Length > 0)
            {
                DiscountCodes.ToList().ForEach(item => item.Validate(validationType));
            }
            if (TotalPriceUsd.HasValue)
            {
                InputValidators.ValidateZeroOrPositiveValue(TotalPriceUsd.Value, "Total Price USD");
            }
            if (TotalDiscounts.HasValue)
            {
                InputValidators.ValidateZeroOrPositiveValue(TotalDiscounts.Value, "Total Discounts");
            }
            if (ClosedAt.HasValue)
            {
                InputValidators.ValidateDateNotDefault(ClosedAt.Value, "Closed At");
            }
        }