0

I have been creating a custom payment gateway which works off the back of Multi Shipping so we can have multiple items go to multiple addresses and still use a credit card for payment. Magento doesn't allow payment gateways on Multi Shipping so we have had to do a large amount of customisation to get it to work properly, e.g. rewriting MultishippingController and Multishipping model to name a few.

We have this working pretty nicely now but I have run into an issue I just can't find the reason for.

When we get a response from the payment gateway, we check for a positive result, which then allows the quote to be turned into orders (there is an order per address). This all functions perfectly and creates an order in the admin with the status 'processing' (via registerCaptureNotification()) which is great

However only the first order payment actually gets applied. They both show that they have 'captured' a payment but only the first one will actually have an invoice and show that there is no outstanding balance. The second / other order will still show that the full payment is required. Strangely though second order DOES have a transaction against it

Here are the functions on my custom model:

public function process(array $responseData, $orderIds)
{

    if(!$orderIds) {
        Mage::throwException('Missing order ids while processing');
        return;
    }

    $orderIdsExplode = explode(',',$orderIds);

    $amount = $responseData['Amount'];
    $amountFormat = (($amount * 1) / 100);
    $total = 0;
    Mage::log('amount response: '.$amountFormat, null, 'logfile.log', true);

    foreach ($orderIdsExplode as $incrementId) {
        $order = Mage::getSingleton('sales/order')->loadByIncrementId($incrementId);
        $payment = $order->getPayment();
        $this->_registerPayment($order, $responseData);

        $price = $order->getGrandTotal();
        $total += $price;
    }

    if($total != $amountFormat) {
        Mage::log('Transaction amount mismatch for: '.$orderIds, null, 'logfile.log', true);
    }

    return $orderIds;
}

And:

protected function _registerPayment($order, $data)
{

    $receipt = $data['Receipt'];

    $payment = $order->getPayment();
    //$payment->setIsTransactionClosed(0);
    $payment->setOrder($order);

    $payment->setTransactionId($receipt);
    $payment->registerCaptureNotification((string)$order->getGrandTotal());

    Mage::log('order id: '.$order->getId(), null, 'logfile.log', true);
    Mage::log('payment id: '.$payment->getId(), null, 'logfile.log', true);
    Mage::log('order grand total: '.$order->getGrandTotal(), null, 'logfile.log', true);

    $invoice = $payment->getCreatedInvoice();
    //$payment->place();
    $payment->save();
    //$order->place();
    $order->save();


    if (!$order->getEmailSent()) {
        $order->queueNewOrderEmail();
    }
}

You will see I have tried using the place() functions to force the payment but this causes the second order to have a unusual balance applied against it (not the full amount or any other amount I can work out, e.g. not minus taxes etc)

Based on the logs, I can see the second payment ID is the same as the first one, even though the order ids are separate:

2016-02-10T20:34:47+00:00 DEBUG (7): amount response: 291
2016-02-10T20:34:47+00:00 DEBUG (7): order id: 69
2016-02-10T20:34:47+00:00 DEBUG (7): payment id: 69
2016-02-10T20:34:47+00:00 DEBUG (7): order grand total: 85.5000
2016-02-10T20:34:47+00:00 DEBUG (7): order id: 70
2016-02-10T20:34:47+00:00 DEBUG (7): payment id: 69
2016-02-10T20:34:47+00:00 DEBUG (7): order grand total: 205.5000

I believe this is the cause of my issue - surely the payment ID should be different for the second order? Looking in the database, it seems there is actually a second payment created but its completely blank.

Here are screenshots of the two orders (ID's 69 and 70) order id 69 order id 70

Any direction on this would be greatly appreciated!! Been pulling my hair out over this one and can't find anything relating to it online!

Unfortunately I am unable to see any of the responses as it happens in the background before the user is actually redirected back so I have to rely on logging for any debugging

This is all on Magento 1.9

  • Here is an example using place() on the payment: imgur.com/dDq6d3u (SO won't let me add 3 images yet) – Onfire Feb 11 '16 at 3:10
0

The answer was simple in the end:

 $orderIdsExplode = explode(',',$orderIds);
 $orders = Mage::getModel('sales/order')
        ->getCollection()
        ->addAttributeToFilter('increment_id', $orderIdsExplode)
        ->addAttributeToSelect('*')->addAttributeToSort('increment_id',DESC);

 foreach ($orders as $order) {
     $this->_registerPayment($order, $responseData);
 }

Rather than looping in increment ids and getting them from the db, I just needed to select them all at once and loop afterwards. I never worked out why this happens, but it seemed to cache the previous request and always select the wrong one.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.