3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class contains payment processor related functions.
39 class CRM_Financial_BAO_PaymentProcessor
extends CRM_Financial_DAO_PaymentProcessor
{
41 * Static holder for the default payment processor
43 static $_defaultPaymentProcessor = NULL;
46 * Create Payment Processor
48 * @param array $params
49 * Parameters for Processor entity.
51 * @return CRM_Financial_DAO_PaymentProcessor
54 public static function create($params) {
55 // FIXME Reconcile with CRM_Admin_Form_PaymentProcessor::updatePaymentProcessor
56 $processor = new CRM_Financial_DAO_PaymentProcessor();
57 $processor->copyValues($params);
59 $ppTypeDAO = new CRM_Financial_DAO_PaymentProcessorType();
60 $ppTypeDAO->id
= $params['payment_processor_type_id'];
61 if (!$ppTypeDAO->find(TRUE)) {
62 CRM_Core_Error
::fatal(ts('Could not find payment processor meta information'));
65 // also copy meta fields from the info DAO
66 $processor->is_recur
= $ppTypeDAO->is_recur
;
67 $processor->billing_mode
= $ppTypeDAO->billing_mode
;
68 $processor->class_name
= $ppTypeDAO->class_name
;
69 $processor->payment_type
= $ppTypeDAO->payment_type
;
72 // CRM-11826, add entry in civicrm_entity_financial_account
73 // if financial_account_id is not NULL
74 if (!empty($params['financial_account_id'])) {
75 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
77 'entity_table' => 'civicrm_payment_processor',
78 'entity_id' => $processor->id
,
79 'account_relationship' => $relationTypeId,
80 'financial_account_id' => $params['financial_account_id'],
82 CRM_Financial_BAO_FinancialTypeAccount
::add($values);
90 public function __construct() {
91 parent
::__construct();
95 * Takes a bunch of params that are needed to match certain criteria and
96 * retrieves the relevant objects. It also stores all the retrieved
97 * values in the default array
99 * @param array $params
100 * (reference ) an assoc array of name/value pairs.
101 * @param array $defaults
102 * (reference ) an assoc array to hold the flattened values.
104 * @return CRM_Financial_DAO_PaymentProcessor|null
105 * object on success, null otherwise
107 public static function retrieve(&$params, &$defaults) {
108 $paymentProcessor = new CRM_Financial_DAO_PaymentProcessor();
109 $paymentProcessor->copyValues($params);
110 if ($paymentProcessor->find(TRUE)) {
111 CRM_Core_DAO
::storeValues($paymentProcessor, $defaults);
112 return $paymentProcessor;
118 * Update the is_active flag in the db
121 * Id of the database record.
122 * @param bool $is_active
123 * Value we want to set the is_active field.
125 * @return CRM_Financial_DAO_PaymentProcessor|null
126 * DAO object on success, null otherwise
129 public static function setIsActive($id, $is_active) {
130 return CRM_Core_DAO
::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active);
134 * Retrieve the default payment processor
138 * @return CRM_Financial_DAO_PaymentProcessor|null
139 * The default payment processor object on success,
142 public static function &getDefault() {
143 if (self
::$_defaultPaymentProcessor == NULL) {
144 $params = array('is_default' => 1);
146 self
::$_defaultPaymentProcessor = self
::retrieve($params, $defaults);
148 return self
::$_defaultPaymentProcessor;
152 * Delete payment processor
154 * @param int $paymentProcessorID
158 public static function del($paymentProcessorID) {
159 if (!$paymentProcessorID) {
160 CRM_Core_Error
::fatal(ts('Invalid value passed to delete function.'));
163 $dao = new CRM_Financial_DAO_PaymentProcessor();
164 $dao->id
= $paymentProcessorID;
165 if (!$dao->find(TRUE)) {
169 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
170 $testDAO->name
= $dao->name
;
171 $testDAO->is_test
= 1;
178 * Get the payment processor details
180 * @param int $paymentProcessorID
181 * Payment processor id.
182 * @param string $mode
183 * Payment mode ie test or live.
186 * associated array with payment processor related fields
188 public static function getPayment($paymentProcessorID, $mode) {
189 if (!$paymentProcessorID) {
190 CRM_Core_Error
::fatal(ts('Invalid value passed to getPayment function'));
193 $dao = new CRM_Financial_DAO_PaymentProcessor();
194 $dao->id
= $paymentProcessorID;
196 if (!$dao->find(TRUE)) {
200 if ($mode == 'test') {
201 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
202 $testDAO->name
= $dao->name
;
203 $testDAO->is_active
= 1;
204 $testDAO->is_test
= 1;
205 if (!$testDAO->find(TRUE)) {
206 CRM_Core_Error
::fatal(ts('Could not retrieve payment processor details'));
208 return self
::buildPayment($testDAO, $mode);
211 return self
::buildPayment($dao, $mode);
216 * @param $paymentProcessorIDs
222 public static function getPayments($paymentProcessorIDs, $mode) {
223 if (!$paymentProcessorIDs) {
224 CRM_Core_Error
::fatal(ts('Invalid value passed to getPayment function'));
228 foreach ($paymentProcessorIDs as $paymentProcessorID) {
229 $payment = self
::getPayment($paymentProcessorID, $mode);
230 $payments[$payment['id']] = $payment;
233 uasort($payments, 'self::defaultComparison');
238 * Compare 2 payment processors to see which should go first based on is_default
239 * (sort function for sortDefaultFirst)
240 * @param array $processor1
241 * @param array $processor2
244 public static function defaultComparison($processor1, $processor2) {
245 $p1 = CRM_Utils_Array
::value('is_default', $processor1);
246 $p2 = CRM_Utils_Array
::value('is_default', $processor2);
250 return ($p1 > $p2) ?
-1 : 1;
254 * Build payment processor details
257 * Payment processor object.
258 * @param string $mode
259 * Payment mode ie test or live.
262 * associated array with payment processor related fields
264 public static function buildPayment($dao, $mode) {
268 'payment_processor_type_id',
285 foreach ($fields as $name) {
286 $result[$name] = $dao->$name;
288 $result['payment_processor_type'] = CRM_Core_PseudoConstant
::paymentProcessorType(FALSE, $dao->payment_processor_type_id
, 'name');
290 $result['instance'] = $result['object'] =& CRM_Core_Payment
::singleton($mode, $result);
296 * Get all payment processors as an array of objects.
298 * @param string|NULL $mode
299 * only return this mode - test|live or NULL for all
302 * @throws CiviCRM_API3_Exception
305 public static function getAllPaymentProcessors($mode, $reset = FALSE) {
307 * $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . ($mode ? 'test' : 'all');
309 * $processors = CRM_Utils_Cache::singleton()->get($cacheKey);
310 * if (!empty($processors)) {
311 * return $processors;
315 $retrievalParameters = array(
317 'options' => array('sort' => 'is_default DESC, name'),
318 'api.payment_processor_type.getsingle' => 1
320 if ($mode == 'test') {
321 $retrievalParameters['is_test'] = 1;
323 elseif ($mode == 'live') {
324 $retrievalParameters['is_test'] = 0;
326 $processors = civicrm_api3('payment_processor', 'get', $retrievalParameters);
327 foreach ($processors['values'] as $processor) {
328 $fieldsToProvide = array('user_name', 'password', 'signature', 'subject');
329 foreach ($fieldsToProvide as $field) {
330 //prevent e-notices in processor classes when not configured
331 if (!isset($processor[$field])) {
332 $processor[$field] = NULL;
335 $processors['values'][$processor['id']]['payment_processor_type'] = $processor['payment_processor_type'] = $processors['values'][$processor['id']]['api.payment_processor_type.getsingle']['name'];
336 $mode = empty($processor['is_test']) ?
'live' : 'test';
337 $processors['values'][$processor['id']]['object'] = CRM_Core_Payment
::singleton($mode, $processor);
340 CRM_Utils_Cache::singleton()->set($cacheKey, $processors);
342 return $processors['values'];
346 * Get Payment processors with specified capabilities.
347 * Note that both the singleton & the pseudoconstant function have caching so we don't add
348 * arguably this could go on the pseudoconstant class
350 * @param array $capabilities
351 * capabilities of processor e.g
360 * available processors
362 public static function getPaymentProcessors($capabilities = array(), $ids = array()) {
364 if (in_array('TestMode', $capabilities)) {
367 elseif (in_array('LiveMode', $capabilities)) {
370 $processors = self
::getAllPaymentProcessors($mode);
372 foreach ($processors as $index => $processor) {
373 if (!empty($ids) && !in_array($processor['id'], $ids)) {
374 unset ($processors[$index]);
377 if (($error = $processor['object']->checkConfig()) != NULL) {
378 unset ($processors[$index]);
381 foreach ($capabilities as $capability) {
382 if (($processor['object']->supports($capability)) == FALSE) {
383 unset ($processors[$index]);
392 * Is there a processor on this site with the specified capability
393 * @param array $capabilities
394 * @param bool $isIncludeTest
398 public static function hasPaymentProcessorSupporting($capabilities = array(), $isIncludeTest = FALSE) {
399 $mode = $isIncludeTest ?
'Test' : 'Live';
400 $capabilities[] = $mode . 'Mode';
401 $result = self
::getPaymentProcessors($capabilities);
402 return (!empty($result)) ?
TRUE : FALSE;
406 * Retrieve payment processor id / info/ object based on component-id.
408 * @param int $entityID
409 * @param string $component
411 * @param string $type
412 * Type of payment information to be retrieved.
414 * @return int|array|object
416 public static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') {
418 if (!in_array($component, array(
427 if ($component == 'membership') {
429 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
430 FROM civicrm_membership mem
431 INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id )
432 INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id )
433 LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id )
434 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
435 WHERE mp.membership_id = %1";
437 elseif ($component == 'contribute') {
439 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
440 FROM civicrm_contribution con
441 LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id )
442 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
445 elseif ($component == 'recur') {
447 SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test
448 FROM civicrm_contribution_recur cr
452 //we are interesting in single record.
455 $params = array(1 => array($entityID, 'Integer'));
456 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
458 if (!$dao->fetch()) {
464 $ppID = (isset($dao->ppID1
) && $dao->ppID1
) ?
$dao->ppID1
: (isset($dao->ppID2
) ?
$dao->ppID2
: NULL);
465 $mode = (isset($dao->is_test
) && $dao->is_test
) ?
'test' : 'live';
466 if (!$ppID ||
$type == 'id') {
469 elseif ($type == 'info') {
470 $result = self
::getPayment($ppID, $mode);
472 elseif ($type == 'obj') {
473 $payment = self
::getPayment($ppID, $mode);
474 $result = CRM_Core_Payment
::singleton($mode, $payment);