CRM-15555 declare processor_type before calling singleton
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessor.php
CommitLineData
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 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class contains payment processor related functions.
38 */
39class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProcessor
40{
41 /**
42 * static holder for the default payment processor
43 */
44 static $_defaultPaymentProcessor = NULL;
45
46 /*
47 * Create Payment Processor
48 *
49 * @params array parameters for Processor entity
50 */
e0ef6999
EM
51 /**
52 * @param $params
53 *
54 * @return CRM_Financial_DAO_PaymentProcessor
55 * @throws Exception
56 */
6a488035
TO
57 static function create($params) {
58 // FIXME Reconcile with CRM_Admin_Form_PaymentProcessor::updatePaymentProcessor
59 $processor = new CRM_Financial_DAO_PaymentProcessor();
60 $processor->copyValues($params);
61
62 $ppTypeDAO = new CRM_Financial_DAO_PaymentProcessorType();
63 $ppTypeDAO->id = $params['payment_processor_type_id'];
64 if (!$ppTypeDAO->find(TRUE)) {
65 CRM_Core_Error::fatal(ts('Could not find payment processor meta information'));
66 }
67
68 // also copy meta fields from the info DAO
69 $processor->is_recur = $ppTypeDAO->is_recur;
70 $processor->billing_mode = $ppTypeDAO->billing_mode;
71 $processor->class_name = $ppTypeDAO->class_name;
72 $processor->payment_type = $ppTypeDAO->payment_type;
73
74 $processor->save();
03e04002 75 // CRM-11826, add entry in civicrm_entity_financial_account
6a488035 76 // if financial_account_id is not NULL
a7488080 77 if (!empty($params['financial_account_id'])) {
f743a6eb 78 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
6a488035
TO
79 $values = array(
80 'entity_table' => 'civicrm_payment_processor',
81 'entity_id' => $processor->id,
82 'account_relationship' => $relationTypeId,
83 'financial_account_id' => $params['financial_account_id']
84 );
03e04002 85 CRM_Financial_BAO_FinancialTypeAccount::add($values);
6a488035
TO
86 }
87 return $processor;
88 }
89
90 /**
91 * class constructor
92 */
93 function __construct() {
94 parent::__construct();
95 }
96
97 /**
98 * Takes a bunch of params that are needed to match certain criteria and
99 * retrieves the relevant objects. It also stores all the retrieved
100 * values in the default array
101 *
102 * @param array $params (reference ) an assoc array of name/value pairs
103 * @param array $defaults (reference ) an assoc array to hold the flattened values
104 *
105 * @return object CRM_Financial_DAO_PaymentProcessor object on success, null otherwise
106 * @access public
107 * @static
108 */
109 static function retrieve(&$params, &$defaults) {
110 $paymentProcessor = new CRM_Financial_DAO_PaymentProcessor();
111 $paymentProcessor->copyValues($params);
112 if ($paymentProcessor->find(TRUE)) {
113 CRM_Core_DAO::storeValues($paymentProcessor, $defaults);
114 return $paymentProcessor;
115 }
116 return NULL;
117 }
118
119 /**
120 * update the is_active flag in the db
121 *
122 * @param int $id id of the database record
123 * @param boolean $is_active value we want to set the is_active field
124 *
125 * @return Object DAO object on sucess, null otherwise
126 *
127 * @access public
128 * @static
129 */
130 static function setIsActive($id, $is_active) {
131 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active);
132 }
133
134 /**
135 * retrieve the default payment processor
136 *
137 * @param NULL
138 *
139 * @return object The default payment processor object on success,
140 * null otherwise
141 * @static
142 * @access public
143 */
144 static function &getDefault() {
145 if (self::$_defaultPaymentProcessor == NULL) {
146 $params = array('is_default' => 1);
147 $defaults = array();
148 self::$_defaultPaymentProcessor = self::retrieve($params, $defaults);
149 }
150 return self::$_defaultPaymentProcessor;
151 }
152
153 /**
154 * Function to delete payment processor
155 *
77b97be7
EM
156 * @param $paymentProcessorID
157 *
158 * @return null
159 * @internal param int $paymentProcessorId ID of the processor to be deleted.
6a488035
TO
160 *
161 * @access public
162 * @static
163 */
164 static function del($paymentProcessorID) {
165 if (!$paymentProcessorID) {
166 CRM_Core_Error::fatal(ts('Invalid value passed to delete function'));
167 }
168
169 $dao = new CRM_Financial_DAO_PaymentProcessor();
170 $dao->id = $paymentProcessorID;
171 if (!$dao->find(TRUE)) {
172 return NULL;
173 }
174
175 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
176 $testDAO->name = $dao->name;
177 $testDAO->is_test = 1;
178 $testDAO->delete();
179
180 $dao->delete();
181 }
182
183 /**
184 * Function to get the payment processor details
185 *
186 * @param int $paymentProcessorID payment processor id
187 * @param string $mode payment mode ie test or live
188 *
189 * @return array associated array with payment processor related fields
190 * @static
191 * @access public
192 */
193 static function getPayment($paymentProcessorID, $mode) {
194 if (!$paymentProcessorID) {
195 CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function'));
196 }
197
198 $dao = new CRM_Financial_DAO_PaymentProcessor( );
199 $dao->id = $paymentProcessorID;
200 $dao->is_active = 1;
201 if (!$dao->find(TRUE)) {
202 return NULL;
203 }
204
205 if ($mode == 'test') {
206 $testDAO = new CRM_Financial_DAO_PaymentProcessor( );
207 $testDAO->name = $dao->name;
208 $testDAO->is_active = 1;
209 $testDAO->is_test = 1;
210 if (!$testDAO->find(TRUE)) {
211 CRM_Core_Error::fatal(ts('Could not retrieve payment processor details'));
212 }
213 return self::buildPayment($testDAO, $mode);
214 }
215 else {
216 return self::buildPayment($dao, $mode);
217 }
218 }
219
e0ef6999
EM
220 /**
221 * @param $paymentProcessorIDs
222 * @param $mode
223 *
224 * @return array
225 * @throws Exception
226 */
6a488035
TO
227 static function getPayments($paymentProcessorIDs, $mode) {
228 if (!$paymentProcessorIDs) {
229 CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function'));
230 }
231
232 $payments = array( );
233 foreach ($paymentProcessorIDs as $paymentProcessorID) {
234 $payment = self::getPayment($paymentProcessorID, $mode);
235 $payments[$payment['id']] = $payment;
236 }
237
13ac605f 238 uasort($payments, 'self::defaultComparison');
6a488035
TO
239 return $payments;
240 }
241
13ac605f
DG
242 /**
243 * compare 2 payment processors to see which should go first based on is_default
244 * (sort function for sortDefaultFirst)
245 * @param array $processor1
246 * @param array_type $processor2
247 * @return number
248 */
249 static function defaultComparison($processor1, $processor2){
250 $p1 = CRM_Utils_Array::value('is_default', $processor1);
251 $p2 = CRM_Utils_Array::value('is_default', $processor2);
252 if ($p1 == $p2) {
253 return 0;
254 }
255 return ($p1 > $p2) ? -1 : 1;
256 }
257
6a488035
TO
258 /**
259 * Function to build payment processor details
260 *
261 * @param object $dao payment processor object
262 * @param string $mode payment mode ie test or live
263 *
264 * @return array associated array with payment processor related fields
265 * @static
266 * @access public
267 */
268 static function buildPayment($dao, $mode) {
269 $fields = array(
270 'id', 'name', 'payment_processor_type_id', 'user_name', 'password',
271 'signature', 'url_site', 'url_api', 'url_recur', 'url_button',
3392cb17 272 'subject', 'class_name', 'is_recur', 'billing_mode', 'is_test',
6a488035
TO
273 'payment_type', 'is_default',
274 );
275 $result = array();
276 foreach ($fields as $name) {
277 $result[$name] = $dao->$name;
278 }
279 $result['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, $dao->payment_processor_type_id, 'name');
280
3392cb17 281 $result['instance'] = $result['object'] =& CRM_Core_Payment::singleton($mode, $result);
6a488035
TO
282
283 return $result;
284 }
285
fbcb6fba
EM
286 /**
287 * get all payment processors as an array of objects.
288 *
289 * @param $isExcludeTest
290 * @param bool $reset
291 *
292 * @throws CiviCRM_API3_Exception
293 * @return array
294 */
295 static function getAllPaymentProcessors($isExcludeTest, $reset = FALSE) {
296 /**
297 * $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . ($isExcludeTest ? 'test' : 'all');
298 if (!$reset) {
299 $processors = CRM_Utils_Cache::singleton()->get($cacheKey);
300 if (!empty($processors)) {
301 return $processors;
302 }
303 }
304 * */
ef523266 305 $retrievalParameters = array('is_active' => TRUE, 'options' => array('sort' => 'is_default DESC, name'), 'api.payment_processor_type.getsingle' => 1);
fbcb6fba
EM
306 if ($isExcludeTest) {
307 $retrievalParameters['is_test'] = 0;
308 }
309 $processors = civicrm_api3('payment_processor', 'get', $retrievalParameters);
310 foreach ($processors['values'] as $processor) {
9d91a9c6 311 $processors['values'][$processor['id']]['payment_processor_type'] = $processor['payment_processor_type'] = $processors['values'][$processor['id']]['api.payment_processor_type.getsingle']['name'];
fbcb6fba
EM
312 $processors['values'][$processor['id']]['object'] = CRM_Core_Payment::singleton(empty($processor['is_test']) ? 'live' : 'test', $processor);
313 }
314 /*
315 CRM_Utils_Cache::singleton()->set($cacheKey, $processors);
316 */
317 return $processors['values'];
318 }
319
320 /**
321 * get Payment processors with specified capabilities.
322 * Note that both the singleton & the pseudoconstant function have caching so we don't add
323 * arguably this could go on the pseudoconstant class
324 *
325 * @param array $capabilities
326 * @param bool $isIncludeTest
327 *
dc913073
EM
328 * @param array $ids
329 *
fbcb6fba
EM
330 * @return array available processors
331 */
dc913073 332 static function getPaymentProcessors($capabilities = array(), $isIncludeTest = FALSE, $ids = array()) {
fbcb6fba
EM
333 $processors = self::getAllPaymentProcessors(!$isIncludeTest);
334 if ($capabilities) {
335 foreach ($processors as $index => $processor) {
dc913073
EM
336 if (!empty($ids) && !in_array($processor['id'], $ids)) {
337 unset ($processors[$index]);
338 continue;
339 }
fbcb6fba
EM
340 if (($error = $processor['object']->checkConfig()) != NULL) {
341 unset ($processors[$index]);
342 continue;
343 }
344 foreach ($capabilities as $capability) {
d8ce0d68 345 if (($processor['object']->supports($capability)) == FALSE) {
fbcb6fba
EM
346 unset ($processors[$index]);
347 }
348 }
349 }
350 }
351 return $processors;
352 }
353
354 /**
355 * Is there a processor on this site with the specified capability
356 * @param array $capabilities
357 * @param bool $isIncludeTest
358 *
359 * @return bool
360 */
361 static function hasPaymentProcessorSupporting($capabilities = array(), $isIncludeTest = FALSE) {
362 $result = self::getPaymentProcessors($capabilities, $isIncludeTest);
363 return (!empty($result)) ? TRUE : FALSE;
364 }
365
6a488035
TO
366 /**
367 * Function to retrieve payment processor id / info/ object based on component-id.
368 *
da6b46f4
EM
369 * @param $entityID
370 * @param string $component component
371 * @param string $type type of payment information to be retrieved
6a488035 372 *
da6b46f4 373 * @internal param int $componentID id of a component
6a488035
TO
374 * @return id / array / object based on type
375 * @static
376 * @access public
377 */
378 static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') {
379 $result = NULL;
380 if (!in_array($component, array(
381 'membership', 'contribute', 'recur'))) {
382 return $result;
383 }
384 //FIXME:
385 if ($component == 'membership') {
386 $sql = "
387 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
388 FROM civicrm_membership mem
389INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id )
390INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id )
391 LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id )
392 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
393 WHERE mp.membership_id = %1";
394 }
395 elseif ($component == 'contribute') {
396 $sql = "
397 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
398 FROM civicrm_contribution con
399 LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id )
400 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
401 WHERE con.id = %1";
402 }
403 elseif ($component == 'recur') {
404 $sql = "
405 SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test
406 FROM civicrm_contribution_recur cr
407 WHERE cr.id = %1";
408 }
409
410 //we are interesting in single record.
411 $sql .= ' LIMIT 1';
412
413 $params = array(1 => array($entityID, 'Integer'));
414 $dao = CRM_Core_DAO::executeQuery($sql, $params);
415
416 if (!$dao->fetch()) {
417
418 return $result;
419
420 }
421
422 $ppID = (isset($dao->ppID1) && $dao->ppID1) ? $dao->ppID1 : (isset($dao->ppID2) ? $dao->ppID2 : NULL);
423 $mode = (isset($dao->is_test) && $dao->is_test) ? 'test' : 'live';
424 if (!$ppID || $type == 'id') {
425 $result = $ppID;
426 }
427 elseif ($type == 'info') {
428 $result = self::getPayment($ppID, $mode);
429 }
430 elseif ($type == 'obj') {
431 $payment = self::getPayment($ppID, $mode);
432 $result = CRM_Core_Payment::singleton($mode, $payment);
433 }
434
435 return $result;
436 }
437}
438