internal static string ToValidApiString(this WalletPaymentType walletRecordState)
        {
            var defaultString = walletRecordState.ToString();
            var jsonString    = char.ToLowerInvariant(defaultString[0]) + defaultString.Substring(1);

            for (var i = 0; i < jsonString.Length; i++)
            {
                if (char.IsUpper(jsonString[i]))
                {
                    var @char = char.ToLowerInvariant(jsonString[i]);
                    jsonString = jsonString.Remove(i, 1);
                    jsonString = jsonString.Insert(i, $"_{@char}");
                }
            }

            return(jsonString);
        }
        WalletEditViewModel BuildWalletEditViewModel(Customer customer, WalletPaymentType model = null)
        {
            var walletEditViewModel = new WalletEditViewModel(
                dateExpirationMonthOptions: BuildMonthOptionsSelectList(),
                dateExpirationYearOptions: BuildYearOptionsSelectList(),
                billingAddressOptions: BuildBillingAddressesSelectList(customer.CustomerID));

            if (model != null)
            {
                walletEditViewModel.BillingAddressId = model.BillingAddressId;
                walletEditViewModel.CardImage        = model.CardImage;
                walletEditViewModel.CardNumber       = model.CardNumber;
                walletEditViewModel.CardSecurityCode = model.CardSecurityCode;
                walletEditViewModel.CardType         = model.CardType;
                walletEditViewModel.ExpirationMonth  = model.ExpirationMonth;
                walletEditViewModel.ExpirationYear   = model.ExpirationYear;
                walletEditViewModel.MakePrimary      = model.MakePrimary;
                walletEditViewModel.PaymentProfileId = model.PaymentProfileId;
            }

            return(walletEditViewModel);
        }
        public ActionResult Edit(WalletPaymentType model)
        {
            if (!ModelState.IsValid)
            {
                return(View(BuildWalletEditViewModel(HttpContext.GetCustomer())));
            }

            var customer = HttpContext.GetCustomer();

            if (model.PaymentProfileId < 0 &&
                !customer.Owns.Wallet(model.PaymentProfileId))
            {
                throw new HttpException(403, "Forbidden");
            }

            if (!WalletProvider.WalletsAreEnabled())
            {
                NoticeProvider.PushNotice(
                    message: AppLogic.GetString("checkout.wallet.disabled"),
                    type: NoticeType.Warning);

                return(RedirectToAction(ActionNames.Index, ControllerNames.Account));
            }

            string errorMessage, errorCode;
            var    paymentProfileId = ProcessTools
                                      .SaveProfileAndPaymentProfile(
                customerId: customer.CustomerID,
                email: customer.EMail,
                storeName: AppLogic.AppConfig("StoreName"),
                paymentProfileId: model.PaymentProfileId,
                addressId: model.BillingAddressId,
                cardNumber: model.CardNumber,
                cardCode: model.CardSecurityCode,
                expMonth: model.ExpirationMonth,
                expYear: model.ExpirationYear,
                errorMessage: out errorMessage,
                errorCode: out errorCode);

            if (paymentProfileId <= 0)
            {
                var message = string.Empty;

                if (errorCode == "E00039")
                {
                    message = AppLogic.GetString("AspDotNetStorefrontGateways.AuthorizeNet.Cim.PaymentProfileAlreadyExists");
                }
                else
                {
                    message = String.Format("{0} {1}",
                                            AppLogic.GetString("AspDotNetStorefrontGateways.AuthorizeNet.Cim.ErrorMessage"),
                                            errorMessage);
                }

                NoticeProvider.PushNotice(message, NoticeType.Failure);

                return(View(BuildWalletEditViewModel(HttpContext.GetCustomer())));
            }

            if (model.MakePrimary)
            {
                var address = new Address();
                address.LoadFromDB(model.BillingAddressId);
                address.MakeCustomersPrimaryAddress(AddressTypes.Billing);
                DataUtility.SetPrimaryPaymentProfile(customer.CustomerID, paymentProfileId);
            }

            return(RedirectToAction(ActionNames.Index, ControllerNames.Wallet));
        }