1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_XmlConnect_Block_Checkout_Payment_Method_List extends Mage_Payment_Block_Form_Container
35: {
36: 37: 38: 39: 40:
41: protected $_methodArray = array(
42: 'payment_ccsave' => 'Mage_Payment_Model_Method_Cc',
43: 'payment_checkmo' => 'Mage_Payment_Model_Method_Checkmo',
44: 'payment_purchaseorder' => 'Mage_Payment_Model_Method_Purchaseorder',
45: );
46:
47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57:
58: protected $_pbridgeMethodArray = array(
59: 'pbridge_authorizenet', 'pbridge_paypal', 'pbridge_verisign', 'pbridge_paypaluk'
60: );
61:
62: 63: 64: 65: 66:
67: protected function _prepareLayout()
68: {
69: return $this;
70: }
71:
72: 73: 74: 75: 76:
77: public function getQuote()
78: {
79: return Mage::getSingleton('checkout/session')->getQuote();
80: }
81:
82: 83: 84: 85: 86:
87: public function addCustomerBalanceToXmlObj(Mage_XmlConnect_Model_Simplexml_Element $methodsXmlObj)
88: {
89:
90: $customerBalanceBlock = $this->getLayout()
91: ->addBlock('enterprise_customerbalance/checkout_onepage_payment_additional', 'customer_balance');
92: $storeCreditFlag = (int) Mage::getStoreConfig(Enterprise_CustomerBalance_Helper_Data::XML_PATH_ENABLED);
93: if ($storeCreditFlag && $customerBalanceBlock->isDisplayContainer()) {
94: $balance = $this->getQuote()->getStore()->formatPrice($customerBalanceBlock->getBalance(), false);
95: $methodsXmlObj->addCustomChild('customer_balance', null, array(
96: 'post_name' => 'payment[use_customer_balance]',
97: 'code' => 1,
98: 'label' => $this->__('Use Store Credit (%s available)', $balance),
99: 'is_cover_a_quote' => intval($customerBalanceBlock->isFullyPaidAfterApplication()),
100: 'selected' => intval($customerBalanceBlock->isCustomerBalanceUsed())
101: ));
102: }
103: }
104:
105: 106: 107: 108: 109:
110: public function addGiftcardToXmlObj(Mage_XmlConnect_Model_Simplexml_Element $methodsXmlObj)
111: {
112: $giftcardInfoBlock = $this->getLayout()->addBlock(
113: 'enterprise_giftcardaccount/checkout_onepage_payment_additional', 'giftcard_info'
114: );
115:
116: if (intval($giftcardInfoBlock->getAppliedGiftCardAmount())) {
117: $amount = $this->getQuote()->getStore()->formatPrice($giftcardInfoBlock->getAppliedGiftCardAmount(), false);
118: $amount = $this->__('Gift Card amount applied to order: %s', $amount);
119:
120: $methodsXmlObj->addCustomChild('information', null, array('label' => $amount, 'disabled' => '1'));
121:
122: if ($this->_isPaymentRequired()) {
123: $methodsXmlObj->addCustomChild('method', null, array(
124: 'post_name' => 'payment[method]',
125: 'code' => 'free',
126: 'label' => $this->__('No Payment Information Required'),
127: 'selected' => '1',
128: 'disabled' => '1'
129: ));
130: }
131: $this->setIsUsedGiftCard(true);
132: }
133: }
134:
135: 136: 137: 138: 139:
140: protected function _isPaymentRequired()
141: {
142: $this->getQuote()->collectTotals();
143: return !intval($this->getQuote()->getGrandTotal()) && !$this->getQuote()->hasNominalItems();
144: }
145:
146: 147: 148: 149: 150:
151: protected function _getPaymentMethodArray()
152: {
153: $methodArray = $this->_methodArray;
154:
155: 156: 157:
158: if (is_object(Mage::getConfig()->getNode('modules/Enterprise_Pbridge'))) {
159:
160: $pbBlockRenderer = 'xmlconnect/checkout_payment_method_';
161: $pbBlockName = 'xmlconnect.checkout.payment.method.';
162:
163: foreach ($this->_pbridgeMethodArray as $block) {
164: $currentBlockRenderer = $pbBlockRenderer . $block;
165: $currentBlockName = $pbBlockName . $block;
166: $this->getLayout()->addBlock($currentBlockRenderer, $currentBlockName);
167: $this->setChild($block, $currentBlockName);
168: }
169: $methodArray = $methodArray + $this->_pbridgeMethodArray;
170: }
171: return $methodArray;
172: }
173:
174: 175: 176: 177: 178: 179:
180: protected function _toHtml()
181: {
182:
183: $methodsXmlObj = Mage::getModel('xmlconnect/simplexml_element', '<payment_methods></payment_methods>');
184:
185: if (is_object(Mage::getConfig()->getNode('modules/Enterprise_GiftCardAccount'))) {
186: $this->addGiftcardToXmlObj($methodsXmlObj);
187: if ($this->getIsUsedGiftCard() && $this->_isPaymentRequired()) {
188: return $methodsXmlObj->asNiceXml();
189: }
190: }
191:
192: if (is_object(Mage::getConfig()->getNode('modules/Enterprise_CustomerBalance'))) {
193: $this->addCustomerBalanceToXmlObj($methodsXmlObj);
194: }
195:
196: $methodArray = $this->_getPaymentMethodArray();
197: $usedMethods = $sortedAvailableMethodCodes = $usedCodes = array();
198:
199: 200: 201:
202: $allAvailableMethods = Mage::helper('payment')->getStoreMethods(
203: Mage::app()->getStore(), $this->getQuote()
204: );
205:
206: 207: 208:
209: foreach ($allAvailableMethods as $method) {
210: $sortedAvailableMethodCodes[] = $method->getCode();
211: }
212:
213: 214: 215:
216: $methodBlocks = $this->getChild();
217:
218: 219: 220:
221: if (!empty($methodBlocks) && is_array($methodBlocks)) {
222: foreach ($methodBlocks as $block) {
223: if (!$block) {
224: continue;
225: }
226:
227: $method = $block->getMethod();
228: if (!$this->_canUseMethod($method) || in_array($method->getCode(), $usedCodes)) {
229: continue;
230: }
231: $this->_assignMethod($method);
232: $usedCodes[] = $method->getCode();
233: $usedMethods[$method->getCode()] = array('renderer' => $block, 'method' => $method);
234: }
235: }
236:
237: 238: 239:
240: foreach ($methodArray as $methodName => $methodModelClassName) {
241: $methodRenderer = $this->getChild($methodName);
242: if (!empty($methodRenderer)) {
243: foreach ($sortedAvailableMethodCodes as $methodCode) {
244: 245: 246:
247: if (in_array($methodCode, $usedCodes)) {
248: continue;
249: }
250: try {
251: $method = Mage::helper('payment')->getMethodInstance($methodCode);
252: if (!is_subclass_of($method, $methodModelClassName)) {
253: continue;
254: }
255: if (!$this->_canUseMethod($method)) {
256: continue;
257: }
258:
259: $this->_assignMethod($method);
260: $usedCodes[] = $method->getCode();
261: $usedMethods[$method->getCode()] = array('renderer' => $methodRenderer, 'method' => $method);
262: } catch (Exception $e) {
263: Mage::logException($e);
264: }
265: }
266: }
267: }
268:
269: 270: 271:
272: foreach ($sortedAvailableMethodCodes as $code) {
273: if (!in_array($code, $usedCodes)) {
274: continue;
275: }
276: $method = $usedMethods[$code]['method'];
277: $renderer = $usedMethods[$code]['renderer'];
278: 279: 280:
281: if ($renderer instanceOf Mage_XmlConnect_Block_Checkout_Payment_Method_Ccsave) {
282: $renderer->setData('method', $method);
283: }
284:
285: $methodItemXmlObj = $methodsXmlObj->addChild('method');
286: $methodItemXmlObj->addAttribute('post_name', 'payment[method]');
287: $methodItemXmlObj->addAttribute('code', $method->getCode());
288: $methodItemXmlObj->addAttribute('label', $methodsXmlObj->escapeXml($method->getTitle()));
289: if ($this->getQuote()->getPayment()->getMethod() == $method->getCode()) {
290: $methodItemXmlObj->addAttribute('selected', 1);
291: }
292: $renderer->addPaymentFormToXmlObj($methodItemXmlObj);
293: }
294: if (!count($usedMethods)) {
295: Mage::throwException($this->__('Sorry, no payment options are available for this order at this time.'));
296: }
297: return $methodsXmlObj->asNiceXml();
298: }
299:
300: 301: 302: 303: 304: 305:
306: protected function _canUseMethod($method)
307: {
308: if (!($method instanceof Mage_Payment_Model_Method_Abstract)
309: || !$method->canUseCheckout()
310: || !$method->isAvailable($this->getQuote())
311: ) {
312: return false;
313: }
314: return parent::_canUseMethod($method);
315: }
316:
317: 318: 319: 320: 321: 322: 323: 324: 325:
326: protected function _addToXml($block, $methodsXmlObj, $usedCodes)
327: {
328: return false;
329: }
330:
331: 332: 333: 334: 335: 336: 337:
338: public function isAvailable($method)
339: {
340: return $method->isAvailable($this->getQuote());
341: }
342: }
343: