-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConfirm.php
148 lines (129 loc) · 4.96 KB
/
Confirm.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
<?php
namespace Affirm\Telesales\Controller\Payment;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Checkout\Model\Session;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Api\OrderManagementInterface as OrderManagement;
use Affirm\Telesales\Model\Adminhtml\Checkout as AffirmCheckout;
class Confirm extends Action implements CsrfAwareActionInterface
{
const CHECKOUT_STATUS_CONFIRMED = 'confirmed';
/**
* Inject objects to the Confirm action
*
* @param Context $context
*/
public function __construct(
Context $context,
Session $checkoutSession,
CartManagementInterface $cartManagement,
OrderFactory $orderFactory,
OrderManagement $orderManagement,
CartRepositoryInterface $quoteRepository,
AffirmCheckout $affirmCheckout,
\Psr\Log\LoggerInterface $logger
) {
parent::__construct($context);
$this->_checkoutSession = $checkoutSession;
$this->quoteManagement = $cartManagement;
$this->orderFactory = $orderFactory;
$this->orderManagement = $orderManagement;
$this->quoteRepository = $quoteRepository;
$this->affirmCheckout = $affirmCheckout;
$this->logger = $logger;
}
/**
* @inheritDoc
*/
public function createCsrfValidationException(
RequestInterface $request
): ?InvalidRequestException {
return null;
}
/**
* @inheritDoc
*/
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}
/**
* @inheritDoc
*/
public function execute()
{
$checkout_token = $this->getRequest()->getParam('checkout_token');
$this->logger->debug('Affirm Telesales checkout confirm action: '.$checkout_token);
if (!isset($checkout_token)) {
return $this->cancelRedirect();
}
// Get orderIncrementId from checkout read
$readCheckoutResponse = $this->affirmCheckout->readCheckout($checkout_token);
$responseBody = json_decode($readCheckoutResponse->getBody(), true);
$order_id = $responseBody['merchant_external_reference'] ?: $responseBody['order_id'];
$checkout_status = $responseBody['checkout_status'];
if (!isset($order_id)) {
$this->logger->debug('Affirm Telesales - Missing merchant external reference');
return $this->cancelRedirect();
}
// Load order
$_order = $this->orderFactory->create()->loadByIncrementId($order_id);
$quoteId = $_order->getQuoteId();
$quote = $this->quoteRepository->get($quoteId);
// Verify checkout_token and checkout_status
if ($checkout_token !== $_order->getPayment()->getAdditionalInformation('checkout_token') || $checkout_status !== self::CHECKOUT_STATUS_CONFIRMED) {
$this->logger->debug('Affirm Telesales - Invalid checkout token');
return $this->cancelRedirect();
}
// Set checkout_token to quote
$payment = $quote->getPayment();
$payment->setAdditionalInformation('checkout_token', $checkout_token);
$payment->save();
try {
$order = $this->orderManagement->place($_order);
$quote->setIsActive(false);
$this->quoteRepository->save($quote);
} catch (LocalizedException $e) {
$this->messageManager->addExceptionMessage(
$e,
$e->getMessage()
);
$_order->addCommentToStatusHistory($e->getMessage());
$_order->save();
return $this->cancelRedirect();
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage(
$e,
__('We can\'t place the order.')
);
return $this->cancelRedirect();
}
$_orderState = $order->getState();
$_orderStatus = $order->getStatus();
$this->_checkoutSession
->setLastQuoteId($quoteId)
->setLastSuccessQuoteId($quoteId);
$this->_checkoutSession
->setLastOrderId($order->getId())
->setLastRealOrderId($order->getIncrementId())
->setLastOrderStatus($_orderStatus);
$this->_eventManager->dispatch(
'affirm_place_order_success',
['order' => $order, 'quote' => $quote ]
);
$this->_redirect('checkout/onepage/success');
return;
}
private function cancelRedirect() {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('telesales/payment/cancel');
return $resultRedirect;
}
}