protected void SaveCardButton_Click(object sender, EventArgs e)
        {
            int   profileId             = AlwaysConvert.ToInt(HiddenProfileId.Value);
            Label ProfileSuccessMessage = (Label)PageHelper.RecursiveFindControl(Page, "ProfileSuccessMessage");
            Label ProfileErrorMessage   = (Label)PageHelper.RecursiveFindControl(Page, "ProfileErrorMessage");

            if (profileId > 0)
            {
                var profile = GatewayPaymentProfileDataSource.Load(profileId);
                if (profile != null)
                {
                    int            gatewayId = PaymentGatewayDataSource.GetPaymentGatewayIdByClassId(profile.GatewayIdentifier);
                    PaymentGateway gateway   = PaymentGatewayDataSource.Load(gatewayId);

                    if (gateway != null)
                    {
                        var provider = gateway.GetInstance();
                        try
                        {
                            AccountDataDictionary cardDetails = new AccountDataDictionary();
                            cardDetails["AccountNumber"]   = "XXX" + profile.ReferenceNumber.Replace("x", "X");
                            cardDetails["ExpirationMonth"] = ExpirationMonth.SelectedItem.Value;
                            cardDetails["ExpirationYear"]  = ExpirationYear.SelectedItem.Value;
                            cardDetails["SecurityCode"]    = SecurityCode.Text.Trim();
                            PaymentMethod         method = PaymentMethodDataSource.Load(profile.InstrumentTypeId);
                            PaymentInstrumentData instr  = PaymentInstrumentData.CreateInstance(cardDetails, method.PaymentInstrumentType, null);
                            var rsp = provider.DoUpdatePaymentProfile(new CommerceBuilder.Payments.Providers.UpdatePaymentProfileRequest(AbleContext.Current.User, instr, profile.CustomerProfileId, profile.PaymentProfileId));
                            if (rsp.Successful || rsp.ResponseCode == "E00040")
                            {
                                int id = profile.Id;
                                profile.Expiry = Misc.GetStartOfDate(new DateTime(AlwaysConvert.ToInt(ExpirationYear.SelectedItem.Value), AlwaysConvert.ToInt(ExpirationMonth.SelectedItem.Value), 1));
                                profile.Save();
                                if (ProfileSuccessMessage != null)
                                {
                                    ProfileSuccessMessage.Text    = string.Format("Profile '{0} ending in {1}' updated successfully!", profile.InstrumentType, profile.ReferenceNumber);
                                    ProfileSuccessMessage.Visible = true;
                                    ProfileErrorMessage.Visible   = false;
                                }
                            }
                            else
                            {
                                if (ProfileErrorMessage != null)
                                {
                                    ProfileErrorMessage.Text      = string.Format("Somthing went wrong! Unable to update profile '{0} ending in {1}'", profile.InstrumentType, profile.ReferenceNumber);
                                    ProfileSuccessMessage.Visible = false;
                                    ProfileErrorMessage.Visible   = true;
                                }

                                Logger.Error(rsp.ResponseMessage);
                            }
                        }
                        catch (Exception exp)
                        {
                            Logger.Error(exp.Message);
                        }
                    }
                }
            }
            EditCardInfoPopUp.Hide();
        }
        protected void RemoveCardButton_Click(object sender, EventArgs e)
        {
            int profileId = AlwaysConvert.ToInt(PreferedCreditCard.SelectedValue);

            if (profileId > 0)
            {
                var profile = GatewayPaymentProfileDataSource.Load(profileId);
                if (profile.Subscriptions.Count == 0)
                {
                    int            gatewayId = PaymentGatewayDataSource.GetPaymentGatewayIdByClassId(profile.GatewayIdentifier);
                    PaymentGateway gateway   = PaymentGatewayDataSource.Load(gatewayId);
                    if (gateway != null)
                    {
                        var provider = gateway.GetInstance();
                        try
                        {
                            var rsp = provider.DoDeletePaymentProfile(new CommerceBuilder.Payments.Providers.DeletePaymentProfileRequest(AbleContext.Current.User, profile.CustomerProfileId, profile.PaymentProfileId));
                            if (rsp.Successful || rsp.ResponseCode == "E00040")
                            {
                                profile.Delete();
                                BindPayments();
                            }
                        }
                        catch (Exception exp)
                        {
                            Logger.Error(exp.Message);
                        }
                    }
                }
            }
        }
 protected void BindCards()
 {
     var paymentProfiles = GatewayPaymentProfileDataSource.LoadForUser(_user.Id);
     CardsList.DataSource = paymentProfiles;
     CardsList.DataBind();
     NoPaymentTypePanel.Visible = paymentProfiles.Count == 0;
     DefaultButtonPanel.Visible = paymentProfiles.Count > 1;
     SetDefaultButton.Visible = paymentProfiles.Count > 1;
     InstructionText.Visible = SetDefaultButton.Visible;
 }
Esempio n. 4
0
        protected void CardsList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DELETE_PROFILE")
            {
                int profileId = AlwaysConvert.ToInt(e.CommandArgument);
                if (profileId > 0 && CanBeDeleted(profileId))
                {
                    var profile = GatewayPaymentProfileDataSource.Load(profileId);
                    if (profile.Subscriptions.Count == 0)
                    {
                        int            gatewayId = PaymentGatewayDataSource.GetPaymentGatewayIdByClassId(profile.GatewayIdentifier);
                        PaymentGateway gateway   = PaymentGatewayDataSource.Load(gatewayId);
                        if (gateway != null)
                        {
                            var provider = gateway.GetInstance();
                            try
                            {
                                var rsp = provider.DoDeletePaymentProfile(new CommerceBuilder.Payments.Providers.DeletePaymentProfileRequest(AbleContext.Current.User, profile.CustomerProfileId, profile.PaymentProfileId));
                                if (rsp.Successful || rsp.ResponseCode == "E00040")
                                {
                                    int id = profile.Id;
                                    profile.Delete();
                                    BindCards();
                                }
                                else
                                {
                                    DeleteMessage.Text = string.Format("Somthing went wrong! Unable to remove profile '{0} ending in {1}'", profile.InstrumentType, profile.ReferenceNumber);
                                    Logger.Error(rsp.ResponseMessage);
                                }
                            }
                            catch (Exception exp)
                            {
                                Logger.Error(exp.Message);
                            }
                        }
                    }
                }
            }

            if (e.CommandName == "EDIT_PROFILE")
            {
                Response.Redirect(string.Format("~/Members/EditPaymentType.aspx?ProfileId={0}", e.CommandArgument));
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _profileId = AlwaysConvert.ToInt(Request.QueryString["ProfileId"]);
            _profile   = GatewayPaymentProfileDataSource.Load(_profileId);

            if (_profile == null || _profile.User != AbleContext.Current.User)
            {
                Response.Redirect("PaymentTypes.aspx");
            }

            if (!Page.IsPostBack)
            {
                CardType.Text   = _profile.InstrumentType.ToString();
                CardName.Text   = _profile.NameOnCard;
                CardNumber.Text = _profile.ReferenceNumber.PadLeft(8, 'x').ToUpper();

                // POPULATE EXPIRATON DATE DROPDOWN
                int thisYear = LocaleHelper.LocalNow.Year;
                for (int i = 0; (i <= 10); i++)
                {
                    ExpirationYear.Items.Add(new ListItem((thisYear + i).ToString()));
                }

                DateTime expirationDate = _profile.Expiry ?? DateTime.MinValue;
                if (expirationDate != DateTime.MinValue)
                {
                    string   eMonth = expirationDate.Month.ToString().PadLeft(2, '0');
                    string   eYear  = expirationDate.Year.ToString();
                    ListItem item   = ExpirationYear.Items.FindByValue(eYear);
                    if (item != null)
                    {
                        item.Selected = true;
                    }
                    item = ExpirationMonth.Items.FindByValue(eMonth);
                    if (item != null)
                    {
                        item.Selected = true;
                    }
                }
            }
        }
        private Payment GetPayment()
        {
            Payment payment = new Payment();

            payment.Amount = GetPaymentAmount(true);

            if (ProfilesPH.Visible)
            {
                var profile = GatewayPaymentProfileDataSource.Load(AlwaysConvert.ToInt(ProfilesList.SelectedValue));
                payment.PaymentProfile    = profile;
                payment.ReferenceNumber   = profile.ReferenceNumber;
                payment.PaymentMethodName = profile.PaymentMethodName;
            }
            else
            {
                payment.PaymentMethod = PaymentMethodDataSource.Load(AlwaysConvert.ToInt(CardType.SelectedValue));
                AccountDataDictionary instrumentBuilder = new AccountDataDictionary();
                instrumentBuilder["AccountName"]     = CardName.Text;
                instrumentBuilder["AccountNumber"]   = CardNumber.Text;
                instrumentBuilder["ExpirationMonth"] = ExpirationMonth.SelectedItem.Value;
                instrumentBuilder["ExpirationYear"]  = ExpirationYear.SelectedItem.Value;
                instrumentBuilder["SecurityCode"]    = SecurityCode.Text;
                if (payment.PaymentMethod.IsIntlDebitCard())
                {
                    if (IssueNumber.Text.Length > 0)
                    {
                        instrumentBuilder["IssueNumber"] = IssueNumber.Text;
                    }
                    if ((StartDateMonth.SelectedIndex > 0) && (StartDateYear.SelectedIndex > 0))
                    {
                        instrumentBuilder["StartDateMonth"] = StartDateMonth.SelectedItem.Value;
                        instrumentBuilder["StartDateYear"]  = StartDateYear.SelectedItem.Value;
                    }
                }
                payment.ReferenceNumber = Payment.GenerateReferenceNumber(CardNumber.Text);
                payment.AccountData     = instrumentBuilder.ToString();
            }

            return(payment);
        }
        void control_OnEditCardInfo(object sender, CardInfoDetailEventArgs e)
        {
            GatewayPaymentProfile profile = GatewayPaymentProfileDataSource.Load(e.ProfileId);

            if (profile != null)
            {
                HiddenProfileId.Value = profile.Id.ToString();
                NameOnCardLabel.Text  = string.Format("{0}", profile.NameOnCard);
                ReferenceLabel.Text   = string.Format("Profile {0} ending in {1}", profile.InstrumentType, profile.ReferenceNumber);

                string   profileExpiry = profile.Expiry.ToString();
                string[] dateParts     = profileExpiry.Split('/');
                string   expiryMonth   = (dateParts[0].Length == 1) ? "0" + dateParts[0] : dateParts[0];
                string   expiryDay     = dateParts[1];
                string   expiryYear    = dateParts[2].Substring(0, 4);

                ListItem selectedYear = ExpirationYear.Items.FindByValue(expiryYear);
                if (selectedYear != null)
                {
                    ExpirationYear.ClearSelection();
                    selectedYear.Selected = true;
                }

                ListItem selectedMonth = ExpirationMonth.Items.FindByValue(expiryMonth);
                if (selectedMonth != null)
                {
                    ExpirationMonth.ClearSelection();
                    selectedMonth.Selected = true;
                }

                EditCardInfoPopUp.Show();
            }
            else
            {
                HiddenProfileId.Value = string.Empty;
            }
        }
        protected void UpdateCardButton_Click(object sender, EventArgs e)
        {
            int profileId = AlwaysConvert.ToInt(PreferedCreditCard.SelectedValue);

            if (profileId > 0)
            {
                _Subscription.PaymentProfile = GatewayPaymentProfileDataSource.Load(profileId);
                IList <PaymentMethod> methods          = AbleCommerce.Code.StoreDataHelper.GetPaymentMethods(AbleContext.Current.UserId);
                PaymentMethod         newPaymentMethod = null;
                if (string.IsNullOrEmpty(_Subscription.PaymentProfile.PaymentMethodName))
                {
                    newPaymentMethod = methods.Where(m => m.PaymentInstrumentType == _Subscription.PaymentProfile.InstrumentType).FirstOrDefault();
                }
                else
                {
                    newPaymentMethod = methods.Where(m => m.Name == _Subscription.PaymentProfile.PaymentMethodName).SingleOrDefault();
                }

                if (newPaymentMethod != null)
                {
                    _Subscription.PaymentMethod = newPaymentMethod;
                }

                try
                {
                    EmailProcessor.NotifySubscriptionUpdated(_Subscription);
                }
                catch (Exception ex)
                {
                    Logger.Error("Error sending subscription updated email.", ex);
                }

                _Subscription.Save();
                BindPayments(profileId);
                CreditCardMessagePH.Visible = true;
            }
        }
 protected bool ShowSelectDefault()
 {
     return GatewayPaymentProfileDataSource.CountForUser(_user.Id) > 1;
 }
        protected void CompleteButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                bool checkOut = true;
                if (CheckingOut != null)
                {
                    CheckingOutEventArgs c = new CheckingOutEventArgs();
                    CheckingOut(this, c);
                    checkOut = !c.Cancel;
                }
                if (checkOut)
                {
                    Basket basket = AbleContext.Current.User.Basket;
                    //PROCESS THE CHECKOUT
                    ICheckoutService checkoutService  = AbleContext.Resolve <ICheckoutService>();
                    CheckoutRequest  checkoutRequest  = new CheckoutRequest(basket);
                    CheckoutResponse checkoutResponse = checkoutService.ExecuteCheckout(checkoutRequest);
                    if (checkoutResponse.Success)
                    {
                        GatewayPaymentProfile profile = null;
                        this.Visible = true;

                        if (ProfilesPH.Visible)
                        {
                            profile = GatewayPaymentProfileDataSource.Load(AlwaysConvert.ToInt(ProfilesList.SelectedValue));
                        }

                        if (CardPH.Visible)
                        {
                            AccountDataDictionary cardDetails = new AccountDataDictionary();
                            cardDetails["AccountName"]     = CardName.Text.Trim();
                            cardDetails["AccountNumber"]   = CardNumber.Text.Trim();
                            cardDetails["ExpirationMonth"] = ExpirationMonth.SelectedItem.Value;
                            cardDetails["ExpirationYear"]  = ExpirationYear.SelectedItem.Value;
                            cardDetails["SecurityCode"]    = SecurityCode.Text.Trim();
                            profile = CreateProfile(cardDetails);
                        }

                        if (profile != null)
                        {
                            IList <Subscription> subscriptions = SubscriptionDataSource.LoadForOrder(checkoutResponse.Order.Id);
                            foreach (Subscription subscription in subscriptions)
                            {
                                if (subscription.PaymentFrequency.HasValue && subscription.PaymentFrequencyUnit.HasValue)
                                {
                                    subscription.PaymentProfile        = profile;
                                    subscription.PaymentProcessingType = PaymentProcessingType.ArbProfileManaged;
                                    subscription.Save();
                                }
                            }
                        }

                        if (CheckedOut != null)
                        {
                            CheckedOut(this, new CheckedOutEventArgs(checkoutResponse));
                        }
                        Response.Redirect(AbleCommerce.Code.NavigationHelper.GetReceiptUrl(checkoutResponse.Order.OrderNumber));
                    }
                    else
                    {
                        IList <string> warningMessages = checkoutResponse.WarningMessages;
                        if (warningMessages.Count == 0)
                        {
                            warningMessages.Add("The order could not be submitted at this time.  Please try again later or contact us for assistance.");
                        }
                        if (CheckedOut != null)
                        {
                            CheckedOut(this, new CheckedOutEventArgs(checkoutResponse));
                        }
                    }
                }
            }
        }
        protected void CreditCardButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid && CustomValidation())
            {
                // CREATE THE PAYMENT OBJECT
                Payment payment = GetPayment();

                // PROCESS CHECKING OUT EVENT
                bool checkOut = true;
                if (CheckingOut != null)
                {
                    CheckingOutEventArgs c = new CheckingOutEventArgs(payment);
                    CheckingOut(this, c);
                    checkOut = !c.Cancel;
                }

                if (checkOut)
                {
                    // CONTINUE TO PROCESS THE CHECKOUT
                    Basket           basket           = AbleContext.Current.User.Basket;
                    ICheckoutService checkoutService  = AbleContext.Resolve <ICheckoutService>();
                    CheckoutRequest  checkoutRequest  = new CheckoutRequest(basket, payment);
                    CheckoutResponse checkoutResponse = checkoutService.ExecuteCheckout(checkoutRequest);
                    if (checkoutResponse.Success)
                    {
                        GatewayPaymentProfile profile = null;
                        if (trSaveCard.Visible && SaveCard.Checked)
                        {
                            AccountDataDictionary cardDetails = new AccountDataDictionary();
                            cardDetails["AccountName"]     = CardName.Text.Trim();
                            cardDetails["AccountNumber"]   = CardNumber.Text.Trim();
                            cardDetails["ExpirationMonth"] = ExpirationMonth.SelectedItem.Value;
                            cardDetails["ExpirationYear"]  = ExpirationYear.SelectedItem.Value;
                            cardDetails["SecurityCode"]    = SecurityCode.Text.Trim();
                            profile = CreateProfile(cardDetails);
                        }

                        if (AbleContext.Current.Store.Settings.ROCreateNewOrdersEnabled && OrderHelper.HasRecurringSubscriptions(checkoutResponse.Order))
                        {
                            IList <Subscription> subscriptions = SubscriptionDataSource.LoadForOrder(checkoutResponse.Order.Id);
                            foreach (Subscription subscription in subscriptions)
                            {
                                OrderItem oi = subscription.OrderItem;
                                if (oi != null && oi.Price == 0 && OrderHelper.HasRecurringSubscriptions(oi) && subscription.PaymentProfile == null)
                                {
                                    if (profile == null)
                                    {
                                        if (ProfilesPH.Visible)
                                        {
                                            profile = GatewayPaymentProfileDataSource.Load(AlwaysConvert.ToInt(ProfilesList.SelectedValue));
                                        }

                                        if (CardPH.Visible)
                                        {
                                            AccountDataDictionary cardDetails = new AccountDataDictionary();
                                            cardDetails["AccountName"]     = CardName.Text.Trim();
                                            cardDetails["AccountNumber"]   = CardNumber.Text.Trim();
                                            cardDetails["ExpirationMonth"] = ExpirationMonth.SelectedItem.Value;
                                            cardDetails["ExpirationYear"]  = ExpirationYear.SelectedItem.Value;
                                            cardDetails["SecurityCode"]    = SecurityCode.Text.Trim();
                                            profile = CreateProfile(cardDetails);
                                        }
                                    }

                                    subscription.PaymentProfile        = profile;
                                    subscription.PaymentProcessingType = PaymentProcessingType.ArbProfileManaged;
                                    subscription.Save();
                                }
                            }
                        }

                        if (profile != null && payment.PaymentProfile == null)
                        {
                            payment.PaymentProfile = profile;
                            payment.Save();
                        }

                        if (CheckedOut != null)
                        {
                            CheckedOut(this, new CheckedOutEventArgs(checkoutResponse));
                        }
                        Response.Redirect(AbleCommerce.Code.NavigationHelper.GetReceiptUrl(checkoutResponse.Order.OrderNumber));
                    }
                    else
                    {
                        IList <string> warningMessages = checkoutResponse.WarningMessages;
                        if (warningMessages.Count == 0)
                        {
                            warningMessages.Add("The order could not be submitted at this time.  Please try again later or contact us for assistance.");
                        }
                        if (CheckedOut != null)
                        {
                            CheckedOut(this, new CheckedOutEventArgs(checkoutResponse));
                        }
                    }
                }
            }
        }
 protected void Page_Load(object sender, EventArgs e)
 {
     _UserId = AlwaysConvert.ToInt(Request.QueryString["UserId"]);
     PaymentProfileGrid.DataSource = GatewayPaymentProfileDataSource.LoadForUser(_UserId);
     PaymentProfileGrid.DataBind();
 }