protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
        {
            return;
        }

        int      selectedAddressId = int.Parse(BillingAddresses.SelectedValue);
        SkinBase page = HttpContext.Current.Handler as SkinBase;

        AspDotNetStorefrontCore.Customer adnsfCustomer = AspDotNetStorefrontCore.Customer.Current;

        string errorMessage, errorCode;

        this.PaymentProfileId = ProcessTools.SaveProfileAndPaymentProfile(adnsfCustomer.CustomerID, adnsfCustomer.EMail, AspDotNetStorefrontCore.AppLogic.AppConfig("StoreName"), this.PaymentProfileId, selectedAddressId, TextCreditCard.Text, TextCardSecurity.Text, ExpirationMonth.SelectedValue, ExpirationYear.SelectedValue, out errorMessage, out errorCode);

        if (PaymentProfileId <= 0)
        {
            ShowError(String.Format("{0} {1}", AspDotNetStorefrontCore.AppLogic.GetString("AspDotNetStorefrontGateways.AuthorizeNet.Cim.ErrorMessage", adnsfCustomer.SkinID, adnsfCustomer.LocaleSetting), errorMessage));
            return;
        }

        if (CBMakePrimaryCard.Checked)
        {
            AspDotNetStorefrontCore.Address adnsfAddress = new AspDotNetStorefrontCore.Address();
            adnsfAddress.LoadFromDB(selectedAddressId);
            adnsfAddress.MakeCustomersPrimaryAddress(AspDotNetStorefrontCore.AddressTypes.Billing);
            DataUtility.SetPrimaryPaymentProfile(adnsfCustomer.CustomerID, this.PaymentProfileId);
        }

        BindPage(this.PaymentProfileId);

        FireCardEditComplete();
    }
        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));
        }