Skip to content

Commit b69ee88

Browse files
committed
Merge remote-tracking branch 'origin/MC-29426' into 2.4-develop-pr4
2 parents ddc0536 + 43e41fa commit b69ee88

File tree

10 files changed

+87
-153
lines changed

10 files changed

+87
-153
lines changed

app/code/Magento/Checkout/Model/GuestPaymentInformationManagement.php

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,13 @@ class GuestPaymentInformationManagement implements \Magento\Checkout\Api\GuestPa
5656
*/
5757
private $logger;
5858

59-
/**
60-
* @var ResourceConnection
61-
*/
62-
private $connectionPool;
63-
6459
/**
6560
* @param \Magento\Quote\Api\GuestBillingAddressManagementInterface $billingAddressManagement
6661
* @param \Magento\Quote\Api\GuestPaymentMethodManagementInterface $paymentMethodManagement
6762
* @param \Magento\Quote\Api\GuestCartManagementInterface $cartManagement
6863
* @param \Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement
6964
* @param \Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory
7065
* @param CartRepositoryInterface $cartRepository
71-
* @param ResourceConnection $connectionPool
7266
* @codeCoverageIgnore
7367
*/
7468
public function __construct(
@@ -77,16 +71,14 @@ public function __construct(
7771
\Magento\Quote\Api\GuestCartManagementInterface $cartManagement,
7872
\Magento\Checkout\Api\PaymentInformationManagementInterface $paymentInformationManagement,
7973
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory,
80-
CartRepositoryInterface $cartRepository,
81-
ResourceConnection $connectionPool = null
74+
CartRepositoryInterface $cartRepository
8275
) {
8376
$this->billingAddressManagement = $billingAddressManagement;
8477
$this->paymentMethodManagement = $paymentMethodManagement;
8578
$this->cartManagement = $cartManagement;
8679
$this->paymentInformationManagement = $paymentInformationManagement;
8780
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
8881
$this->cartRepository = $cartRepository;
89-
$this->connectionPool = $connectionPool ?: ObjectManager::getInstance()->get(ResourceConnection::class);
9082
}
9183

9284
/**
@@ -98,33 +90,23 @@ public function savePaymentInformationAndPlaceOrder(
9890
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
9991
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
10092
) {
101-
$salesConnection = $this->connectionPool->getConnection('sales');
102-
$checkoutConnection = $this->connectionPool->getConnection('checkout');
103-
$salesConnection->beginTransaction();
104-
$checkoutConnection->beginTransaction();
105-
93+
$this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
10694
try {
107-
$this->savePaymentInformation($cartId, $email, $paymentMethod, $billingAddress);
108-
try {
109-
$orderId = $this->cartManagement->placeOrder($cartId);
110-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
111-
throw new CouldNotSaveException(
112-
__($e->getMessage()),
113-
$e
114-
);
115-
} catch (\Exception $e) {
116-
$this->getLogger()->critical($e);
117-
throw new CouldNotSaveException(
118-
__('An error occurred on the server. Please try to place the order again.'),
119-
$e
120-
);
121-
}
122-
$salesConnection->commit();
123-
$checkoutConnection->commit();
95+
$orderId = $this->cartManagement->placeOrder($cartId);
96+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
97+
$this->getLogger()->critical(
98+
'Placing an order with quote_id ' . $cartId . ' is failed: ' . $e->getMessage()
99+
);
100+
throw new CouldNotSaveException(
101+
__($e->getMessage()),
102+
$e
103+
);
124104
} catch (\Exception $e) {
125-
$salesConnection->rollBack();
126-
$checkoutConnection->rollBack();
127-
throw $e;
105+
$this->getLogger()->critical($e);
106+
throw new CouldNotSaveException(
107+
__('An error occurred on the server. Please try to place the order again.'),
108+
$e
109+
);
128110
}
129111

130112
return $orderId;

app/code/Magento/Checkout/Model/PaymentInformationManagement.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\Framework\Exception\CouldNotSaveException;
1010

1111
/**
12-
* Payment information management
12+
* Payment information management service.
1313
*
1414
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1515
*/
@@ -85,6 +85,9 @@ public function savePaymentInformationAndPlaceOrder(
8585
try {
8686
$orderId = $this->cartManagement->placeOrder($cartId);
8787
} catch (\Magento\Framework\Exception\LocalizedException $e) {
88+
$this->getLogger()->critical(
89+
'Placing an order with quote_id ' . $cartId . ' is failed: ' . $e->getMessage()
90+
);
8891
throw new CouldNotSaveException(
8992
__($e->getMessage()),
9093
$e

app/code/Magento/Checkout/Test/Unit/Model/GuestPaymentInformationManagementTest.php

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Magento\Checkout\Test\Unit\Model;
99

10-
use Magento\Framework\App\ResourceConnection;
11-
use Magento\Framework\DB\Adapter\AdapterInterface;
1210
use Magento\Quote\Model\Quote;
1311
use Magento\Quote\Model\Quote\Address;
1412
use Magento\Quote\Model\QuoteIdMask;
@@ -53,11 +51,6 @@ class GuestPaymentInformationManagementTest extends \PHPUnit\Framework\TestCase
5351
*/
5452
private $loggerMock;
5553

56-
/**
57-
* @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
58-
*/
59-
private $resourceConnectionMock;
60-
6154
protected function setUp()
6255
{
6356
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -75,19 +68,14 @@ protected function setUp()
7568
['create']
7669
);
7770
$this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
78-
$this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
79-
->disableOriginalConstructor()
80-
->getMock();
81-
8271
$this->model = $objectManager->getObject(
8372
\Magento\Checkout\Model\GuestPaymentInformationManagement::class,
8473
[
8574
'billingAddressManagement' => $this->billingAddressManagementMock,
8675
'paymentMethodManagement' => $this->paymentMethodManagementMock,
8776
'cartManagement' => $this->cartManagementMock,
8877
'cartRepository' => $this->cartRepositoryMock,
89-
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock,
90-
'connectionPool' => $this->resourceConnectionMock,
78+
'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock
9179
]
9280
);
9381
$objectManager->setBackwardCompatibleProperty($this->model, 'logger', $this->loggerMock);
@@ -104,26 +92,6 @@ public function testSavePaymentInformationAndPlaceOrder()
10492

10593
$billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
10694

107-
$adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
108-
->disableOriginalConstructor()
109-
->getMockForAbstractClass();
110-
$adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
111-
->disableOriginalConstructor()
112-
->getMockForAbstractClass();
113-
114-
$this->resourceConnectionMock->expects($this->at(0))
115-
->method('getConnection')
116-
->with('sales')
117-
->willReturn($adapterMockForSales);
118-
$adapterMockForSales->expects($this->once())->method('beginTransaction');
119-
$adapterMockForSales->expects($this->once())->method('commit');
120-
121-
$this->resourceConnectionMock->expects($this->at(1))
122-
->method('getConnection')
123-
->with('checkout')
124-
->willReturn($adapterMockForCheckout);
125-
$adapterMockForCheckout->expects($this->once())->method('beginTransaction');
126-
$adapterMockForCheckout->expects($this->once())->method('commit');
12795
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
12896
$this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
12997

@@ -146,27 +114,6 @@ public function testSavePaymentInformationAndPlaceOrderException()
146114
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
147115
$billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
148116

149-
$adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
150-
->disableOriginalConstructor()
151-
->getMockForAbstractClass();
152-
$adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
153-
->disableOriginalConstructor()
154-
->getMockForAbstractClass();
155-
156-
$this->resourceConnectionMock->expects($this->at(0))
157-
->method('getConnection')
158-
->with('sales')
159-
->willReturn($adapterMockForSales);
160-
$adapterMockForSales->expects($this->once())->method('beginTransaction');
161-
$adapterMockForSales->expects($this->once())->method('rollback');
162-
163-
$this->resourceConnectionMock->expects($this->at(1))
164-
->method('getConnection')
165-
->with('checkout')
166-
->willReturn($adapterMockForCheckout);
167-
$adapterMockForCheckout->expects($this->once())->method('beginTransaction');
168-
$adapterMockForCheckout->expects($this->once())->method('rollback');
169-
170117
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
171118
$exception = new \Magento\Framework\Exception\CouldNotSaveException(__('DB exception'));
172119
$this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
@@ -236,31 +183,9 @@ public function testSavePaymentInformationAndPlaceOrderWithLocalizedException()
236183

237184
$billingAddressMock->expects($this->once())->method('setEmail')->with($email)->willReturnSelf();
238185

239-
$adapterMockForSales = $this->getMockBuilder(AdapterInterface::class)
240-
->disableOriginalConstructor()
241-
->getMockForAbstractClass();
242-
$adapterMockForCheckout = $this->getMockBuilder(AdapterInterface::class)
243-
->disableOriginalConstructor()
244-
->getMockForAbstractClass();
245-
246-
$this->resourceConnectionMock->expects($this->at(0))
247-
->method('getConnection')
248-
->with('sales')
249-
->willReturn($adapterMockForSales);
250-
$adapterMockForSales->expects($this->once())->method('beginTransaction');
251-
$adapterMockForSales->expects($this->once())->method('rollback');
252-
253-
$this->resourceConnectionMock->expects($this->at(1))
254-
->method('getConnection')
255-
->with('checkout')
256-
->willReturn($adapterMockForCheckout);
257-
$adapterMockForCheckout->expects($this->once())->method('beginTransaction');
258-
$adapterMockForCheckout->expects($this->once())->method('rollback');
259-
260186
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
261187
$phrase = new \Magento\Framework\Phrase(__('DB exception'));
262188
$exception = new \Magento\Framework\Exception\LocalizedException($phrase);
263-
$this->loggerMock->expects($this->never())->method('critical');
264189
$this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
265190

266191
$this->model->savePaymentInformationAndPlaceOrder($cartId, $email, $paymentMock, $billingAddressMock);

app/code/Magento/Checkout/Test/Unit/Model/PaymentInformationManagementTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
157157
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
158158
$phrase = new \Magento\Framework\Phrase(__('DB exception'));
159159
$exception = new \Magento\Framework\Exception\LocalizedException($phrase);
160-
$this->loggerMock->expects($this->never())->method('critical');
161160
$this->cartManagementMock->expects($this->once())->method('placeOrder')->willThrowException($exception);
162161

163162
$this->model->savePaymentInformationAndPlaceOrder($cartId, $paymentMock, $billingAddressMock);

app/code/Magento/SalesRule/Model/Coupon/UpdateCouponUsages.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Magento\SalesRule\Model\Coupon;
99

10-
use Magento\Sales\Model\Order;
10+
use Magento\Sales\Api\Data\OrderInterface;
1111
use Magento\SalesRule\Model\Coupon;
1212
use Magento\SalesRule\Model\ResourceModel\Coupon\Usage;
1313
use Magento\SalesRule\Model\Rule\CustomerFactory;
@@ -59,11 +59,11 @@ public function __construct(
5959
/**
6060
* Executes the current command.
6161
*
62-
* @param Order $subject
62+
* @param OrderInterface $subject
6363
* @param bool $increment
64-
* @return Order
64+
* @return OrderInterface
6565
*/
66-
public function execute(Order $subject, bool $increment): Order
66+
public function execute(OrderInterface $subject, bool $increment): OrderInterface
6767
{
6868
if (!$subject || !$subject->getAppliedRuleIds()) {
6969
return $subject;
@@ -133,11 +133,11 @@ private function updateCustomerRuleUsages(bool $increment, int $ruleId, int $cus
133133
/**
134134
* Update the number of coupon usages.
135135
*
136-
* @param Order $subject
136+
* @param OrderInterface $subject
137137
* @param bool $increment
138138
* @param int $customerId
139139
*/
140-
private function updateCouponUsages(Order $subject, bool $increment, int $customerId): void
140+
private function updateCouponUsages(OrderInterface $subject, bool $increment, int $customerId): void
141141
{
142142
$this->coupon->load($subject->getCouponCode(), 'code');
143143
if ($this->coupon->getId()) {

app/code/Magento/SalesRule/Plugin/CouponUsagesDecrement.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
namespace Magento\SalesRule\Plugin;
99

10-
use Magento\Sales\Model\Order;
10+
use Magento\Sales\Model\OrderRepository;
11+
use Magento\Sales\Model\Service\OrderService;
1112
use Magento\SalesRule\Model\Coupon\UpdateCouponUsages;
1213

1314
/**
@@ -20,30 +21,39 @@ class CouponUsagesDecrement
2021
*/
2122
private $updateCouponUsages;
2223

24+
/**
25+
* @var OrderRepository
26+
*/
27+
private $orderRepository;
28+
2329
/**
2430
* @param UpdateCouponUsages $updateCouponUsages
31+
* @param OrderRepository $orderRepository
2532
*/
2633
public function __construct(
27-
UpdateCouponUsages $updateCouponUsages
34+
UpdateCouponUsages $updateCouponUsages,
35+
OrderRepository $orderRepository
2836
) {
2937
$this->updateCouponUsages = $updateCouponUsages;
38+
$this->orderRepository = $orderRepository;
3039
}
3140

3241
/**
3342
* Decrements number of coupon usages after cancelling order.
3443
*
35-
* @param Order $subject
36-
* @param callable $proceed
37-
* @return Order
44+
* @param OrderService $subject
45+
* @param bool $result
46+
* @param int $orderId
47+
* @return bool
48+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3849
*/
39-
public function aroundCancel(Order $subject, callable $proceed): Order
50+
public function afterCancel(OrderService $subject, bool $result, $orderId): bool
4051
{
41-
$canCancel = $subject->canCancel();
42-
$returnValue = $proceed();
43-
if ($canCancel) {
44-
$returnValue = $this->updateCouponUsages->execute($returnValue, false);
52+
$order = $this->orderRepository->get($orderId);
53+
if ($result) {
54+
$this->updateCouponUsages->execute($order, false);
4555
}
4656

47-
return $returnValue;
57+
return $result;
4858
}
4959
}

app/code/Magento/SalesRule/Plugin/CouponUsagesIncrement.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
namespace Magento\SalesRule\Plugin;
99

10-
use Magento\Sales\Model\Order;
10+
use Magento\Sales\Api\Data\OrderInterface;
11+
use Magento\Sales\Model\Service\OrderService;
1112
use Magento\SalesRule\Model\Coupon\UpdateCouponUsages;
1213

1314
/**
@@ -32,15 +33,15 @@ public function __construct(
3233
/**
3334
* Increments number of coupon usages after placing order.
3435
*
35-
* @param Order $subject
36-
* @param Order $result
37-
* @return Order
36+
* @param OrderService $subject
37+
* @param OrderInterface $result
38+
* @return OrderInterface
3839
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3940
*/
40-
public function afterPlace(Order $subject, Order $result): Order
41+
public function afterPlace(OrderService $subject, OrderInterface $result): OrderInterface
4142
{
42-
$this->updateCouponUsages->execute($subject, true);
43+
$this->updateCouponUsages->execute($result, true);
4344

44-
return $subject;
45+
return $result;
4546
}
4647
}

app/code/Magento/SalesRule/etc/di.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@
185185
<type name="Magento\Quote\Model\Cart\CartTotalRepository">
186186
<plugin name="coupon_label_plugin" type="Magento\SalesRule\Plugin\CartTotalRepository" />
187187
</type>
188-
<type name="Magento\Sales\Model\Order">
189-
<plugin name="coupon_uses_increment_plugin" type="Magento\SalesRule\Plugin\CouponUsagesIncrement" sortOrder="20"/>
188+
<type name="Magento\Sales\Model\Service\OrderService">
190189
<plugin name="coupon_uses_decrement_plugin" type="Magento\SalesRule\Plugin\CouponUsagesDecrement" />
190+
<plugin name="coupon_uses_increment_plugin" type="Magento\SalesRule\Plugin\CouponUsagesIncrement" sortOrder="20"/>
191191
</type>
192192
<preference
193193
for="Magento\SalesRule\Model\Spi\CodeLimitManagerInterface"

0 commit comments

Comments
 (0)