Skip to content

Commit

Permalink
MAGETWO-90761: [2.3.0] PayPal orders status display always as Processing
Browse files Browse the repository at this point in the history
  • Loading branch information
StasKozar committed Jun 7, 2018
1 parent d3a849c commit f3234e1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 108 deletions.
161 changes: 65 additions & 96 deletions app/code/Magento/Paypal/Model/Ipn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
namespace Magento\Paypal\Model;

use Exception;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\Order\Email\Sender\CreditmemoSender;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Magento\Paypal\Model\Info;

/**
* PayPal Instant Payment Notification processor model
Expand Down Expand Up @@ -164,11 +164,11 @@ protected function _processOrder()
case Info::TXN_TYPE_NEW_CASE:
$this->_registerDispute();
break;
// handle new adjustment is created
// handle new adjustment is created
case Info::TXN_TYPE_ADJUSTMENT:
$this->_registerAdjustment();
break;
//handle new transaction created
//handle new transaction created
default:
$this->_registerTransaction();
break;
Expand Down Expand Up @@ -239,16 +239,16 @@ protected function _registerTransaction()
case Info::PAYMENTSTATUS_COMPLETED:
$this->_registerPaymentCapture(true);
break;
// the holded payment was denied on paypal side
// the holded payment was denied on paypal side
case Info::PAYMENTSTATUS_DENIED:
$this->_registerPaymentDenial();
break;
// customer attempted to pay via bank account, but failed
// customer attempted to pay via bank account, but failed
case Info::PAYMENTSTATUS_FAILED:
// cancel order
$this->_registerPaymentFailure();
break;
// payment was obtained, but money were not captured yet
// payment was obtained, but money were not captured yet
case Info::PAYMENTSTATUS_PENDING:
$this->_registerPaymentPending();
break;
Expand All @@ -263,7 +263,7 @@ protected function _registerTransaction()
case Info::PAYMENTSTATUS_REFUNDED:
$this->_registerPaymentRefund();
break;
// authorization expire/void
// authorization expire/void
case Info::PAYMENTSTATUS_EXPIRED:
// break is intentionally omitted
case Info::PAYMENTSTATUS_VOIDED:
Expand All @@ -288,24 +288,12 @@ protected function _registerPaymentCapture($skipFraudDetection = false)
$parentTransactionId = $this->getRequestData('parent_txn_id');
$this->_importPaymentInformation();
$payment = $this->_order->getPayment();
$payment->setTransactionId(
$this->getRequestData('txn_id')
);
$payment->setCurrencyCode(
$this->getRequestData('mc_currency')
);
$payment->setPreparedMessage(
$this->_createIpnComment('')
);
$payment->setParentTransactionId(
$parentTransactionId
);
$payment->setShouldCloseParentTransaction(
'Completed' === $this->getRequestData('auth_status')
);
$payment->setIsTransactionClosed(
0
);
$payment->setTransactionId($this->getRequestData('txn_id'));
$payment->setCurrencyCode($this->getRequestData('mc_currency'));
$payment->setPreparedMessage($this->_createIpnComment(''));
$payment->setParentTransactionId($parentTransactionId);
$payment->setShouldCloseParentTransaction('Completed' === $this->getRequestData('auth_status'));
$payment->setIsTransactionClosed(0);
$payment->registerCaptureNotification(
$this->getRequestData('mc_gross'),
$skipFraudDetection && $parentTransactionId
Expand All @@ -318,9 +306,9 @@ protected function _registerPaymentCapture($skipFraudDetection = false)
$this->orderSender->send($this->_order);
$this->_order->addStatusHistoryComment(
__('You notified customer about invoice #%1.', $invoice->getIncrementId())
)->setIsCustomerNotified(
true
)->save();
)
->setIsCustomerNotified(true)
->save();
}
}

Expand All @@ -334,15 +322,13 @@ protected function _registerPaymentDenial()
{
try {
$this->_importPaymentInformation();
$this->_order->getPayment()->setTransactionId(
$this->getRequestData('txn_id')
)->setNotificationResult(
true
)->setIsTransactionClosed(
true
)->deny(false);
$this->_order->getPayment()
->setTransactionId($this->getRequestData('txn_id'))
->setNotificationResult(true)
->setIsTransactionClosed(true)
->deny(false);
$this->_order->save();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
} catch (LocalizedException $e) {
if ($e->getMessage() != __('We cannot cancel this order.')) {
throw $e;
}
Expand Down Expand Up @@ -386,13 +372,11 @@ public function _registerPaymentPending()

$this->_importPaymentInformation();

$this->_order->getPayment()->setPreparedMessage(
$this->_createIpnComment($this->_paypalInfo->explainPendingReason($reason))
)->setTransactionId(
$this->getRequestData('txn_id')
)->setIsTransactionClosed(
0
)->update(false);
$this->_order->getPayment()
->setPreparedMessage($this->_createIpnComment($this->_paypalInfo->explainPendingReason($reason)))
->setTransactionId($this->getRequestData('txn_id'))
->setIsTransactionClosed(0)
->update(false);
$this->_order->save();
}

Expand All @@ -409,19 +393,12 @@ protected function _registerPaymentAuthorization()
$payment->update(true);
} else {
$this->_importPaymentInformation();
$payment->setPreparedMessage(
$this->_createIpnComment('')
)->setTransactionId(
$this->getRequestData('txn_id')
)->setParentTransactionId(
$this->getRequestData('parent_txn_id')
)->setCurrencyCode(
$this->getRequestData('mc_currency')
)->setIsTransactionClosed(
0
)->registerAuthorizationNotification(
$this->getRequestData('mc_gross')
);
$payment->setPreparedMessage($this->_createIpnComment(''))
->setTransactionId($this->getRequestData('txn_id'))
->setParentTransactionId($this->getRequestData('parent_txn_id'))
->setCurrencyCode($this->getRequestData('mc_currency'))
->setIsTransactionClosed(0)
->registerAuthorizationNotification($this->getRequestData('mc_gross'));
}
if (!$this->_order->getEmailSent()) {
$this->orderSender->send($this->_order);
Expand Down Expand Up @@ -449,12 +426,13 @@ protected function _registerPaymentReversal()
{
$reasonCode = $this->getRequestData('reason_code');
$reasonComment = $this->_paypalInfo->explainReasonCode($reasonCode);
$notificationAmount = $this->_order->getBaseCurrency()->formatTxt(
$this->getRequestData('mc_gross') + $this->getRequestData('mc_fee')
);
$notificationAmount = $this->_order->getBaseCurrency()
->formatTxt(
$this->getRequestData('mc_gross') + $this->getRequestData('mc_fee')
);
$paymentStatus = $this->_filterPaymentStatus($this->getRequestData('payment_status'));
$orderStatus = $paymentStatus ==
Info::PAYMENTSTATUS_REVERSED ? Info::ORDER_STATUS_REVERSED : Info::ORDER_STATUS_CANCELED_REVERSAL;
Info::PAYMENTSTATUS_REVERSED ? Info::ORDER_STATUS_REVERSED : Info::ORDER_STATUS_CANCELED_REVERSAL;
//Change order status to PayPal Reversed/PayPal Cancelled Reversal if it is possible.
$message = __(
'IPN "%1". %2 Transaction amount %3. Transaction ID: "%4"',
Expand All @@ -464,8 +442,9 @@ protected function _registerPaymentReversal()
$this->getRequestData('txn_id')
);
$this->_order->setStatus($orderStatus);
$this->_order->save();
$this->_order->addStatusHistoryComment($message, $orderStatus)->setIsCustomerNotified(false)->save();
$this->_order->addStatusHistoryComment($message, $orderStatus)
->setIsCustomerNotified(false)
->save();
}

/**
Expand All @@ -478,17 +457,12 @@ 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')
);
$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
Expand All @@ -498,9 +472,9 @@ protected function _registerPaymentRefund()
$this->creditmemoSender->send($creditMemo);
$this->_order->addStatusHistoryComment(
__('You notified customer about creditmemo #%1.', $creditMemo->getIncrementId())
)->setIsCustomerNotified(
true
)->save();
)
->setIsCustomerNotified(true)
->save();
}
}

Expand All @@ -513,19 +487,14 @@ protected function _registerPaymentVoid()
{
$this->_importPaymentInformation();

$parentTxnId = $this->getRequestData(
'transaction_entity'
) == 'auth' ? $this->getRequestData(
'txn_id'
) : $this->getRequestData(
'parent_txn_id'
);
$parentTxnId = $this->getRequestData('transaction_entity') == 'auth'
? $this->getRequestData('txn_id')
: $this->getRequestData('parent_txn_id');

$this->_order->getPayment()->setPreparedMessage(
$this->_createIpnComment('')
)->setParentTransactionId(
$parentTxnId
)->registerVoidNotification();
$this->_order->getPayment()
->setPreparedMessage($this->_createIpnComment(''))
->setParentTransactionId($parentTxnId)
->registerVoidNotification();

$this->_order->save();
}
Expand All @@ -546,14 +515,14 @@ protected function _importPaymentInformation()
// collect basic information
$from = [];
foreach ([
Info::PAYER_ID,
'payer_email' => Info::PAYER_EMAIL,
Info::PAYER_STATUS,
Info::ADDRESS_STATUS,
Info::PROTECTION_EL,
Info::PAYMENT_STATUS,
Info::PENDING_REASON,
] as $privateKey => $publicKey) {
Info::PAYER_ID,
'payer_email' => Info::PAYER_EMAIL,
Info::PAYER_STATUS,
Info::ADDRESS_STATUS,
Info::PROTECTION_EL,
Info::PAYMENT_STATUS,
Info::PENDING_REASON,
] as $privateKey => $publicKey) {
if (is_int($privateKey)) {
$privateKey = $publicKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(StatusResolver $statusResolver = null)
*/
public function execute(OrderPaymentInterface $payment, $amount, OrderInterface $order)
{
$state = Order::STATE_PROCESSING;
$state = $order->getState() ?: Order::STATE_PROCESSING;
$status = null;
$message = 'Registered notification about captured amount of %1.';

Expand Down
Loading

0 comments on commit f3234e1

Please sign in to comment.