/** * @param int $configValue * @param bool|null $forceSyncMode * @param bool|null $customerNoteNotify * @param bool|null $emailSendingResult * @dataProvider sendDataProvider * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testSend($configValue, $forceSyncMode, $customerNoteNotify, $emailSendingResult) { $comment = 'comment_test'; $address = 'address_test'; $configPath = 'sales_email/general/async_sending'; $this->creditmemoMock->expects($this->once())->method('setSendEmail')->with(true); $this->globalConfig->expects($this->once())->method('getValue')->with($configPath)->willReturn($configValue); if (!$configValue || $forceSyncMode) { $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false); $this->addressRenderer->expects($this->any())->method('format')->with($addressMock, 'html')->willReturn($address); $this->orderMock->expects($this->any())->method('getBillingAddress')->willReturn($addressMock); $this->orderMock->expects($this->any())->method('getShippingAddress')->willReturn($addressMock); $this->creditmemoMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn($customerNoteNotify); $this->creditmemoMock->expects($this->any())->method('getCustomerNote')->willReturn($comment); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'creditmemo' => $this->creditmemoMock, 'comment' => $customerNoteNotify ? $comment : '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $address, 'formattedBillingAddress' => $address]); $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn($emailSendingResult); if ($emailSendingResult) { $this->senderBuilderFactoryMock->expects($this->once())->method('create')->willReturn($this->senderMock); $this->senderMock->expects($this->once())->method('send'); $this->senderMock->expects($this->once())->method('sendCopyTo'); $this->creditmemoMock->expects($this->once())->method('setEmailSent')->with(true); $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, ['send_email', 'email_sent']); $this->assertTrue($this->sender->send($this->creditmemoMock)); } else { $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, 'send_email'); $this->assertFalse($this->sender->send($this->creditmemoMock)); } } else { $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, 'send_email'); $this->assertFalse($this->sender->send($this->creditmemoMock)); } }
/** * Add comment to creditmemo history * * @return \Magento\Framework\Controller\Result\Raw|\Magento\Framework\Controller\Result\Json */ public function execute() { try { $this->getRequest()->setParam('creditmemo_id', $this->getRequest()->getParam('id')); $data = $this->getRequest()->getPost('comment'); if (empty($data['comment'])) { throw new \Magento\Framework\Exception\LocalizedException(__('The Comment Text field cannot be empty.')); } $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id')); $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo')); $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id')); $creditmemo = $this->creditmemoLoader->load(); $comment = $creditmemo->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front'])); $comment->save(); $this->creditmemoSender->send($creditmemo, !empty($data['is_customer_notified']), $data['comment']); $resultPage = $this->resultPageFactory->create(); $response = $resultPage->getLayout()->getBlock('creditmemo_comments')->toHtml(); } catch (\Magento\Framework\Exception\LocalizedException $e) { $response = ['error' => true, 'message' => $e->getMessage()]; } catch (\Exception $e) { $response = ['error' => true, 'message' => __('Cannot add new comment.')]; } if (is_array($response)) { $resultJson = $this->resultJsonFactory->create(); $resultJson->setData($response); return $resultJson; } else { $resultRaw = $this->resultRawFactory->create(); $resultRaw->setContents($response); return $resultRaw; } }
/** * Add comment to creditmemo history * * @return void */ public function execute() { try { $this->getRequest()->setParam('creditmemo_id', $this->getRequest()->getParam('id')); $data = $this->getRequest()->getPost('comment'); if (empty($data['comment'])) { throw new \Magento\Framework\Model\Exception(__('The Comment Text field cannot be empty.')); } $this->_title->add(__('Credit Memos')); $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id')); $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo')); $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id')); $creditmemo = $this->creditmemoLoader->load(); $comment = $creditmemo->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front'])); $comment->save(); $this->creditmemoSender->send($creditmemo, !empty($data['is_customer_notified']), $data['comment']); $this->_view->loadLayout(); $response = $this->_view->getLayout()->getBlock('creditmemo_comments')->toHtml(); } catch (\Magento\Framework\Model\Exception $e) { $response = array('error' => true, 'message' => $e->getMessage()); } catch (\Exception $e) { $response = array('error' => true, 'message' => __('Cannot add new comment.')); } if (is_array($response)) { $response = $this->_objectManager->get('Magento\\Core\\Helper\\Data')->jsonEncode($response); $this->getResponse()->representJson($response); } else { $this->getResponse()->setBody($response); } }
/** * Save creditmemo * We can save only new creditmemo. Existing creditmemos are not editable * * @return \Magento\Backend\Model\View\Result\Redirect|\Magento\Backend\Model\View\Result\Forward * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute() { $resultRedirect = $this->resultRedirectFactory->create(); $data = $this->getRequest()->getPost('creditmemo'); if (!empty($data['comment_text'])) { $this->_getSession()->setCommentText($data['comment_text']); } try { $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id')); $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo')); $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id')); $creditmemo = $this->creditmemoLoader->load(); if ($creditmemo) { if (!$creditmemo->isValidGrandTotal()) { throw new \Magento\Framework\Exception\LocalizedException(__('The credit memo\'s total must be positive.')); } if (!empty($data['comment_text'])) { $creditmemo->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front'])); $creditmemo->setCustomerNote($data['comment_text']); $creditmemo->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } if (isset($data['do_offline'])) { //do not allow online refund for Refund to Store Credit if (!$data['do_offline'] && !empty($data['refund_customerbalance_return_enable'])) { throw new \Magento\Framework\Exception\LocalizedException(__('Cannot create online refund for Refund to Store Credit.')); } } $creditmemoManagement = $this->_objectManager->create('Magento\\Sales\\Api\\CreditmemoManagementInterface'); $creditmemoManagement->refund($creditmemo, (bool) $data['do_offline'], !empty($data['send_email'])); if (!empty($data['send_email'])) { $this->creditmemoSender->send($creditmemo); } $this->messageManager->addSuccess(__('You created the credit memo.')); $this->_getSession()->getCommentText(true); $resultRedirect->setPath('sales/order/view', ['order_id' => $creditmemo->getOrderId()]); return $resultRedirect; } else { $resultForward = $this->resultForwardFactory->create(); $resultForward->forward('noroute'); return $resultForward; } } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addError($e->getMessage()); $this->_getSession()->setFormData($data); } catch (\Exception $e) { $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e); $this->messageManager->addError(__('We can\'t save the credit memo right now.')); } $resultRedirect->setPath('sales/*/new', ['_current' => true]); return $resultRedirect; }
/** * Process a refund * * @return void */ protected function _registerPaymentRefund() { $this->_importPaymentInformation(); $reason = $this->getRequestData('reason_code'); $isRefundFinal = !$this->_paypalInfo->isReversalDisputable($reason); $payment = $this->_order->getPayment()->setPreparedMessage($this->_createIpnComment($this->_paypalInfo->explainReasonCode($reason)))->setTransactionId($this->getRequestData('txn_id'))->setParentTransactionId($this->getRequestData('parent_txn_id'))->setIsTransactionClosed($isRefundFinal)->registerRefundNotification(-1 * $this->getRequestData('mc_gross')); $this->_order->save(); // TODO: there is no way to close a capture right now $creditMemo = $payment->getCreatedCreditmemo(); if ($creditMemo) { $this->creditmemoSender->send($creditMemo); $this->_order->addStatusHistoryComment(__('You notified customer about creditmemo #%1.', $creditMemo->getIncrementId()))->setIsCustomerNotified(true)->save(); } }
public function testSendTrueWithoutCustomerCopy() { $billingAddress = 'billing_address'; $comment = 'comment_test'; $this->orderMock->expects($this->once())->method('getCustomerIsGuest')->will($this->returnValue(false)); $this->orderMock->expects($this->any())->method('getBillingAddress')->will($this->returnValue($billingAddress)); $this->identityContainerMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with($this->equalTo(['order' => $this->orderMock, 'invoice' => $this->creditmemoMock, 'billing' => $billingAddress, 'payment_html' => 'payment', 'comment' => $comment, 'store' => $this->storeMock])); $senderMock = $this->getMock('Magento\\Sales\\Model\\Order\\Email\\Sender', ['send', 'sendCopyTo'], [], '', false); $senderMock->expects($this->never())->method('send'); $senderMock->expects($this->once())->method('sendCopyTo'); $this->senderBuilderFactoryMock->expects($this->once())->method('create')->will($this->returnValue($senderMock)); $result = $this->sender->send($this->creditmemoMock, false, $comment); $this->assertTrue($result); }
/** * @param bool $isVirtualOrder * @param int $formatCallCount * @param string|null $expectedShippingAddress * @dataProvider sendVirtualOrderDataProvider */ public function testSendVirtualOrder($isVirtualOrder, $formatCallCount, $expectedShippingAddress) { $billingAddress = 'address_test'; $this->orderMock->setData(\Magento\Sales\Api\Data\OrderInterface::IS_VIRTUAL, $isVirtualOrder); $this->creditmemoMock->expects($this->once())->method('setSendEmail')->with(true); $this->globalConfig->expects($this->once())->method('getValue')->with('sales_email/general/async_sending')->willReturn(false); $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false); $this->addressRenderer->expects($this->exactly($formatCallCount))->method('format')->with($addressMock, 'html')->willReturn($billingAddress); $this->stepAddressFormat($addressMock, $isVirtualOrder); $this->creditmemoMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn(true); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'creditmemo' => $this->creditmemoMock, 'comment' => '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $expectedShippingAddress, 'formattedBillingAddress' => $billingAddress]); $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn(false); $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, 'send_email'); $this->assertFalse($this->sender->send($this->creditmemoMock)); }
/** * Save creditmemo * We can save only new creditmemo. Existing creditmemos are not editable * * @return void */ public function execute() { $data = $this->getRequest()->getPost('creditmemo'); if (!empty($data['comment_text'])) { $this->_getSession()->setCommentText($data['comment_text']); } try { $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id')); $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo')); $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id')); $creditmemo = $this->creditmemoLoader->load(); if ($creditmemo) { if (!$creditmemo->isValidGrandTotal()) { throw new \Magento\Framework\Model\Exception(__('Credit memo\'s total must be positive.')); } $comment = ''; if (!empty($data['comment_text'])) { $creditmemo->addComment($data['comment_text'], isset($data['comment_customer_notify']), isset($data['is_visible_on_front'])); if (isset($data['comment_customer_notify'])) { $comment = $data['comment_text']; } } if (isset($data['do_refund'])) { $creditmemo->setRefundRequested(true); } if (isset($data['do_offline'])) { //do not allow online refund for Refund to Store Credit if (!$data['do_offline'] && !empty($data['refund_customerbalance_return_enable'])) { throw new \Magento\Framework\Model\Exception(__('Cannot create online refund for Refund to Store Credit.')); } $creditmemo->setOfflineRequested((bool) (int) $data['do_offline']); } $creditmemo->register(); if (!empty($data['send_email'])) { $creditmemo->setEmailSent(true); } $creditmemo->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); $transactionSave = $this->_objectManager->create('Magento\\Framework\\DB\\Transaction')->addObject($creditmemo)->addObject($creditmemo->getOrder()); if ($creditmemo->getInvoice()) { $transactionSave->addObject($creditmemo->getInvoice()); } $transactionSave->save(); $this->creditmemoSender->send($creditmemo, !empty($data['send_email']), $comment); $this->messageManager->addSuccess(__('You created the credit memo.')); $this->_getSession()->getCommentText(true); $this->_redirect('sales/order/view', array('order_id' => $creditmemo->getOrderId())); return; } else { $this->_forward('noroute'); return; } } catch (\Magento\Framework\Model\Exception $e) { $this->messageManager->addError($e->getMessage()); $this->_getSession()->setFormData($data); } catch (\Exception $e) { $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e); $this->messageManager->addError(__('Cannot save the credit memo.')); } $this->_redirect('sales/*/new', array('_current' => true)); }