Skip to content

Commit

Permalink
Merge pull request #504 from magento-firedrakes/MAGETWO-39265
Browse files Browse the repository at this point in the history
[Firedrakes] Code Improvements in Sales module MAGETWO-39265
  • Loading branch information
Logvin, Michael(mlogvin) committed Aug 6, 2015
2 parents 12f560b + 467603a commit 04c2e63
Show file tree
Hide file tree
Showing 159 changed files with 7,757 additions and 2,931 deletions.
20 changes: 18 additions & 2 deletions app/code/Magento/Authorizenet/Model/Directpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class Directpost extends \Magento\Authorizenet\Model\Authorizenet implements Tra
*/
protected $orderFactory;

/**
* @var \Magento\Sales\Api\TransactionRepositoryInterface
*/
protected $transactionRepository;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -129,6 +134,7 @@ class Directpost extends \Magento\Authorizenet\Model\Authorizenet implements Tra
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Quote\Model\QuoteRepository $quoteRepository
* @param \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender
* @param \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
Expand All @@ -151,6 +157,7 @@ public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Quote\Model\QuoteRepository $quoteRepository,
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender,
\Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository,
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
Expand All @@ -160,6 +167,7 @@ public function __construct(
$this->quoteRepository = $quoteRepository;
$this->response = $responseFactory->create();
$this->orderSender = $orderSender;
$this->transactionRepository = $transactionRepository;
$this->_code = static::METHOD_CODE;

parent::__construct(
Expand Down Expand Up @@ -795,7 +803,11 @@ protected function declineOrder(\Magento\Sales\Model\Order $order, $message = ''
*/
protected function getRealParentTransactionId($payment)
{
$transaction = $payment->getTransaction($payment->getParentTransactionId());
$transaction = $this->transactionRepository->getByTransactionId(
$payment->getParentTransactionId(),
$payment->getId(),
$payment->getOrder()->getId()
);
return $transaction->getAdditionalInformation(self::REAL_TRANSACTION_ID_KEY);
}

Expand Down Expand Up @@ -851,7 +863,11 @@ public function canCapture()
*/
public function fetchTransactionInfo(\Magento\Payment\Model\InfoInterface $payment, $transactionId)
{
$transaction = $payment->getTransaction($transactionId);
$transaction = $this->transactionRepository->getByTransactionId(
$transactionId,
$payment->getId(),
$payment->getOrder()->getId()
);

$response = $this->getTransactionResponse($transactionId);
if ($response->getXResponseCode() == self::RESPONSE_CODE_APPROVED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ public function testAuthorizeSuccess(
$this->assertEquals($this->model, $this->model->authorize($paymentObject, $amount));
foreach ($expectedPaymentFields as $key => $value) {
if ($key == 'getTransactionAdditionalInfo') {
$this->assertEquals($value, $paymentObject->getTransactionAdditionalInfo('token'));
$this->assertEquals($value, $paymentObject->getTransactionAdditionalInfo());
} else {
$this->assertEquals($value, $paymentObject->getData($key), 'Incorrect field in paymentobject: ' . $key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ public function testAuthorizeSuccess(
$this->assertEquals($this->model, $this->model->authorize($paymentObject, $amount));
foreach ($expectedPaymentFields as $key => $value) {
if ($key == 'getTransactionAdditionalInfo') {
$this->assertEquals($value, $paymentObject->getTransactionAdditionalInfo('token'));
$this->assertEquals($value, $paymentObject->getTransactionAdditionalInfo());
} else {
$this->assertEquals($value, $paymentObject->getData($key), 'Incorrect field in paymentobject: ' . $key);
}
Expand Down Expand Up @@ -1385,7 +1385,7 @@ public function authorizeDataProvider()
'processorResponseCode' => '1000',
'processorResponseText' => 'Approved',
],
'getTransactionAdditionalInfo' => self::CC_TOKEN
'getTransactionAdditionalInfo' => ['token' => self::CC_TOKEN]
],
],
'token_with_3dsecure' => [
Expand Down Expand Up @@ -1458,7 +1458,7 @@ public function authorizeDataProvider()
'processorResponseCode' => '1000',
'processorResponseText' => 'Approved',
],
'getTransactionAdditionalInfo' => self::CC_TOKEN
'getTransactionAdditionalInfo' => ['token' => self::CC_TOKEN]
],
],
'token_with_3dsecure_backend' => [
Expand Down Expand Up @@ -1526,7 +1526,7 @@ public function authorizeDataProvider()
'processorResponseCode' => '1000',
'processorResponseText' => 'Approved',
],
'getTransactionAdditionalInfo' => self::CC_TOKEN
'getTransactionAdditionalInfo' => ['token' => self::CC_TOKEN]
],
'appState' => 'adminhtml',
],
Expand Down
60 changes: 53 additions & 7 deletions app/code/Magento/Paypal/Model/Express.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Paypal\Model\Api\Nvp;
use Magento\Paypal\Model\Api\ProcessableException as ApiProcessableException;
use Magento\Paypal\Model\Express\Checkout as ExpressCheckout;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Model\Order\Payment;
use Magento\Sales\Model\Order\Payment\Transaction;
use Magento\Quote\Model\Quote;
Expand Down Expand Up @@ -166,6 +167,16 @@ class Express extends \Magento\Payment\Model\Method\AbstractMethod
*/
protected $_exception;

/**
* @var \Magento\Sales\Api\TransactionRepositoryInterface
*/
protected $transactionRepository;

/**
* @var Transaction\BuilderInterface
*/
protected $transactionBuilder;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -180,6 +191,8 @@ class Express extends \Magento\Payment\Model\Method\AbstractMethod
* @param CartFactory $cartFactory
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\Exception\LocalizedExceptionFactory $exception
* @param \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository
* @param Transaction\BuilderInterface $transactionBuilder
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
Expand All @@ -199,6 +212,8 @@ public function __construct(
\Magento\Paypal\Model\CartFactory $cartFactory,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\Exception\LocalizedExceptionFactory $exception,
\Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository,
\Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $transactionBuilder,
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
Expand All @@ -220,6 +235,8 @@ public function __construct(
$this->_cartFactory = $cartFactory;
$this->_checkoutSession = $checkoutSession;
$this->_exception = $exception;
$this->transactionRepository = $transactionRepository;
$this->transactionBuilder = $transactionBuilder;

$proInstance = array_shift($data);
if ($proInstance && $proInstance instanceof \Magento\Paypal\Model\Pro) {
Expand Down Expand Up @@ -380,7 +397,11 @@ public function order(\Magento\Payment\Model\InfoInterface $payment, $amount)
$message = __('Ordered amount of %1', $formattedPrice);
}

$payment->addTransaction(Transaction::TYPE_ORDER, null, false, $message);
$transaction = $this->transactionBuilder->setPayment($payment)
->setOrder($order)
->setTransactionId($payment->getTransactionId())
->build(Transaction::TYPE_ORDER);
$payment->addTransactionCommentsToOrder($transaction, $message);

$this->_pro->importPaymentInfo($api, $payment);

Expand All @@ -402,7 +423,11 @@ public function order(\Magento\Payment\Model\InfoInterface $payment, $amount)
$payment->setTransactionId($api->getTransactionId());
$payment->setParentTransactionId($orderTransactionId);

$payment->addTransaction(Transaction::TYPE_AUTH, null, false, $message);
$transaction = $this->transactionBuilder->setPayment($payment)
->setOrder($order)
->setTransactionId($payment->getTransactionId())
->build(Transaction::TYPE_AUTH);
$payment->addTransactionCommentsToOrder($transaction, $message);

$order->setState($state)
->setStatus($status);
Expand Down Expand Up @@ -437,7 +462,7 @@ public function void(\Magento\Payment\Model\InfoInterface $payment)
$this->_isOrderPaymentActionKey
) && !$payment->getVoidOnlyAuthorization()
) {
$orderTransaction = $payment->lookupTransaction(false, Transaction::TYPE_ORDER);
$orderTransaction = $this->getOrderTransaction($payment);
if ($orderTransaction) {
$payment->setParentTransactionId($orderTransaction->getTxnId());
$payment->setTransactionId($orderTransaction->getTxnId() . '-void');
Expand Down Expand Up @@ -511,14 +536,19 @@ public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
$message = __('The authorized amount is %1.', $formatedPrice);
}

$transaction = $payment->addTransaction(Transaction::TYPE_AUTH, null, true, $message);
$transaction = $this->transactionBuilder->setPayment($payment)
->setOrder($order)
->setTransactionId($payment->getTransactionId())
->setFailSafe(true)
->build(Transaction::TYPE_AUTH);
$payment->addTransactionCommentsToOrder($transaction, $message);

$payment->setParentTransactionId($api->getTransactionId());
$isAuthorizationCreated = true;
}
//close order transaction if needed
if ($payment->getShouldCloseParentTransaction()) {
$orderTransaction = $payment->lookupTransaction(false, Transaction::TYPE_ORDER);
$orderTransaction = $this->getOrderTransaction($payment);

if ($orderTransaction) {
$orderTransaction->setIsClosed(true);
Expand Down Expand Up @@ -737,7 +767,7 @@ public function canVoid()
{
$info = $this->getInfoInstance();
if ($info->getAdditionalInformation($this->_isOrderPaymentActionKey)) {
$orderTransaction = $info->lookupTransaction(false, Transaction::TYPE_ORDER);
$orderTransaction = $this->getOrderTransaction($info);
if ($orderTransaction) {
$info->setParentTransactionId($orderTransaction->getTxnId());
}
Expand All @@ -757,7 +787,7 @@ public function canCapture()
$this->_pro->getConfig()->setStoreId($payment->getOrder()->getStore()->getId());

if ($payment->getAdditionalInformation($this->_isOrderPaymentActionKey)) {
$orderTransaction = $payment->lookupTransaction(false, Transaction::TYPE_ORDER);
$orderTransaction = $this->getOrderTransaction($payment);
if ($orderTransaction->getIsClosed()) {
return false;
}
Expand All @@ -772,6 +802,7 @@ public function canCapture()
return false;
}
}

return $this->_canCapture;
}

Expand Down Expand Up @@ -853,4 +884,19 @@ public function isActive($storeId = null)
|| (bool)(int)$this->_scopeConfig->getValue($pathStandardExpress, ScopeInterface::SCOPE_STORE, $storeId)
|| (bool)(int)$this->_scopeConfig->getValue($pathPaypalExpress, ScopeInterface::SCOPE_STORE, $storeId);
}

/**
* Get transaction with type order
*
* @param OrderPaymentInterface $payment
* @return false|\Magento\Sales\Api\Data\TransactionInterface
*/
protected function getOrderTransaction($payment)
{
return $this->transactionRepository->getByTransactionType(
Transaction::TYPE_ORDER,
$payment->getId(),
$payment->getOrder()->getId()
);
}
}
6 changes: 4 additions & 2 deletions app/code/Magento/Paypal/Model/Payflow/Pro.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ protected function _isCaptureNeeded()
protected function _getParentTransactionId(\Magento\Framework\DataObject $payment)
{
if ($payment->getParentTransactionId()) {
return $payment->getTransaction(
$payment->getParentTransactionId()
return $this->transactionRepository->getByTransactionId(
$payment->getParentTransactionId(),
$payment->getId(),
$payment->getOrder()->getId()
)->getAdditionalInformation(
self::TRANSPORT_PAYFLOW_TXN_ID
);
Expand Down
12 changes: 11 additions & 1 deletion app/code/Magento/Paypal/Model/PayflowExpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class PayflowExpress extends \Magento\Paypal\Model\Express
* @param CartFactory $cartFactory
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\Exception\LocalizedExceptionFactory $exception
* @param \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository
* @param Transaction\BuilderInterface $transactionBuilder
* @param InfoFactory $paypalInfoFactory
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
Expand All @@ -83,6 +85,8 @@ public function __construct(
CartFactory $cartFactory,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\Exception\LocalizedExceptionFactory $exception,
\Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository,
\Magento\Sales\Model\Order\Payment\Transaction\BuilderInterface $transactionBuilder,
InfoFactory $paypalInfoFactory,
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
Expand All @@ -102,6 +106,8 @@ public function __construct(
$cartFactory,
$checkoutSession,
$exception,
$transactionRepository,
$transactionBuilder,
$resource,
$resourceCollection,
$data
Expand Down Expand Up @@ -181,7 +187,11 @@ public function canRefund()
/** @var \Magento\Sales\Model\Order\Payment $payment */
$payment = $this->getInfoInstance();
// we need the last capture transaction was made
$captureTransaction = $payment->lookupTransaction('', Transaction::TYPE_CAPTURE);
$captureTransaction = $this->transactionRepository->getByTransactionType(
Transaction::TYPE_CAPTURE,
$payment->getId(),
$payment->getOrder()->getId()
);
return $captureTransaction && $captureTransaction->getAdditionalInformation(
Payflow\Pro::TRANSPORT_PAYFLOW_TXN_ID
) && $this->_canRefund;
Expand Down
11 changes: 10 additions & 1 deletion app/code/Magento/Paypal/Model/Pro.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Magento\Paypal\Model;

use Magento\Paypal\Model\Api\AbstractApi;
use Magento\Sales\Api\TransactionRepositoryInterface;

/**
* PayPal Website Payments Pro implementation for payment method instances
Expand Down Expand Up @@ -73,19 +74,27 @@ class Pro
*/
protected $_infoFactory;

/**
* @var TransactionRepositoryInterface
*/
protected $transactionRepository;

/**
* @param \Magento\Paypal\Model\Config\Factory $configFactory
* @param \Magento\Paypal\Model\Api\Type\Factory $apiFactory
* @param \Magento\Paypal\Model\InfoFactory $infoFactory
* @param TransactionRepositoryInterface $transactionRepository
*/
public function __construct(
\Magento\Paypal\Model\Config\Factory $configFactory,
\Magento\Paypal\Model\Api\Type\Factory $apiFactory,
\Magento\Paypal\Model\InfoFactory $infoFactory
\Magento\Paypal\Model\InfoFactory $infoFactory,
TransactionRepositoryInterface $transactionRepository
) {
$this->_configFactory = $configFactory;
$this->_apiFactory = $apiFactory;
$this->_infoFactory = $infoFactory;
$this->transactionRepository = $transactionRepository;
}

/**
Expand Down
Loading

0 comments on commit 04c2e63

Please sign in to comment.