public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
 {
     return AuthorizeOrSale(processPaymentRequest, false);
 }
Пример #2
0
        public ActionResult ProcessPayment(GutterCleanPaymentRequestModel paymentRequestmodel)
        {
            var customer = _workContext.CurrentCustomer;
            if ((_workContext.CurrentCustomer.IsGuest()))
                return RedirectToRoute("GutterCleanInfoRegister");
            //return new HttpUnauthorizedResult();

            //GutterCleanRequestModel requestModel = _httpContext.Session["GutterCleanRequestModel"] as GutterCleanRequestModel;
            GutterCleanRequestModel model = _httpContext.Session["GutterCleanRequestModel"] as GutterCleanRequestModel;
            if (model == null)
            {
                return RedirectToRoute("GutterCleanRequest");
            }

            ZipCode zipcodeResult;

               // int addAddress_flag = 0;

            if (paymentRequestmodel.SelectedAddressId > 0)
            {
                var selectedAddress = customer.Addresses.Where(a => a.Id == paymentRequestmodel.SelectedAddressId).SingleOrDefault();
                zipcodeResult = _zipCodeService.GetZipCodeDetailByZipcode(selectedAddress.ZipPostalCode);
                if (zipcodeResult != null)
                {
                }
                else
                {
                    ModelState.AddModelError("Zipcode", "Selected Address Zipcode is not valid. Please select Another Addresss Or Enter New Address.");
                }

            }
            else
            {
                zipcodeResult = _zipCodeService.GetZipCodeDetailByZipcode(paymentRequestmodel.Zipcode);
                if (zipcodeResult != null)
                {
                }
                else
                {
                    ModelState.AddModelError("Zipcode", "Zipcode is not valid.");
                }
            }

            if (ModelState.IsValid)
            {
                //GutterCleanRequestModel model = _httpContext.Session["GutterCleanRequestModel"] as GutterCleanRequestModel;
                if (model == null)
                {
                    return RedirectToRoute("GutterCleanRequest");
                }
                paymentRequestmodel.OrderTotal = model.OrderTotal;
                ProcessPaymentRequest processPaymentRequest = new ProcessPaymentRequest();
                processPaymentRequest.OrderTotal = model.OrderTotal;

                processPaymentRequest.CreditCardName = paymentRequestmodel.NameOnCard;
                processPaymentRequest.CreditCardNumber = paymentRequestmodel.CardNumber;
                processPaymentRequest.CreditCardCvv2 = paymentRequestmodel.CardSecurityCode;
                processPaymentRequest.CreditCardExpireMonth = paymentRequestmodel.CardExpiryMonth;
                processPaymentRequest.CreditCardExpireYear = paymentRequestmodel.CardExpiryYear;

                processPaymentRequest.CustomerId = _workContext.CurrentCustomer.Id;
                if (processPaymentRequest.OrderGuid == Guid.Empty)
                    processPaymentRequest.OrderGuid = Guid.NewGuid();

                try
                {
                    ProcessPaymentResult processPaymentResult = null;
                    processPaymentResult = _paymentMethod.ProcessPayment(processPaymentRequest);

                    if (processPaymentResult.Success)
                    {
                        var zipcodeDetail = _zipCodeService.GetZipCodeDetailByZipcode(paymentRequestmodel.Zipcode);

                        GutterCleanOrder entity = new GutterCleanOrder();
                        entity.CustomerId = _workContext.CurrentCustomer.Id;
                        entity.CustomerIp = _webHelper.GetCurrentIpAddress();
                        entity.OrderStatusId = (int)OrderStatus.Processing;
                        entity.PaymentStatusId = (int)PaymentStatus.Paid;
                        entity.CaptureTransactionId = processPaymentResult.CaptureTransactionId;
                        entity.CaptureTransactionResult = processPaymentResult.CaptureTransactionResult;

                        entity.QuestionSquareFootage = model.QuestionSquareFootage;
                        entity.QuestionStyleOfGutter = model.QuestionStyleOfGutter;
                        entity.QuestionYearBuilt = model.QuestionYearBuilt;
                        entity.RoofMaterial = model.RoofMaterial;
                        entity.QuestionDeliveryTime = model.QuestionDeliveryTime;

                        entity.OrderTotal = model.OrderTotal;
                        entity.OrderGuid = processPaymentRequest.OrderGuid;

                        if (paymentRequestmodel.SelectedAddressId > 0)
                        {
                            var selectedAddress = customer.Addresses.Where(a => a.Id == paymentRequestmodel.SelectedAddressId).SingleOrDefault();

                            entity.Address = selectedAddress.Address1;
                            entity.Zipcode = selectedAddress.ZipPostalCode;

                            zipcodeDetail = _zipCodeService.GetZipCodeDetailByZipcode(entity.Zipcode);

                            if (zipcodeDetail != null)
                            {
                                entity.City = zipcodeDetail.CityName; ;
                                entity.State = zipcodeDetail.StateName;
                            }
                        }
                        else
                        {
                            entity.Address = paymentRequestmodel.Address;
                            entity.Zipcode = paymentRequestmodel.Zipcode;

                            if (zipcodeDetail != null)
                            {
                                entity.City = zipcodeDetail.CityName; ;
                                entity.State = zipcodeDetail.StateName;
                            }
                        }

                        entity.AgentId = 0;
                        entity.WorkerId = 0;
                        entity.AgentStatusId = 0;
                        entity.WorkerStatusId = 0;
                        entity.CreatedOnUtc = DateTime.UtcNow;
                        //entity.LastUpdatedDateUtc = DateTime.UtcNow;
                        _orderService.InsertOrder(entity);

                        // add new address if user enter new address which is not in its address list
                        if (paymentRequestmodel.SelectedAddressId <= 0)
                        {
                            addAddress(entity);
                        }

                        _httpContext.Session["GutterCleanRequestModel"] = null;

                        _workflowMessageService.SendOrderPlacedCustomerNotification(entity);
                        _workflowMessageService.SendOrderPlacedSiteOwnerNotification(entity);

                        return RedirectToRoute("Completed", new { orderId = entity.Id });

                    }

                    foreach (var error in processPaymentResult.Errors)
                        paymentRequestmodel.Warnings.Add(error);
                }
                catch (Exception exc)
                {

                    paymentRequestmodel.Warnings.Add(exc.Message);
                }

                // return View(paymentRequestmodel);
            }

            // Model is not valid then redisplay model

            var AddressList = customer.Addresses.ToList();
            string AddressText = string.Empty;
            foreach (var address in AddressList)
            {
                AddressText = string.Empty;
                AddressText = address.Address1 + ", " + address.City + "," + address.State + "," + address.ZipPostalCode;
                paymentRequestmodel.AvailableAddress.Add(new SelectListItem
                {
                    Text = AddressText,
                    Value = address.Id.ToString(),
                });
            }
            paymentRequestmodel.AvailableAddress.Add(new SelectListItem
            {
                Text = "New Address",
                Value = "-1",
            });

            //CC types
            paymentRequestmodel.CreditCardTypes.Add(new SelectListItem
            {
                Text = "Visa",
                Value = "Visa",
            });
            paymentRequestmodel.CreditCardTypes.Add(new SelectListItem
            {
                Text = "Master card",
                Value = "MasterCard",
            });
            paymentRequestmodel.CreditCardTypes.Add(new SelectListItem
            {
                Text = "Discover",
                Value = "Discover",
            });
            paymentRequestmodel.CreditCardTypes.Add(new SelectListItem
            {
                Text = "Amex",
                Value = "Amex",
            });

            //years
            paymentRequestmodel.ExpireYears.Add(new SelectListItem
            {
                Text = "Year",
                Value = "",
            });
            for (int i = 0; i < 25; i++)
            {
                string year = Convert.ToString(DateTime.Now.Year + i);
                paymentRequestmodel.ExpireYears.Add(new SelectListItem
                {
                    Text = year,
                    Value = year,
                });
            }

            //months
            paymentRequestmodel.ExpireMonths.Add(new SelectListItem
            {
                Text = "Month",
                Value = "",
            });
            for (int i = 1; i <= 12; i++)
            {
                string text = (i < 10) ? "0" + i : i.ToString();
                paymentRequestmodel.ExpireMonths.Add(new SelectListItem
                {
                    Text = text,
                    Value = i.ToString(),
                });
            }

            //prepare review Parameter
            paymentRequestmodel.QuestionSquareFootage = model.QuestionSquareFootage;

            var r = _questionAnswerEntityDataService.StyleOfGuttersAnswer().Where(i => i.Value == model.QuestionStyleOfGutter).SingleOrDefault();
            paymentRequestmodel.QuestionStyleOfGutterStr = r.Text;

            var yearRecord = _questionAnswerEntityDataService.YearBuiltAnswer().Where(i => i.Value == model.QuestionYearBuilt.ToString()).SingleOrDefault();
            paymentRequestmodel.QuestionYearBuiltStr = yearRecord.Text;

            paymentRequestmodel.RoofMaterial = model.RoofMaterial;

            paymentRequestmodel.QuestionDeliveryTimeStr = "5 business days";
            if (model.QuestionDeliveryTime == 1)
                paymentRequestmodel.QuestionDeliveryTimeStr = "5 business days";

            if (model.QuestionDeliveryTime == 2)
                paymentRequestmodel.QuestionDeliveryTimeStr = "8 hours";

            if (model.QuestionDeliveryTime == 3)
                paymentRequestmodel.QuestionDeliveryTimeStr = "4 hours";

            //var zipcodeResultCustomer = _zipCodeService.GetZipCodeDetailByZipcode(customer.ZipPostalCode);
            //paymentRequestmodel.Address = customer.Address1;

            //paymentRequestmodel.AddressService = customer.Address1;
            //if (zipcodeResult != null)
            //{
            //    paymentRequestmodel.AddressService = customer.Address1 + ", " + zipcodeResultCustomer.CityName + ", " + zipcodeResultCustomer.StateName;
            //}
            //paymentRequestmodel.zipcodeService = customer.ZipPostalCode;

            paymentRequestmodel.AccetTermandCondition = false;

            return View(paymentRequestmodel);
        }
        protected ProcessPaymentResult AuthorizeOrSale(ProcessPaymentRequest processPaymentRequest, bool authorizeOnly)
        {
            var result = new ProcessPaymentResult();

            var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);
            if (customer == null)
                throw new Exception("Customer cannot be loaded");

            var req = new DoDirectPaymentReq();
            req.DoDirectPaymentRequest = new DoDirectPaymentRequestType();
            req.DoDirectPaymentRequest.Version = GetApiVersion();
            var details = new DoDirectPaymentRequestDetailsType();
            req.DoDirectPaymentRequest.DoDirectPaymentRequestDetails = details;
            details.IPAddress = _webHelper.GetCurrentIpAddress() ?? "";
            if (authorizeOnly)
                details.PaymentAction = PaymentActionCodeType.AUTHORIZATION;
            else
                details.PaymentAction = PaymentActionCodeType.SALE;
            //credit card
            details.CreditCard = new CreditCardDetailsType();
            details.CreditCard.CreditCardNumber = processPaymentRequest.CreditCardNumber;
            details.CreditCard.CreditCardType = GetPaypalCreditCardType(processPaymentRequest.CreditCardType);
            details.CreditCard.ExpMonth = processPaymentRequest.CreditCardExpireMonth;
            details.CreditCard.ExpYear = processPaymentRequest.CreditCardExpireYear;
            details.CreditCard.CVV2 = processPaymentRequest.CreditCardCvv2;

            //details.CreditCard.CardOwner = new PayerInfoType();
            //details.CreditCard.CardOwner.PayerCountry = GetPaypalCountryCodeType(customer.BillingAddress.Country);
            ////billing address
            //details.CreditCard.CardOwner.Address = new AddressType();
            //details.CreditCard.CardOwner.Address.Street1 = customer.BillingAddress.Address1;
            //details.CreditCard.CardOwner.Address.Street2 = customer.BillingAddress.Address2;
            //details.CreditCard.CardOwner.Address.CityName = customer.BillingAddress.City;
            //if (customer.BillingAddress.StateProvince != null)
            //    details.CreditCard.CardOwner.Address.StateOrProvince = customer.BillingAddress.StateProvince.Abbreviation;
            //else
            //    details.CreditCard.CardOwner.Address.StateOrProvince = "CA";
            //details.CreditCard.CardOwner.Address.Country = GetPaypalCountryCodeType(customer.BillingAddress.Country);
            //details.CreditCard.CardOwner.Address.PostalCode = customer.BillingAddress.ZipPostalCode;
            //details.CreditCard.CardOwner.Payer = customer.BillingAddress.Email;
            //details.CreditCard.CardOwner.PayerName = new PersonNameType();
            //details.CreditCard.CardOwner.PayerName.FirstName = customer.BillingAddress.FirstName;
            //details.CreditCard.CardOwner.PayerName.LastName = customer.BillingAddress.LastName;

            //order totals
            var payPalCurrency = PaypalHelper.GetPaypalCurrency("USD");
            details.PaymentDetails = new PaymentDetailsType();
            details.PaymentDetails.OrderTotal = new BasicAmountType();
            details.PaymentDetails.OrderTotal.value = Math.Round(processPaymentRequest.OrderTotal, 2).ToString("N", new CultureInfo("en-us"));
            details.PaymentDetails.OrderTotal.currencyID = payPalCurrency;
            details.PaymentDetails.Custom = processPaymentRequest.OrderGuid.ToString();
            details.PaymentDetails.ButtonSource = "EGSWPaymentButton";

            ////shipping
            //if (customer.ShippingAddress != null)
            //{
            //    if (customer.ShippingAddress.StateProvince != null && customer.ShippingAddress.Country != null)
            //    {
            //        var shippingAddress = new AddressType();
            //        shippingAddress.Name = customer.ShippingAddress.FirstName + " " + customer.ShippingAddress.LastName;
            //        shippingAddress.Street1 = customer.ShippingAddress.Address1;
            //        shippingAddress.Street2 = customer.ShippingAddress.Address2;
            //        shippingAddress.CityName = customer.ShippingAddress.City;
            //        shippingAddress.StateOrProvince = customer.ShippingAddress.StateProvince.Abbreviation;
            //        shippingAddress.PostalCode = customer.ShippingAddress.ZipPostalCode;
            //        shippingAddress.Country = (CountryCodeType)Enum.Parse(typeof(CountryCodeType), customer.ShippingAddress.Country.TwoLetterIsoCode, true);
            //        details.PaymentDetails.ShipToAddress = shippingAddress;
            //    }
            //}

            //send request
            var service = GetService();
            DoDirectPaymentResponseType response = service.DoDirectPayment(req);

            string error;
            bool success = PaypalHelper.CheckSuccess(response, out error);
            if (success)
            {
                result.AvsResult = response.AVSCode;
                result.AuthorizationTransactionCode = response.CVV2Code;
                if (authorizeOnly)
                {
                    result.AuthorizationTransactionId = response.TransactionID;
                    result.AuthorizationTransactionResult = response.Ack.ToString();

                    result.NewPaymentStatus = PaymentStatus.Authorized;
                }
                else
                {
                    result.CaptureTransactionId = response.TransactionID;
                    result.CaptureTransactionResult = response.Ack.ToString();

                    result.NewPaymentStatus = PaymentStatus.Paid;
                }
            }
            else
            {
                result.AddError(error);
            }
            return result;
        }