3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * This class stores logic for managing CiviCRM extensions.
32 * @copyright CiviCRM LLC (c) 2004-2015
36 class CRM_Extension_Manager_Payment
extends CRM_Extension_Manager_Base
{
39 * @var CRM_Extension_Mapper
44 * @param CRM_Extension_Mapper $mapper
46 public function __construct(CRM_Extension_Mapper
$mapper) {
47 parent
::__construct(TRUE);
48 $this->mapper
= $mapper;
54 public function onPreInstall(CRM_Extension_Info
$info) {
55 $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name');
57 if (array_key_exists($info->key
, $paymentProcessorTypes)) {
58 CRM_Core_Error
::fatal('This payment processor type is already installed.');
61 $ppByName = $this->_getAllPaymentProcessorTypes('name');
62 if (array_key_exists($info->name
, $ppByName)) {
63 CRM_Core_Error
::fatal('This payment processor type already exists.');
66 $dao = new CRM_Financial_DAO_PaymentProcessorType();
69 $dao->class_name
= trim($info->key
);
70 $dao->title
= trim($info->name
) . ' (' . trim($info->key
) . ')';
71 $dao->name
= trim($info->name
);
72 $dao->description
= trim($info->description
);
74 $dao->user_name_label
= trim($info->typeInfo
['userNameLabel']);
75 $dao->password_label
= trim($info->typeInfo
['passwordLabel']);
76 $dao->signature_label
= trim($info->typeInfo
['signatureLabel']);
77 $dao->subject_label
= trim($info->typeInfo
['subjectLabel']);
78 $dao->url_site_default
= trim($info->typeInfo
['urlSiteDefault']);
79 $dao->url_api_default
= trim($info->typeInfo
['urlApiDefault']);
80 $dao->url_recur_default
= trim($info->typeInfo
['urlRecurDefault']);
81 $dao->url_site_test_default
= trim($info->typeInfo
['urlSiteTestDefault']);
82 $dao->url_api_test_default
= trim($info->typeInfo
['urlApiTestDefault']);
83 $dao->url_recur_test_default
= trim($info->typeInfo
['urlRecurTestDefault']);
84 $dao->url_button_default
= trim($info->typeInfo
['urlButtonDefault']);
85 $dao->url_button_test_default
= trim($info->typeInfo
['urlButtonTestDefault']);
87 switch (trim($info->typeInfo
['billingMode'])) {
89 $dao->billing_mode
= CRM_Core_Payment
::BILLING_MODE_FORM
;
93 $dao->billing_mode
= CRM_Core_Payment
::BILLING_MODE_BUTTON
;
97 $dao->billing_mode
= CRM_Core_Payment
::BILLING_MODE_NOTIFY
;
101 CRM_Core_Error
::fatal('Billing mode in info file has wrong value.');
104 $dao->is_recur
= trim($info->typeInfo
['isRecur']);
105 $dao->payment_type
= trim($info->typeInfo
['paymentType']);
113 public function onPostInstall(CRM_Extension_Info
$info) {
114 $this->_runPaymentHook($info, 'install');
120 public function onPreUninstall(CRM_Extension_Info
$info) {
121 $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name');
122 if (!array_key_exists($info->key
, $paymentProcessorTypes)) {
123 CRM_Core_Error
::fatal('This payment processor type is not registered.');
126 $dao = new CRM_Financial_DAO_PaymentProcessor();
127 $dao->payment_processor_type_id
= $paymentProcessorTypes[$info->key
];
129 while ($dao->fetch()) {
130 throw new CRM_Extension_Exception_DependencyException('payment');
133 $this->_runPaymentHook($info, 'uninstall');
134 return CRM_Financial_BAO_PaymentProcessorType
::del($paymentProcessorTypes[$info->key
]);
140 public function onPreDisable(CRM_Extension_Info
$info) {
141 // HMM? // if ($this->type == 'payment' && $this->status != 'missing') {
142 $this->_runPaymentHook($info, 'disable');
144 $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name');
145 CRM_Financial_BAO_PaymentProcessorType
::setIsActive($paymentProcessorTypes[$info->key
], 0);
151 public function onPreEnable(CRM_Extension_Info
$info) {
152 $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name');
153 CRM_Financial_BAO_PaymentProcessorType
::setIsActive($paymentProcessorTypes[$info->key
], 1);
159 public function onPostEnable(CRM_Extension_Info
$info) {
160 // HMM? // if ($this->type == 'payment' && $this->status != 'missing') {
161 $this->_runPaymentHook($info, 'enable');
165 * @param string $attr
166 * The attribute used to key the array.
170 private function _getAllPaymentProcessorTypes($attr) {
172 $dao = new CRM_Financial_DAO_PaymentProcessorType();
174 while ($dao->fetch()) {
175 $ppt[$dao->$attr] = $dao->id
;
181 * Run hooks in the payment processor class.
182 * Load requested payment processor and call the method specified.
184 * @param CRM_Extension_Info $info
185 * @param string $method
186 * The method to call in the payment processor class.
188 private function _runPaymentHook(CRM_Extension_Info
$info, $method) {
189 // Not concerned about performance at this stage, as these are seldom performed tasks
190 // (payment processor enable/disable/install/uninstall). May wish to implement some
191 // kind of registry/caching system if more hooks are added.
194 $paymentClass = $this->mapper
->keyToClass($info->key
, 'payment');
195 $file = $this->mapper
->classToPath($paymentClass);
196 if (!file_exists($file)) {
197 CRM_Core_Session
::setStatus(ts('Failed to load file (%3) for payment processor (%1) while running "%2"', array(
208 catch (CRM_Extension_Exception
$e) {
209 CRM_Core_Session
::setStatus(ts('Failed to determine file path for payment processor (%1) while running "%2"', array(
216 $processorDAO = CRM_Core_DAO
::executeQuery(
217 " SELECT pp.id, ppt.class_name
218 FROM civicrm_extension ext
219 INNER JOIN civicrm_payment_processor_type ppt
220 ON ext.name = ppt.name
221 LEFT JOIN civicrm_payment_processor pp
222 ON ppt.id = pp.payment_processor_type_id
223 WHERE ext.type = 'payment'
224 AND ext.full_name = %1
227 1 => array($info->key
, 'String'),
231 while ($processorDAO->fetch()) {
232 $class_name = $processorDAO->class_name
;
233 $processor_id = $processorDAO->id
;
236 if (empty($class_name)) {
237 CRM_Core_Error
::fatal("Unable to find payment processor in " . __CLASS__
. '::' . __METHOD__
);
240 // In the case of uninstall, check for instances of PP first.
241 // Don't run hook if any are found.
242 if ($method == 'uninstall' && $processor_id > 0) {
251 // Instantiate PP - the getClass function allows us to do this when no payment processor instances exist.
252 $processorInstance = Civi\Payment\System
::singleton()->getByClass($class_name);
254 // Does PP implement this method, and can we call it?
255 if (method_exists($processorInstance, $method) && is_callable(array(
260 // If so, call it ...
261 $processorInstance->$method();
266 CRM_Core_Session
::setStatus(ts("Unrecognized payment hook (%1) in %2::%3",
267 array(1 => $method, 2 => __CLASS__
, 3 => __METHOD__
)),