Skip to content

Commit

Permalink
Simplify capture code - fixes #10, add mass capture functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
RBech committed Nov 1, 2018
1 parent 3c93494 commit 1848898
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
5 changes: 3 additions & 2 deletions app/code/community/Quickpay/Payment/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public function capture($payment, $amount)
$this->apiKey = Mage::getStoreConfig('payment/quickpaypayment_payment/apikey', $storeId);

$newCapturedAmount = $capturedAmount + ($amount * 100);

try {
$result = $this->qpCapture($quickPayTransactionId, round($amount * 100));
} catch (Exception $e) {
Expand All @@ -283,8 +284,8 @@ public function capture($payment, $amount)

Mage::log($result, null, 'qp_capture.log');

if ($result->qp_status_code == "20000" && ($result->aq_status_code == "000" || $result->aq_status_code == "20000")) {
$session->addSuccess(Mage::helper('quickpaypayment')->__('Betalingen er hævet online.'));
if ($result->qp_status_code == "20000") {
$session->addSuccess(Mage::helper('quickpaypayment')->__('Betalingen for ordre %s er hævet online.', $order->getIncrementId()));
$write = $resource->getConnection('core_write');

$query = "UPDATE {$table} SET status = :status, time = :time, qpstat = :qpstat, qpstatmsg = :qpstatmsg, chstat = :chstat, chstatmsg = :chstatmsg, splitpayment = :splitpayment, capturedAmount = :capturedAmount WHERE ordernum = :order_number";
Expand Down
7 changes: 7 additions & 0 deletions app/code/community/Quickpay/Payment/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function saveOrder($observer)
public function onBlockHtmlBefore(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();

if (! isset($block)) return;

if ($block->getType() === 'adminhtml/sales_order_grid') {
Expand All @@ -97,6 +98,12 @@ public function onBlockHtmlBefore(Varien_Event_Observer $observer)
// order columns
$block->addColumnsOrder('fraudprobability', 'massaction')->sortColumnsByOrder();
$block->addColumnsOrder('massaction', 'fraudprobability')->sortColumnsByOrder();

$massAction = $block->getMassactionBlock();
$massAction->addItem('quickpay_mass_capture', array(
'label' => 'Capture with QuickPay',
'url' => $block->getUrl('adminhtml/quickpay/massCapture')
));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

class Quickpay_Payment_Adminhtml_QuickpayController extends Mage_Adminhtml_Controller_Action
{
/**
* Mass capture action
*
* @throws Mage_Core_Exception
*/
public function massCaptureAction()
{
$orderIds = $this->getRequest()->getPost('order_ids', array());

foreach ($orderIds as $orderId) {
/** @var Mage_Sales_Model_Order $order */
$order = Mage::getModel("sales/order")->load($orderId);

if (!$order->getPayment()->getMethodInstance() instanceof Quickpay_Payment_Model_Method_Abstract) {
$this->_getSession()->addError($this->__('%s Order was not placed using QuickPay', $order->getIncrementId()));
continue;
}

try {

if (!$order->canInvoice()) {
$this->_getSession()->addError($this->__('Could not create invoice for %s', $order->getIncrementId()));
}

/* @var $invoice Mage_Sales_Model_Order_Invoice */
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

if (!$invoice->getTotalQty()) {
$this->_getSession()->addError($this->__('Cannot create an invoice without products for %s.', $order->getIncrementId()));
}

$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();

$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());

$transactionSave->save();
} catch (Exception $e) {
$this->_getSession()->addError($this->__('Invoice and capture failed for %s: %s', $order->getIncrementId(), $e->getMessage()));
}
}

$this->_redirect('*/sales_order/');
}
}
13 changes: 12 additions & 1 deletion app/code/community/Quickpay/Payment/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<Quickpay_Payment>
<version>3.4.3</version>
<version>3.4.4</version>
</Quickpay_Payment>
</modules>
<global>
Expand Down Expand Up @@ -129,6 +129,17 @@
</updates>
</layout>
</frontend>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<Quickpay_Payment after="Mage_Adminhtml">Quickpay_Payment_Adminhtml</Quickpay_Payment>
</modules>
</args>
</adminhtml>
</routers>
</admin>
<adminhtml>
<translate>
<modules>
Expand Down

0 comments on commit 1848898

Please sign in to comment.