Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
PlaceOrder Class Reference
Inheritance diagram for PlaceOrder:
AbstractExpress Action RedirectLoginInterface AbstractAction ActionInterface PlaceOrder PlaceOrder

Public Member Functions

 __construct (\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory, \Magento\Framework\Session\Generic $paypalSession, \Magento\Framework\Url\Helper\Data $urlHelper, \Magento\Customer\Model\Url $customerUrl, \Magento\Checkout\Api\AgreementsValidatorInterface $agreementValidator, \Magento\Sales\Api\PaymentFailuresInterface $paymentFailures=null)
 
 execute ()
 
- Public Member Functions inherited from AbstractExpress
 __construct (\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory, \Magento\Framework\Session\Generic $paypalSession, \Magento\Framework\Url\Helper\Data $urlHelper, \Magento\Customer\Model\Url $customerUrl)
 
 getCustomerBeforeAuthUrl ()
 
 getActionFlagList ()
 
 getLoginUrl ()
 
 getRedirectActionName ()
 
 redirectLogin ()
 
- Public Member Functions inherited from Action
 __construct (Context $context)
 
 dispatch (RequestInterface $request)
 
 getActionFlag ()
 
- Public Member Functions inherited from AbstractAction
 __construct (Context $context)
 
 dispatch (RequestInterface $request)
 
 getRequest ()
 
 getResponse ()
 

Protected Member Functions

 _processPaypalApiError ($exception)
 
 _redirectSameToken ()
 
 _redirectToCartAndShowError ($errorMessage)
 
 isValidationRequired ()
 
- Protected Member Functions inherited from AbstractExpress
 _initCheckout ()
 
 _initToken ($setToken=null)
 
 _getSession ()
 
 _getCheckoutSession ()
 
 _getQuote ()
 
- Protected Member Functions inherited from Action
 _forward ($action, $controller=null, $module=null, array $params=null)
 
 _redirect ($path, $arguments=[])
 

Protected Attributes

 $agreementsValidator
 
- Protected Attributes inherited from AbstractExpress
 $_checkout
 
 $_checkoutTypes = []
 
 $_config
 
 $_quote = false
 
 $_configType
 
 $_configMethod
 
 $_checkoutType
 
 $_customerSession
 
 $_checkoutSession
 
 $_orderFactory
 
 $_checkoutFactory
 
 $_paypalSession
 
 $_urlHelper
 
 $_customerUrl
 
- Protected Attributes inherited from Action
 $_objectManager
 
 $_sessionNamespace
 
 $_eventManager
 
 $_actionFlag
 
 $_redirect
 
 $_view
 
 $_url
 
 $messageManager
 
- Protected Attributes inherited from AbstractAction
 $_request
 
 $_response
 
 $resultRedirectFactory
 
 $resultFactory
 

Additional Inherited Members

- Data Fields inherited from ActionInterface
const FLAG_NO_DISPATCH = 'no-dispatch'
 
const FLAG_NO_POST_DISPATCH = 'no-postDispatch'
 
const FLAG_NO_DISPATCH_BLOCK_EVENT = 'no-beforeGenerateLayoutBlocksDispatch'
 
const PARAM_NAME_BASE64_URL = 'r64'
 
const PARAM_NAME_URL_ENCODED = 'uenc'
 

Detailed Description

Class PlaceOrder @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 17 of file PlaceOrder.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\Action\Context  $context,
\Magento\Customer\Model\Session  $customerSession,
\Magento\Checkout\Model\Session  $checkoutSession,
\Magento\Sales\Model\OrderFactory  $orderFactory,
\Magento\Paypal\Model\Express\Checkout\Factory  $checkoutFactory,
\Magento\Framework\Session\Generic  $paypalSession,
\Magento\Framework\Url\Helper\Data  $urlHelper,
\Magento\Customer\Model\Url  $customerUrl,
\Magento\Checkout\Api\AgreementsValidatorInterface  $agreementValidator,
\Magento\Sales\Api\PaymentFailuresInterface  $paymentFailures = null 
)
Parameters
\Magento\Framework\App\Action\Context$context
\Magento\Customer\Model\Session$customerSession
\Magento\Checkout\Model\Session$checkoutSession
\Magento\Sales\Model\OrderFactory$orderFactory
\Magento\Paypal\Model\Express\Checkout\Factory$checkoutFactory
\Magento\Framework\Session\Generic$paypalSession
\Magento\Framework\Url\Helper\Data$urlHelper
\Magento\Customer\Model\Url$customerUrl
\Magento\Checkout\Api\AgreementsValidatorInterface$agreementValidator
\Magento\Sales\Api\PaymentFailuresInterface | null$paymentFailures@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 42 of file PlaceOrder.php.

53  {
54  parent::__construct(
55  $context,
56  $customerSession,
57  $checkoutSession,
58  $orderFactory,
59  $checkoutFactory,
60  $paypalSession,
61  $urlHelper,
63  );
64 
65  $this->agreementsValidator = $agreementValidator;
66  $this->paymentFailures = $paymentFailures ? : $this->_objectManager->get(
67  \Magento\Sales\Api\PaymentFailuresInterface::class
68  );
69  }
$customerUrl
Definition: info.phtml:28

Member Function Documentation

◆ _processPaypalApiError()

_processPaypalApiError (   $exception)
protected

Process PayPal API's processable errors

Parameters
\Magento\Paypal\Model\Api\ProcessableException$exception
Returns
void

Definition at line 161 of file PlaceOrder.php.

162  {
163  $this->paymentFailures->handle((int)$this->_getCheckoutSession()->getQuoteId(), $exception->getMessage());
164 
165  switch ($exception->getCode()) {
166  case ApiProcessableException::API_MAX_PAYMENT_ATTEMPTS_EXCEEDED:
167  case ApiProcessableException::API_TRANSACTION_EXPIRED:
168  $this->getResponse()->setRedirect(
169  $this->_getQuote()->getPayment()->getCheckoutRedirectUrl()
170  );
171  break;
172  case ApiProcessableException::API_DO_EXPRESS_CHECKOUT_FAIL:
173  $this->_redirectSameToken();
174  break;
175  case ApiProcessableException::API_ADDRESS_MATCH_FAIL:
176  $this->redirectToOrderReviewPageAndShowError($exception->getUserMessage());
177  break;
178  case ApiProcessableException::API_UNABLE_TRANSACTION_COMPLETE:
179  if ($this->_config->getPaymentAction() == \Magento\Payment\Model\Method\AbstractMethod::ACTION_ORDER) {
180  $paypalTransactionData = $this->_getCheckoutSession()->getPaypalTransactionData();
181  $this->getResponse()->setRedirect(
182  $this->_config->getExpressCheckoutOrderUrl($paypalTransactionData['transaction_id'])
183  );
184  } else {
185  $this->_redirectSameToken();
186  }
187  break;
188  default:
189  $this->_redirectToCartAndShowError($exception->getUserMessage());
190  break;
191  }
192  }

◆ _redirectSameToken()

_redirectSameToken ( )
protected

Redirect customer back to PayPal with the same token

Returns
void

Definition at line 199 of file PlaceOrder.php.

200  {
201  $token = $this->_initToken();
202  $this->getResponse()->setRedirect(
203  $this->_config->getExpressCheckoutStartUrl($token)
204  );
205  }

◆ _redirectToCartAndShowError()

_redirectToCartAndShowError (   $errorMessage)
protected

Redirect customer to shopping cart and show error message

Parameters
string$errorMessage
Returns
void

Definition at line 213 of file PlaceOrder.php.

214  {
215  $this->messageManager->addErrorMessage($errorMessage);
216  $this->_redirect('checkout/cart');
217  }
_redirect($path, $arguments=[])
Definition: Action.php:167

◆ execute()

execute ( )

Submit the order

Returns
void
Exceptions

Implements ActionInterface.

Definition at line 77 of file PlaceOrder.php.

78  {
79  if ($this->isValidationRequired() &&
80  !$this->agreementsValidator->isValid(array_keys($this->getRequest()->getPost('agreement', [])))
81  ) {
82  $e = new \Magento\Framework\Exception\LocalizedException(
83  __(
84  "The order wasn't placed. "
85  . "First, agree to the terms and conditions, then try placing your order again."
86  )
87  );
88  $this->messageManager->addExceptionMessage(
89  $e,
90  $e->getMessage()
91  );
92  $this->_redirect('*/*/review');
93  return;
94  }
95 
96  try {
97  $this->_initCheckout();
98  $this->_checkout->place($this->_initToken());
99 
100  // prepare session to success or cancellation page
101  $this->_getCheckoutSession()->clearHelperData();
102 
103  // "last successful quote"
104  $quoteId = $this->_getQuote()->getId();
105  $this->_getCheckoutSession()->setLastQuoteId($quoteId)->setLastSuccessQuoteId($quoteId);
106 
107  // an order may be created
108  $order = $this->_checkout->getOrder();
109  if ($order) {
110  $this->_getCheckoutSession()->setLastOrderId($order->getId())
111  ->setLastRealOrderId($order->getIncrementId())
112  ->setLastOrderStatus($order->getStatus());
113  }
114 
115  $this->_eventManager->dispatch(
116  'paypal_express_place_order_success',
117  [
118  'order' => $order,
119  'quote' => $this->_getQuote()
120  ]
121  );
122 
123  // redirect if PayPal specified some URL (for example, to Giropay bank)
124  $url = $this->_checkout->getRedirectUrl();
125  if ($url) {
126  $this->getResponse()->setRedirect($url);
127  return;
128  }
129  $this->_initToken(false); // no need in token anymore
130  $this->_redirect('checkout/onepage/success');
131  return;
132  } catch (ApiProcessableException $e) {
133  $this->_processPaypalApiError($e);
134  } catch (LocalizedException $e) {
135  $this->processException($e, $e->getRawMessage());
136  } catch (\Exception $e) {
137  $this->processException($e, 'We can\'t place the order.');
138  }
139  }
_redirect($path, $arguments=[])
Definition: Action.php:167
$order
Definition: order.php:55
__()
Definition: __.php:13

◆ isValidationRequired()

isValidationRequired ( )
protected

Return true if agreements validation required

Returns
bool

Definition at line 236 of file PlaceOrder.php.

237  {
238  return is_array($this->getRequest()->getBeforeForwardInfo())
239  && empty($this->getRequest()->getBeforeForwardInfo());
240  }

Field Documentation

◆ $agreementsValidator

$agreementsValidator
protected

Definition at line 22 of file PlaceOrder.php.


The documentation for this class was generated from the following file: