Advertisement
Guest User

Untitled

a guest
Jan 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.59 KB | None | 0 0
  1. public class AnonymousCheckoutController : StoreController
  2. {
  3. public AnonymousCheckoutController(
  4. Services.ISalesService salesService
  5. , Services.ICartService cartService
  6. , Services.IAccountService accountService
  7. , Services.IEmailerService emailerService
  8. , Services.IDocumentService documentService
  9. , Services.ICacheService cacheService
  10. , Services.IAddressService addressService
  11. , Services.CryptoService cryptoService
  12. , Services.IIncentiveService IncentiveService
  13. )
  14. {
  15. this.SalesService = salesService;
  16. this.CartService = cartService;
  17. this.EmailerService = emailerService;
  18. this.AccountService = accountService;
  19. this.DocumentService = documentService;
  20. this.CacheService = cacheService;
  21. this.CryptoService = cryptoService;
  22. this.AddressService = addressService;
  23. }
  24.  
  25. protected Services.ISalesService SalesService { get; set; }
  26.  
  27. protected Services.ICartService CartService { get; set; }
  28.  
  29. protected Services.IAccountService AccountService { get; set; }
  30.  
  31. protected Services.IEmailerService EmailerService { get; set; }
  32.  
  33. protected Services.IDocumentService DocumentService { get; set; }
  34.  
  35. protected Services.ICacheService CacheService { get; set; }
  36.  
  37. internal Services.CryptoService CryptoService { get; set; }
  38.  
  39. internal Services.IAddressService AddressService { get; set; }
  40.  
  41. protected Services.IIncentiveService IncentiveService { get; private set; }
  42.  
  43. [ActionFilters.ZeroCacheActionFilter]
  44. [ActionFilters.TrackerActionFilter]
  45. public ActionResult Shipping()
  46. {
  47. Logger.Debug("Choix de l'adresse de livraison");
  48. var cart = CartService.GetCurrentOrderCart(User.GetUserPrincipal());
  49. if (cart == null
  50. || cart.ItemCount == 0)
  51. {
  52. return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
  53. }
  54.  
  55. var brokenRules = SalesService.ValidateOrderCart(cart, HttpContext);
  56. ModelState.AddModelErrors(brokenRules);
  57.  
  58. if (!ModelState.IsValid)
  59. {
  60. ViewData.Model = cart;
  61. return RedirectToERPStoreRoute(ERPStoreRoutes.CART);
  62. }
  63.  
  64. var user = User.GetUserPrincipal().CurrentUser;
  65. ViewData.Model = cart;
  66.  
  67. var viewName = "shipping";
  68. int index = -1;
  69. if (user == null)
  70. {
  71. var registration = AccountService.GetRegistrationUser(User.GetUserPrincipal().VisitorId);
  72. if (registration == null)
  73. {
  74. registration = AccountService.CreateRegistrationUser();
  75. bool isNewCustomer = false;
  76. registration.VisitorId = HttpContext.GetOrCreateVisitorId(out isNewCustomer);
  77. AccountService.SaveRegistrationUser(User.GetUserPrincipal().VisitorId, registration);
  78. }
  79. user = AccountService.CreateUserFromRegistration(registration);
  80. ViewData["RegistrationUser"] = registration;
  81. }
  82. else
  83. {
  84. viewName = "connectedshipping";
  85. }
  86.  
  87. if (cart.BillingAddress == null)
  88. {
  89. cart.BillingAddress = user.DefaultAddress;
  90. }
  91.  
  92. if (cart.DeliveryAddress != null)
  93. {
  94. index = user.DeliveryAddressList.FindIndex(i => i.Id == cart.DeliveryAddress.Id);
  95. }
  96. else if (user.LastDeliveredAddress != null)
  97. {
  98. index = user.DeliveryAddressList.FindIndex(i => i.Id == user.LastDeliveredAddress.Id);
  99. cart.DeliveryAddress = user.LastDeliveredAddress;
  100. }
  101. else if (user.DeliveryAddressList.IsNotNullOrEmpty())
  102. {
  103. cart.DeliveryAddress = user.DeliveryAddressList[0];
  104. index = 0;
  105. }
  106. else
  107. {
  108. if (user.DefaultAddress != null)
  109. {
  110. cart.DeliveryAddress = user.DefaultAddress;
  111. }
  112. index = -1;
  113. }
  114.  
  115. using (var ts = TransactionHelper.GetNewReadCommitted())
  116. {
  117. CartService.Save(cart);
  118. ts.Complete();
  119. }
  120.  
  121. ViewData["SelectedAddressId"] = index;
  122.  
  123. return View(viewName);
  124. }
  125.  
  126. [AcceptVerbs(HttpVerbs.Post)]
  127. [ActionFilters.ZeroCacheActionFilter]
  128. [ActionFilters.TrackerActionFilter]
  129. public ActionResult Shipping(string shippingRecipientName, string shippingStreet, string shippingZipCode, string shippingCity, int shippingCountryId
  130. , bool sameBillingAddress
  131. , int addressIndex
  132. , string billingRecipientName, string billingStreet, string billingZipCode, string billingCity, int billingCountryId
  133. , Models.RegistrationUser registrationUser
  134. , string emailConfirmation)
  135. {
  136.  
  137. var cart = CartService.GetCurrentOrderCart(User.GetUserPrincipal());
  138. if (cart == null || cart.ItemCount == 0)
  139. {
  140. return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
  141. }
  142.  
  143. var user = User.GetUserPrincipal().CurrentUser;
  144. ViewData.Model = cart;
  145.  
  146. var shippingAddress = new Models.Address();
  147. var billingAddress = new Models.Address();
  148. Models.RegistrationUser registration = null;
  149.  
  150. if (user == null)
  151. {
  152. shippingAddress.RecipientName = shippingRecipientName;
  153. shippingAddress.Street = shippingStreet;
  154. shippingAddress.ZipCode = shippingZipCode;
  155. shippingAddress.CountryId = shippingCountryId;
  156. shippingAddress.City = shippingCity;
  157.  
  158. var shippingAddressBrokenrules = AccountService.ValidateUserAddress(shippingAddress, HttpContext);
  159. foreach (var item in shippingAddressBrokenrules)
  160. {
  161. item.PropertyName = "shipping" + item.PropertyName;
  162. }
  163. ViewData.ModelState.AddModelErrors(shippingAddressBrokenrules);
  164.  
  165. if (!sameBillingAddress)
  166. {
  167. billingAddress.RecipientName = billingRecipientName;
  168. billingAddress.Street = billingStreet;
  169. billingAddress.ZipCode = billingZipCode;
  170. billingAddress.CountryId = billingCountryId;
  171. billingAddress.City = billingCity;
  172.  
  173. var billingAddressBrokenrules = AccountService.ValidateUserAddress(billingAddress, HttpContext);
  174. foreach (var item in billingAddressBrokenrules)
  175. {
  176. item.PropertyName = "billing" + item.PropertyName;
  177. }
  178. ModelState.AddModelErrors(billingAddressBrokenrules);
  179. }
  180.  
  181. // Pour passer tout test
  182. registrationUser.Password = (registrationUser.Password.IsNullOrTrimmedEmpty()) ? "1234567489abcdefg" : registrationUser.Password;
  183.  
  184. var registrationUserBrokenRules = AccountService.ValidateRegistrationUser(registrationUser, HttpContext);
  185. ModelState.AddModelErrors(registrationUserBrokenRules);
  186.  
  187. registration = AccountService.GetRegistrationUser(User.GetUserPrincipal().VisitorId);
  188. if (registration == null)
  189. {
  190. // Ce cas ne doit etre possible normalement
  191. registration = AccountService.CreateRegistrationUser();
  192. }
  193.  
  194. if (registration.Email.IsNullOrTrimmedEmpty()
  195. && (emailConfirmation.IsNullOrTrimmedEmpty()
  196. || !registrationUser.Email.Equals(emailConfirmation, StringComparison.InvariantCultureIgnoreCase)))
  197. {
  198. ModelState.AddModelError("emailConfirmation", "L'Email indiqué n'est pas confirmé");
  199. }
  200.  
  201. // Adresse de livraison
  202. registration.ShippingAddressCity = shippingAddress.City;
  203. registration.ShippingAddressCountryId = shippingAddress.CountryId;
  204. registration.ShippingAddressRecipientName = shippingAddress.RecipientName;
  205. registration.ShippingAddressRegion = shippingAddress.Region;
  206. registration.ShippingAddressStreet = shippingAddress.Street;
  207. registration.ShippingAddressZipCode = shippingAddress.ZipCode;
  208.  
  209. registration.IsSameBillingAddress = sameBillingAddress;
  210. if (!sameBillingAddress)
  211. {
  212. // Adresse de facturation
  213. registration.BillingAddressCity = billingAddress.City;
  214. registration.BillingAddressCountryId = billingAddress.CountryId;
  215. registration.BillingAddressRecipientName = billingAddress.RecipientName;
  216. registration.BillingAddressRegion = billingAddress.Region;
  217. registration.BillingAddressStreet = billingAddress.Street;
  218. registration.BillingAddressZipCode = billingAddress.ZipCode;
  219. }
  220.  
  221. // Informations sur la société
  222. registration.CorporateEmail = registrationUser.CorporateEmail;
  223. registration.CorporateFaxNumber = registrationUser.CorporateFaxNumber;
  224. registration.CorporateName = registrationUser.CorporateName;
  225. registration.CorporatePhoneNumber = registrationUser.CorporatePhoneNumber;
  226. registration.CorporateSocialStatus = registrationUser.CorporateSocialStatus;
  227. registration.CorporateWebSite = registrationUser.CorporateWebSite;
  228. registration.FaxNumber = registrationUser.FaxNumber;
  229. registration.NAFCode = registrationUser.NAFCode;
  230. registration.SiretNumber = registrationUser.SiretNumber;
  231. registration.VATNumber = registrationUser.TVANumber;
  232. registration.VatMandatory = registrationUser.VatMandatory;
  233. registration.RcsNumber = registrationUser.RcsNumber;
  234.  
  235. // Informations personnelles
  236. registration.Email = registrationUser.Email;
  237. registration.FirstName = registrationUser.FirstName;
  238. registration.LastName = registrationUser.LastName;
  239. registration.MobileNumber = registrationUser.MobileNumber;
  240. // registration.Password = registrationUser.Password;
  241. registration.PhoneNumber = registrationUser.PhoneNumber;
  242. registration.PresentationId = registrationUser.PresentationId;
  243. registration.ReturnUrl = registrationUser.ReturnUrl;
  244.  
  245. AccountService.SaveRegistrationUser(User.GetUserPrincipal().VisitorId, registration);
  246. user = AccountService.CreateUserFromRegistration(registration);
  247. }
  248. else
  249. {
  250. if (addressIndex == -1) // Cas d'une nouvelle adresse
  251. {
  252. shippingAddress.RecipientName = shippingRecipientName;
  253. shippingAddress.Street = shippingStreet;
  254. shippingAddress.ZipCode = shippingZipCode;
  255. shippingAddress.CountryId = shippingCountryId;
  256. shippingAddress.City = shippingCity;
  257.  
  258. var shippingAddressBrokenrules = AccountService.ValidateUserAddress(shippingAddress, HttpContext);
  259. foreach (var item in shippingAddressBrokenrules)
  260. {
  261. item.PropertyName = "shipping" + item.PropertyName;
  262. }
  263. ViewData.ModelState.AddModelErrors(shippingAddressBrokenrules);
  264.  
  265. if (ModelState.IsValid)
  266. {
  267. AddressService.SaveAddress(user, shippingAddress, true);
  268. }
  269. }
  270. }
  271.  
  272. ViewData["SelectedAddressId"] = addressIndex;
  273.  
  274. Models.Address address = null;
  275. if (addressIndex == -1) // Nouvelle adresse
  276. {
  277. address = shippingAddress;
  278. user.DeliveryAddressList.Add(address);
  279. }
  280. else
  281. {
  282. address = user.DeliveryAddressList[addressIndex];
  283. }
  284.  
  285. cart.DeliveryAddress = address;
  286.  
  287. if (sameBillingAddress)
  288. {
  289. cart.BillingAddress = cart.DeliveryAddress;
  290. }
  291. else
  292. {
  293. cart.BillingAddress = user.DefaultAddress;
  294. }
  295.  
  296. if (!ModelState.IsValid)
  297. {
  298. ViewData["RegistrationUser"] = registration;
  299. if (registration != null)
  300. {
  301. return View("shipping");
  302. }
  303. else
  304. {
  305. return View("connectedshipping");
  306. }
  307. }
  308.  
  309. using (var ts = TransactionHelper.GetNewReadCommitted())
  310. {
  311. CartService.Save(cart);
  312. ts.Complete();
  313. }
  314.  
  315. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT_CONFIGURATION);
  316. }
  317.  
  318. [ActionFilters.ZeroCacheActionFilter]
  319. [ActionFilters.TrackerActionFilter]
  320. public ActionResult Configuration()
  321. {
  322. Logger.Debug("Configuration of order");
  323. var cart = CartService.GetCurrentOrderCart(User.GetUserPrincipal());
  324. if (cart == null
  325. || cart.ItemCount == 0)
  326. {
  327. return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
  328. }
  329.  
  330. var user = User.GetUserPrincipal().CurrentUser;
  331.  
  332. if (user != null)
  333. {
  334. if (cart.DeliveryAddress == null
  335. || cart.BillingAddress == null)
  336. {
  337. // Dans le cas d'un user connecté l'etape de saisie
  338. // des adresses n'a pas été réalisée, on y retourne
  339. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  340. }
  341. }
  342. else
  343. {
  344. // Cas d'un vistieur anonyme
  345. var registration = AccountService.GetRegistrationUser(User.GetUserPrincipal().VisitorId);
  346. if (registration == null)
  347. {
  348. // La session à expirée ou tout autre chose
  349. // on retourne sur l'etape de saisie des adresses
  350. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  351. }
  352. }
  353.  
  354. ViewData.Model = cart;
  355. return View();
  356. }
  357.  
  358. [AcceptVerbs(HttpVerbs.Post)]
  359. [ActionFilters.ZeroCacheActionFilter]
  360. [ActionFilters.TrackerActionFilter]
  361. public ActionResult Configuration(string message, string documentReference, string partialDelivery, int conveyorIndex)
  362. {
  363. var cart = CartService.GetCurrentOrderCart(User.GetUserPrincipal());
  364. if (cart == null
  365. || cart.ItemCount == 0)
  366. {
  367. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  368. }
  369.  
  370. var user = User.GetUserPrincipal().CurrentUser;
  371. ViewData.Model = cart;
  372.  
  373. Models.Conveyor conveyor = null;
  374.  
  375. try
  376. {
  377. conveyor = ERPStoreApplication.WebSiteSettings.Shipping.ConveyorList[conveyorIndex];
  378. }
  379. catch (Exception ex)
  380. {
  381. conveyor = ERPStoreApplication.WebSiteSettings.Shipping.DefaultConveyor;
  382. Logger.Warn(ex.Message);
  383. }
  384.  
  385. cart.Message = message;
  386. cart.CustomerDocumentReference = documentReference;
  387. cart.AllowPartialDelivery = (partialDelivery == "true");
  388. cart.Conveyor = conveyor;
  389.  
  390. // On recalcule les frais de port si le transporteur à changé
  391. SalesService.CalculateShippingFee(cart, user);
  392.  
  393. using (var ts = TransactionHelper.GetNewReadCommitted())
  394. {
  395. CartService.Save(cart);
  396. ts.Complete();
  397. }
  398.  
  399. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT_PAYMENT);
  400. }
  401.  
  402. [ActionFilters.ZeroCacheActionFilter]
  403. [ActionFilters.TrackerActionFilter]
  404. public ActionResult Payment()
  405. {
  406. Logger.Debug("Choosing payment mode");
  407. var cart = CartService.GetCurrentOrderCart(User.GetUserPrincipal());
  408. if (cart == null
  409. || cart.ItemCount == 0)
  410. {
  411. return RedirectToERPStoreRoute(ERPStore.ERPStoreRoutes.HOME);
  412. }
  413.  
  414. var user = User.GetUserPrincipal().CurrentUser;
  415.  
  416. if (user != null
  417. && (cart.DeliveryAddress == null
  418. || cart.BillingAddress == null))
  419. {
  420. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  421. }
  422.  
  423. var paymentList = SalesService.GetPaymentList(cart, User.GetUserPrincipal());
  424. ViewData["paymentList"] = paymentList;
  425.  
  426. ViewData.Model = cart;
  427. return View();
  428. }
  429.  
  430. [AcceptVerbs(HttpVerbs.Post)]
  431. [ActionFilters.ZeroCacheActionFilter]
  432. [ActionFilters.TrackerActionFilter]
  433. public ActionResult Payment(string paymentModeName)
  434. {
  435. var cart = CartService.GetCurrentOrderCart(User.GetUserPrincipal());
  436.  
  437. if (cart == null
  438. || cart.ItemCount == 0)
  439. {
  440. return RedirectToERPStoreRoute(ERPStore.ERPStoreRoutes.CHECKOUT);
  441. }
  442.  
  443. var user = User.GetUserPrincipal().CurrentUser;
  444.  
  445. if (user != null
  446. && (cart.DeliveryAddress == null
  447. || cart.BillingAddress == null))
  448. {
  449. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  450. }
  451.  
  452. if (paymentModeName.IsNullOrTrimmedEmpty())
  453. {
  454. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT_PAYMENT);
  455. }
  456.  
  457. ViewData.Model = cart;
  458.  
  459. var paymentList = SalesService.GetPaymentList(cart, User.GetUserPrincipal());
  460. ViewData["paymentList"] = paymentList;
  461. var selectedPayment = paymentList.SingleOrDefault(i => i.Name.Equals(paymentModeName, StringComparison.InvariantCultureIgnoreCase));
  462.  
  463. if (selectedPayment == null)
  464. {
  465. Logger.Warn("Payment mode not authorized");
  466. ViewData.ModelState.AddModelError("_FORM", "Vous devez selectionner un mode de règlement valide");
  467. return View("payment");
  468. }
  469.  
  470. cart.PaymentModeName = selectedPayment.Name;
  471. Logger.Info("Choosing payment mode : {0}", selectedPayment.Name);
  472. using (var ts = TransactionHelper.GetNewReadCommitted())
  473. {
  474. CartService.Save(cart);
  475. ts.Complete();
  476. }
  477.  
  478. string routeName = selectedPayment.ConfirmationRouteName;
  479. return RedirectToERPStoreRoute(routeName);
  480. }
  481.  
  482. [ActionFilters.ZeroCacheActionFilter]
  483. [ActionFilters.TrackerActionFilter]
  484. public ActionResult Confirmation()
  485. {
  486. var cart = CartService.GetCurrentOrderCart(User.GetUserPrincipal());
  487. if (cart == null
  488. || cart.ItemCount == 0)
  489. {
  490. return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
  491. }
  492.  
  493. var user = User.GetUserPrincipal().CurrentUser;
  494.  
  495. if (user != null)
  496. {
  497. if (cart.DeliveryAddress == null
  498. || cart.BillingAddress == null)
  499. {
  500. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  501. }
  502. }
  503. else
  504. {
  505. var registration = AccountService.GetRegistrationUser(User.GetUserPrincipal().VisitorId);
  506. if (registration == null)
  507. {
  508. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  509. }
  510. user = AccountService.CreateUserFromRegistration(registration);
  511. cart.BillingAddress = user.DefaultAddress;
  512. cart.DeliveryAddress = user.LastDeliveredAddress;
  513. }
  514.  
  515. if (cart.PaymentModeName.IsNullOrTrimmedEmpty())
  516. {
  517. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT_PAYMENT);
  518. }
  519.  
  520. var paymentList = SalesService.GetPaymentList(cart, User.GetUserPrincipal());
  521. var selectedPayment = paymentList.SingleOrDefault(i => i.Name.Equals(cart.PaymentModeName, StringComparison.InvariantCultureIgnoreCase));
  522.  
  523. if (selectedPayment == null)
  524. {
  525. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT_PAYMENT);
  526. }
  527.  
  528. SalesService.ProcessExport(cart, User.GetUserPrincipal().CurrentUser);
  529. CartService.ApplyProductStockInfoList(cart as Models.OrderCart);
  530.  
  531. ViewData.Model = cart;
  532. if (selectedPayment.ConfirmationViewName.StartsWith("~"))
  533. {
  534. return View(selectedPayment.ConfirmationViewName);
  535. }
  536. return View(selectedPayment.ConfirmationViewName);
  537. }
  538.  
  539. [AcceptVerbs(HttpVerbs.Get)]
  540. [ActionFilters.ZeroCacheActionFilter]
  541. [ActionFilters.TrackerActionFilter]
  542. public ActionResult DirectConfirmation(string cartId)
  543. {
  544. var cart = CartService.GetActiveCartById(cartId) as Models.OrderCart;
  545. if (cart == null || cart.ItemCount == 0)
  546. {
  547. return View("EmptyCart");
  548. }
  549. if (!cart.CustomerId.HasValue)
  550. {
  551. bool isNewVisitor = false;
  552. var userId = this.HttpContext.GetOrCreateVisitorId(out isNewVisitor);
  553. if (cart.VisitorId != userId)
  554. {
  555. var registrationUser = AccountService.GetRegistrationUser(cart.VisitorId);
  556. if (registrationUser != null
  557. && !registrationUser.UserId.HasValue)
  558. {
  559. registrationUser.VisitorId = userId;
  560. AccountService.SaveRegistrationUser(userId, registrationUser);
  561. }
  562.  
  563. cart.VisitorId = userId;
  564. CartService.Save(cart);
  565. CartService.ChangeCurrentCart(cart.Code, User.GetUserPrincipal());
  566. }
  567. }
  568. else
  569. {
  570. Response.AddAuthenticatedCookie(cart.CustomerId.Value, true);
  571. }
  572.  
  573. var paymentList = SalesService.GetPaymentList(cart, User.GetUserPrincipal());
  574. var selectedPayment = paymentList.SingleOrDefault(i => i.Name.Equals(cart.PaymentModeName, StringComparison.InvariantCultureIgnoreCase));
  575.  
  576. string routeName = selectedPayment.ConfirmationRouteName;
  577. return RedirectToERPStoreRoute(routeName);
  578. }
  579.  
  580. [AcceptVerbs(HttpVerbs.Post)]
  581. [ActionFilters.ZeroCacheActionFilter]
  582. [ActionFilters.TrackerActionFilter]
  583. public ActionResult Confirmation(string condition)
  584. {
  585. bool confirmation = (condition == "on");
  586. var cart = CartService.GetCurrentOrderCart(User.GetUserPrincipal());
  587.  
  588. if (cart == null
  589. || cart.ItemCount == 0)
  590. {
  591. return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
  592. }
  593.  
  594. var user = User.GetUserPrincipal().CurrentUser;
  595.  
  596. if (user != null)
  597. {
  598. if (cart.DeliveryAddress == null
  599. || cart.BillingAddress == null)
  600. {
  601. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  602. }
  603. }
  604. else
  605. {
  606. var registration = AccountService.GetRegistrationUser(User.GetUserPrincipal().VisitorId);
  607. if (registration == null)
  608. {
  609. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  610. }
  611. user = AccountService.CreateUserFromRegistration(registration);
  612. cart.BillingAddress = user.DefaultAddress;
  613. cart.DeliveryAddress = user.LastDeliveredAddress;
  614. }
  615.  
  616. var brokenRules = SalesService.ValidateOrderCart(cart, HttpContext);
  617. ModelState.AddModelErrors(brokenRules);
  618.  
  619. if (!ModelState.IsValid)
  620. {
  621. ViewData.Model = cart;
  622. return RedirectToERPStoreRoute(ERPStoreRoutes.CART);
  623. }
  624.  
  625. var paymentList = SalesService.GetPaymentList(cart, User.GetUserPrincipal());
  626. var selectedPayment = paymentList.SingleOrDefault(i => i.Name.Equals(cart.PaymentModeName, StringComparison.InvariantCultureIgnoreCase));
  627.  
  628. if (!confirmation)
  629. {
  630. ViewData.Model = cart;
  631. ModelState.AddModelError("condition", "Vous devez accepter nos conditions de ventes pour pouvoir enregistrer cette commande");
  632.  
  633. return View(selectedPayment.ConfirmationViewName);
  634. }
  635.  
  636. // Dans le cas ou un petit malin sauterait
  637. // l'etape de la confirmation on recalcule les frais de port
  638. SalesService.CalculateShippingFee(cart, User.GetUserPrincipal().CurrentUser);
  639.  
  640. cart.AcceptCondition = true;
  641. using (var ts = TransactionHelper.GetNewReadCommitted())
  642. {
  643. CartService.Save(cart);
  644. ts.Complete();
  645. }
  646. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT_FINALIZE);
  647. }
  648.  
  649. [ActionFilters.ZeroCacheActionFilter]
  650. [ActionFilters.TrackerActionFilter]
  651. public ActionResult Finalize()
  652. {
  653. Models.ISaleDocument order = null;
  654. var cart = CartService.GetCurrentOrderCart(User.GetUserPrincipal());
  655. if (cart == null
  656. || cart.ItemCount == 0)
  657. {
  658. return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
  659. }
  660. ViewData.Model = cart;
  661.  
  662. var user = User.GetUserPrincipal().CurrentUser;
  663.  
  664. if (user != null
  665. && (cart.DeliveryAddress == null
  666. || cart.BillingAddress == null))
  667. {
  668. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  669. }
  670.  
  671. if (!cart.AcceptCondition)
  672. {
  673. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT_CONFIRMATION);
  674. }
  675.  
  676. var brokenRules = SalesService.ValidateOrderCart(cart, HttpContext);
  677. ModelState.AddModelErrors(brokenRules);
  678.  
  679. if (!ModelState.IsValid)
  680. {
  681. return RedirectToERPStoreRoute(ERPStoreRoutes.CART);
  682. }
  683.  
  684. bool isNewCustomer = false;
  685. string password = string.Empty;
  686. // l'etape de la confirmation on recalcule les frais de port
  687. if (user == null)
  688. {
  689. var registration = AccountService.GetRegistrationUser(User.GetUserPrincipal().VisitorId);
  690. if (registration == null)
  691. {
  692. return RedirectToERPStoreRoute(ERPStoreRoutes.CHECKOUT);
  693. }
  694.  
  695. password = registration.Password;
  696. if (registration.IsSameBillingAddress)
  697. {
  698. registration.BillingAddressCity = registration.ShippingAddressCity;
  699. registration.BillingAddressCountryId = registration.ShippingAddressCountryId;
  700. registration.BillingAddressRecipientName = registration.ShippingAddressRecipientName;
  701. registration.BillingAddressRegion = registration.ShippingAddressRegion;
  702. registration.BillingAddressStreet = registration.ShippingAddressStreet;
  703. registration.BillingAddressZipCode = registration.ShippingAddressZipCode;
  704. }
  705.  
  706. try
  707. {
  708. user = AccountService.RegisterUser(registration);
  709. Response.AddAuthenticatedCookie(user.Id, true);
  710. isNewCustomer = true;
  711.  
  712. bool isNewVisitor = false;
  713. EventPublisherService.Publish(new Models.Events.UserAuthenticatedEvent()
  714. {
  715. UserId = user.Id,
  716. VisitorId = HttpContext.GetOrCreateVisitorId(out isNewVisitor),
  717. });
  718. }
  719. catch(Exception ex)
  720. {
  721. LogError(Logger, ex);
  722. ModelState.AddModelError("_FORM", "Un problème technique empèche la creation de votre commande, veuillez reessayer ultérieurement");
  723. }
  724.  
  725. // Affectation de l'adresse de livraison
  726. cart.DeliveryAddress = new ERPStore.Models.Address();
  727. cart.DeliveryAddress.City = registration.ShippingAddressCity;
  728. cart.DeliveryAddress.CountryId = registration.ShippingAddressCountryId;
  729. cart.DeliveryAddress.RecipientName = registration.ShippingAddressRecipientName;
  730. cart.DeliveryAddress.Region = registration.ShippingAddressRegion;
  731. cart.DeliveryAddress.Street = registration.ShippingAddressStreet;
  732. cart.DeliveryAddress.ZipCode = registration.ShippingAddressZipCode;
  733.  
  734. // Affectation de l'adresse de facturation
  735. cart.BillingAddress = user.DefaultAddress;
  736. cart.CustomerId = user.Id;
  737. }
  738.  
  739. var paymentList = SalesService.GetPaymentList(cart, User.GetUserPrincipal());
  740. var selectedPayment = paymentList.SingleOrDefault(i => i.Name.Equals(cart.PaymentModeName, StringComparison.InvariantCultureIgnoreCase));
  741.  
  742. if (!ModelState.IsValid)
  743. {
  744. return View(selectedPayment.ConfirmationViewName);
  745. }
  746.  
  747. try
  748. {
  749. // Calcul des taxes
  750. SalesService.CalculateShippingFee(cart, user);
  751. // Traitement de l'export
  752. SalesService.ProcessExport(cart, user);
  753. // création de la commande
  754. order = SalesService.CreateOrderFromCart(user, cart);
  755. using (var ts = TransactionHelper.GetNewReadCommitted())
  756. {
  757. cart.ConvertedEntityId = order.Id;
  758. CartService.Save(cart);
  759. ts.Complete();
  760. }
  761. }
  762. catch (Exception ex)
  763. {
  764. LogError(Logger, ex);
  765. ModelState.AddModelError("_FORM", "Un problème technique empèche la creation de votre commande, veuillez reessayer ultérieurement");
  766. }
  767.  
  768. if (isNewCustomer)
  769. {
  770. AccountService.CloseRegistrationUser(User.GetUserPrincipal().VisitorId, user.Id);
  771. }
  772.  
  773. if (!ModelState.IsValid)
  774. {
  775. return View(selectedPayment.ConfirmationViewName);
  776. }
  777.  
  778. // Préparation d'un paramètre encrypté
  779. var subject = new
  780. {
  781. OrderCode = order.Code,
  782. DocumentType = (order.Document == ERPStore.Models.SaleDocumentType.Order) ? "1" : "2",
  783. ExpirationDate = DateTime.Now.AddDays(1),
  784. IsNewCustomer = isNewCustomer,
  785. Password = password,
  786. };
  787. var key = CryptoService.Encrypt(subject);
  788.  
  789. return RedirectToERPStoreRoute(selectedPayment.FinalizedRouteName, new { key = key });
  790. }
  791.  
  792. [Authorize(Roles = "customer")]
  793. [ActionFilters.ZeroCacheActionFilter]
  794. [ActionFilters.TrackerActionFilter]
  795. public ActionResult Finalized(string key)
  796. {
  797. Models.ISaleDocument order = null;
  798. var subject = new
  799. {
  800. Code = string.Empty,
  801. DocumentType = string.Empty,
  802. ExpirationDate = DateTime.MinValue,
  803. IsNewCustomer = false,
  804. Password = string.Empty,
  805. };
  806. var result = CryptoService.Decrypt(key, subject);
  807. var code = Convert.ToString(result[0]);
  808. var documentType = Convert.ToString(result[1]);
  809. var expirationDate = Convert.ToDateTime(result[2]);
  810. var isNewCustomer = Convert.ToBoolean(result[3]);
  811. var password = Convert.ToString(result[4]);
  812. if (expirationDate < DateTime.Now)
  813. {
  814. // Tentative probable de hack
  815. Logger.Warn("Hack on order {0}", code);
  816. return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
  817. }
  818.  
  819. if (documentType == "1")
  820. {
  821. order = SalesService.GetOrderByCode(code);
  822. }
  823. else
  824. {
  825. order = SalesService.GetQuoteByCode(code);
  826. }
  827.  
  828. if (order == null)
  829. {
  830. return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
  831. }
  832.  
  833. //var user = User.GetUserPrincipal().CurrentUser;
  834.  
  835. //if (order.User.Id != user.Id)
  836. //{
  837. // return RedirectToERPStoreRoute(ERPStoreRoutes.HOME);
  838. //}
  839.  
  840. // Envoi du mail de confirmation de commande
  841. try
  842. {
  843. if (isNewCustomer)
  844. {
  845. EmailerService.SendNewCustomerOrderConfirmation(this, order, password);
  846. }
  847. else
  848. {
  849. EmailerService.SendOrderConfirmation(this, order);
  850. }
  851. }
  852. catch (Exception ex)
  853. {
  854. LogError(Logger, ex);
  855. }
  856.  
  857. var paymentList = SalesService.GetPaymentList();
  858. var selectedPayment = paymentList.SingleOrDefault(i => i.Name.Equals(order.PaymentModeName, StringComparison.InvariantCultureIgnoreCase));
  859.  
  860. ViewData.Model = order;
  861. return View(selectedPayment.FinalizedViewName);
  862. }
  863. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement