/// <summary> /// Converts a payment request to a card payment entry. /// </summary> /// <param name="request">The payment request.</param> /// <param name="errorList">The errors during conversion.</param> /// <returns>The card payment entry.</returns> 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); }