Skip to content

Commit 4e074d0

Browse files
author
Jan Polak
committed
Place order flow refactor
1 parent 5402bc3 commit 4e074d0

15 files changed

+222
-100
lines changed

app/code/Magento/OneTouchOrdering/Block/Button.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
/**
1313
* Class Button
14-
* @package Magento\OneTouchOrdering\Block
1514
* @api
1615
*/
1716
class Button extends Template

app/code/Magento/OneTouchOrdering/Controller/Button/Available.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,17 @@ public function execute()
6666
$customer = $this->customerSession->getCustomer();
6767
$available = $this->oneTouchOrdering->isAvailableForCustomer($customer);
6868
$resultData = [
69-
'available' => $available,
70-
'cards' => $this->customerCardsFormatter->getFormattedCards($customer),
71-
'addresses' => $this->customerAddressesFormatter->getFormattedAddresses($customer),
72-
'defaultShipping' => $customer->getDefaultShippingAddress()->getId(),
73-
'defaultBilling' => $customer->getDefaultBillingAddress()->getId(),
74-
'selectAddressAvailable' => $this->oneTouchOrderingConfig->isSelectAddressEnabled()
69+
'available' => $available
7570
];
71+
if ($available) {
72+
$resultData += [
73+
'cards' => $this->customerCardsFormatter->getFormattedCards($customer),
74+
'addresses' => $this->customerAddressesFormatter->getFormattedAddresses($customer),
75+
'defaultShipping' => $customer->getDefaultShippingAddress()->getId(),
76+
'defaultBilling' => $customer->getDefaultBillingAddress()->getId(),
77+
'selectAddressAvailable' => $this->oneTouchOrderingConfig->isSelectAddressEnabled()
78+
];
79+
}
7680

7781
$result->setData($resultData);
7882

app/code/Magento/OneTouchOrdering/Controller/Button/PlaceOrder.php

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Framework\App\Action\Context;
1111
use Magento\Framework\Controller\Result\Json as JsonResult;
1212
use Magento\Framework\Controller\ResultFactory;
13-
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Data\Form\FormKey\Validator;
1414
use Magento\Framework\Exception\NoSuchEntityException;
1515
use Magento\OneTouchOrdering\Model\CustomerData;
1616
use Magento\OneTouchOrdering\Model\PlaceOrder as PlaceOrderModel;
@@ -43,6 +43,10 @@ class PlaceOrder extends \Magento\Framework\App\Action\Action
4343
* @var CustomerData
4444
*/
4545
private $customerData;
46+
/**
47+
* @var Validator
48+
*/
49+
private $formKeyValidator;
4650

4751
/**
4852
* PlaceOrder constructor.
@@ -53,6 +57,7 @@ class PlaceOrder extends \Magento\Framework\App\Action\Action
5357
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
5458
* @param Session $customerSession
5559
* @param CustomerData $customerData
60+
* @param Validator $formKeyValidator
5661
*/
5762
public function __construct(
5863
Context $context,
@@ -61,7 +66,8 @@ public function __construct(
6166
PlaceOrderModel $placeOrder,
6267
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
6368
Session $customerSession,
64-
CustomerData $customerData
69+
CustomerData $customerData,
70+
Validator $formKeyValidator
6571
) {
6672
parent::__construct($context);
6773
$this->storeManager = $storeManager;
@@ -70,47 +76,31 @@ public function __construct(
7076
$this->orderRepository = $orderRepository;
7177
$this->customerSession = $customerSession;
7278
$this->customerData = $customerData;
79+
$this->formKeyValidator = $formKeyValidator;
7380
}
7481

7582
public function execute()
7683
{
77-
$product = $this->initProduct();
78-
/** @var JsonResult $result */
79-
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
84+
$errorMsg = __('Something went wrong while processing your order. Please try again later.');
8085

86+
if (!$this->formKeyValidator->validate($this->getRequest())) {
87+
return $this->createResponse($errorMsg, false);
88+
}
89+
$product = $this->initProduct();
8190
$params = $this->getRequest()->getParams();
8291
try {
8392
$customerData = $this->customerData->setCustomer($this->customerSession->getCustomer());
8493
$orderId = $this->placeOrder->placeOrder($product, $customerData, $params);
8594
} catch (NoSuchEntityException $e) {
86-
$errorMsg = __('Something went wrong while processing your order. Please try again later.');
87-
$this->messageManager->addErrorMessage($errorMsg);
88-
$result->setData([
89-
'response' => $errorMsg
90-
]);
91-
return $result;
92-
} catch (LocalizedException $e) {
93-
$this->messageManager->addErrorMessage($e->getMessage());
94-
$result->setData([
95-
'response' => $e->getMessage()
96-
]);
97-
return $result;
95+
return $this->createResponse($errorMsg, false);
9896
} catch (Exception $e) {
99-
$errorMsg = __('Something went wrong while processing your order. Please try again later.');
100-
$this->messageManager->addErrorMessage($errorMsg);
101-
$result->setData([
102-
'response' => $e->getMessage()
103-
]);
104-
return $result;
97+
return $this->createResponse($e->getMessage(), false);
10598
}
10699

107100
$order = $this->orderRepository->get($orderId);
108101
$message = __('Your order number is: %1.', $order->getIncrementId());
109-
$this->messageManager->addSuccessMessage($message);
110-
$result->setData([
111-
'response' => $message
112-
]);
113-
return $result;
102+
103+
return $this->createResponse($message, true);
114104
}
115105

116106
/**
@@ -126,4 +116,25 @@ private function initProduct()
126116
}
127117
throw new NoSuchEntityException(__('Requested product doesn\'t exist'));
128118
}
119+
120+
/**
121+
* @param string $message
122+
* @param bool $successMessage
123+
* @return JsonResult
124+
*/
125+
private function createResponse(string $message, bool $successMessage)
126+
{
127+
/** @var JsonResult $result */
128+
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
129+
$result->setData([
130+
'response' => $message
131+
]);
132+
if ($successMessage) {
133+
$this->messageManager->addSuccessMessage($message);
134+
} else {
135+
$this->messageManager->addErrorMessage($message);
136+
}
137+
138+
return $result;
139+
}
129140
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\OneTouchOrdering\Model;
7+
8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Quote\Model\Quote;
10+
11+
class CheapestShippingRateChooserRule implements ShippingRateChooserRuleInterface
12+
{
13+
/**
14+
* @param array $shippingRates
15+
* @return string
16+
*/
17+
public function choose(array $shippingRates): string
18+
{
19+
$rate = array_shift($shippingRates);
20+
foreach ($shippingRates as $tmpRate) {
21+
if ($tmpRate['price'] < $rate['price']) {
22+
$rate = $tmpRate;
23+
}
24+
}
25+
26+
return $rate['code'];
27+
}
28+
}

app/code/Magento/OneTouchOrdering/Model/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function isSelectAddressEnabled(): string
5757
*/
5858
public function getButtonText(): string
5959
{
60-
return $this->getValue(self::ONE_TOUCH_ORDERING_MODULE_BUTTON_TEXT);
60+
return strip_tags($this->getValue(self::ONE_TOUCH_ORDERING_MODULE_BUTTON_TEXT));
6161
}
6262

6363
/**

app/code/Magento/OneTouchOrdering/Model/CustomerCreditCardManager.php

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ public function __construct(
5757
}
5858

5959
/**
60-
* @param $customerId
61-
* @return \Magento\Vault\Api\Data\PaymentTokenInterface
60+
* @param string $customerId
61+
* @param string $cardId
62+
* @return mixed
6263
* @throws LocalizedException
6364
*/
64-
public function getCustomerCreditCard($customerId, $cardId)
65+
public function getCustomerCreditCard(string $customerId, string $cardId)
6566
{
6667
$tokens = $this->getVisibleAvailableTokens($customerId);
6768
if (empty($tokens) || !$cardId || !isset($tokens[$cardId])) {
@@ -72,23 +73,10 @@ public function getCustomerCreditCard($customerId, $cardId)
7273
}
7374

7475
/**
75-
* @param $publicHash
76-
* @param $customerId
77-
* @throws Exception
78-
* @return string
79-
*/
80-
public function getNonce($publicHash, $customerId): string
81-
{
82-
return $this->getNonce->execute(
83-
['public_hash' => $publicHash, 'customer_id' => $customerId]
84-
)->get()['paymentMethodNonce'];
85-
}
86-
87-
/**
88-
* @param $customerId
89-
* @return \Magento\Vault\Api\Data\PaymentTokenInterface[]
76+
* @param string $customerId
77+
* @return array
9078
*/
91-
public function getVisibleAvailableTokens($customerId): array
79+
public function getVisibleAvailableTokens(string $customerId): array
9280
{
9381
$customerFilter = $this->getFilter(\Magento\Vault\Api\Data\PaymentTokenInterface::CUSTOMER_ID, $customerId);
9482
$visibleFilter = $this->getFilter(\Magento\Vault\Api\Data\PaymentTokenInterface::IS_VISIBLE, 1);
@@ -120,16 +108,44 @@ public function getVisibleAvailableTokens($customerId): array
120108
}
121109

122110
/**
123-
* @param $field
111+
* @param string $customerId
112+
* @param string $publicHash
113+
* @return array
114+
*/
115+
public function getPaymentAdditionalInformation(string $customerId, string $publicHash): array
116+
{
117+
return [
118+
'customer_id' => $customerId,
119+
'public_hash' => $publicHash,
120+
'payment_method_nonce' => $this->getNonce($publicHash, $customerId),
121+
'is_active_payment_token_enabler' => true
122+
];
123+
}
124+
125+
/**
126+
* @param string $field
124127
* @param $value
125128
* @return array
126129
*/
127-
private function getFilter($field, $value): array
130+
private function getFilter(string $field, $value): array
128131
{
129132
return [
130133
$this->filterBuilder->setField($field)
131134
->setValue($value)
132135
->create()
133136
];
134137
}
138+
139+
/**
140+
* @param string $publicHash
141+
* @param string $customerId
142+
* @return string
143+
* @throws Exception
144+
*/
145+
private function getNonce(string $publicHash, string $customerId): string
146+
{
147+
return $this->getNonce->execute(
148+
['public_hash' => $publicHash, 'customer_id' => $customerId]
149+
)->get()['paymentMethodNonce'];
150+
}
135151
}

app/code/Magento/OneTouchOrdering/Model/PlaceOrder.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class PlaceOrder
2626
*/
2727
private $prepareQuote;
2828
/**
29-
* @var ShippingRateChooserInterface
29+
* @var ShippingRateChooserRuleInterface
3030
*/
3131
private $shippingRateChooser;
3232
/**
@@ -39,13 +39,14 @@ class PlaceOrder
3939
* @param QuoteRepository $quoteRepository
4040
* @param \Magento\Quote\Api\CartManagementInterface $cartManagementInterface
4141
* @param PrepareQuote $prepareQuote
42-
* @param ShippingRateChooserInterface $shippingRateChooser
42+
* @param ShippingRateChooser $shippingRateChooser
43+
* @param Config $oneTouchOrderingConfig
4344
*/
4445
public function __construct(
4546
QuoteRepository $quoteRepository,
4647
\Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
4748
PrepareQuote $prepareQuote,
48-
ShippingRateChooserInterface $shippingRateChooser,
49+
ShippingRateChooser $shippingRateChooser,
4950
Config $oneTouchOrderingConfig
5051
) {
5152
$this->cartManagementInterface = $cartManagementInterface;
@@ -59,7 +60,7 @@ public function __construct(
5960
* @param Product $product
6061
* @param CustomerData $customerData
6162
* @param array $params
62-
* @throws Exception
63+
* @throws \Exception
6364
* @return int
6465
*/
6566
public function placeOrder(Product $product, CustomerData $customerData, array $params): int

app/code/Magento/OneTouchOrdering/Model/PrepareQuote.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,19 @@ public function prepare(CustomerData $customerData, DataObject $params): Quote
7676

7777
/**
7878
* @param Quote $quote
79+
* @param string $customerId
80+
* @param string $ccId
7981
* @throws Exception
8082
*/
81-
public function preparePayment(Quote $quote, $customerId, $ccId)
83+
public function preparePayment(Quote $quote, string $customerId, string $ccId)
8284
{
8385
$cc = $this->customerCreditCardManager->getCustomerCreditCard($customerId, $ccId);
8486
$publicHash = $cc->getPublicHash();
8587
$quote->getPayment()->setQuote($quote)->importData(
8688
['method' => BrainTreeConfigProvider::CC_VAULT_CODE]
87-
)->setAdditionalInformation([
88-
'customer_id' => $customerId,
89-
'public_hash' => $publicHash,
90-
'payment_method_nonce' => $this->customerCreditCardManager->getNonce($publicHash, $customerId),
91-
'is_active_payment_token_enabler' => true
92-
]);
89+
)->setAdditionalInformation(
90+
$this->customerCreditCardManager->getPaymentAdditionalInformation($customerId, $publicHash)
91+
);
9392
$quote->collectTotals();
9493
}
9594
}

app/code/Magento/OneTouchOrdering/Model/CheapestShippingRateChooser.php renamed to app/code/Magento/OneTouchOrdering/Model/ShippingRateChooser.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@
88
use Magento\Framework\Exception\LocalizedException;
99
use Magento\Quote\Model\Quote;
1010

11-
class CheapestShippingRateChooser implements ShippingRateChooserInterface
11+
class ShippingRateChooser
1212
{
13+
/**
14+
* @var ShippingRateChooserRuleInterface
15+
*/
16+
private $shippingRateChooserRule;
17+
18+
/**
19+
* ShippingRateChooser constructor.
20+
* @param ShippingRateChooserRuleInterface $shippingRateChooserRule
21+
*/
22+
public function __construct(
23+
ShippingRateChooserRuleInterface $shippingRateChooserRule
24+
) {
25+
$this->shippingRateChooserRule = $shippingRateChooserRule;
26+
}
27+
1328
/**
1429
* @param Quote $quote
1530
* @return Quote
@@ -29,19 +44,13 @@ public function choose(Quote $quote): Quote
2944
->getAllShippingRates();
3045
if (empty($shippingRates)) {
3146
throw new LocalizedException(
32-
__('There are no shipping methods available for default shipping address.')
47+
__('There are no shipping methods available.')
3348
);
3449
}
3550

36-
$rate = array_shift($shippingRates);
37-
38-
foreach ($shippingRates as $tmpRate) {
39-
if ($tmpRate['price'] < $rate['price']) {
40-
$rate = $tmpRate;
41-
}
42-
}
43-
$address->setShippingMethod($rate['code']);
51+
$shippingRate = $this->shippingRateChooserRule->choose($shippingRates);
52+
$address->setShippingMethod($shippingRate);
4453

4554
return $quote;
4655
}
47-
}
56+
}

0 commit comments

Comments
 (0)