public UpdateCustomerMobileCreditCardResponse AddMobileCustomerCreditCard(CustomerCreditCards customer, string token)
        {
            UpdateCustomerMobileCreditCardResponse result = null;

            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(ConstantData.ApiURL.ToString() + "CustomerMobile/AddMobileCustomerCreditCard");
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                    var myContent   = JsonConvert.SerializeObject(customer);
                    var buffer      = Encoding.UTF8.GetBytes(myContent);
                    var byteContent = new ByteArrayContent(buffer);
                    byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");



                    var response = client.PostAsync(client.BaseAddress, byteContent).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var responseStream = response.Content.ReadAsStringAsync().Result;
                        result = JsonConvert.DeserializeObject <UpdateCustomerMobileCreditCardResponse>(responseStream);
                    }
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
            return(result);
        }
        public UpdateCustomerMobileCreditCardResponse UpdateMobileCustomerCreditCard(CreditCards cards, string token)
        {
            UpdateCustomerMobileCreditCardResponse response = new UpdateCustomerMobileCreditCardResponse();

            try
            {
                response = customerService.UpdateMobileCustomerCreditCard(cards, token);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(response);
        }
        public AddPaymentMethodPage()
        {
            InitializeComponent();
            token = App.Current.Properties["currentToken"].ToString();
            customerCreditCards = new CustomerCreditCards();
            cardResponse        = null;
            //List<string> cardTypes = Enum.GetValues(typeof(CreditCardType)).Cast<CreditCardType>().Select(t => t.ToString()).ToList();
            List <string> cardTypes = new List <string>()
            {
                "Visa", "Mastercard", "American Express", "Discover"
            };

            CardTypePicker.ItemsSource = cardTypes;
            On <Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);
        }
        private async void btnSave_Clicked(object sender, EventArgs e)
        {
            string userCardNu = "";

            char[]        charSeparators = new char[] { '/' };
            string        userExDAte;
            List <string> exList = new List <string>();

            if (CardTypePicker.SelectedIndex == -1)
            {
                await PopupNavigation.Instance.PushAsync(new Error_popup("Please select a card type."));
            }
            else if (string.IsNullOrEmpty(CardNumber.Text))
            {
                await PopupNavigation.Instance.PushAsync(new Error_popup("Please enter valid credit card number"));
            }
            else
            {
                userCardNu = CardNumber.Text;
                userCardNu = userCardNu.Replace(" ", "");

                if (!string.IsNullOrEmpty(txtExpiryDate.Text))
                {
                    userExDAte = txtExpiryDate.Text;
                    exList     = userExDAte.Replace(" ", "").Split(charSeparators).ToList <string>();
                }

                bool isIssue = false;

                if (CardTypePicker.SelectedItem.ToString() == "American Express")
                {
                    if (userCardNu.Length != 15)
                    {
                        isIssue         = true;
                        CardNumber.Text = null;
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please enter valid credit card number"));
                    }
                    else if (txtCvv.Text.Length != 4)
                    {
                        isIssue = true;
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please enter a valid CVV number. Click on the help button for further details."));
                    }
                }
                else
                {
                    if (userCardNu.Length != 16)
                    {
                        isIssue         = true;
                        CardNumber.Text = null;
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please enter valid credit card number"));
                    }
                    else if (txtCvv.Text.Length != 3)
                    {
                        isIssue = true;
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please enter a valid CVV number. Click on the help button for further details."));
                    }
                }

                if (!isIssue)
                {
                    if (exList == null)
                    {
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please enter expired date of your card."));
                    }
                    else if (exList.Count != 2)
                    {
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please check your card expiry date details"));
                    }
                    else if (Convert.ToInt32(exList[0].ToString()) > 12 || Convert.ToInt32(exList[0].ToString()) < 1)
                    {
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please check the month of expiry date details"));
                    }
                    else if (Convert.ToInt32(exList[1].ToString()) < 0)
                    {
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please check the year of expiry date details"));
                    }
                    else if (string.IsNullOrEmpty(txtCvv.Text))
                    {
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please enter CVV number"));
                    }

                    else if (string.IsNullOrEmpty(txtName.Text))
                    {
                        await PopupNavigation.Instance.PushAsync(new Error_popup("Please enter the name details."));
                    }

                    else
                    {
                        customerCreditCards.CustomerId     = (int)App.Current.Properties["CustomerId"];
                        customerCreditCards.CreatedDate    = DateTime.Now;
                        customerCreditCards.IsDeleted      = 0;
                        customerCreditCards.CreditCardNo   = userCardNu;
                        customerCreditCards.CreditCardType = Constants.returnNavotarCardTypes(CardTypePicker.SelectedItem.ToString());


                        customerCreditCards.NameOnCard      = txtName.Text;
                        customerCreditCards.Month           = Convert.ToInt32(exList[0].ToString());
                        customerCreditCards.Year            = Convert.ToInt32(exList[1].ToString());
                        customerCreditCards.CreditCardCVSNo = txtCvv.Text;
                        if (customerCreditCards.Year > 0 && customerCreditCards.Month > 0)
                        {
                            var year = (customerCreditCards.Year);
                            if (year < 2000)
                            {
                                year += 2000;
                            }
                            var month = (customerCreditCards.Month);
                            var day   = DateTime.DaysInMonth(year, month);
                            var d     = new DateTime(year, month, day);
                            customerCreditCards.CreditCardExpiryDate    = d;
                            customerCreditCards.CreditCardExpiryDateStr = d.ToString();
                            bool busy = false;
                            if (!busy)
                            {
                                try
                                {
                                    busy = true;
                                    await PopupNavigation.Instance.PushAsync(new LoadingPopup("."));

                                    await Task.Run(async() =>
                                    {
                                        cardResponse = addCreditCard(customerCreditCards, token);
                                    });
                                }
                                catch (Exception ex)
                                {
                                    await PopupNavigation.Instance.PushAsync(new Error_popup(ex.Message));
                                }
                                finally
                                {
                                    busy = false;
                                    await PopupNavigation.Instance.PopAsync();

                                    if (cardResponse != null)
                                    {
                                        if (cardResponse.creditCardID > 0)
                                        {
                                            if (App.Current.Properties.ContainsKey("LastCreditCardId"))
                                            {
                                                App.Current.Properties["LastCreditCardId"] = cardResponse.creditCardID;
                                            }
                                            else
                                            {
                                                App.Current.Properties.Add("LastCreditCardId", cardResponse.creditCardID);
                                            }
                                            await Navigation.PopModalAsync();

                                            //await PopupNavigation.Instance.PushAsync(new SuccessPopUp("Your CrediCard added Successfully.", 3));
                                        }
                                        else
                                        {
                                            await PopupNavigation.Instance.PushAsync(new Error_popup("Sorry, Update failed. Pease try again."));
                                        }
                                    }
                                    else
                                    {
                                        await PopupNavigation.Instance.PushAsync(new Error_popup("Sorry, Update failed. Pease try again."));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }