/// <summary>
        /// Gets the supported card types based on a provided card type list. If no card type list is provided, return the full supported list.
        /// </summary>
        /// <param name="chosenCardTypes">The provided card types.</param>
        /// <returns>The supported card types.</returns>
        public static string GetSupportedCardTypes(string chosenCardTypes)
        {
            string[] chosenCardTypeArray = CardTypes.ToArray(chosenCardTypes);

            if (chosenCardTypeArray == null || chosenCardTypeArray.Length == 0)
            {
                // Return the full list of supported card types
                return(CardTypes.ToString(CardTypes.supportedCardTypes));
            }
            else
            {
                // Filter unsupported card types
                var supportedChosenCardTypes = new List <string>();

                foreach (var chosenCardType in chosenCardTypeArray)
                {
                    foreach (var supportedCardType in CardTypes.supportedCardTypes)
                    {
                        if (supportedCardType.Equals(chosenCardType, StringComparison.OrdinalIgnoreCase))
                        {
                            supportedChosenCardTypes.Add(supportedCardType);
                        }
                    }
                }

                return(CardTypes.ToString(supportedChosenCardTypes.ToArray()));
            }
        }
Beispiel #2
0
        private static CardPaymentEntry ConvertRequestToCardPaymentEntry(Request request, List <PaymentError> errorList)
        {
            string locale = request.Locale;

            if (string.IsNullOrWhiteSpace(locale))
            {
                errorList.Add(new PaymentError(ErrorCode.LocaleNotSupported, "Locale is not specified."));
            }

            var requestProperties = PaymentProperty.ConvertToHashtable(request.Properties);

            string serviceAccountId = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.MerchantAccount,
                MerchantAccountProperties.ServiceAccountId,
                required: true,
                errors: errorList);

            string industryType = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.TransactionData,
                TransactionDataProperties.IndustryType,
                required: false,
                errors: errorList);
            IndustryType industryTypeEnum;

            if (string.IsNullOrEmpty(industryType) || !Enum.TryParse(industryType, true, out industryTypeEnum))
            {
                industryTypeEnum = IndustryType.Ecommerce;
            }

            string transactionType = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.TransactionData,
                TransactionDataProperties.TransactionType,
                required: true,
                errors: errorList);
            TransactionType transactionTypeEnum = TransactionType.None;

            if (!Enum.TryParse(transactionType, true, out transactionTypeEnum) ||
                (transactionTypeEnum != TransactionType.None && transactionTypeEnum != TransactionType.Authorize && transactionTypeEnum != TransactionType.Capture))
            {
                errorList.Add(new PaymentError(ErrorCode.InvalidRequest, "The transaction type is not suppoted."));
            }

            string supportCardSwipeString = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.TransactionData,
                TransactionDataProperties.SupportCardSwipe,
                required: false,
                errors: errorList);
            bool supportCardSwipe = false;

            if (!string.IsNullOrWhiteSpace(supportCardSwipeString))
            {
                bool.TryParse(supportCardSwipeString, out supportCardSwipe);
            }

            string supportCardTokenizationString = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.TransactionData,
                TransactionDataProperties.SupportCardTokenization,
                required: false,
                errors: errorList);
            bool supportCardTokenization = false;

            if (!string.IsNullOrWhiteSpace(supportCardTokenizationString))
            {
                bool.TryParse(supportCardTokenizationString, out supportCardTokenization);
            }

            // When transaction type is None, support card tokenization must be enabled.
            if (transactionTypeEnum == TransactionType.None && !supportCardTokenization)
            {
                errorList.Add(new PaymentError(ErrorCode.InvalidRequest, "When transaction type is None, support card tokenization must be enabled."));
            }

            string allowVoiceAuthorizationString = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.TransactionData,
                TransactionDataProperties.AllowVoiceAuthorization,
                required: false,
                errors: errorList);
            bool allowVoiceAuthorization = false;

            if (!string.IsNullOrWhiteSpace(allowVoiceAuthorizationString))
            {
                bool.TryParse(allowVoiceAuthorizationString, out allowVoiceAuthorization);
            }

            string hostPageOrigin = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.TransactionData,
                TransactionDataProperties.HostPageOrigin,
                required: true,
                errors: errorList);

            if (string.IsNullOrWhiteSpace(hostPageOrigin))
            {
                errorList.Add(new PaymentError(ErrorCode.InvalidRequest, "The host page origin is not specified."));
            }

            string cardTypes = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.PaymentCard,
                PaymentCardProperties.CardType,
                required: false,
                errors: errorList);

            string defaultCardHolderName = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.PaymentCard,
                PaymentCardProperties.Name,
                required: false,
                errors: errorList);

            string defaultStreet1 = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.PaymentCard,
                PaymentCardProperties.StreetAddress,
                required: false,
                errors: errorList);

            string defaultStreet2 = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.PaymentCard,
                PaymentCardProperties.StreetAddress2,
                required: false,
                errors: errorList);

            string defaultCity = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.PaymentCard,
                PaymentCardProperties.City,
                required: false,
                errors: errorList);

            string defaultStateOrProvince = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.PaymentCard,
                PaymentCardProperties.State,
                required: false,
                errors: errorList);

            string defaultPostalCode = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.PaymentCard,
                PaymentCardProperties.PostalCode,
                required: false,
                errors: errorList);

            string defaultCountryCode = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.PaymentCard,
                PaymentCardProperties.Country,
                required: false,
                errors: errorList);

            string showSameAsShippingAddressString = GetPropertyStringValue(
                requestProperties,
                GenericNamespace.PaymentCard,
                PaymentCardProperties.ShowSameAsShippingAddress,
                required: false,
                errors: errorList);

            bool showSameAsShippingAddress = false;

            if (!string.IsNullOrWhiteSpace(showSameAsShippingAddressString))
            {
                bool.TryParse(showSameAsShippingAddressString, out showSameAsShippingAddress);
            }

            string entryData = JsonConvert.SerializeObject(request);

            // Create the request in database with an unique entry ID
            var cardPaymentEntry = new CardPaymentEntry()
            {
                AllowVoiceAuthorization = allowVoiceAuthorization,
                CardTypes              = CardTypes.GetSupportedCardTypes(cardTypes),
                DefaultCardHolderName  = defaultCardHolderName,
                DefaultCity            = defaultCity,
                DefaultCountryCode     = defaultCountryCode,
                DefaultPostalCode      = defaultPostalCode,
                DefaultStateOrProvince = defaultStateOrProvince,
                DefaultStreet1         = defaultStreet1,
                DefaultStreet2         = defaultStreet2,
                EntryData              = entryData,
                EntryId                   = CommonUtility.NewGuid().ToString(),
                EntryLocale               = locale,
                EntryUtcTime              = DateTime.UtcNow,
                HostPageOrigin            = hostPageOrigin,
                IndustryType              = industryTypeEnum.ToString(),
                ServiceAccountId          = serviceAccountId,
                ShowSameAsShippingAddress = showSameAsShippingAddress,
                SupportCardSwipe          = supportCardSwipe,
                SupportCardTokenization   = supportCardTokenization,
                TransactionType           = transactionType,
                Used = false,
            };

            return(cardPaymentEntry);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // Enable Right-to-left for some cultures
            this.TextDirection = Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft ? "rtl" : "ltr";

            // Load custom styles
            this.LoadCustomStyles();

            // Check payment entry ID
            if (string.IsNullOrEmpty(this.entryId))
            {
                this.ShowErrorMessage(WebResources.CardPage_Error_MissingRequestId);
                return;
            }

            // Check payment entry
            if (this.entry == null)
            {
                this.ShowErrorMessage(WebResources.CardPage_Error_InvalidRequestId);
            }
            else if (this.entry.Used)
            {
                this.ShowErrorMessage(WebResources.CardPage_Error_UsedRequest);
            }
            else if (this.entry.IsExpired)
            {
                this.ShowErrorMessage(WebResources.CardPage_Error_RequestTimedOut);
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    bool isRetail = IndustryType.Retail.ToString().Equals(this.entry.IndustryType, StringComparison.OrdinalIgnoreCase);
                    this.CardDetailsHeaderPanel.Visible = !isRetail;

                    // Load card entry modes
                    this.CardEntryModePanel.Visible = this.entry.SupportCardSwipe || this.entry.AllowVoiceAuthorization;
                    if (this.CardEntryModePanel.Visible)
                    {
                        if (this.entry.SupportCardSwipe)
                        {
                            this.CardEntryModeDropDownList.Items.Add(new ListItem(WebResources.CardPage_CardEntryModeDropDownList_Swipe, "swipe"));
                        }

                        this.CardEntryModeDropDownList.Items.Add(new ListItem(WebResources.CardPage_CardEntryModeDropDownList_Manual, "manual"));

                        if (this.entry.AllowVoiceAuthorization)
                        {
                            this.CardEntryModeDropDownList.Items.Add(new ListItem(WebResources.CardPage_CardEntryModeDropDownList_Voice, "voice"));
                        }
                    }

                    this.CardHolderNamePanel.Visible = !isRetail;
                    this.CardTypePanel.Visible       = !isRetail;

                    // Load card types
                    if (this.CardTypePanel.Visible)
                    {
                        this.CardTypeDropDownList.Items.Add(new ListItem(WebResources.CardPage_CardTypeDropDownList_PleaseSelect, string.Empty));
                        string[] cardTypes = CardTypes.ToArray(this.entry.CardTypes);
                        foreach (var ct in cardTypes)
                        {
                            if (CardTypes.Amex.Equals(ct, StringComparison.OrdinalIgnoreCase))
                            {
                                this.CardTypeDropDownList.Items.Add(new ListItem(WebResources.CardPage_CardTypeDropDownList_AmericanExpress, CardTypes.Amex));
                            }
                            else if (CardTypes.Discover.Equals(ct, StringComparison.OrdinalIgnoreCase))
                            {
                                this.CardTypeDropDownList.Items.Add(new ListItem(WebResources.CardPage_CardTypeDropDownList_Discover, CardTypes.Discover));
                            }
                            else if (CardTypes.MasterCard.Equals(ct, StringComparison.OrdinalIgnoreCase))
                            {
                                this.CardTypeDropDownList.Items.Add(new ListItem(WebResources.CardPage_CardTypeDropDownList_MasterCard, CardTypes.MasterCard));
                            }
                            else if (CardTypes.Visa.Equals(ct, StringComparison.OrdinalIgnoreCase))
                            {
                                this.CardTypeDropDownList.Items.Add(new ListItem(WebResources.CardPage_CardTypeDropDownList_Visa, CardTypes.Visa));
                            }
                        }
                    }

                    // Load month list
                    this.ExpirationMonthDropDownList.Items.Add(new ListItem(WebResources.CardPage_ExpirationMonthDropDownList_PleaseSelect, "0"));
                    string[] monthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
                    for (int i = 1; i <= 12; i++)
                    {
                        this.ExpirationMonthDropDownList.Items.Add(new ListItem(monthNames[i - 1], string.Format(CultureInfo.InvariantCulture, "{0}", i)));
                    }

                    // Load year list
                    this.ExpirationYearDropDownList.Items.Add(new ListItem(WebResources.CardPage_ExpirationYearDropDownList_PleaseSelect));
                    int currentYear = DateTime.UtcNow.Year;
                    for (int i = 0; i < 20; i++)
                    {
                        this.ExpirationYearDropDownList.Items.Add(new ListItem(string.Format(CultureInfo.InvariantCulture, "{0}", currentYear + i)));
                    }

                    // Show/hide security code and voice authorization code
                    this.SecurityCodePanel.Visible           = false;
                    this.VoiceAuthorizationCodePanel.Visible = false;
                    TransactionType transactionType = (TransactionType)Enum.Parse(typeof(TransactionType), this.entry.TransactionType, true);
                    if (transactionType == TransactionType.Authorize || transactionType == TransactionType.Capture)
                    {
                        this.SecurityCodePanel.Visible = true;

                        if (this.entry.AllowVoiceAuthorization)
                        {
                            this.VoiceAuthorizationCodePanel.Visible = true;
                        }
                    }

                    this.ZipPanel.Visible            = isRetail;
                    this.BillingAddressPanel.Visible = !isRetail;
                    if (this.BillingAddressPanel.Visible)
                    {
                        // Load country list
                        // Note: the value of country/region must be two-letter ISO code.
                        // TO DO: Filter the countries down to the list you support.
                        this.CountryRegionDropDownList.Items.Add(new ListItem(WebResources.CardPage_CountryRegionDropDownList_PleaseSelect, string.Empty));
                        var dataManager = new DataManager();
                        IEnumerable <CountryOrRegion> countries = dataManager.GetCountryRegionListByLocale(this.entry.EntryLocale);
                        countries = countries.OrderBy(c => c.LongName).ToList();
                        foreach (var country in countries)
                        {
                            this.CountryRegionDropDownList.Items.Add(new ListItem(country.LongName, country.TwoLetterCode));
                        }

                        // Populate default values
                        if (this.CardHolderNamePanel.Visible)
                        {
                            this.CardHolderNameTextBox.Text = this.entry.DefaultCardHolderName;
                        }

                        if (this.entry.ShowSameAsShippingAddress)
                        {
                            this.SameAsShippingPanel.Visible           = true;
                            this.DefaultStreetHiddenField.Value        = this.entry.DefaultStreet1;
                            this.DefaultCityHiddenField.Value          = this.entry.DefaultCity;
                            this.DefaultStateProvinceHiddenField.Value = this.entry.DefaultStateOrProvince;
                            this.DefaultZipHiddenField.Value           = this.entry.DefaultPostalCode;
                            this.DefaultCountryRegionHiddenField.Value = this.entry.DefaultCountryCode;
                        }
                        else
                        {
                            this.SameAsShippingPanel.Visible             = false;
                            this.StreetTextBox.Text                      = this.entry.DefaultStreet1;
                            this.CityTextBox.Text                        = this.entry.DefaultCity;
                            this.StateProvinceTextBox.Text               = this.entry.DefaultStateOrProvince;
                            this.ZipTextBox1.Text                        = this.entry.DefaultPostalCode;
                            this.CountryRegionDropDownList.SelectedValue = this.entry.DefaultCountryCode;
                        }
                    }

                    this.HostPageOriginHiddenField.Value = this.entry.HostPageOrigin;
                }
                else
                {
                    bool.TryParse(this.IsSwipeHiddenField.Value, out this.isSwipe);

                    // Validate inputs
                    if (this.ValidateInputs())
                    {
                        if (this.isSwipe)
                        {
                            this.track1     = this.CardTrack1HiddenField.Value;
                            this.track2     = this.CardTrack2HiddenField.Value;
                            this.cardNumber = this.CardNumberHiddenField.Value;
                        }
                        else
                        {
                            this.cardNumber = this.CardNumberTextBox.Text.Trim();
                        }

                        if (this.CardTypePanel.Visible)
                        {
                            this.cardType = this.CardTypeDropDownList.SelectedItem.Value;
                        }
                        else
                        {
                            this.cardType = this.CardTypeHiddenField.Value;
                        }

                        this.cardExpirationMonth    = int.Parse(this.ExpirationMonthDropDownList.SelectedItem.Value, CultureInfo.InvariantCulture);
                        this.cardExpirationYear     = int.Parse(this.ExpirationYearDropDownList.SelectedItem.Text, CultureInfo.InvariantCulture);
                        this.cardSecurityCode       = this.SecurityCodeTextBox.Text.Trim();
                        this.voiceAuthorizationCode = this.VoiceAuthorizationCodeTextBox.Text.Trim();

                        if (this.CardHolderNamePanel.Visible)
                        {
                            this.cardHolderName = this.CardHolderNameTextBox.Text.Trim();
                        }
                        else
                        {
                            this.cardHolderName = this.CardHolderNameHiddenField.Value.Trim();
                        }

                        this.cardStreet1         = this.StreetTextBox.Text.Trim();
                        this.cardCity            = this.CityTextBox.Text.Trim();
                        this.cardStateOrProvince = this.StateProvinceTextBox.Text.Trim();

                        if (this.BillingAddressPanel.Visible)
                        {
                            this.cardPostalCode = this.ZipTextBox1.Text.Trim();
                        }
                        else
                        {
                            this.cardPostalCode = this.ZipTextBox2.Text.Trim();
                        }

                        this.cardCountryOrRegion = this.CountryRegionDropDownList.SelectedValue;

                        if (!string.IsNullOrEmpty(this.PaymentAmountHiddenField.Value))
                        {
                            this.paymentAmount = decimal.Parse(this.PaymentAmountHiddenField.Value, NumberStyles.Number, Thread.CurrentThread.CurrentCulture);
                        }

                        // Process payment, e.g. tokenize, authorize, capture.
                        try
                        {
                            CardPaymentResult result = this.ProcessPayment(this.entry);

                            if (result != null)
                            {
                                // Save the payment result
                                var dataManager = new DataManager();
                                dataManager.CreateCardPaymentResult(result);

                                // Set request access code in hidden field
                                this.ResultAccessCodeHiddenField.Value = result.ResultAccessCode;
                            }
                        }
                        catch (CardPaymentException ex)
                        {
                            // Return the errors from UX
                            this.InputErrorsHiddenField.Value = JsonConvert.SerializeObject(ex.PaymentErrors);
                        }
                    }
                }
            }
        }