1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Paygate
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: class Mage_Paygate_Model_Authorizenet_Cards
28: {
29: const CARDS_NAMESPACE = 'authorize_cards';
30: const CARD_ID_KEY = 'id';
31: const CARD_PROCESSED_AMOUNT_KEY = 'processed_amount';
32: const CARD_CAPTURED_AMOUNT_KEY = 'captured_amount';
33: const CARD_REFUNDED_AMOUNT_KEY = 'refunded_amount';
34:
35: /**
36: * Cards information
37: *
38: * @var mixed
39: */
40: protected $_cards = array();
41:
42: /**
43: * Payment instance
44: *
45: * @var Mage_Payment_Model_Info
46: */
47: protected $_payment = null;
48:
49: /**
50: * Set payment instance for storing credit card information and partial authorizations
51: *
52: * @param Mage_Payment_Model_Info $payment
53: * @return Mage_Paygate_Model_Authorizenet_Cards
54: */
55: public function setPayment(Mage_Payment_Model_Info $payment)
56: {
57: $this->_payment = $payment;
58: $paymentCardsInformation = $this->_payment->getAdditionalInformation(self::CARDS_NAMESPACE);
59: if ($paymentCardsInformation) {
60: $this->_cards = $paymentCardsInformation;
61: }
62:
63: return $this;
64: }
65:
66: /**
67: * Add based on $cardInfo card to payment and return Id of new item
68: *
69: * @param mixed $cardInfo
70: * @return string
71: */
72: public function registerCard($cardInfo = array())
73: {
74: $this->_isPaymentValid();
75: $cardId = md5(microtime(1));
76: $cardInfo[self::CARD_ID_KEY] = $cardId;
77: $this->_cards[$cardId] = $cardInfo;
78: $this->_payment->setAdditionalInformation(self::CARDS_NAMESPACE, $this->_cards);
79: return $this->getCard($cardId);
80: }
81:
82: /**
83: * Save data from card object in cards storage
84: *
85: * @param Varien_Object $card
86: * @return Mage_Paygate_Model_Authorizenet_Cards
87: */
88: public function updateCard($card)
89: {
90: $cardId = $card->getData(self::CARD_ID_KEY);
91: if ($cardId && isset($this->_cards[$cardId])) {
92: $this->_cards[$cardId] = $card->getData();
93: $this->_payment->setAdditionalInformation(self::CARDS_NAMESPACE, $this->_cards);
94: }
95: return $this;
96: }
97:
98: /**
99: * Retrieve card by ID
100: *
101: * @param string $cardId
102: * @return Varien_Object|bool
103: */
104: public function getCard($cardId)
105: {
106: if (isset($this->_cards[$cardId])) {
107: $card = new Varien_Object($this->_cards[$cardId]);
108: return $card;
109: }
110: return false;
111: }
112:
113: /**
114: * Get all stored cards
115: *
116: * @return array
117: */
118: public function getCards()
119: {
120: $this->_isPaymentValid();
121: $_cards = array();
122: foreach(array_keys($this->_cards) as $key) {
123: $_cards[$key] = $this->getCard($key);
124: }
125: return $_cards;
126: }
127:
128: /**
129: * Return count of saved cards
130: *
131: * @return int
132: */
133: public function getCardsCount()
134: {
135: $this->_isPaymentValid();
136: return count($this->_cards);
137: }
138:
139: /**
140: * Return processed amount for all cards
141: *
142: * @return float
143: */
144: public function getProcessedAmount()
145: {
146: return $this->_getAmount(self::CARD_PROCESSED_AMOUNT_KEY);
147: }
148:
149: /**
150: * Return captured amount for all cards
151: *
152: * @return float
153: */
154: public function getCapturedAmount()
155: {
156: return $this->_getAmount(self::CARD_CAPTURED_AMOUNT_KEY);
157: }
158:
159: /**
160: * Return refunded amount for all cards
161: *
162: * @return float
163: */
164: public function getRefundedAmount()
165: {
166: return $this->_getAmount(self::CARD_REFUNDED_AMOUNT_KEY);
167: }
168:
169: /**
170: * Remove all cards from payment instance
171: *
172: * @return Mage_Paygate_Model_Authorizenet_Cart
173: */
174: public function flushCards()
175: {
176: $this->_cards = array();
177: $this->_payment->setAdditionalInformation(self::CARDS_NAMESPACE, null);
178: return $this;
179: }
180:
181: /**
182: * Check for payment instace present
183: *
184: * @throws Exception
185: */
186: protected function _isPaymentValid()
187: {
188: if (!$this->_payment) {
189: throw new Exception('Payment instance is not set');
190: }
191: }
192: /**
193: * Return total for cards data fields
194: *
195: * $param string $key
196: * @return float
197: */
198: public function _getAmount($key)
199: {
200: $amount = 0;
201: foreach ($this->_cards as $card) {
202: if (isset($card[$key])) {
203: $amount += $card[$key];
204: }
205: }
206: return $amount;
207: }
208: }
209: