public void Test_Incorrect_LocationId_From_Checkout()
        {
            var co = new ProductCheckoutModel();

            co.LocationId = _posResponse.Locations.Max(x => x.Id) + 10;

            _productsBll.SaveProductInformationFromPos("12345678901234567890123456789012", _posResponse, co);
        }
        public void Test_SaveProductInformationFromPos_Map_Response()
        {
            DateTime validTill        = DateTime.UtcNow;
            string   phoneReservation = "8 600 54321";
            string   emailReservation = "*****@*****.**";

            ProductServiceLocation location = new ProductServiceLocation();

            location.Id      = 1;
            location.Name    = "LocName";
            location.City    = "Wilno";
            location.Address = "Juozapavi\u010diaus g. 9A - 174";

            uint productValidTillTm = new HelperBll().GetUnixTimestamp();
            var  jsonResponse       = "{\"Status\":\"true\",\"Message\":\"\",\"PosId\":1005,\"ProductName\":\"Gilus prisilietimas\",\"ProductDuration\":\"40 min\",\"ProductDescription\":\"NO\",\"RequestedAmountMinor\":\"3500\",\"CurrencyCode\":\"EUR\",\"ProductValidTillTm\":" + productValidTillTm + ",\"PosName\":\"\",\"PosUrl\":\"\",\"PosCity\":\"\",\"PosAddress\":\"\",\"PhoneForReservation\":\"" + phoneReservation + "\",\"EmailForReservation\":\"" + emailReservation + "\",\"Locations\":[{\"Id\":\"" + location.Id + "\",\"Name\":\"" + location.Name + "\",\"City\":\"" + location.City + "\",\"Address\":\"" + location.Address + "\"},{\"Id\":\"2\",\"Name\":\"SIGMOS SPORTO KLUBAS\",\"City\":\"Vilnius\",\"Address\":\"Kalvarij\u0173 g.131  Luk\u0161io g. 2\"}]}";
            var  posResponse        = JsonConvert.DeserializeObject <PaymentRequestValidationResponse>(jsonResponse);

            var co = new ProductCheckoutModel();

            co.PaymentSystem = PaymentSystems.Paysera;
            co.LocationId    = location.Id;
            co.CurrencyCode  = "EUR";
            co.CustomerEmail = "*****@*****.**";
            co.CustomerName  = "Aleksej Tak";
            co.CustomerPhone = "+370 600 12345";
            co.Remarks       = "Suck it!";

            var product = _productsBll.SaveProductInformationFromPos("12345678901234567890123456789012", posResponse, co);

            Assert.AreEqual("Gilus prisilietimas", product.ProductName);
            Assert.AreEqual("NO", product.ProductDescription);
            Assert.AreEqual(35m, product.ProductPrice);
            Assert.AreEqual("EUR", product.CurrencyCode);
            Assert.AreEqual(1005, product.PosId);
            Assert.IsTrue((validTill - product.ValidTill).TotalSeconds < 1);

            Assert.AreEqual(co.CustomerName, product.CustomerName);
            Assert.AreEqual(co.CustomerEmail, product.CustomerEmail);
            Assert.AreEqual(co.CustomerPhone, product.CustomerPhone);
            Assert.AreEqual(co.Remarks, product.Remarks);

            Assert.AreEqual(PaymentSystems.Paysera, product.PaymentSystem);

            Assert.IsFalse(String.IsNullOrEmpty(product.ProductUid));

            Assert.AreEqual(location.Name, product.PosName);
            Assert.AreEqual(location.City, product.PosCity);
            Assert.AreEqual(location.Address, product.PosAddress);

            Assert.AreEqual(product.PhoneForReservation, phoneReservation);
            Assert.AreEqual(product.EmailForReservation, emailReservation);
        }
        // GET: /Payment/Make/UniquePaymentId
        public ActionResult Make(string id)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Got request to make payment");
                sb.AppendFormat("  POS URL: {0}", Request.UserHostAddress).AppendLine();
                sb.AppendFormat("  UID from POS: `{0}`", id);
                Logger.Info(sb.ToString());

                int posId = Factory.PosBll.GetPosIdFromUid(id);
                PosBdo pos = Factory.PosBll.GetById(posId);

                var posResponse = Factory.SecurityBll.ValidatePosPaymentRequest(pos, id);
                posResponse.PosId = pos.Id;
                SessionStore.PosId = pos.Id;
                if (posResponse.Status != true)
                {
                    Logger.ErrorFormat("POS returns false on request. " + posResponse.Message);
                    throw new BadResponseException(posResponse.Message);
                }

                this.Session["__Product"] = posResponse;

                ProductCheckoutModel model = new ProductCheckoutModel();
                Mapper.CreateMap<PaymentRequestValidationResponse, ProductCheckoutModel>();
                model = Mapper.Map<ProductCheckoutModel>(posResponse);
                model.PosUserUid = id;
                //model.ProductName = posResponse.ProductName;
                model.ProductDuration = posResponse.ProductDuration;
                model.ProductValidTill = Factory.HelperBll.ConvertFromUnixTimestamp(posResponse.ProductValidTillTm);
                model.RequestedAmount = posResponse.RequestedAmountMinor / 100m;
                //model.CurrencyCode = posResponse.CurrencyCode;

                //model.Locations = posResponse.Locations ?? new List<ProductServiceLocation>();
                foreach (var l in model.Locations)
                {
                    l.LatLngCoordinates.LatLngString = Factory.HelperBll.GetLatLngString(l.LatLngCoordinates, MapTypes.YandexMap);
                }

                return View("Checkout", GetLayoutForPos(posId), model);
            }
            catch (IncorrectPaymentParamersException ippex)
            {
                Logger.Error("Incorrect price of product (less than 0)", ippex);
                return Incorrect(Resources.Language.Payment_Make_Error_IncorrectPriceFromRequest);
            }
            catch (System.Net.WebException wex)
            {
                Logger.Error("Probably, there is no connection with POS", wex);
                //throw;
                return Incorrect();
            }
            catch (Newtonsoft.Json.JsonReaderException jre)
            {
                Logger.Error("Incorrect JSON response from POS", jre);
                return Incorrect();
            }
            catch (Exception ex)
            {
                Logger.Error("Error validating payment request from POS", ex);
                //return new RedirectResult(Url.Action("Error"));
                return Incorrect();
            }
        }
        public ActionResult Checkout(string id, ProductCheckoutModel checkout)
        {
            try
            {
                var posResponse = Session["__Product"] as PaymentRequestValidationResponse;
                if (posResponse == null)
                {
                    throw new ArgumentNullException("No product information from POS in session");
                }

                if (checkout.LocationId < 1)
                {
                    ModelState.AddModelError("LocationId", Resources.Language.Payment_Checkout_ChooseLocation);
                }
                if (ModelState.IsValid == false)
                {
                    checkout.ProductName = posResponse.ProductName;
                    checkout.ProductDescription = posResponse.ProductDescription;
                    checkout.ProductDuration = posResponse.ProductDuration;
                    checkout.PosUserUid = id;
                    checkout.ProductValidTill = Factory.HelperBll.ConvertFromUnixTimestamp(posResponse.ProductValidTillTm);
                    checkout.RequestedAmount = posResponse.RequestedAmountMinor / 100m;
                    checkout.Locations = posResponse.Locations ?? new List<ProductServiceLocation>();
                    return View("Checkout", GetLayoutForPos(posResponse.PosId), checkout);
                }

                var product = Factory.GiftsBll.SaveProductInformationFromPos(id, posResponse, checkout);
                var transaction = Factory.TransactionsBll.StartTransaction(id, product);

                var configuration = Factory.ConfigurationBll.Get();

                //Factory.CommunicationBll.SendEmailToClientOnSuccess(product);
                var rq = new PayseraPaymentRequest();

                // Paysera information is stored in POS
                var pos = Factory.PosBll.GetById(posResponse.PosId);
                rq.ProjectId = pos.PayseraPayerId;
                rq.PayseraProjectPassword = pos.PayseraPassword;
                Logger.DebugFormat("  configuring Paysera payment to use payer ID: {0} for POS ID: {1}", rq.ProjectId, pos.Id);

                //rq.OrderId = transaction.PaySystemUid;
                rq.OrderId = transaction.OrderNr;
                rq.AmountToPay = posResponse.RequestedAmountMinor / 100m;
                rq.CurrencyCode = posResponse.CurrencyCode;
                rq.Country = PayseraPaymentRequest.Countries.LT;

                rq.AcceptUrl = Url.Action("accept", "payment", null, Request.Url.Scheme);
                rq.CancelUrl = Url.Action("cancel", "payment", null, Request.Url.Scheme);
                rq.CallbackUrl = Url.Action("callback", "payment", null, Request.Url.Scheme);

                rq.CustomerName = checkout.CustomerName;
                rq.CustomerEmail = checkout.CustomerEmail;
                rq.CustomerPhone = checkout.CustomerPhone;

                rq.PayText = Factory.PosBll.FormatNoteForPayment(pos, product, configuration.MaxLengthOfPayseraNote);
                Logger.Debug("  sending PayText: " + rq.PayText);

                //rq.Language = PayseraPaymentRequest.Languages.LIT;
                //rq.IsTestPayment = configuration.UseTestPayment;
                rq.IsTestPayment = pos.UseTestPayment;
                Logger.InfoFormat("  use test payment for POS #{0}: {1}", pos.Id, pos.UseTestPayment);

                Uri paymentUri = Factory.PayseraBll.PreparePaymentLink(configuration.PayseraPaymentUrl, rq);
                Logger.Info("Redirecting to Paysera:");
                Logger.Info(paymentUri.ToString());

                Logger.InfoFormat("  saving payment order nr in session in case of cancel: `{0}`", rq.OrderId);
                //Session["__PaySystemUid"] = rq.OrderId;
                SessionStore.PaymentOrderNr = rq.OrderId;

                return Redirect(paymentUri.ToString());

            }
            catch (Exception ex)
            {
                Logger.Error("Error processing payment form", ex);
                return Incorrect();
            }
        }
Beispiel #5
0
        public ProductBdo SaveProductInformationFromPos(string posUserUid, PaymentRequestValidationResponse posResponse, ProductCheckoutModel checkout)
        {
            if (posResponse == null)
            {
                throw new ArgumentNullException("Response from POS is NULL");
            }

            ProductBdo product = new ProductBdo();
            PosBdo     pos     = new PosBdo();

            // TODO: use automapper
            Mapper.CreateMap <PaymentRequestValidationResponse, ProductBdo>()
            .ForMember(dest => dest.ProductPrice,
                       orig => orig.MapFrom(x => x.RequestedAmountMinor / 100m))
            .ForMember(dest => dest.ValidTill,
                       orig => orig.MapFrom(x => BllFactory.Current.HelperBll.ConvertFromUnixTimestamp(x.ProductValidTillTm)));

            product = Mapper.Map <ProductBdo>(posResponse);

            if (checkout.LocationId > 0)
            {
                var location = posResponse.Locations.FirstOrDefault(x => x.Id == checkout.LocationId);
                if (location == null)
                {
                    throw new ArgumentOutOfRangeException("LocationId", "Incorrect POS service location!");
                }

                product.PosName             = location.Name;
                product.PosCity             = location.City;
                product.PosAddress          = location.Address;
                product.PhoneForReservation = location.PhoneReservation;
                product.EmailForReservation = location.EmailReservation;
            }



            product.CustomerName  = checkout.CustomerName;
            product.CustomerEmail = checkout.CustomerEmail;
            product.CustomerPhone = checkout.CustomerPhone;
            product.Remarks       = checkout.Remarks;

            product.PaymentSystem = checkout.PaymentSystem;
            product.ProductUid    = Guid.NewGuid().ToString("N");
            product.PosUserUid    = posUserUid;
            product.PaySystemUid  = Guid.NewGuid().ToString("N");

            Logger.Debug("Saving product information from POS and customer form");
            Logger.DebugFormat("  Pass product duration to save: `{0}`", product.ProductDuration);
            Logger.Debug(DumpBll.Dump(product));

            return(_productsDal.SaveProductInformationFromPos(product, pos));
        }