Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * This class stores logic for managing CiviCRM extensions. | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | class CRM_Extension_Manager_Payment extends CRM_Extension_Manager_Base { | |
37 | ||
38 | /** | |
39 | @var CRM_Extension_Mapper | |
40 | */ | |
41 | protected $mapper; | |
42 | ||
43 | public function __construct(CRM_Extension_Mapper $mapper) { | |
44 | parent::__construct(TRUE); | |
45 | $this->mapper = $mapper; | |
46 | } | |
47 | ||
48 | /** | |
49 | * {@inheritdoc} | |
50 | */ | |
51 | public function onPreInstall(CRM_Extension_Info $info) { | |
52 | $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name'); | |
53 | ||
54 | if (array_key_exists($info->key, $paymentProcessorTypes)) { | |
55 | CRM_Core_Error::fatal('This payment processor type is already installed.'); | |
56 | } | |
57 | ||
58 | $ppByName = $this->_getAllPaymentProcessorTypes('name'); | |
59 | if (array_key_exists($info->name, $ppByName)) { | |
60 | CRM_Core_Error::fatal('This payment processor type already exists.'); | |
61 | } | |
62 | ||
63 | $dao = new CRM_Financial_DAO_PaymentProcessorType(); | |
64 | ||
65 | $dao->is_active = 1; | |
66 | $dao->class_name = trim($info->key); | |
67 | $dao->title = trim($info->name) . ' (' . trim($info->key) . ')'; | |
68 | $dao->name = trim($info->name); | |
69 | $dao->description = trim($info->description); | |
70 | ||
71 | $dao->user_name_label = trim($info->typeInfo['userNameLabel']); | |
72 | $dao->password_label = trim($info->typeInfo['passwordLabel']); | |
73 | $dao->signature_label = trim($info->typeInfo['signatureLabel']); | |
74 | $dao->subject_label = trim($info->typeInfo['subjectLabel']); | |
75 | $dao->url_site_default = trim($info->typeInfo['urlSiteDefault']); | |
76 | $dao->url_api_default = trim($info->typeInfo['urlApiDefault']); | |
77 | $dao->url_recur_default = trim($info->typeInfo['urlRecurDefault']); | |
78 | $dao->url_site_test_default = trim($info->typeInfo['urlSiteTestDefault']); | |
79 | $dao->url_api_test_default = trim($info->typeInfo['urlApiTestDefault']); | |
80 | $dao->url_recur_test_default = trim($info->typeInfo['urlRecurTestDefault']); | |
81 | $dao->url_button_default = trim($info->typeInfo['urlButtonDefault']); | |
82 | $dao->url_button_test_default = trim($info->typeInfo['urlButtonTestDefault']); | |
83 | ||
84 | switch (trim($info->typeInfo['billingMode'])) { | |
85 | case 'form': | |
86 | $dao->billing_mode = CRM_Core_Payment::BILLING_MODE_FORM; | |
87 | break; | |
88 | ||
89 | case 'button': | |
90 | $dao->billing_mode = CRM_Core_Payment::BILLING_MODE_BUTTON; | |
91 | break; | |
92 | ||
93 | case 'notify': | |
94 | $dao->billing_mode = CRM_Core_Payment::BILLING_MODE_NOTIFY; | |
95 | break; | |
96 | ||
97 | default: | |
98 | CRM_Core_Error::fatal('Billing mode in info file has wrong value.'); | |
99 | } | |
100 | ||
101 | $dao->is_recur = trim($info->typeInfo['isRecur']); | |
102 | $dao->payment_type = trim($info->typeInfo['paymentType']); | |
103 | ||
104 | $dao->save(); | |
105 | } | |
106 | ||
107 | /** | |
108 | * {@inheritdoc} | |
109 | */ | |
110 | public function onPostInstall(CRM_Extension_Info $info) { | |
111 | $this->_runPaymentHook($info, 'install'); | |
112 | } | |
113 | ||
114 | /** | |
115 | * {@inheritdoc} | |
116 | */ | |
117 | public function onPreUninstall(CRM_Extension_Info $info) { | |
118 | $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name'); | |
119 | if (!array_key_exists($info->key, $paymentProcessorTypes)) { | |
120 | CRM_Core_Error::fatal('This payment processor type is not registered.'); | |
121 | } | |
122 | ||
123 | $dao = new CRM_Financial_DAO_PaymentProcessor(); | |
124 | $dao->payment_processor_type_id = $paymentProcessorTypes[$info->key]; | |
125 | $dao->find(); | |
126 | while ($dao->fetch()) { | |
127 | throw new CRM_Extension_Exception_DependencyException('payment'); | |
128 | } | |
129 | ||
130 | $this->_runPaymentHook($info, 'uninstall'); | |
131 | return CRM_Financial_BAO_PaymentProcessorType::del($paymentProcessorTypes[$info->key]); | |
132 | } | |
133 | ||
134 | /** | |
135 | * {@inheritdoc} | |
136 | */ | |
137 | public function onPreDisable(CRM_Extension_Info $info) { | |
138 | // HMM? // if ($this->type == 'payment' && $this->status != 'missing') { | |
139 | $this->_runPaymentHook($info, 'disable'); | |
140 | ||
141 | $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name'); | |
142 | CRM_Financial_BAO_PaymentProcessorType::setIsActive($paymentProcessorTypes[$info->key], 0); | |
143 | } | |
144 | ||
145 | /** | |
146 | * {@inheritdoc} | |
147 | */ | |
148 | public function onPreEnable(CRM_Extension_Info $info) { | |
149 | $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name'); | |
150 | CRM_Financial_BAO_PaymentProcessorType::setIsActive($paymentProcessorTypes[$info->key], 1); | |
151 | } | |
152 | ||
153 | /** | |
154 | * {@inheritdoc} | |
155 | */ | |
156 | public function onPostEnable(CRM_Extension_Info $info) { | |
157 | // HMM? // if ($this->type == 'payment' && $this->status != 'missing') { | |
158 | $this->_runPaymentHook($info, 'enable'); | |
159 | } | |
160 | ||
161 | /** | |
162 | * @param string $attr the attribute used to key the array | |
163 | * @return array ($$attr => $id) | |
164 | */ | |
165 | private function _getAllPaymentProcessorTypes($attr) { | |
166 | $ppt = array(); | |
167 | $dao = new CRM_Financial_DAO_PaymentProcessorType(); | |
168 | $dao->find(); | |
169 | while ($dao->fetch()) { | |
170 | $ppt[$dao->$attr] = $dao->id; | |
171 | } | |
172 | return $ppt; | |
173 | } | |
174 | ||
175 | /** | |
176 | * Function to run hooks in the payment processor class | |
177 | * Load requested payment processor and call the method specified. | |
178 | * | |
179 | * @param string $method - the method to call in the payment processor class | |
180 | * @private | |
181 | */ | |
182 | private function _runPaymentHook(CRM_Extension_Info $info, $method) { | |
183 | // Not concerned about performance at this stage, as these are seldomly performed tasks | |
184 | // (payment processor enable/disable/install/uninstall). May wish to implement some | |
185 | // kind of registry/caching system if more hooks are added. | |
186 | ||
187 | try { | |
188 | $paymentClass = $this->mapper->keyToClass($info->key, 'payment'); | |
189 | $file = $this->mapper->classToPath($paymentClass); | |
190 | if (! file_exists($file)) { | |
191 | CRM_Core_Session::setStatus(ts('Failed to load file (%3) for payment processor (%1) while running "%2"', array(1 => $info->key, 2 => $method, 3 => $file)), '', 'error'); | |
192 | return; | |
193 | } else { | |
194 | require_once $file; | |
195 | } | |
196 | } catch (CRM_Extension_Exception $e) { | |
197 | CRM_Core_Session::setStatus(ts('Failed to determine file path for payment processor (%1) while running "%2"', array(1 => $info->key, 2 => $method)), '', 'error'); | |
198 | return; | |
199 | } | |
200 | ||
201 | // See if we have any instances of this PP defined .. | |
202 | if ($processor_id = CRM_Core_DAO::singleValueQuery(" | |
203 | SELECT pp.id | |
204 | FROM civicrm_extension ext | |
205 | INNER JOIN civicrm_payment_processor_type ppt | |
206 | ON ext.name = ppt.name | |
207 | INNER JOIN civicrm_payment_processor pp | |
208 | ON ppt.id = pp.payment_processor_type_id | |
209 | WHERE ext.type = 'payment' | |
210 | AND ext.full_name = %1 | |
211 | ", | |
212 | array( | |
213 | 1 => array($info->key, 'String'), | |
214 | ) | |
215 | )) { | |
216 | // If so, load params in the usual way .. | |
217 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($processor_id, NULL); | |
218 | } | |
219 | else { | |
220 | // Otherwise, do the best we can to construct some .. | |
221 | $dao = CRM_Core_DAO::executeQuery(" | |
222 | SELECT ppt.* | |
223 | FROM civicrm_extension ext | |
224 | INNER JOIN civicrm_payment_processor_type ppt | |
225 | ON ppt.name = ext.name | |
226 | WHERE ext.name = %1 | |
227 | AND ext.type = 'payment' | |
228 | ", | |
229 | array( | |
230 | 1 => array($info->name, 'String'), | |
231 | ) | |
232 | ); | |
233 | if ($dao->fetch()) $paymentProcessor = array( | |
234 | 'id' => -1, | |
235 | 'name' => $dao->title, | |
236 | 'payment_processor_type_id' => $dao->id, | |
237 | 'user_name' => 'nothing', | |
238 | 'password' => 'nothing', | |
239 | 'signature' => '', | |
240 | 'url_site' => $dao->url_site_default, | |
241 | 'url_api' => $dao->url_api_default, | |
242 | 'url_recur' => $dao->url_recur_default, | |
243 | 'url_button' => $dao->url_button_default, | |
244 | 'subject' => '', | |
245 | 'class_name' => $dao->class_name, | |
246 | 'is_recur' => $dao->is_recur, | |
247 | 'billing_mode' => $dao->billing_mode, | |
248 | 'payment_type' => $dao->payment_type, | |
249 | ); | |
250 | else CRM_Core_Error::fatal("Unable to find payment processor in " . __CLASS__ . '::' . __METHOD__); | |
251 | } | |
252 | ||
253 | // In the case of uninstall, check for instances of PP first. | |
254 | // Don't run hook if any are found. | |
255 | if ($method == 'uninstall' && $paymentProcessor['id'] > 0) { | |
256 | return; | |
257 | } | |
258 | ||
259 | switch ($method) { | |
260 | case 'install': | |
261 | case 'uninstall': | |
262 | case 'enable': | |
263 | case 'disable': | |
264 | ||
265 | // Instantiate PP | |
0e6e8724 | 266 | $processorInstance = $paymentClass::singleton(NULL, $paymentProcessor); |
6a488035 TO |
267 | |
268 | // Does PP implement this method, and can we call it? | |
269 | if (method_exists($processorInstance, $method) && is_callable(array( | |
270 | $processorInstance, $method))) { | |
271 | // If so, call it ... | |
272 | $processorInstance->$method(); | |
273 | } | |
274 | break; | |
275 | ||
276 | default: | |
277 | CRM_Core_Session::setStatus(ts( "Unrecognized payment hook (%1) in %2::%3", | |
278 | array(1 => $method, 2 => __CLASS__ , 3 => __METHOD__) ), | |
279 | '', 'error'); | |
280 | } | |
281 | } | |
282 | } |