/** * Save order into registry to use it in the overloaded controller. * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { /* @var $order Order */ $order = $this->coreRegistry->registry('directpost_order'); if (!$order || !$order->getId()) { return $this; } $payment = $order->getPayment(); if (!$payment || $payment->getMethod() != $this->payment->getCode()) { return $this; } $result = $observer->getData('result')->getData(); if (!empty($result['error'])) { return $this; } // if success, then set order to session and add new fields $this->session->addCheckoutOrderIncrementId($order->getIncrementId()); $this->session->setLastOrderIncrementId($order->getIncrementId()); $requestToAuthorizenet = $payment->getMethodInstance()->generateRequestFromOrder($order); $requestToAuthorizenet->setControllerActionName($observer->getData('action')->getRequest()->getControllerName()); $requestToAuthorizenet->setIsSecure((string) $this->storeManager->getStore()->isCurrentlySecure()); $result[$this->payment->getCode()] = ['fields' => $requestToAuthorizenet->getData()]; $observer->getData('result')->setData($result); return $this; }
/** * Test for addFieldsToResponse method * * @return void */ public function testAddFieldsToResponseSuccess() { $testData = $this->getAddFieldsToResponseSuccessTestData(); $observerMock = $this->getMockBuilder('Magento\\Framework\\Event\\Observer')->disableOriginalConstructor()->getMock(); $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->getMock(); $orderPaymentMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Payment')->disableOriginalConstructor()->getMock(); $instanceMock = $this->getMockBuilder('Magento\\Authorizenet\\Model\\Directpost')->disableOriginalConstructor()->getMock(); $requestToAuthorizenetMock = $this->getMockBuilder('Magento\\Authorizenet\\Model\\Directpost\\Request')->disableOriginalConstructor()->setMethods(['setControllerActionName', 'setIsSecure', 'getData'])->getMock(); $requestMock = $this->getMockBuilder('Magento\\Framework\\App\\RequestInterface')->disableOriginalConstructor()->setMethods(['getControllerName'])->getMockForAbstractClass(); $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $this->coreRegistryMock->expects($this->once())->method('registry')->with('directpost_order')->willReturn($orderMock); $orderMock->expects($this->once())->method('getId')->willReturn($testData['order.getId']); $orderMock->expects($this->once())->method('getPayment')->willReturn($orderPaymentMock); $orderPaymentMock->expects($this->once())->method('getMethod')->willReturn($testData['orderPayment.getMethod']); $this->paymentMock->expects($this->exactly(2))->method('getCode')->willReturn($testData['payment.getCode']); $observerMock->expects($this->atLeastOnce())->method('getData')->willReturnMap($testData['observer.getData']); $this->resultMock->expects($this->once())->method('getData')->willReturn($testData['result.getData']); $orderMock->expects($this->atLeastOnce())->method('getIncrementId')->willReturn($testData['order.getIncrementId']); $this->sessionMock->expects($this->once())->method('addCheckoutOrderIncrementId')->with($testData['session.addCheckoutOrderIncrementId']); $this->sessionMock->expects($this->once())->method('setLastOrderIncrementId')->with($testData['session.setLastOrderIncrementId']); $orderPaymentMock->expects($this->once())->method('getMethodInstance')->willReturn($instanceMock); $instanceMock->expects($this->once())->method('generateRequestFromOrder')->with($orderMock)->willReturn($requestToAuthorizenetMock); $this->actionMock->expects($this->once())->method('getRequest')->willReturn($requestMock); $requestMock->expects($this->once())->method('getControllerName')->willReturn($testData['request.getControllerName']); $requestToAuthorizenetMock->expects($this->once())->method('setControllerActionName')->with($testData['requestToAuthorizenet.setControllerActionName']); $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock); $storeMock->expects($this->once())->method('isCurrentlySecure')->willReturn($testData['store.isCurrentlySecure']); $requestToAuthorizenetMock->expects($this->once())->method('setIsSecure')->with($testData['requestToAuthorizenet.setIsSecure']); $requestToAuthorizenetMock->expects($this->once())->method('getData')->willReturn($testData['requestToAuthorizenet.getData']); $this->resultMock->expects($this->once())->method('setData')->with($testData['result.setData']); $this->addFieldsToResponseObserver->execute($observerMock); }
/** * Set data for response of frontend saveOrder action * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function addAdditionalFieldsToResponseFrontend(\Magento\Framework\Event\Observer $observer) { /* @var $order \Magento\Sales\Model\Order */ $order = $this->_coreRegistry->registry('directpost_order'); if ($order && $order->getId()) { $payment = $order->getPayment(); if ($payment && $payment->getMethod() == $this->_payment->getCode()) { /** @var \Magento\Checkout\Controller\Action $controller */ $controller = $observer->getEvent()->getData('controller_action'); $request = $controller->getRequest(); $response = $controller->getResponse(); $result = $this->_coreData->jsonDecode($response->getBody('default')); if (empty($result['error'])) { $payment = $order->getPayment(); //if success, then set order to session and add new fields $this->_session->addCheckoutOrderIncrementId($order->getIncrementId()); $this->_session->setLastOrderIncrementId($order->getIncrementId()); $requestToAuthorizenet = $payment->getMethodInstance()->generateRequestFromOrder($order); $requestToAuthorizenet->setControllerActionName($request->getControllerName()); $requestToAuthorizenet->setIsSecure((string) $this->_storeManager->getStore()->isCurrentlySecure()); $result['directpost'] = array('fields' => $requestToAuthorizenet->getData()); $response->clearHeader('Location'); $response->representJson($this->_coreData->jsonEncode($result)); } } } return $this; }
/** * @covers \Magento\Authorizenet\Model\Directpost::fetchTransactionInfo * * @param $transactionId * @param $resultStatus * @param $responseStatus * @param $responseCode * @return void * * @dataProvider dataProviderTransaction */ public function testFetchVoidedTransactionInfo($transactionId, $resultStatus, $responseStatus, $responseCode) { $paymentId = 36; $orderId = 36; $this->paymentMock->expects(static::once())->method('getId')->willReturn($paymentId); $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods(['getId', '__wakeup'])->getMock(); $orderMock->expects(static::once())->method('getId')->willReturn($orderId); $this->paymentMock->expects(static::once())->method('getOrder')->willReturn($orderMock); $transactionMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Payment\\Transaction')->disableOriginalConstructor()->getMock(); $this->transactionRepositoryMock->expects(static::once())->method('getByTransactionId')->with($transactionId, $paymentId, $orderId)->willReturn($transactionMock); $document = $this->getTransactionXmlDocument($transactionId, TransactionService::PAYMENT_UPDATE_STATUS_CODE_SUCCESS, $resultStatus, $responseStatus, $responseCode); $this->transactionServiceMock->expects(static::once())->method('getTransactionDetails')->with($this->directpost, $transactionId)->willReturn($document); // transaction should be closed $this->paymentMock->expects(static::once())->method('setIsTransactionDenied')->with(true); $this->paymentMock->expects(static::once())->method('setIsTransactionClosed')->with(true); $transactionMock->expects(static::once())->method('close'); $this->directpost->fetchTransactionInfo($this->paymentMock, $transactionId); }
/** * @covers \Magento\Authorizenet\Model\Directpost::refund() * @return void */ public function testSuccessRefund() { $card = 1111; $this->paymentMock->expects(static::exactly(2))->method('getCcLast4')->willReturn($card); $this->paymentMock->expects(static::once())->method('decrypt')->willReturn($card); $this->paymentMock->expects(static::exactly(3))->method('getParentTransactionId')->willReturn(self::TRANSACTION_ID . '-capture'); $this->paymentMock->expects(static::once())->method('getPoNumber')->willReturn(self::INVOICE_NUM); $this->paymentMock->expects(static::once())->method('setIsTransactionClosed')->with(true)->willReturnSelf(); $orderMock = $this->getOrderMock(); $this->paymentMock->expects(static::exactly(2))->method('getOrder')->willReturn($orderMock); $transactionMock = $this->getMockBuilder(Order\Payment\Transaction::class)->disableOriginalConstructor()->setMethods(['getAdditionalInformation'])->getMock(); $transactionMock->expects(static::once())->method('getAdditionalInformation')->with(Directpost::REAL_TRANSACTION_ID_KEY)->willReturn(self::TRANSACTION_ID); $this->transactionRepositoryMock->expects(static::once())->method('getByTransactionId')->willReturn($transactionMock); $response = $this->getRefundResponseBody(Directpost::RESPONSE_CODE_APPROVED, Directpost::RESPONSE_REASON_CODE_APPROVED, 'Successful'); $this->httpClientMock->expects(static::once())->method('getBody')->willReturn($response); $this->responseMock->expects(static::once())->method('getXResponseCode')->willReturn(Directpost::RESPONSE_CODE_APPROVED); $this->responseMock->expects(static::once())->method('getXResponseReasonCode')->willReturn(Directpost::RESPONSE_REASON_CODE_APPROVED); $this->dataHelperMock->expects(static::never())->method('wrapGatewayError'); $this->directpost->refund($this->paymentMock, self::TOTAL_AMOUNT); }
/** * Set entity data to request * * @param \Magento\Sales\Model\Order $order * @param \Magento\Authorizenet\Model\Directpost $paymentMethod * @return $this */ public function setDataFromOrder(\Magento\Sales\Model\Order $order, \Magento\Authorizenet\Model\Directpost $paymentMethod) { $payment = $order->getPayment(); $this->setXType($payment->getAnetTransType()); $this->setXFpSequence($order->getQuoteId()); $this->setXInvoiceNum($order->getIncrementId()); $this->setXAmount($payment->getBaseAmountAuthorized()); $this->setXCurrencyCode($order->getBaseCurrencyCode()); $this->setXTax(sprintf('%.2F', $order->getBaseTaxAmount()))->setXFreight(sprintf('%.2F', $order->getBaseShippingAmount())); //need to use strval() because NULL values IE6-8 decodes as "null" in JSON in JavaScript, //but we need "" for null values. $billing = $order->getBillingAddress(); if (!empty($billing)) { $this->setXFirstName(strval($billing->getFirstname()))->setXLastName(strval($billing->getLastname()))->setXCompany(strval($billing->getCompany()))->setXAddress(strval($billing->getStreetLine(1)))->setXCity(strval($billing->getCity()))->setXState(strval($billing->getRegion()))->setXZip(strval($billing->getPostcode()))->setXCountry(strval($billing->getCountry()))->setXPhone(strval($billing->getTelephone()))->setXFax(strval($billing->getFax()))->setXCustId(strval($billing->getCustomerId()))->setXCustomerIp(strval($order->getRemoteIp()))->setXCustomerTaxId(strval($billing->getTaxId()))->setXEmail(strval($order->getCustomerEmail()))->setXEmailCustomer(strval($paymentMethod->getConfigData('email_customer')))->setXMerchantEmail(strval($paymentMethod->getConfigData('merchant_email'))); } $shipping = $order->getShippingAddress(); if (!empty($shipping)) { $this->setXShipToFirstName(strval($shipping->getFirstname()))->setXShipToLastName(strval($shipping->getLastname()))->setXShipToCompany(strval($shipping->getCompany()))->setXShipToAddress(strval($shipping->getStreetLine(1)))->setXShipToCity(strval($shipping->getCity()))->setXShipToState(strval($shipping->getRegion()))->setXShipToZip(strval($shipping->getPostcode()))->setXShipToCountry(strval($shipping->getCountry())); } $this->setXPoNum(strval($payment->getPoNumber())); return $this; }
/** * @param bool $isGatewayActionsLocked * @param bool $canCapture * * @dataProvider canCaptureDataProvider */ public function testCanCapture($isGatewayActionsLocked, $canCapture) { $this->directpost->setData('info_instance', $this->paymentMock); $this->paymentMock->expects($this->any())->method('getAdditionalInformation')->with(Directpost::GATEWAY_ACTIONS_LOCKED_STATE_KEY)->willReturn($isGatewayActionsLocked); $this->assertEquals($canCapture, $this->directpost->canCapture()); }
/** * Render block HTML * If method is not directpost - nothing to return * * @return string */ protected function _toHtml() { return $this->getMethod()->getCode() == $this->_model->getCode() ? parent::_toHtml() : ''; }