Skip to content

Commit 89c45e7

Browse files
author
Volodymyr Kublytskyi
committed
Merge magento-partners/magento2ce#23.
2 parents 8462688 + 401f729 commit 89c45e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2916
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\InstantPurchase\Block;
7+
8+
use Magento\Framework\View\Element\Template;
9+
use Magento\Framework\View\Element\Template\Context;
10+
use Magento\InstantPurchase\Model\Config;
11+
12+
/**
13+
* Class Button
14+
* @api
15+
*/
16+
class Button extends Template
17+
{
18+
/**
19+
* @var Config
20+
*/
21+
private $instantPurchaseConfig;
22+
23+
/**
24+
* Button constructor.
25+
* @param Context $context
26+
* @param Config $instantPurchaseConfig
27+
* @param array $data
28+
*/
29+
public function __construct(
30+
Context $context,
31+
Config $instantPurchaseConfig,
32+
array $data = []
33+
) {
34+
parent::__construct($context, $data);
35+
$this->instantPurchaseConfig = $instantPurchaseConfig;
36+
}
37+
38+
/**
39+
* @return string
40+
*/
41+
public function getJsLayout(): string
42+
{
43+
$buttonText = $this->instantPurchaseConfig->getButtonText();
44+
$this->jsLayout['components']['instant-purchase']['config']['buttonText'] = $buttonText;
45+
$this->jsLayout['components']['instant-purchase']['config']['purchaseUrl'] =
46+
$this->getUrl('instantpurchase/button/placeOrder', ['_secure' => true]);
47+
48+
return parent::getJsLayout();
49+
}
50+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\InstantPurchase\Controller\Button;
7+
8+
use Magento\Framework\App\Action\Action;
9+
use Magento\Framework\App\Action\Context;
10+
use Magento\Framework\Controller\Result\Json as JsonResult;
11+
use Magento\Framework\Controller\ResultFactory;
12+
use Magento\InstantPurchase\Model\Config;
13+
use Magento\InstantPurchase\Model\CustomerAddressesFormatter;
14+
use Magento\InstantPurchase\Model\CustomerCardsFormatter;
15+
use Magento\InstantPurchase\Model\InstantPurchase;
16+
use Magento\Customer\Model\Session;
17+
18+
class Available extends Action
19+
{
20+
/**
21+
* @var InstantPurchase
22+
*/
23+
private $instantPurchase;
24+
/**
25+
* @var Session
26+
*/
27+
private $customerSession;
28+
/**
29+
* @var CustomerAddressesFormatter
30+
*/
31+
private $customerAddressesFormatter;
32+
/**
33+
* @var Config
34+
*/
35+
private $instantPurchaseConfig;
36+
/**
37+
* @var CustomerCardsFormatter
38+
*/
39+
private $customerCardsFormatter;
40+
41+
public function __construct(
42+
Context $context,
43+
InstantPurchase $instantPurchase,
44+
Session $customerSession,
45+
CustomerAddressesFormatter $customerAddressesFormatter,
46+
CustomerCardsFormatter $customerCardsFormatter,
47+
Config $instantPurchaseConfig
48+
) {
49+
parent::__construct($context);
50+
$this->instantPurchase = $instantPurchase;
51+
$this->customerSession = $customerSession;
52+
$this->customerAddressesFormatter = $customerAddressesFormatter;
53+
$this->instantPurchaseConfig = $instantPurchaseConfig;
54+
$this->customerCardsFormatter = $customerCardsFormatter;
55+
}
56+
57+
public function execute()
58+
{
59+
$resultData = ['available' => false];
60+
/** @var JsonResult $result */
61+
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
62+
if (!$this->customerSession->isLoggedIn()) {
63+
$result->setData($resultData);
64+
return $result;
65+
}
66+
$customer = $this->customerSession->getCustomer();
67+
$available = $this->instantPurchase->isAvailableForCustomer($customer);
68+
$resultData = [
69+
'available' => $available
70+
];
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->instantPurchaseConfig->isSelectAddressEnabled()
78+
];
79+
}
80+
81+
$result->setData($resultData);
82+
83+
return $result;
84+
}
85+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\InstantPurchase\Controller\Button;
7+
8+
use Exception;
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Customer\Model\Session;
12+
use Magento\Framework\App\Action\Context;
13+
use Magento\Framework\Controller\Result\Json as JsonResult;
14+
use Magento\Framework\Controller\ResultFactory;
15+
use Magento\Framework\Data\Form\FormKey\Validator;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\InstantPurchase\Model\CustomerDataGetter;
18+
use Magento\InstantPurchase\Model\CustomerDataGetterFactory;
19+
use Magento\InstantPurchase\Model\PlaceOrder as PlaceOrderModel;
20+
use Magento\Sales\Api\OrderRepositoryInterface;
21+
use Magento\Store\Model\StoreManagerInterface;
22+
23+
/**
24+
* Class PlaceOrder
25+
* @api
26+
*/
27+
class PlaceOrder extends \Magento\Framework\App\Action\Action
28+
{
29+
/**
30+
* @var StoreManagerInterface
31+
*/
32+
private $storeManager;
33+
/**
34+
* @var ProductRepositoryInterface
35+
*/
36+
private $productRepository;
37+
/**
38+
* @var PlaceOrderModel
39+
*/
40+
private $placeOrder;
41+
/**
42+
* @var OrderRepositoryInterface
43+
*/
44+
private $orderRepository;
45+
/**
46+
* @var Session
47+
*/
48+
private $customerSession;
49+
/**
50+
* @var CustomerDataGetter
51+
*/
52+
private $customerData;
53+
/**
54+
* @var Validator
55+
*/
56+
private $formKeyValidator;
57+
58+
/**
59+
* PlaceOrder constructor.
60+
* @param Context $context
61+
* @param StoreManagerInterface $storeManager
62+
* @param ProductRepositoryInterface $productRepository
63+
* @param PlaceOrderModel $placeOrder
64+
* @param OrderRepositoryInterface $orderRepository
65+
* @param Session $customerSession
66+
* @param CustomerDataGetterFactory $customerData
67+
* @param Validator $formKeyValidator
68+
*/
69+
public function __construct(
70+
Context $context,
71+
StoreManagerInterface $storeManager,
72+
ProductRepositoryInterface $productRepository,
73+
PlaceOrderModel $placeOrder,
74+
OrderRepositoryInterface $orderRepository,
75+
Session $customerSession,
76+
CustomerDataGetterFactory $customerData,
77+
Validator $formKeyValidator
78+
) {
79+
parent::__construct($context);
80+
$this->storeManager = $storeManager;
81+
$this->productRepository = $productRepository;
82+
$this->placeOrder = $placeOrder;
83+
$this->orderRepository = $orderRepository;
84+
$this->customerSession = $customerSession;
85+
$this->customerData = $customerData;
86+
$this->formKeyValidator = $formKeyValidator;
87+
}
88+
89+
public function execute()
90+
{
91+
$errorMsg = __('Something went wrong while processing your order. Please try again later.');
92+
93+
if (!$this->formKeyValidator->validate($this->getRequest())) {
94+
return $this->createResponse($errorMsg, false);
95+
}
96+
$product = $this->initProduct();
97+
$params = $this->getRequest()->getParams();
98+
try {
99+
$customerData = $this->customerData->create($this->customerSession->getCustomer());
100+
$orderId = $this->placeOrder->placeOrder($product, $customerData, $params);
101+
} catch (NoSuchEntityException $e) {
102+
return $this->createResponse($errorMsg, false);
103+
} catch (Exception $e) {
104+
return $this->createResponse($e->getMessage(), false);
105+
}
106+
107+
$order = $this->orderRepository->get($orderId);
108+
$message = __('Your order number is: %1.', $order->getIncrementId());
109+
110+
return $this->createResponse($message, true);
111+
}
112+
113+
/**
114+
* @return ProductInterface
115+
* @throws NoSuchEntityException
116+
*/
117+
private function initProduct()
118+
{
119+
$productId = (int)$this->getRequest()->getParam('product');
120+
if ($productId) {
121+
$storeId = $this->storeManager->getStore()->getId();
122+
return $this->productRepository->getById($productId, false, $storeId);
123+
}
124+
throw new NoSuchEntityException(__('Requested product doesn\'t exist'));
125+
}
126+
127+
/**
128+
* @param string $message
129+
* @param bool $successMessage
130+
* @return JsonResult
131+
*/
132+
private function createResponse(string $message, bool $successMessage)
133+
{
134+
/** @var JsonResult $result */
135+
$result = $this->resultFactory->create(ResultFactory::TYPE_JSON);
136+
$result->setData([
137+
'response' => $message
138+
]);
139+
if ($successMessage) {
140+
$this->messageManager->addSuccessMessage($message);
141+
} else {
142+
$this->messageManager->addErrorMessage($message);
143+
}
144+
145+
return $result;
146+
}
147+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\InstantPurchase\Model;
7+
8+
class CheapestShippingRateChooserRule implements ShippingRateChooserRuleInterface
9+
{
10+
/**
11+
* @param array $shippingRates
12+
* @return string
13+
*/
14+
public function choose(array $shippingRates): string
15+
{
16+
$rate = array_shift($shippingRates);
17+
foreach ($shippingRates as $tmpRate) {
18+
if ($tmpRate['price'] < $rate['price']) {
19+
$rate = $tmpRate;
20+
}
21+
}
22+
23+
return $rate['code'];
24+
}
25+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\InstantPurchase\Model;
7+
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Store\Model\StoreManagerInterface;
10+
11+
class Config
12+
{
13+
const ONE_TOUCH_ORDERING_MODULE_ACTIVE = 'sales/one_touch/active';
14+
const ONE_TOUCH_ORDERING_MODULE_BUTTON_TEXT = 'sales/one_touch/button_text';
15+
const ONE_TOUCH_ORDERING_MODULE_ADDRESS_SELECT = 'sales/one_touch/address_select';
16+
17+
/**
18+
* @var StoreManagerInterface
19+
*/
20+
private $storeManager;
21+
/**
22+
* @var ScopeConfigInterface
23+
*/
24+
private $scopeConfig;
25+
26+
/**
27+
* Data constructor.
28+
* @param StoreManagerInterface $storeManager
29+
* @param ScopeConfigInterface $scopeConfig
30+
*/
31+
public function __construct(
32+
StoreManagerInterface $storeManager,
33+
ScopeConfigInterface $scopeConfig
34+
) {
35+
$this->storeManager = $storeManager;
36+
$this->scopeConfig = $scopeConfig;
37+
}
38+
39+
/**
40+
* @return bool
41+
*/
42+
public function isModuleEnabled(): bool
43+
{
44+
return $this->isSetFlag(self::ONE_TOUCH_ORDERING_MODULE_ACTIVE);
45+
}
46+
47+
/**
48+
* @return bool
49+
*/
50+
public function isSelectAddressEnabled(): string
51+
{
52+
return $this->isSetFlag(self::ONE_TOUCH_ORDERING_MODULE_ADDRESS_SELECT);
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getButtonText(): string
59+
{
60+
return strip_tags($this->getValue(self::ONE_TOUCH_ORDERING_MODULE_BUTTON_TEXT));
61+
}
62+
63+
/**
64+
* @param $path
65+
* @return mixed
66+
*/
67+
private function getValue($path)
68+
{
69+
return $this->scopeConfig->getValue(
70+
$path,
71+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
72+
$this->storeManager->getStore()->getId()
73+
);
74+
}
75+
76+
/**
77+
* @param $path
78+
* @return bool
79+
*/
80+
private function isSetFlag($path)
81+
{
82+
return $this->scopeConfig->isSetFlag(
83+
$path,
84+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
85+
$this->storeManager->getStore()->getId()
86+
);
87+
}
88+
}

0 commit comments

Comments
 (0)