@@ -532,19 +532,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
532
532
);
533
533
$ this ->quoteRepository ->save ($ quote );
534
534
} catch (\Exception $ e ) {
535
- if (!empty ($ this ->addressesToSync )) {
536
- foreach ($ this ->addressesToSync as $ addressId ) {
537
- $ this ->addressRepository ->deleteById ($ addressId );
538
- }
539
- }
540
- $ this ->eventManager ->dispatch (
541
- 'sales_model_service_quote_submit_failure ' ,
542
- [
543
- 'order ' => $ order ,
544
- 'quote ' => $ quote ,
545
- 'exception ' => $ e
546
- ]
547
- );
535
+ $ this ->rollbackAddresses ($ quote , $ order , $ e );
548
536
throw $ e ;
549
537
}
550
538
return $ order ;
@@ -611,4 +599,41 @@ protected function _prepareCustomerQuote($quote)
611
599
$ shipping ->setIsDefaultBilling (true );
612
600
}
613
601
}
602
+
603
+ /**
604
+ * Remove related to order and quote addresses and submit exception to further processing.
605
+ *
606
+ * @param Quote $quote
607
+ * @param \Magento\Sales\Api\Data\OrderInterface $order
608
+ * @param \Exception $e
609
+ * @throws \Exception
610
+ */
611
+ private function rollbackAddresses (
612
+ QuoteEntity $ quote ,
613
+ \Magento \Sales \Api \Data \OrderInterface $ order ,
614
+ \Exception $ e
615
+ ): void {
616
+ try {
617
+ if (!empty ($ this ->addressesToSync )) {
618
+ foreach ($ this ->addressesToSync as $ addressId ) {
619
+ $ this ->addressRepository ->deleteById ($ addressId );
620
+ }
621
+ }
622
+ $ this ->eventManager ->dispatch (
623
+ 'sales_model_service_quote_submit_failure ' ,
624
+ [
625
+ 'order ' => $ order ,
626
+ 'quote ' => $ quote ,
627
+ 'exception ' => $ e ,
628
+ ]
629
+ );
630
+ } catch (\Exception $ consecutiveException ) {
631
+ $ message = sprintf (
632
+ "An exception occurred on 'sales_model_service_quote_submit_failure' event: %s " ,
633
+ $ consecutiveException ->getMessage ()
634
+ );
635
+
636
+ throw new \Exception ($ message , 0 , $ e );
637
+ }
638
+ }
614
639
}
0 commit comments