Skip to content

Commit 1938046

Browse files
author
Viktor Tymchynskyi
committed
MAGETWO-37717: IPN messages doesn't show relevant info about transaction
1 parent fca00a7 commit 1938046

File tree

2 files changed

+120
-18
lines changed

2 files changed

+120
-18
lines changed

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -939,25 +939,31 @@ public function accept()
939939
/**
940940
* Accept order with payment method instance
941941
*
942+
* @param bool $isOnline
942943
* @return $this
944+
* @throws \Magento\Framework\Exception\LocalizedException
943945
*/
944-
public function deny()
946+
public function deny($isOnline = true)
945947
{
946-
$transactionId = $this->getLastTransId();
948+
$transactionId = $isOnline ? $this->getLastTransId() : $this->getTransactionId();
947949

948-
/** @var \Magento\Payment\Model\Method\AbstractMethod $method */
949-
$method = $this->getMethodInstance()->setStore($this->getOrder()->getStoreId());
950-
if ($method->denyPayment($this)) {
950+
$result = $isOnline ?
951+
$this->getMethodInstance()->setStore($this->getOrder()->getStoreId())->denyPayment($this) :
952+
(bool)$this->getNotificationResult();
953+
954+
if ($result) {
951955
$invoice = $this->_getInvoiceForTransactionId($transactionId);
952956
$message = $this->_appendTransactionToMessage(
953957
$transactionId,
954958
$this->_prependMessage(__('Denied the payment online'))
955959
);
956960
$this->cancelInvoiceAndRegisterCancellation($invoice, $message);
957961
} else {
962+
$txt = $isOnline ?
963+
'There is no need to deny this payment.' : 'Registered notification about denied payment.';
958964
$message = $this->_appendTransactionToMessage(
959965
$transactionId,
960-
$this->_prependMessage(__('There is no need to deny this payment.'))
966+
$this->_prependMessage(__($txt))
961967
);
962968
$this->setOrderStatePaymentReview($message, $transactionId);
963969
}
@@ -967,16 +973,19 @@ public function deny()
967973
/**
968974
* Performs registered payment update.
969975
*
970-
* @throws \Magento\Framework\Exception\LocalizedException
976+
* @param bool $isOnline
971977
* @return $this
978+
* @throws \Magento\Framework\Exception\LocalizedException
972979
*/
973-
public function update()
980+
public function update($isOnline = true)
974981
{
975-
$transactionId = $this->getLastTransId();
982+
$transactionId = $isOnline ? $this->getLastTransId() : $this->getTransactionId();
976983
$invoice = $this->_getInvoiceForTransactionId($transactionId);
977984

978-
$this->getMethodInstance()->setStore($this->getOrder()->getStoreId())
979-
->fetchTransactionInfo($this, $transactionId);
985+
if ($isOnline) {
986+
$this->getMethodInstance()->setStore($this->getOrder()->getStoreId())
987+
->fetchTransactionInfo($this, $transactionId);
988+
}
980989

981990
if ($this->getIsTransactionApproved()) {
982991
$message = $this->_appendTransactionToMessage(
@@ -1441,8 +1450,8 @@ protected function _prependMessage($messagePrependTo)
14411450
if (is_string($preparedMessage)) {
14421451
return $preparedMessage . ' ' . $messagePrependTo;
14431452
} elseif (is_object(
1444-
$preparedMessage
1445-
) && $preparedMessage instanceof \Magento\Sales\Model\Order\Status\History
1453+
$preparedMessage
1454+
) && $preparedMessage instanceof \Magento\Sales\Model\Order\Status\History
14461455
) {
14471456
$comment = $preparedMessage->getComment() . ' ' . $messagePrependTo;
14481457
$preparedMessage->setComment($comment);
@@ -1666,8 +1675,8 @@ protected function _getInvoiceForTransactionId($transactionId)
16661675
}
16671676
foreach ($this->getOrder()->getInvoiceCollection() as $invoice) {
16681677
if ($invoice->getState() == \Magento\Sales\Model\Order\Invoice::STATE_OPEN && $invoice->load(
1669-
$invoice->getId()
1670-
)
1678+
$invoice->getId()
1679+
)
16711680
) {
16721681
$invoice->setTransactionId($transactionId);
16731682
return $invoice;

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
@@ -586,6 +586,26 @@ public function testDenyPaymentFalse()
586586
$this->payment->deny();
587587
}
588588

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

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

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

0 commit comments

Comments
 (0)