81 private $paymentFailures;
88 $this->scopeConfigMock = $this->getMockBuilder(\
Magento\Framework\
App\
Config\ScopeConfigInterface::class)
90 $this->paymentMock = $this->getMockBuilder(\
Magento\Sales\Model\
Order\Payment::class)
91 ->disableOriginalConstructor()
93 'getOrder',
'getId',
'setAdditionalInformation',
'getAdditionalInformation',
94 'setIsTransactionDenied',
'setIsTransactionClosed',
'decrypt',
'getCcLast4',
95 'getParentTransactionId',
'getPoNumber' 98 $this->dataHelperMock = $this->getMockBuilder(\
Magento\Authorizenet\Helper\Data::class)
99 ->disableOriginalConstructor()
102 $this->initResponseFactoryMock();
104 $this->transactionRepositoryMock = $this->getMockBuilder(
107 ->disableOriginalConstructor()
108 ->setMethods([
'getByTransactionId'])
111 $this->transactionServiceMock = $this->getMockBuilder(\
Magento\Authorizenet\Model\TransactionService::class)
112 ->disableOriginalConstructor()
113 ->setMethods([
'getTransactionDetails'])
116 $this->paymentFailures = $this->getMockBuilder(
117 PaymentFailuresInterface::class
119 ->disableOriginalConstructor()
122 $this->requestFactory = $this->getRequestFactoryMock();
123 $httpClientFactoryMock = $this->getHttpClientFactoryMock();
125 $helper =
new ObjectManagerHelper($this);
126 $this->directpost =
$helper->getObject(
127 \
Magento\Authorizenet\Model\Directpost::class,
129 'scopeConfig' => $this->scopeConfigMock,
130 'dataHelper' => $this->dataHelperMock,
131 'requestFactory' => $this->requestFactory,
132 'responseFactory' => $this->responseFactoryMock,
133 'transactionRepository' => $this->transactionRepositoryMock,
134 'transactionService' => $this->transactionServiceMock,
135 'httpClientFactory' => $httpClientFactoryMock,
136 'paymentFailures' => $this->paymentFailures,
143 $this->assertInstanceOf(
145 $this->directpost->getConfigInterface()
151 $field =
'some_field';
152 $returnValue =
'expected';
153 $this->scopeConfigMock->expects($this->once())
155 ->with(
'payment/authorizenet_directpost/' . $field)
156 ->willReturn($returnValue);
157 $this->assertEquals($returnValue, $this->directpost->getValue($field));
163 $expectedResult =
'relay-url';
165 $helperDataMock = $this->getMockBuilder(\
Magento\Authorizenet\Helper\Backend\Data::class)
166 ->disableOriginalConstructor()
169 $helperDataMock->expects($this->once())
170 ->method(
'getRelayUrl')
172 ->willReturn($expectedResult);
174 $this->directpost->setDataHelper($helperDataMock);
175 $this->assertEquals($expectedResult, $this->directpost->getRelayUrl(
$storeId));
180 $paymentAction =
'some_action';
182 $this->scopeConfigMock->expects($this->any())
184 ->with(
'payment/authorizenet_directpost/payment_action',
'store',
null)
185 ->willReturn($paymentAction);
186 $this->paymentMock->expects($this->once())
187 ->method(
'setAdditionalInformation')
188 ->with(
'payment_type', $paymentAction);
190 $this->directpost->authorize($this->paymentMock, 10);
197 $this->scopeConfigMock->expects($this->any())
199 ->with(
'payment/authorizenet_directpost/cgi_url',
'store',
null)
202 $this->assertEquals(
$url, $this->directpost->getCgiUrl());
207 $this->scopeConfigMock->expects($this->any())
209 ->with(
'payment/authorizenet_directpost/cgi_url',
'store',
null)
219 $this->directpost->setData(
'store',
$storeId);
221 $this->dataHelperMock->expects($this->any())
222 ->method(
'getRelayUrl')
226 $this->assertEquals(
$url, $this->directpost->getRelayUrl());
227 $this->assertEquals(
$url, $this->directpost->getRelayUrl(
$storeId));
232 $this->assertSame($this->responseMock, $this->directpost->getResponse());
241 $this->responseMock->expects($this->once())
246 $this->assertSame($this->directpost, $this->directpost->setResponseData(
$data));
252 $this->assertEquals(
true, $this->directpost->validateResponse());
261 $this->directpost->validateResponse();
271 $this->scopeConfigMock->expects($this->any())
275 [
'payment/authorizenet_directpost/trans_md5',
'store',
null, $transMd5],
276 [
'payment/authorizenet_directpost/login',
'store',
null, $login]
279 $this->responseMock->expects($this->any())
280 ->method(
'isValidHash')
281 ->with($transMd5, $login)
282 ->willReturn($isValidHash);
287 $this->responseMock->expects($this->once())
288 ->method(
'getXTransId')
291 $this->assertEquals(
true, $this->directpost->checkTransId());
299 $this->responseMock->expects($this->once())
300 ->method(
'getXTransId')
303 $this->directpost->checkTransId();
313 $this->responseMock->expects($this->once())
314 ->method(
'getXResponseCode')
315 ->willReturn($responseCode);
317 $this->assertEquals(
true, $this->directpost->checkResponseCode());
343 $reasonText =
'reason text';
345 $this->responseMock->expects($this->once())
346 ->method(
'getXResponseCode')
347 ->willReturn($responseCode);
348 $this->responseMock->expects($this->any())
349 ->method(
'getXResponseReasonText')
350 ->willReturn($reasonText);
351 $this->dataHelperMock->expects($this->any())
352 ->method(
'wrapGatewayError')
354 ->willReturn(
__(
'Gateway error: %1', $reasonText));
356 $orderMock = $this->getMockBuilder(Order::class)
357 ->disableOriginalConstructor()
360 $orderMock->expects($this->exactly($failuresHandlerCalls))
361 ->method(
'getQuoteId')
364 $this->paymentFailures->expects($this->exactly($failuresHandlerCalls))
368 $reflection = new \ReflectionClass($this->directpost);
369 $order = $reflection->getProperty(
'order');
370 $order->setAccessible(
true);
371 $order->setValue($this->directpost, $orderMock);
373 $this->directpost->checkResponseCode();
384 [
'responseCode' => 999999, 0],
395 $this->directpost->setIsInitializeNeeded($isInitializeNeeded);
396 $this->assertEquals($isInitializeNeeded, $this->directpost->isInitializeNeeded());
405 [
'isInitializationNeeded' =>
true],
406 [
'isInitializationNeeded' =>
false]
418 $this->directpost->setData(
'info_instance', $this->paymentMock);
420 $this->paymentMock->expects($this->any())
421 ->method(
'getAdditionalInformation')
423 ->willReturn($isGatewayActionsLocked);
425 $this->assertEquals($canCapture, $this->directpost->canCapture());
434 [
'isGatewayActionsLocked' =>
false,
'canCapture' =>
true],
435 [
'isGatewayActionsLocked' =>
true,
'canCapture' =>
false]
455 $this->paymentMock->expects(static::once())
457 ->willReturn($paymentId);
459 $orderMock = $this->getMockBuilder(\
Magento\Sales\Model\Order::class)
460 ->disableOriginalConstructor()
461 ->setMethods([
'getId',
'__wakeup'])
463 $orderMock->expects(static::once())
465 ->willReturn($orderId);
467 $this->paymentMock->expects(static::once())
469 ->willReturn($orderMock);
471 $transactionMock = $this->getMockBuilder(\
Magento\Sales\Model\
Order\
Payment\Transaction::class)
472 ->disableOriginalConstructor()
474 $this->transactionRepositoryMock->expects(static::once())
475 ->method(
'getByTransactionId')
476 ->with($transactionId, $paymentId, $orderId)
477 ->willReturn($transactionMock);
479 $document = $this->getTransactionXmlDocument(
486 $this->transactionServiceMock->expects(static::once())
487 ->method(
'getTransactionDetails')
488 ->with($this->directpost, $transactionId)
489 ->willReturn($document);
492 $this->paymentMock->expects(static::once())
493 ->method(
'setIsTransactionDenied')
495 $this->paymentMock->expects(static::once())
496 ->method(
'setIsTransactionClosed')
498 $transactionMock->expects(static::once())
501 $this->directpost->fetchTransactionInfo($this->paymentMock, $transactionId);
512 $this->paymentMock->expects(static::exactly(2))
513 ->method(
'getCcLast4')
515 $this->paymentMock->expects(static::once())
518 $this->paymentMock->expects(static::exactly(3))
519 ->method(
'getParentTransactionId')
520 ->willReturn(self::TRANSACTION_ID .
'-capture');
521 $this->paymentMock->expects(static::once())
522 ->method(
'getPoNumber')
523 ->willReturn(self::INVOICE_NUM);
524 $this->paymentMock->expects(static::once())
525 ->method(
'setIsTransactionClosed')
529 $orderMock = $this->getOrderMock();
531 $this->paymentMock->expects(static::exactly(2))
533 ->willReturn($orderMock);
535 $transactionMock = $this->getMockBuilder(
Order\
Payment\Transaction::class)
536 ->disableOriginalConstructor()
537 ->setMethods([
'getAdditionalInformation'])
539 $transactionMock->expects(static::once())
540 ->method(
'getAdditionalInformation')
542 ->willReturn(self::TRANSACTION_ID);
544 $this->transactionRepositoryMock->expects(static::once())
545 ->method(
'getByTransactionId')
546 ->willReturn($transactionMock);
548 $response = $this->getRefundResponseBody(
553 $this->httpClientMock->expects(static::once())
557 $this->responseMock->expects(static::once())
558 ->method(
'getXResponseCode')
560 $this->responseMock->expects(static::once())
561 ->method(
'getXResponseReasonCode')
564 $this->dataHelperMock->expects(static::never())
565 ->method(
'wrapGatewayError');
567 $this->directpost->refund($this->paymentMock, self::TOTAL_AMOUNT);
578 'transactionId' =>
'9941997799',
579 'resultStatus' =>
'Successful.',
580 'responseStatus' =>
'voided',
590 private function initResponseFactoryMock()
592 $this->responseFactoryMock = $this->getMockBuilder(
594 )->disableOriginalConstructor()->getMock();
595 $this->responseMock = $this->getMockBuilder(\
Magento\Authorizenet\Model\
Directpost\Response::class)
599 'getXTransId',
'getXResponseCode',
'getXResponseReasonCode',
'getXResponseReasonText',
'getXAmount',
600 'setXResponseCode',
'setXResponseReasonCode',
'setXAvsCode',
'setXResponseReasonText',
601 'setXTransId',
'setXInvoiceNum',
'setXAmount',
'setXMethod',
'setXType',
'setData',
606 ->disableOriginalConstructor()
609 $this->responseMock->expects(static::any())
610 ->method(
'setXResponseCode')
612 $this->responseMock->expects(static::any())
613 ->method(
'setXResponseReasonCode')
615 $this->responseMock->expects(static::any())
616 ->method(
'setXResponseReasonText')
618 $this->responseMock->expects(static::any())
619 ->method(
'setXAvsCode')
621 $this->responseMock->expects(static::any())
622 ->method(
'setXTransId')
624 $this->responseMock->expects(static::any())
625 ->method(
'setXInvoiceNum')
627 $this->responseMock->expects(static::any())
628 ->method(
'setXAmount')
630 $this->responseMock->expects(static::any())
631 ->method(
'setXMethod')
633 $this->responseMock->expects(static::any())
636 $this->responseMock->expects(static::any())
640 $this->responseFactoryMock->expects($this->any())
642 ->willReturn($this->responseMock);
654 private function getTransactionXmlDocument(
662 '<?xml version="1.0" encoding="utf-8"?> 663 <getTransactionDetailsResponse 664 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 665 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 666 xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"> 668 <resultCode>%s</resultCode> 675 <transId>%s</transId> 676 <transactionType>authOnlyTransaction</transactionType> 677 <transactionStatus>%s</transactionStatus> 678 <responseCode>%s</responseCode> 679 <responseReasonCode>%s</responseReasonCode> 681 </getTransactionDetailsResponse>',
689 libxml_use_internal_errors(
true);
690 $document =
new Element($body);
691 libxml_use_internal_errors(
false);
699 private function getRequestFactoryMock()
702 ->disableOriginalConstructor()
703 ->setMethods([
'create'])
705 $request = $this->getMockBuilder(Request::class)
706 ->disableOriginalConstructor()
707 ->setMethods([
'__wakeup'])
719 private function getOrderMock()
721 $orderMock = $this->getMockBuilder(Order::class)
722 ->disableOriginalConstructor()
724 'getId',
'getIncrementId',
'getStoreId',
'getBillingAddress',
'getShippingAddress',
725 'getBaseCurrencyCode',
'getBaseTaxAmount',
'__wakeup' 729 $orderMock->expects(static::once())
733 $orderMock->expects(static::exactly(2))
734 ->method(
'getIncrementId')
735 ->willReturn(self::INVOICE_NUM);
737 $orderMock->expects(static::once())
738 ->method(
'getStoreId')
741 $orderMock->expects(static::once())
742 ->method(
'getBaseCurrencyCode')
751 private function getHttpClientFactoryMock()
753 $this->httpClientMock = $this->getMockBuilder(\
Magento\Framework\HTTP\ZendClient::class)
754 ->disableOriginalConstructor()
755 ->setMethods([
'request',
'getBody',
'__wakeup'])
758 $this->httpClientMock->expects(static::any())
762 $httpClientFactoryMock = $this->getMockBuilder(\
Magento\Framework\HTTP\ZendClientFactory::class)
763 ->disableOriginalConstructor()
764 ->setMethods([
'create'])
767 $httpClientFactoryMock->expects(static::any())
769 ->willReturn($this->httpClientMock);
770 return $httpClientFactoryMock;
780 private function getRefundResponseBody(
$code, $reasonCode, $reasonText)
782 $result = array_fill(0, 50,
'');
791 $result[37] = md5(self::TRANSACTION_ID);
testCheckTransIdSuccess()
testCanCapture($isGatewayActionsLocked, $canCapture)
dataProviderTransaction()
const GATEWAY_ACTIONS_LOCKED_STATE_KEY
const RESPONSE_REASON_CODE_APPROVED
checkResponseCodeSuccessDataProvider()
testCheckResponseCodeFailure(int $responseCode, int $failuresHandlerCalls)
setIsInitializeNeededDataProvider()
testValidateResponseSuccess()
testCheckResponseCodeSuccess($responseCode)
$transactionRepositoryMock
testValidateResponseFailure()
prepareTestValidateResponse($transMd5, $login, $isValidHash)
const PAYMENT_UPDATE_STATUS_CODE_SUCCESS
const RESPONSE_CODE_ERROR
const RESPONSE_CODE_DECLINED
testCheckTransIdFailure()
const REQUEST_TYPE_CREDIT
const RESPONSE_CODE_APPROVED
testGetCgiUrlWithEmptyConfigValue()
const RESPONSE_DELIM_CHAR
checkResponseCodeFailureDataProvider()
const REAL_TRANSACTION_ID_KEY
testFetchVoidedTransactionInfo($transactionId, $resultStatus, $responseStatus, $responseCode)
testSetIsInitializeNeeded($isInitializeNeeded)