| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * it under the terms of the GNU Lesser General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * PAYONE Magento 2 Connector is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * GNU Lesser General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * You should have received a copy of the GNU Lesser General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * PHP version 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @category  Payone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @package   Payone_Magento2_Plugin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author    FATCHIP GmbH <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @copyright 2003 - 2016 Payone GmbH | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @link      http://www.payone.de | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | namespace Payone\Core\Controller\Paypal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Magento\Framework\Controller\ResultFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Magento\Quote\Model\Quote; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Payone\Core\Model\PayoneConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * Controller for PayPal Express initiation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class Express extends \Magento\Framework\App\Action\Action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Checkout session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var \Magento\Checkout\Model\Session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected $checkoutSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * PAYONE request model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @var \Payone\Core\Model\Api\Request\Genericpayment\PayPalExpress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected $genericRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * PayPal payment model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @var \Payone\Core\Model\Methods\Paypal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected $paypalPayment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Checkout helper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @var \Magento\Checkout\Helper\Data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     protected $checkoutHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * Customer session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @var \Magento\Customer\Model\Session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     protected $customerSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * PAYONE payment helper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @var \Payone\Core\Helper\Payment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     protected $paymentHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param \Magento\Framework\App\Action\Context                       $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param \Magento\Checkout\Model\Session                             $checkoutSession | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param \Payone\Core\Model\Api\Request\Genericpayment\PayPalExpress $genericRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param \Payone\Core\Model\Methods\Paypal                           $paypalPayment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param \Magento\Checkout\Helper\Data                               $checkoutHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param \Magento\Customer\Model\Session                             $customerSession | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param \Payone\Core\Helper\Payment                                 $paymentHelper | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         \Magento\Framework\App\Action\Context $context, | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         \Magento\Checkout\Model\Session $checkoutSession, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         \Payone\Core\Model\Api\Request\Genericpayment\PayPalExpress $genericRequest, | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         \Payone\Core\Model\Methods\Paypal $paypalPayment, | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         \Magento\Checkout\Helper\Data $checkoutHelper, | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         \Magento\Customer\Model\Session $customerSession, | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         \Payone\Core\Helper\Payment $paymentHelper | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         parent::__construct($context); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         $this->checkoutSession = $checkoutSession; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         $this->genericRequest = $genericRequest; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         $this->paypalPayment = $paypalPayment; | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         $this->checkoutHelper = $checkoutHelper; | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         $this->customerSession = $customerSession; | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         $this->paymentHelper = $paymentHelper; | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Determine if a logged in customer is required for an express checkout | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * For example needed for virtual products ( download-products etc. ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param  Quote $oQuote | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function loginNeededForExpressCheckout(Quote $oQuote) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $oCustomer = $this->customerSession->getCustomerDataObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         if (!$oCustomer->getId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $sCheckoutMethod = $oQuote->getCheckoutMethod(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             if ((!$sCheckoutMethod || $sCheckoutMethod != \Magento\Checkout\Model\Type\Onepage::METHOD_REGISTER) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 && !$this->checkoutHelper->isAllowedGuestCheckout($oQuote, $oQuote->getStoreId()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * Redirect to payment-provider or to success page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $oQuote = $this->checkoutSession->getQuote(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         if ($this->paymentHelper->isPayPalExpressActive() && $oQuote && $oQuote->hasItems()) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 140 |  | View Code Duplication |             if ($this->loginNeededForExpressCheckout($oQuote)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 $this->messageManager->addNoticeMessage(__('Please sign in to check out.')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 return $resultRedirect->setPath('customer/account/login'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             $this->paypalPayment->setIsPayPalExpress(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             $aResponse = $this->genericRequest->sendRequest($oQuote, $this->paypalPayment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             if ($aResponse['status'] == 'ERROR') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 $this->messageManager->addError(__($aResponse['customermessage'])); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 return $resultRedirect->setPath('checkout/cart'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             } elseif ($aResponse['status'] == 'REDIRECT') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                 $oPayment = $oQuote->getPayment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 $oPayment->setMethod(PayoneConfig::METHOD_PAYPAL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 $oQuote->setPayment($oPayment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 $oQuote->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 $this->checkoutSession->setPayoneWorkorderId($aResponse['workorderid']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 $this->_redirect($aResponse['redirecturl']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         return $resultRedirect->setPath('checkout/cart'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.