Every shop owner – online and offline – knows that dealing with credit cards is a very complex subject. There are rules about storing cards, how long you can store them (if you need to at all), authorizations, captures, rules regarding those, and how to handle certain situations.
One easy step you can take to handle regulations is to only authorize at checkout. This is an easy change in Magento. Log into your admin, and go into the settings for your payment processor. You’ll notice something called “Payment Action”. For most, there’s “Authorize Only” and “Authorize and Capture”. PayPal is a little different; they use “Authorization” and “Sale” (same concept though). This is great. Set that to Authorize and you’re good to go. But wait – how do you actually get your money?
This is the not-so-fun part. When you ship every order, you’ll need to log into Magento, invoice the order by capturing online (Available in Magento 1.4.2.0 and higher), and then you’ll need to create shipments – either manually or via 3rd party integration.
A developer who does a lot of work with us, Kamil Durski, figured out a solution to this. Amazingly, it’s only 2 files. That’s right – 2. Why this functionality isn’t there is beyond me.
First, you’ll create app/code/local/SUMOHeavy/ShippingInvoice/etc/config.xml:
<?xml version="1.0"?> <config> <modules> <SUMOHeavy_ShippingInvoice> <version>1.0.0</version> </SUMOHeavy_ShippingInvoice> </modules> <global> <models> <shippinginvoice> <class>SUMOHeavy_ShippingInvoice_Model</class> </shippinginvoice> </models> <events> <sales_order_shipment_save_after> <observers> <sumo_shippinginvoice> <type>singleton</type> <class>shippinginvoice/observer</class> <method>createInvoiceAndCapturePayment</method> </sumo_shippinginvoice> </observers> </sales_order_shipment_save_after> </events> </global> </config> |
Then you’ll need to create app/code/local/SUMOHeavy/ShippingInvoice/Model/Observer.php:
<?php class SUMOHeavy_ShippingInvoice_Model_Observer extends Mage_Core_Model_Abstract { public function createInvoiceAndCapturePayment($observer) { try { $shipment = $observer->getEvent()->getShipment(); $order = $shipment->getOrder(); if (!$order->canInvoice()) { return $this; } $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($savedQtys); if (!$invoice->getTotalQty()) { return $this; } $invoice->setRequestedCaptureCase('online'); $invoice->register(); $invoice->getOrder()->setIsInProcess(true); $transactionSave = Mage::getModel('core/resource_transaction') ->addObject($invoice) ->addObject($invoice->getOrder()); $transactionSave->save(); // Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The invoice has been created.')); } catch(Exception $e) { die($e->getMessage()); } } } |
In the first file, we’re setting up or observer to watch the sales_order_shipment_save_after event. This means that after a shipment is committed to the database, we’ll capture the payment. In the observer file, we’re doing a few things:
1) Look up the shipping and order info:
$shipment = $observer->getEvent()->getShipment(); $order = $shipment->getOrder(); |
2) Check to see if the order can be invoiced
if (!$order->canInvoice()) { return $this; } |
3) Load all necessary info we need to process the transaction:
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($savedQtys); if (!$invoice->getTotalQty()) { return $this; } |
4) Capture the funds online and save the order:
$invoice->setRequestedCaptureCase('online'); $invoice->register(); $invoice->getOrder()->setIsInProcess(true); $transactionSave = Mage::getModel('core/resource_transaction') ->addObject($invoice) ->addObject($invoice->getOrder()); $transactionSave->save(); |
5) If we can’t capture, it will fail and present an error:
catch(Exception $e) { die($e->getMessage()); } |
This is basic functionality and assumes that all orders are capturing against a live authorization. A great idea would be to extend the payment modules to store authorization tokens so that if you need to re-authorize the order you’d be able to. I’ve been keeping an eye on the subversion repository Magento 1.6.0.0 and one thing is really intriguing:
Added settings Authorization Honor Period and Order Valid Period into EC tab in the backend
I’m not 100% sure what this is going to accomplish in the end but have some ideas. It would be great if there’s some better CC handling methods in 1.6. If this makes it into the stable version, it could be a doorway to even greater opportunities.
Feel free to try this out, or add to it here: https://github.com/sumoheavy/SUMOHeavy_ShippingInvoice

