public function getOrder() { if (!$this->_order) { $this->_order = $this->_checkoutSession->getLastRealOrder(); } return $this->_order; }
/** * Cancel last placed order with specified comment message * * @param string $comment Comment appended to order history * @return bool True if order cancelled, false otherwise */ public function cancelCurrentOrder($comment) { $order = $this->session->getLastRealOrder(); if ($order->getId() && $order->getState() != Order::STATE_CANCELED) { $order->registerCancellation($comment)->save(); return true; } return false; }
/** * @param \Magento\Framework\View\Element\Template\Context $context * @param \Magento\Checkout\Model\Session $checkoutSession * @param \Yireo\GoogleTagManager2\Helper\Data $helper * @param \Yireo\GoogleTagManager2\Model\Container $container * @param array $data */ public function __construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Checkout\Model\Session $checkoutSession, \Yireo\GoogleTagManager2\Helper\Data $helper, \Yireo\GoogleTagManager2\Model\Container $container, array $data = []) { $this->helper = $helper; $this->container = $container; $this->checkoutSession = $checkoutSession; $this->order = $this->checkoutSession->getLastRealOrder(); $this->quote = $this->checkoutSession->getQuote(); parent::__construct($context, $data); $this->storeManager = $this->_storeManager; $this->layout = $this->_layout; }
/** * Redirect to payment * * @return \Magento\Framework\Controller\Result\Redirect */ public function execute() { try { return $this->_resultRedirectFactory->create()->setPath($this->_payment->createPaymentRequest()); } catch (\Exception $exception) { /** @var \Magento\Sales\Model\Order $order */ $order = $this->_objectManager->create('\\Magento\\Sales\\Model\\Order')->load($this->_checkoutSession->getLastRealOrder()->getId()); /** change payment status in magento */ $order->addStatusToHistory('pagseguro_cancelada', null, true); /** save order */ $order->save(); return $this->_redirect('pagseguro/payment/failure'); } }
/** * @param int|null $orderId * @param int|null $incrementId * @param \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject $orderMock * @dataProvider getLastRealOrderDataProvider */ public function testGetLastRealOrder($orderId, $incrementId, $orderMock) { $orderFactory = $this->getMockBuilder('Magento\\Sales\\Model\\OrderFactory')->disableOriginalConstructor()->setMethods(array('create'))->getMock(); $orderFactory->expects($this->once())->method('create')->will($this->returnValue($orderMock)); $messageCollectionFactory = $this->getMockBuilder('Magento\\Framework\\Message\\CollectionFactory')->disableOriginalConstructor()->getMock(); $quoteFactory = $this->getMockBuilder('Magento\\Sales\\Model\\QuoteFactory')->disableOriginalConstructor()->getMock(); $appState = $this->getMock('\\Magento\\Framework\\App\\State', array(), array(), '', false); $appState->expects($this->any())->method('isInstalled')->will($this->returnValue(true)); $request = $this->getMock('\\Magento\\Framework\\App\\Request\\Http', array(), array(), '', false); $request->expects($this->any())->method('getHttpHost')->will($this->returnValue(array())); $constructArguments = $this->_helper->getConstructArguments('Magento\\Checkout\\Model\\Session', array('request' => $request, 'orderFactory' => $orderFactory, 'messageCollectionFactory' => $messageCollectionFactory, 'quoteFactory' => $quoteFactory, 'storage' => new \Magento\Framework\Session\Storage())); $this->_session = $this->_helper->getObject('Magento\\Checkout\\Model\\Session', $constructArguments); $this->_session->setLastRealOrderId($orderId); $this->assertSame($orderMock, $this->_session->getLastRealOrder()); if ($orderId == $incrementId) { $this->assertSame($orderMock, $this->_session->getLastRealOrder()); } }
/** * Return order and it's item related variables * * @return array */ public function getVariables() { $variables = []; $order = $order = $this->checkoutSession->getLastRealOrder(); if ($order) { $variables['order.id'] = $order->getIncrementId(); $variables['order.subtotal'] = $order->getBaseSubtotalInclTax(); $variables['order.discount_total'] = $order->getDiscountAmount(); $variables['order.shipping_total'] = $order->getShippingAmount(); $variables['order.grand_total'] = $order->getBaseGrandTotal(); $variables['order.shipping_method'] = $order->getShippingMethod(); $variables['order.payment_method'] = $order->getPayment()->getMethod(); $variables['order.salesrules'] = $order->getAppliedRuleIds(); foreach ($order->getAllItems() as $item) { $variables = array_merge($variables, $this->getOrderItemVariables($item)); } } return $variables; }
/** * Show payment page * @return \Magento\Framework\View\Result\PageFactory */ public function execute() { $resultPage = $this->_resultPageFactory->create(); $resultPage->getLayout()->getBlock('pagseguro.direct.payment')->setData('order', $this->_checkoutSession->getLastRealOrder()->getId()); try { $this->_library->setEnvironment(); $resultPage->getLayout()->getBlock('pagseguro.direct.payment')->setData('sessionCode', $this->_library->getSession()); $resultPage->getLayout()->getBlock('pagseguro.direct.payment')->setData('paymentUrl', $this->_library->getDirectPaymentUrl()); } catch (\Exception $exc) { /** @var \Magento\Sales\Model\Order $order */ $order = $this->_objectManager->create('\\Magento\\Sales\\Model\\Order')->load($this->_checkoutSession->getLastRealOrder()->getId()); /** change payment status in magento */ $order->addStatusToHistory('pagseguro_cancelada', null, true); /** save order */ $order->save(); return $this->_redirect('pagseguro/payment/failure'); } return $resultPage; }
/** * create an order in ComproPago * * @return array */ private function createOrder() { $order = $this->checSession->getLastRealOrder(); $dataorder = new PlaceOrderInfo($order->getRealOrderId(), $order->getRealOrderId(), $order->getData('total_due'), $order->getCustomerFirstname() . " " . $order->getCustomerLastname(), $order->getCustomerEmail(), $_COOKIE['provider'], null, 'magento2', $this->metada->getVersion()); if (isset($_COOKIE['payment_method']) && $_COOKIE['payment_method'] == 'compropago') { try { $client = new Client($this->instance->getPublicKey(), $this->instance->getPrivateKey(), $this->instance->getLiveMode()); $response = $client->api->placeOrder($dataorder); $_COOKIE['provider'] = null; $_COOKIE['payment_method'] = null; unset($_COOKIE['provider']); unset($_COOKIE['payment_method']); return ["type" => "success", "value" => base64_encode($response->getId())]; } catch (\Exception $e) { return ["type" => "error", "value" => $e->getMessage()]; } } else { return ["type" => "error", "value" => "Metodo no reconocido"]; } }
/** * Instantiate * * @return void * @throws \Magento\Framework\Exception\LocalizedException */ protected function _initCheckout() { $pre = __METHOD__ . " : "; $this->_logger->debug($pre . 'bof'); $this->_order = $this->_checkoutSession->getLastRealOrder(); if (!$this->_order->getId()) { $this->getResponse()->setStatusHeader(404, '1.1', 'Not found'); throw new \Magento\Framework\Exception\LocalizedException(__('We could not find "Order" for processing')); } if ($this->_order->getState() != \Magento\Sales\Model\Order::STATE_PENDING_PAYMENT) { $this->_order->setState(\Magento\Sales\Model\Order::STATE_PENDING_PAYMENT)->save(); } if ($this->_order->getQuoteId()) { $this->_checkoutSession->setPayfastQuoteId($this->_checkoutSession->getQuoteId()); $this->_checkoutSession->setPayfastSuccessQuoteId($this->_checkoutSession->getLastSuccessQuoteId()); $this->_checkoutSession->setPayfastRealOrderId($this->_checkoutSession->getLastRealOrderId()); $this->_checkoutSession->getQuote()->setIsActive(false)->save(); //$this->_checkoutSession->clear(); } $this->_logger->debug($pre . 'eof'); //$this->_checkout = $this->_checkoutTypes[$this->_checkoutType]; }
/** * this where we compile data posted by the form to payfast * @return array */ public function getStandardCheckoutFormFields() { $pre = __METHOD__ . ' : '; // Variable initialization $order = $this->_checkoutSession->getLastRealOrder(); $description = ''; $this->_logger->debug($pre . 'serverMode : ' . $this->getConfigData('server')); // If NOT test mode, use normal credentials if ($this->getConfigData('server') == 'live') { $merchantId = $this->getConfigData('merchant_id'); $merchantKey = $this->getConfigData('merchant_key'); } else { $merchantId = '10000100'; $merchantKey = '46f0cd694581a'; } // Create description foreach ($order->getAllItems() as $items) { $description .= $this->getNumberFormat($items->getQtyOrdered()) . ' x ' . $items->getName() . ';'; } $pfDescription = trim(substr($description, 0, 254)); // Construct data for the form $data = array('merchant_id' => $merchantId, 'merchant_key' => $merchantKey, 'return_url' => $this->getPaidSuccessUrl(), 'cancel_url' => $this->getPaidCancelUrl(), 'notify_url' => $this->getPaidNotifyUrl(), 'name_first' => $order->getData('customer_firstname'), 'name_last' => $order->getData('customer_lastname'), 'email_address' => $order->getData('customer_email'), 'm_payment_id' => $order->getRealOrderId(), 'amount' => $this->getTotalAmount($order), 'item_name' => $this->_storeManager->getStore()->getName() . ', Order #' . $order->getRealOrderId(), 'item_description' => $pfDescription); $pfOutput = ''; // Create output string foreach ($data as $key => $val) { if (!empty($val)) { $pfOutput .= $key . '=' . urlencode($val) . '&'; } } $passPhrase = $this->getConfigData('passphrase'); $pfOutput = substr($pfOutput, 0, -1); if (!empty($passPhrase) && $this->getConfigData('server') !== 'test') { $pfOutput = $pfOutput . "&passphrase=" . urlencode($passPhrase); } pflog($pre . 'pfOutput for signature is : ' . $pfOutput); $pfSignature = md5($pfOutput); $data['signature'] = $pfSignature; $data['user_agent'] = 'Magento 2.0'; pflog($pre . 'data is :' . print_r($data, true)); return $data; }
/** * Constructor * * @param Template\Context $context * @param CheckoutSession $checkoutSession * @param array $data */ public function __construct(Template\Context $context, CheckoutSession $checkoutSession, array $data = []) { $this->orderPayment = $checkoutSession->getLastRealOrder()->getPayment(); parent::__construct($context, $data); }
/** * Get order amount (subtotal) * @return Float * @author edudeleon * @date 2015-08-07 */ public function getAmount() { //Get last order from session $order = $this->_checkoutSession->getLastRealOrder(); return number_format($order->getSubtotal(), 2); }
/** * Retrieve current email address * * @return string * @codeCoverageIgnore */ public function getEmailAddress() { return $this->checkoutSession->getLastRealOrder()->getCustomerEmail(); }
/** * Prepares block data * * @return void */ protected function prepareBlockData() { $order_is_ok = true; $order_error_message = ''; if (!($order = $this->_checkoutSession->getLastRealOrder())) { $order_error_message = __('Couldn\'t extract order information.'); } elseif ($order->getState() != Order::STATE_NEW) { $order_error_message = __('Order was already processed or session information expired.'); } elseif (!($additional_info = $order->getPayment()->getAdditionalInformation()) or !is_array($additional_info) or empty($additional_info['sp_method']) or empty($additional_info['sp_transaction'])) { $order_error_message = __('Couldn\'t extract payment information from order.'); } if (!empty($order_error_message)) { $order_is_ok = false; } $smart2pay_config = $this->_s2pModel->getFullConfigArray(); $merchant_transaction_id = $order->getRealOrderId(); // assume live environment if we don't get something valid from config if (empty($smart2pay_config['environment']) or !($environment = Environment::validEnvironment($smart2pay_config['environment']))) { $environment = Environment::ENV_LIVE; } if ($environment == Environment::ENV_DEMO) { $merchant_transaction_id = $this->_helper->convert_to_demo_merchant_transaction_id($merchant_transaction_id); } $form_data = $smart2pay_config; if ($order_is_ok) { $form_data['environment'] = $environment; $form_data['method_id'] = !empty($additional_info['sp_method']) ? intval($additional_info['sp_method']) : 0; $form_data['order_id'] = $merchant_transaction_id; $form_data['currency'] = $order->getOrderCurrency()->getCurrencyCode(); $form_data['amount'] = number_format($order->getGrandTotal(), 2, '.', '') * 100; //anonymous user, get the info from billing details if ($order->getCustomerId() === null) { $form_data['customer_last_name'] = $this->_helper->s2p_mb_substr($order->getBillingAddress()->getLastname(), 0, 30); $form_data['customer_first_name'] = $this->_helper->s2p_mb_substr($order->getBillingAddress()->getFirstname(), 0, 30); $form_data['customer_name'] = $this->_helper->s2p_mb_substr($form_data['customer_first_name'] . ' ' . $form_data['customer_last_name'], 0, 30); } else { $form_data['customer_name'] = $this->_helper->s2p_mb_substr($order->getCustomerName(), 0, 30); $form_data['customer_last_name'] = $this->_helper->s2p_mb_substr($order->getCustomerLastname(), 0, 30); $form_data['customer_first_name'] = $this->_helper->s2p_mb_substr($order->getCustomerFirstname(), 0, 30); } $form_data['customer_email'] = trim($order->getCustomerEmail()); $form_data['country'] = $order->getBillingAddress()->getCountryId(); $messageToHash = 'MerchantID' . $form_data['mid'] . 'MerchantTransactionID' . $form_data['order_id'] . 'Amount' . $form_data['amount'] . 'Currency' . $form_data['currency'] . 'ReturnURL' . $form_data['return_url']; if ($form_data['site_id']) { $messageToHash .= 'SiteID' . $form_data['site_id']; } $messageToHash .= 'CustomerName' . $form_data['customer_name']; $messageToHash .= 'CustomerLastName' . $form_data['customer_last_name']; $messageToHash .= 'CustomerFirstName' . $form_data['customer_first_name']; $messageToHash .= 'CustomerEmail' . $form_data['customer_email']; $messageToHash .= 'Country' . $form_data['country']; $messageToHash .= 'MethodID' . $form_data['method_id']; $form_data['order_description'] = 'Ref. no.: ' . $form_data['order_id']; if (empty($form_data['product_description_ref'])) { $form_data['order_description'] = $form_data['product_description_custom']; } $messageToHash .= 'Description' . $form_data['order_description']; $form_data['skip_hpp'] = 0; if ($form_data['skip_payment_page'] and (!in_array($form_data['method_id'], [Smart2Pay::PAYMENT_METHOD_BT, Smart2Pay::PAYMENT_METHOD_SIBS]) or $form_data['notify_payment_instructions'])) { $form_data['skip_hpp'] = 1; $messageToHash .= 'SkipHpp1'; } if ($form_data['redirect_in_iframe']) { $messageToHash .= 'RedirectInIframe1'; } if ($form_data['skin_id']) { $messageToHash .= 'SkinID' . $form_data['skin_id']; } $messageToHash .= $form_data['signature']; $form_data['message_to_hash'] = $this->_helper->s2p_mb_strtolower($messageToHash); $form_data['hash'] = $this->_helper->computeSHA256Hash($messageToHash); $this->_s2pLogger->write('Form hash: [' . $messageToHash . ']', 'info'); $s2p_transaction = $this->_s2pTransaction->create(); $s2p_transaction->setID($additional_info['sp_transaction'])->setMethodID($form_data['method_id'])->setMerchantTransactionID($form_data['order_id'])->setSiteID($form_data['site_id'])->setEnvironment($form_data['environment']); $s2p_transaction->save(); $order->addStatusHistoryComment('Smart2Pay :: redirecting to payment page with MethodID: ' . $form_data['method_id']); $order->save(); } $this->addData(['order_ok' => $order_is_ok, 'error_message' => $order_error_message, 'order_id' => $order->getIncrementId(), 'form_data' => $form_data]); }
/** * Return a url to go to order detail page * * @return string */ public function getOrderUrl() { $params = ['order_id' => $this->_checkoutSession->getLastRealOrder()->getId()]; $url = $this->_urlBuilder->getUrl('sales/order/view', $params); return $url; }
/** * Returns the Nosto order meta-data model. * * @return \NostoOrder the order meta data model. */ public function getNostoOrder() { /** @var \Magento\Sales\Model\Order $order */ return $this->_orderBuilder->build($this->_checkoutSession->getLastRealOrder()); }
/** * @return \Magento\Sales\Model\Order */ public function getOrder() { return $this->session->getLastRealOrder(); }
/** * Prepares block data * * @return void */ protected function prepareBlockData() { $order = $this->_checkoutSession->getLastRealOrder(); $this->addData(['is_order_visible' => $this->isVisible($order), 'view_order_url' => $this->getUrl('sales/order/view/', ['order_id' => $order->getEntityId()]), 'print_url' => $this->getUrl('sales/order/print', ['order_id' => $order->getEntityId()]), 'can_print_order' => $this->isVisible($order), 'can_view_order' => $this->canViewOrder($order), 'order_id' => $order->getIncrementId()]); }
/** * Get the billing address data of the Order * * @return \Magento\Sales\Model\Order\Address|null */ private function getBillingAddress() { return $this->_checkoutSession->getLastRealOrder()->getBillingAddress(); }