Magento – Save Credit Card Number for Authorize NET
It’s been a while now since I wrote my first blog, during the time I have been working on some cool magento projects. One of my clients asked me to have the credit card number saved for all orders placed using Authorize NET, by default magento saves the credit card number only for Saved CC payment method.
Magento uses it’s built in encryption algorithm based on mcrypt php extension to save the credit card number, in order to enable saving credit card numbers we need to override a default Authorize NET Paygate model Mage_Paygate_Authorizenet.
If you are using Magento 1.4.x then setting $_canSave=true will be enough to have magento start saving credit card numbers, but in Magento 1.5.x things has changed a bit, you would also need to override _registerCard method to force and save the credit card number below is the sample code.
<config> <global> <models> <mymodule> <class>Displaze_MyModule_Model</class> </mymodule> <paygate> <rewrite> <authorizenet>Displaze_MyModule_Model_Authorizenet</authorizenet> </rewrite> </paygate> </models> </global> </config>
<!--?php /* * Make Authorizenet to save credit card number * */ class Displaze_MyModule_Model_Authorizenet extends Mage_Paygate_Model_Authorizenet { /** * @todo uncomment the following if you want to refund the payment, or make a patial payment */ //protected $_canCapturePartial = false; //protected $_canRefund = false; protected $_canSaveCc = true; protected $_canUseInternal = true; /** * It sets card`s data into additional information of payment model * AuthorizeNet has added additional_information field in sale_flat_order_payment table * where they savve credit card info, and disallow to save the card in other fields, * This method is temprory and we need to fetch the card info from additional_information * field. * @param Mage_Paygate_Model_Authorizenet_Result $response * @param Mage_Sales_Model_Order_Payment $payment * @return Varien_Object */ protected function _registerCard(Varien_Object $response, Mage_Sales_Model_Order_Payment $payment) { $cardsStorage = $this--->getCardsStorage($payment); $card = $cardsStorage->registerCard(); $card ->setRequestedAmount($response->getRequestedAmount()) ->setBalanceOnCard($response->getBalanceOnCard()) ->setLastTransId($response->getTransactionId()) ->setProcessedAmount($response->getAmount()) ->setCcType($payment->getCcType()) ->setCcOwner($payment->getCcOwner()) ->setCcLast4($payment->getCcLast4()) ->setCcExpMonth($payment->getCcExpMonth()) ->setCcExpYear($payment->getCcExpYear()) ->setCcSsIssue($payment->getCcSsIssue()) ->setCcSsStartMonth($payment->getCcSsStartMonth()) ->setCcSsStartYear($payment->getCcSsStartYear()); $cardsStorage->updateCard($card); //below is the only reason to override this method, //$this->_clearAssignedData($payment); return $card; } }