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_Sales
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: /**
28: * Sales Billing Agreement Payment Method Abstract model
29: *
30: * @author Magento Core Team <core@magentocommerce.com>
31: */
32: abstract class Mage_Sales_Model_Payment_Method_Billing_AgreementAbstract extends Mage_Payment_Model_Method_Abstract
33: {
34: /**
35: * Transport billing agreement id
36: *
37: */
38: const TRANSPORT_BILLING_AGREEMENT_ID = 'ba_agreement_id';
39: const PAYMENT_INFO_REFERENCE_ID = 'ba_reference_id';
40:
41: protected $_infoBlockType = 'sales/payment_info_billing_agreement';
42: protected $_formBlockType = 'sales/payment_form_billing_agreement';
43:
44: /**
45: * Is method instance available
46: *
47: * @var null|bool
48: */
49: protected $_isAvailable = null;
50:
51: /**
52: * Check whether method is available
53: *
54: * @param Mage_Sales_Model_Quote $quote
55: * @return bool
56: */
57: public function isAvailable($quote = null)
58: {
59: if (is_null($this->_isAvailable)) {
60: if (is_object($quote) && $quote->getCustomer()) {
61: $availableBA = Mage::getModel('sales/billing_agreement')->getAvailableCustomerBillingAgreements(
62: $quote->getCustomer()->getId()
63: );
64: $isAvailableBA = count($availableBA) > 0;
65: $this->_canUseCheckout = $this->_canUseInternal = $isAvailableBA;
66: }
67: $this->_isAvailable = parent::isAvailable($quote) && $this->_isAvailable($quote);
68: $this->_canUseCheckout = ($this->_isAvailable && $this->_canUseCheckout);
69: $this->_canUseInternal = ($this->_isAvailable && $this->_canUseInternal);
70: }
71: return $this->_isAvailable;
72: }
73:
74: /**
75: * Assign data to info model instance
76: *
77: * @param mixed $data
78: * @return Mage_Payment_Model_Info
79: */
80: public function assignData($data)
81: {
82: $result = parent::assignData($data);
83:
84: $key = self::TRANSPORT_BILLING_AGREEMENT_ID;
85: $id = false;
86: if (is_array($data) && isset($data[$key])) {
87: $id = $data[$key];
88: } elseif ($data instanceof Varien_Object && $data->getData($key)) {
89: $id = $data->getData($key);
90: }
91: if ($id) {
92: $info = $this->getInfoInstance();
93: $ba = Mage::getModel('sales/billing_agreement')->load($id);
94: if ($ba->getId() && $ba->getCustomerId() == $info->getQuote()->getCustomer()->getId()) {
95: $info->setAdditionalInformation($key, $id)
96: ->setAdditionalInformation(self::PAYMENT_INFO_REFERENCE_ID, $ba->getReferenceId());
97: }
98: }
99: return $result;
100: }
101:
102: /**
103: *
104: *
105: * @param unknown_type $quote
106: */
107: abstract protected function _isAvailable($quote);
108: }
109: