Skip to content

Commit 7c509f6

Browse files
author
Viktor Tymchynskyi
committed
Merge branch 'MAGETWO-37717' into develop
Conflicts: app/code/Magento/Sales/Model/Order/Payment.php
2 parents 9c3137c + 6b29e6b commit 7c509f6

File tree

4 files changed

+124
-23
lines changed

4 files changed

+124
-23
lines changed

app/code/Magento/Sales/Model/Order/Payment.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -944,28 +944,31 @@ public function accept()
944944
/**
945945
* Accept order with payment method instance
946946
*
947+
* @param bool $isOnline
947948
* @return $this
949+
* @throws \Magento\Framework\Exception\LocalizedException
948950
*/
949-
public function deny()
951+
public function deny($isOnline = true)
950952
{
951-
$transactionId = $this->getLastTransId();
953+
$transactionId = $isOnline ? $this->getLastTransId() : $this->getTransactionId();
952954

953-
/** @var \Magento\Payment\Model\Method\AbstractMethod $method */
954-
$method = $this->getMethodInstance();
955-
$method->setStore(
956-
$this->getOrder()->getStoreId()
957-
);
958-
if ($method->denyPayment($this)) {
955+
$result = $isOnline ?
956+
$this->getMethodInstance()->setStore($this->getOrder()->getStoreId())->denyPayment($this) :
957+
(bool)$this->getNotificationResult();
958+
959+
if ($result) {
959960
$invoice = $this->_getInvoiceForTransactionId($transactionId);
960961
$message = $this->_appendTransactionToMessage(
961962
$transactionId,
962963
$this->_prependMessage(__('Denied the payment online'))
963964
);
964965
$this->cancelInvoiceAndRegisterCancellation($invoice, $message);
965966
} else {
967+
$txt = $isOnline ?
968+
'There is no need to deny this payment.' : 'Registered notification about denied payment.';
966969
$message = $this->_appendTransactionToMessage(
967970
$transactionId,
968-
$this->_prependMessage(__('There is no need to deny this payment.'))
971+
$this->_prependMessage(__($txt))
969972
);
970973
$this->setOrderStatePaymentReview($message, $transactionId);
971974
}
@@ -975,17 +978,21 @@ public function deny()
975978
/**
976979
* Performs registered payment update.
977980
*
978-
* @throws \Magento\Framework\Exception\LocalizedException
981+
* @param bool $isOnline
979982
* @return $this
983+
* @throws \Magento\Framework\Exception\LocalizedException
980984
*/
981-
public function update()
985+
public function update($isOnline = true)
982986
{
983-
$transactionId = $this->getLastTransId();
987+
$transactionId = $isOnline ? $this->getLastTransId() : $this->getTransactionId();
984988
$invoice = $this->_getInvoiceForTransactionId($transactionId);
985989

986-
$method = $this->getMethodInstance();
987-
$method->setStore($this->getOrder()->getStoreId());
988-
$method->fetchTransactionInfo($this, $transactionId);
990+
991+
if ($isOnline) {
992+
$method = $this->getMethodInstance();
993+
$method->setStore($this->getOrder()->getStoreId());
994+
$method->fetchTransactionInfo($this, $transactionId);
995+
}
989996

990997
if ($this->getIsTransactionApproved()) {
991998
$message = $this->_appendTransactionToMessage(
@@ -1459,8 +1466,8 @@ protected function _prependMessage($messagePrependTo)
14591466
if (is_string($preparedMessage)) {
14601467
return $preparedMessage . ' ' . $messagePrependTo;
14611468
} elseif (is_object(
1462-
$preparedMessage
1463-
) && $preparedMessage instanceof \Magento\Sales\Model\Order\Status\History
1469+
$preparedMessage
1470+
) && $preparedMessage instanceof \Magento\Sales\Model\Order\Status\History
14641471
) {
14651472
$comment = $preparedMessage->getComment() . ' ' . $messagePrependTo;
14661473
$preparedMessage->setComment($comment);
@@ -1684,8 +1691,8 @@ protected function _getInvoiceForTransactionId($transactionId)
16841691
}
16851692
foreach ($this->getOrder()->getInvoiceCollection() as $invoice) {
16861693
if ($invoice->getState() == \Magento\Sales\Model\Order\Invoice::STATE_OPEN && $invoice->load(
1687-
$invoice->getId()
1688-
)
1694+
$invoice->getId()
1695+
)
16891696
) {
16901697
$invoice->setTransactionId($transactionId);
16911698
return $invoice;

app/code/Magento/Sales/Test/Unit/Controller/Adminhtml/Order/ReviewPaymentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function setUp()
7777

7878
$this->paymentMock = $this->getMock(
7979
'Magento\Sales\Model\Order\Payment',
80-
['registerPaymentReviewAction', 'update'],
80+
['update'],
8181
[],
8282
'',
8383
false

app/code/Magento/Sales/Test/Unit/Model/Order/PaymentTest.php

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,21 @@ function ($value) {
182182

183183
$this->transactionFactory = $this->getMock(
184184
'Magento\Sales\Model\Order\Payment\TransactionFactory',
185-
['create'],
185+
[],
186186
[],
187187
'',
188188
false
189189
);
190190
$this->transactionCollectionFactory = $this->getMock(
191191
'Magento\Sales\Model\Resource\Order\Payment\Transaction\CollectionFactory',
192-
['create'],
192+
[],
193193
[],
194194
'',
195195
false
196196
);
197197
$this->serviceOrderFactory = $this->getMock(
198198
'Magento\Sales\Model\Service\OrderFactory',
199-
['create'],
199+
[],
200200
[],
201201
'',
202202
false
@@ -585,6 +585,26 @@ public function testDenyPaymentFalse()
585585
$this->payment->deny();
586586
}
587587

588+
/**
589+
* Test offline IPN calls
590+
*/
591+
public function testDenyPaymentIpn()
592+
{
593+
$isOnline = false;
594+
$message = sprintf('Denied the payment online Transaction ID: "%s"', $this->transactionId);
595+
596+
$this->payment->setTransactionId($this->transactionId);
597+
$this->payment->setNotificationResult(true);
598+
599+
$this->mockInvoice($this->transactionId);
600+
$this->mockResultFalseMethods($message);
601+
602+
$this->helperMock->expects($this->never())
603+
->method('getMethodInstance');
604+
605+
$this->payment->deny($isOnline);
606+
}
607+
588608
/**
589609
* @dataProvider acceptPaymentFalseProvider
590610
* @param bool $isFraudDetected
@@ -657,6 +677,44 @@ public function testDenyPaymentNegativeStateReview()
657677
$this->payment->deny();
658678
}
659679

680+
/**
681+
* Test offline IPN call, negative
682+
*/
683+
public function testDenyPaymentIpnNegativeStateReview()
684+
{
685+
$isOnline = false;
686+
$message = sprintf('Registered notification about denied payment. Transaction ID: "%s"', $this->transactionId);
687+
688+
$orderState = Order::STATE_PAYMENT_REVIEW;
689+
690+
$this->payment->setTransactionId($this->transactionId);
691+
$this->payment->setNotificationResult(false);
692+
693+
$this->orderMock->expects($this->once())
694+
->method('getState')
695+
->willReturn($orderState);
696+
697+
$this->orderMock->expects($this->never())
698+
->method('setState');
699+
$this->orderMock->expects($this->once())
700+
->method('addStatusHistoryComment')
701+
->with($message);
702+
703+
$this->helperMock->expects($this->never())
704+
->method('getMethodInstance')
705+
->will($this->returnValue($this->paymentMethodMock));
706+
707+
$this->paymentMethodMock->expects($this->never())
708+
->method('setStore')
709+
->will($this->returnSelf());
710+
711+
$this->paymentMethodMock->expects($this->never())
712+
->method('denyPayment')
713+
->with($this->payment);
714+
715+
$this->payment->deny($isOnline);
716+
}
717+
660718
/**
661719
* @param int $transactionId
662720
* @param int $countCall
@@ -709,6 +767,41 @@ public function testUpdateOnlineTransactionApproved()
709767
$this->assertEquals($baseGrandTotal, $this->payment->getBaseAmountPaidOnline());
710768
}
711769

770+
/**
771+
* Test update calls from IPN controller
772+
*/
773+
public function testUpdateOnlineTransactionApprovedIpn()
774+
{
775+
$isOnline = false;
776+
$message = sprintf('Registered update about approved payment. Transaction ID: "%s"', $this->transactionId);
777+
778+
$storeId = 50;
779+
$baseGrandTotal = 299.99;
780+
781+
$this->payment->setTransactionId($this->transactionId);
782+
$this->payment->setData('is_transaction_approved', true);
783+
784+
$this->mockInvoice($this->transactionId);
785+
$this->mockResultTrueMethods($this->transactionId, $baseGrandTotal, $message);
786+
787+
$this->orderMock->expects($this->never())
788+
->method('getStoreId')
789+
->willReturn($storeId);
790+
$this->helperMock->expects($this->never())
791+
->method('getMethodInstance')
792+
->will($this->returnValue($this->paymentMethodMock));
793+
$this->paymentMethodMock->expects($this->never())
794+
->method('setStore')
795+
->with($storeId)
796+
->willReturn($this->paymentMethodMock);
797+
$this->paymentMethodMock->expects($this->never())
798+
->method('fetchTransactionInfo')
799+
->with($this->payment, $this->transactionId);
800+
801+
$this->payment->update($isOnline);
802+
$this->assertEquals($baseGrandTotal, $this->payment->getBaseAmountPaidOnline());
803+
}
804+
712805
public function testUpdateOnlineTransactionDenied()
713806
{
714807
$message = sprintf('Registered update about denied payment. Transaction ID: "%s"', $this->transactionId);

dev/tests/static/testsuite/Magento/Test/Legacy/_files/obsolete_methods.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,7 @@
16791679
['getVisibleOnFrontStates', 'Magento\Sales\Model\Order\Config', 'getVisibleOnFrontStatuses'],
16801680
['getInvisibleOnFrontStates', 'Magento\Sales\Model\Order\Config', 'getInvisibleOnFrontStatuses'],
16811681
['_authorize', 'Magento\Sales\Model\Order\Payment'],
1682+
['registerPaymentReviewAction', 'Magento\Sales\Model\Order\Payment'],
16821683
['_shouldBeConverted', 'Magento\Sales\Model\Resource\AbstractResource'],
16831684
['_beforeSave', 'Magento\Sales\Model\Resource\AbstractResource'],
16841685
['_afterSave', 'Magento\Sales\Model\Resource\AbstractResource'],

0 commit comments

Comments
 (0)