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