Skip to content

Commit

Permalink
Merge branch 'feat/no-replace-quote'
Browse files Browse the repository at this point in the history
  • Loading branch information
iesus committed Jun 28, 2018
2 parents 06d43c0 + 0458f27 commit 4cf65ec
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 76 deletions.
2 changes: 1 addition & 1 deletion app/code/community/Svea/Checkout/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public function replaceQuote()
$newQuote->merge($oldQuote)
->collectTotals()
->save();
$session->replaceQuote($newQuote);
$oldQuote->setIsActive(0);
$session->setQuoteId($newQuote->getId());

return $newQuote;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public function createSveaOrderFromQuote($quote, $test = false)
{
$sveaOrder = $this->setupCommunication();

if ($test === false) {
/*if ($test === false) {
$quote = $this->checkQuote($sveaOrder, $quote);
}
}*/
$this->_setupOrderConfig($sveaOrder, $quote)
->setLocales($sveaOrder, $quote)
->_presetValues($quote,$sveaOrder)
Expand All @@ -116,7 +116,7 @@ public function createSveaOrderFromQuote($quote, $test = false)
* @return mixed
*/
protected function checkQuote($sveaOrder, $quote) {
$sveaId = (int)$quote->getPaymentReference();
/*$sveaId = (int)$quote->getPaymentReference();
if ($sveaId) {
try {
$orderResponse = $sveaOrder->setCheckoutOrderId($sveaId)->getOrder($sveaId);
Expand All @@ -127,7 +127,7 @@ protected function checkQuote($sveaOrder, $quote) {
if (isset($orderResponse) && $orderResponse['Status'] != 'Created'){
$quote = Mage::helper('sveacheckout')->replaceQuote();
}
}
}*/

return $quote;
}
Expand Down
54 changes: 20 additions & 34 deletions app/code/community/Svea/Checkout/Model/Payment/Acknowledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,26 @@ public function acknowledge($quote, $mode, $sveaId)
$adapter->beginTransaction();

try {
$magentoOrders = Mage::getModel('sales/order')
->getCollection()
->addAttributeToFilter('quote_id', $quote->getId())
->addAttributeToSelect('payment_reference');

$indicationOfError = false;
if ($magentoOrders->count() >= 2) {
$indicationOfError = true;
}

$orderThatMatch = [];
foreach ($magentoOrders as $order) {
if ($order->getPaymentReference() == $sveaId) {
$orderThatMatch[] = $order;
}
}

if (sizeof($orderThatMatch) >= 2) {
foreach ($orderThatMatch as $k => $order) {
if ($order->getStatus() == 'canceled') {
unset($orderThatMatch[$k]);
}
$svea = Mage::getModel('sveacheckout/Checkout_Api_BuildOrder');
$sveaOrder = $svea->setupCommunication();
$sveaData = new Varien_Object( $sveaOrder->setCheckoutOrderId((int)$quote->getPaymentReference())->getOrder() );
$sveaReference = $sveaData->getData('ClientOrderNumber');

$useForReference = Mage::getStoreConfig('payment/sveacheckout_dev_settings/reference');
if (in_array($useForReference , ['suffixed-increment-id','suffixed-order-id'])) {
$reference = $sveaReference;
$separator = '_';
$lastChar = strrpos ($reference , $separator,0);
$reference = substr($reference,0, $lastChar);
if ($useForReference == 'suffixed-increment-id') {
$magentoOrder = Mage::getModel('sales/order')->load($reference, 'increment_id');
} else {
$magentoOrder = Mage::getModel('sales/order')->load($reference, 'entity_id');
}
}

if (sizeof($orderThatMatch) >= 2) {

throw new Mage_Core_Exception("There were multiple orders that matched SveaOrder with ID {$sveaId}.");
} elseif($indicationOfError && sizeof($orderThatMatch)) {
$magentoOrder = $orderThatMatch[0]->load();
}

if(!isset($magentoOrder)) {
$magentoOrder = Mage::getModel('sales/order')->load($quote->getId(), 'quote_id');
} elseif ($useForReference == 'plain-increment-id') {
$magentoOrder = Mage::getModel('sales/order')->load($sveaReference, 'increment_id');
} else {
$magentoOrder = Mage::getModel('sales/order')->load($sveaReference, 'entity_id');
}

if(!isset($magentoOrder)) {
Expand All @@ -90,7 +76,7 @@ public function acknowledge($quote, $mode, $sveaId)
$magentoOrder->setCustomerEmail($sveaData->getData('EmailAddress'));
}

$statusText = Mage::getStoreConfig('payment/SveaCheckout/order_status_after_acknowledge');
$statusText = Mage::getStoreConfig('payment/SveaCheckout/order_status_after_acknowledge');
$message = $helper->__("Order was acknowledged by Svea.");

$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function createOrder($quote, $orderData, $orderQueueItem)

$order->setPaymentReference($quote->getPaymentReference())
->save();
$quote->setIsActive(0)->save();

$type = Mage_Sales_Model_Order_Payment_Transaction::TYPE_ORDER;
$id = $orderData->getData('OrderId');
Mage::helper('sveacheckout/transaction')->createTransaction(
Expand Down
64 changes: 49 additions & 15 deletions app/code/community/Svea/Checkout/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ protected function _getPaymentSettings() {
];
}


/**
* Handles return to payment window with failed payment.
*
* @param $id
*
* @return Mage_Sales_Model_Quote
*/
protected function _restoreQuote($id)
protected function _restoreQuote($id, $cancelOrderReference = null)
{
$svea = Mage::getModel('sveacheckout/Checkout_Api_BuildOrder');
$session = Mage::getSingleton('checkout/session');
Expand All @@ -53,30 +52,40 @@ protected function _restoreQuote($id)
$hasOrder = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('id')
->addAttributeToFilter('quote_id', $id)
->getSize();

if (
!$hasOrder
&& $oldQuote->getIsActive()
&& $oldQuote->hasItems()
&& !$oldQuote->getHasError()
->getSize() > 0;
if (
!$hasOrder &&
$oldQuote->getIsActive() &&
$oldQuote->hasItems() &&
!$oldQuote->getHasError()
) {

return $oldQuote;
} else {
$sveaOrderId = null;
} elseif($hasOrder && $cancelOrderReference) {
$order = Mage::getModel('sales/order')->load($cancelOrderReference, 'increment_id');
if (
'new' == $order->getState()
&& $order->canCancel()
&& ($order->getPayment() && !sizeof($order->getPayment()->getAdditionalInformation()))
) {
$order->cancel()
->save();
Mage::helper('sveacheckout/Debug')->writeToLog(
'cancelled duplicate order id'. $order->getId()
);
}
}

$quote = Mage::getModel('sales/quote')->setStore(Mage::app()->getStore());

$quote->merge($oldQuote)
->setIsActive(1)
->setReservedOrderId(null)
->setPaymentReference($sveaOrderId)
->collectTotals()
->unsLastRealOrderId()
->save();

$session->replaceQuote($quote)
->unsLastRealOrderId();
$sveaOrder = $svea->createSveaOrderFromQuote($quote);

//Create a new order in Sveas end, to reset URIs.
Expand Down Expand Up @@ -132,11 +141,30 @@ public function indexAction()

if (isset($requestParams['reactivate']) && isset($requestParams['quoteId'])) {
$quoteId =(int)$requestParams['quoteId'];
$this->_restoreQuote($quoteId);
$cancelOrderId = null;
$quote = Mage::getModel('sales/quote')->load($quoteId);

$reservedOrderId = $quote->getReservedOrderId();
if ($reservedOrderId && $reservedOrderId !== $quoteId) {
$orderExists = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('increment_id', $reservedOrderId)->getSize();
$cancelOrderReference = $orderExists ? $reservedOrderId : null;
}

$quote = $this->_restoreQuote($quoteId, $cancelOrderReference);


return $this->_redirect('sveacheckout/index');
} else {
$quote = $session->getQuote();
$svea = Mage::getModel('sveacheckout/Checkout_Api_BuildOrder');
if ($quote->getPaymentReference() > 0) {
$sveaOrderId = $quote->getData('payment_reference');
$sveaOrder = $svea->createSveaOrderFromQuote($quote);
$sveaOrder = $sveaOrder->setCheckoutOrderId((int)$sveaOrderId)->getOrder();
$quote->setReservedOrderId(null)->save();
$session->unsLastRealOrderId();
}
}

if (!$quote->hasItems() || $quote->getHasError()) {
Expand Down Expand Up @@ -180,8 +208,9 @@ public function indexAction()
return $this->_redirect('checkout/onepage');
}

$svea = Mage::getModel('sveacheckout/Checkout_Api_BuildOrder');
$svea = Mage::getModel('sveacheckout/Checkout_Api_BuildOrder');
$sveaOrder = $svea->createSveaOrderFromQuote($quote);

try {
$response = $this->_getSveaResponse($sveaOrder);

Expand Down Expand Up @@ -270,6 +299,11 @@ protected function _getSveaResponse($sveaOrder)
$response = $this->_invalidateQuote($sveaOrder, $quote);
}

if ($response['Status'] == 'Cancelled') {

$response = $this->_invalidateQuote($sveaOrder, $quote);
}

if ($svea->sveaOrderHasErrors($sveaOrder, $quote, $response)) {
$message = sprintf(
$this->__('Error code 205-q%d - Could not load Svea Ekonomi checkout%s'),
Expand Down
24 changes: 24 additions & 0 deletions app/code/community/Svea/Checkout/controllers/PushController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ public function indexAction()
if ('final' == strtolower($responseObject->getData('Status'))) {
Mage::getModel('sveacheckout/payment_acknowledge')
->acknowledge($quote, $request->getParam('mode'), $request->getParam('sveaId'));

/**
* Cancel Duplicates.
*/
$orders = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('quote_id', $quoteId);
if (count($orders) > 1) {
foreach ($orders as $order) {
if ($order->getPayment()->getMethod() != 'sveacheckout') {
break;
}

if (
'new' == $order->getState()
&& $order->canCancel()
&& ($order->getPayment() && !sizeof($order->getPayment()->getAdditionalInformation()))
) {
$order->cancel()->save();
Mage::helper('sveacheckout/Debug')->writeToLog(
'cancelled duplicate order id'. $order->getId()
);
}
}
}
}

if ($this->getResponse()->getHttpResponseCode() == 200) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,17 @@ public function indexAction()
->save();

$responseObject = new Varien_Object($response);
if ($orderQueueItem->getState() == $orderQueueItem::SVEA_QUEUE_STATE_OK) {

return $this->reportAndReturn(208, "QueueItem {$quoteId} already handled.", $orderQueueItem->getOrderId());
}

if ($svea->sveaOrderHasErrors($sveaOrder, $quote, $response)) {

Mage::throwException("Quote " . intval($quoteId) . " is not valid");
}

if (!($quote->getPayment()->getMethod())) {
$payment = $quote->getPayment();
$payment->setMethod('sveacheckout');
}

if (
!$orderQueueItem->getData('order_id')
&& $orderQueueItem->getData('state') != $orderQueueItem::SVEA_QUEUE_STATE_NEW
&& $orderQueueItem->getData('state') != $orderQueueItem::SVEA_QUEUE_STATE_OK
) {
$createdOrder = Mage::getModel('sveacheckout/Payment_CreateOrder')
->createOrder($quote, $responseObject, $orderQueueItem);
}

$createdOrder = Mage::getModel('sveacheckout/Payment_CreateOrder')
->createOrder($quote, $responseObject, $orderQueueItem);


if (isset($createdOrder) && true !== $createdOrder) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,47 @@
<?php
$data = $this->getQueueData();
$helper = Mage::helper('sveacheckout/renderer');
foreach ($data['details'] as $key => $detail) :?>
foreach ($data['details'] as $key => $detail) :
$shippingAddress = (isset($billingAddress['IsGeneric']) && $billingAddress['IsGeneric'])
? $shippingAddress['AddressLines']
: $detail['ShippingAddress'];
unset($shippingAddress['AddressLines']);
unset($shippingAddress['IsGeneric']);

$billingAddress = (isset($billingAddress['IsGeneric']) && $billingAddress['IsGeneric'])
? $billingAddress['AddressLines']
: $detail['BillingAddress'];
unset($billingAddress['AddressLines']);
unset($billingAddress['IsGeneric']);
?>
<div class="wrapper half-width">
<h1><?= $key ?></h1>
<div class="details">
<div class="address-details">
<?php if (isset($detail['ShippingAddress'])): ?>
<?php if (isset($shippingAddress)): ?>
<div class="address-card half-width">
<div class="entry-edit">
<div class="entry-edit-head">
<h4 class="icon-head head-billing-address"><?= $this->__('Shipping Address') ?></h4>
</div>
<fieldset>
<address>
<?= implode("<br/>", $detail['ShippingAddress']) ?>
<?= implode("<br/>", $shippingAddress) ?>
</address>
</fieldset>
</div>
</div>
<?php endif; ?>

<?php if (isset($detail['BillingAddress'])): ?>
<?php if (isset($billingAddress)): ?>
<div class="address-card half-width">
<div class="entry-edit">
<div class="entry-edit-head">
<h4 class="icon-head head-billing-address"><?= $this->__('Billing Address') ?></h4>
</div>
<fieldset>
<address>
<?= implode("<br/>", $detail['BillingAddress']) ?>
<?= implode("<br/>", $billingAddress) ?>
</address>
</fieldset>
</div>
Expand Down

0 comments on commit 4cf65ec

Please sign in to comment.