forked from luke-denton-aligent/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckout.php
157 lines (127 loc) · 5.2 KB
/
Checkout.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace ZipMoney\ZipMoneyPayment\Model;
use \Magento\Checkout\Model\Type\Onepage;
use \ZipMoney\ZipMoneyPayment\Model\Config;
use \ZipMoney\ZipMoneyPayment\Model\Checkout\AbstractCheckout;
class Checkout extends AbstractCheckout
{
/**
* @var Magento\Checkout\Helper\Data
*/
protected $_checkoutHelper;
/**
* @var string
*/
protected $_redirectUrl = null;
/**
* @var string
*/
protected $_checkoutId = null;
const STATUS_MAGENTO_AUTHORIZED = "zip_authorised";
public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Checkout\Helper\Data $checkoutHelper,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
\Magento\Sales\Model\OrderFactory $orderFactory,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Checkout\Model\PaymentInformationManagement $paymentInformationManagement,
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Framework\Message\ManagerInterface $messageManager,
\ZipMoney\ZipMoneyPayment\Helper\Payload $payloadHelper,
\ZipMoney\ZipMoneyPayment\Helper\Logger $logger,
\ZipMoney\ZipMoneyPayment\Helper\Data $helper,
\ZipMoney\ZipMoneyPayment\Model\Config $config,
\zipMoney\Api\CheckoutsApi $checkoutsApi,
array $data = []
)
{
$this->_checkoutHelper = $checkoutHelper;
$this->_api = $checkoutsApi;
if (isset($data['quote'])) {
if($data['quote'] instanceof \Magento\Quote\Model\Quote){
$this->setQuote($data['quote']);
} else {
throw new \Magento\Framework\Exception\LocalizedException(__('Quote instance is required.'));
}
}
parent::__construct( $customerSession, $checkoutSession, $customerFactory, $quoteRepository, $payloadHelper, $logger, $helper, $config);
}
/**
* Create quote in Zip side if not existed, and request for redirect url
*
* @param \Magento\$quote
* @return \zipMoney\Model\Checkout
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function start()
{
if (!$this->_quote || !$this->_quote->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(__('The quote does not exist.'));
}
if ($this->_quote->getIsMultiShipping()) {
$this->_quote->setIsMultiShipping(false);
$this->_quote->removeAllAddresses();
}
$checkoutMethod = $this->getCheckoutMethod();
$isAllowedGuestCheckout = $this->_checkoutHelper->isAllowedGuestCheckout($this->_quote, $this->_quote->getStoreId());
$isCustomerLoggedIn = $this->_getCustomerSession()->isLoggedIn();
$this->_logger->debug("Checkout Method:- ".$checkoutMethod);
$this->_logger->debug("Is Allowed Guest Checkout :- ".$isAllowedGuestCheckout);
$this->_logger->debug("Is Customer Logged In :- ".$isCustomerLoggedIn);
if ((!$checkoutMethod || $checkoutMethod != Onepage::METHOD_REGISTER) &&
!$isAllowedGuestCheckout &&
!$isCustomerLoggedIn) {
throw new \Magento\Framework\Exception\LocalizedException(__('Please log in to proceed to checkout.'));
}
// Calculate Totals
$this->_quote->collectTotals();
if (!$this->_quote->getGrandTotal() && !$this->_quote->hasNominalItems()) {
throw new \Magento\Framework\Exception\LocalizedException(__('Cannot process the order due to zero amount.'));
}
$this->_quote->reserveOrderId();
/*
Commenting out the following line.
Apparantly triggering more than one quote save results in "We don't have as many "Produt Name" as you requested." error when the product has 1 item left.
*/
//$this->_quoteRepository->save($this->_quote);
$request = $this->_payloadHelper->getCheckoutPayload($this->_quote);
$this->_logger->debug("Checkout Request:- ".$this->_payloadHelper->jsonEncode($request));
try {
$checkout = $this->getApi()->checkoutsCreate($request);
$this->_logger->debug("Checkout Response:- ".$this->_payloadHelper->jsonEncode($checkout));
if(isset($checkout->error)){
throw new \Magento\Framework\Exception\LocalizedException(__('Cannot get redirect URL from zipMoney.'));
}
$this->_checkoutId = $checkout->getId();
$this->_quote->setZipmoneyCheckoutId($this->_checkoutId);
$this->_quoteRepository->save($this->_quote);
$this->_redirectUrl = $checkout->getUri();
} catch(\zipMoney\ApiException $e){
$this->_logger->debug("Errors:- ".json_encode($e->getResponseBody()));
$this->_logger->debug("Errors:- ".json_encode($e->getCode()));
$this->_logger->debug("Errors:- ".json_encode($e->getResponseObject()));
throw new \Magento\Framework\Exception\LocalizedException(__('An error occurred while to requesting the redirect url.'));
}
return $checkout;
}
/**
* Returns the zipMoney Redirect Url
*
* @return string
*/
public function getRedirectUrl()
{
return $this->_redirectUrl;
}
/**
* Returns the zipMoney Checkout Id
*
* @return string
*/
public function getCheckoutId()
{
return $this->_checkoutId;
}
}