3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
42 * static holder for the default payment processor
44 static $_defaultPaymentProcessor = NULL;
47 * Create Payment Processor
49 * @params array parameters for Processor entity
54 * @return CRM_Financial_DAO_PaymentProcessor
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);
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'));
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
;
75 // CRM-11826, add entry in civicrm_entity_financial_account
76 // if financial_account_id is not NULL
77 if (!empty($params['financial_account_id'])) {
78 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
80 'entity_table' => 'civicrm_payment_processor',
81 'entity_id' => $processor->id
,
82 'account_relationship' => $relationTypeId,
83 'financial_account_id' => $params['financial_account_id']
85 CRM_Financial_BAO_FinancialTypeAccount
::add($values);
93 function __construct() {
94 parent
::__construct();
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
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
105 * @return object CRM_Financial_DAO_PaymentProcessor object on success, null otherwise
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;
120 * update the is_active flag in the db
122 * @param int $id id of the database record
123 * @param boolean $is_active value we want to set the is_active field
125 * @return Object DAO object on sucess, null otherwise
130 static function setIsActive($id, $is_active) {
131 return CRM_Core_DAO
::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active);
135 * retrieve the default payment processor
139 * @return object The default payment processor object on success,
144 static function &getDefault() {
145 if (self
::$_defaultPaymentProcessor == NULL) {
146 $params = array('is_default' => 1);
148 self
::$_defaultPaymentProcessor = self
::retrieve($params, $defaults);
150 return self
::$_defaultPaymentProcessor;
154 * Function to delete payment processor
156 * @param $paymentProcessorID
159 * @internal param int $paymentProcessorId ID of the processor to be deleted.
164 static function del($paymentProcessorID) {
165 if (!$paymentProcessorID) {
166 CRM_Core_Error
::fatal(ts('Invalid value passed to delete function.'));
169 $dao = new CRM_Financial_DAO_PaymentProcessor();
170 $dao->id
= $paymentProcessorID;
171 if (!$dao->find(TRUE)) {
175 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
176 $testDAO->name
= $dao->name
;
177 $testDAO->is_test
= 1;
184 * Function to get the payment processor details
186 * @param int $paymentProcessorID payment processor id
187 * @param string $mode payment mode ie test or live
189 * @return array associated array with payment processor related fields
193 static function getPayment($paymentProcessorID, $mode) {
194 if (!$paymentProcessorID) {
195 CRM_Core_Error
::fatal(ts('Invalid value passed to getPayment function'));
198 $dao = new CRM_Financial_DAO_PaymentProcessor( );
199 $dao->id
= $paymentProcessorID;
201 if (!$dao->find(TRUE)) {
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'));
213 return self
::buildPayment($testDAO, $mode);
216 return self
::buildPayment($dao, $mode);
221 * @param $paymentProcessorIDs
227 static function getPayments($paymentProcessorIDs, $mode) {
228 if (!$paymentProcessorIDs) {
229 CRM_Core_Error
::fatal(ts('Invalid value passed to getPayment function'));
232 $payments = array( );
233 foreach ($paymentProcessorIDs as $paymentProcessorID) {
234 $payment = self
::getPayment($paymentProcessorID, $mode);
235 $payments[$payment['id']] = $payment;
238 uasort($payments, 'self::defaultComparison');
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
249 static function defaultComparison($processor1, $processor2){
250 $p1 = CRM_Utils_Array
::value('is_default', $processor1);
251 $p2 = CRM_Utils_Array
::value('is_default', $processor2);
255 return ($p1 > $p2) ?
-1 : 1;
259 * Function to build payment processor details
261 * @param object $dao payment processor object
262 * @param string $mode payment mode ie test or live
264 * @return array associated array with payment processor related fields
268 static function buildPayment($dao, $mode) {
270 'id', 'name', 'payment_processor_type_id', 'user_name', 'password',
271 'signature', 'url_site', 'url_api', 'url_recur', 'url_button',
272 'subject', 'class_name', 'is_recur', 'billing_mode', 'is_test',
273 'payment_type', 'is_default',
276 foreach ($fields as $name) {
277 $result[$name] = $dao->$name;
279 $result['payment_processor_type'] = CRM_Core_PseudoConstant
::paymentProcessorType(FALSE, $dao->payment_processor_type_id
, 'name');
281 $result['instance'] = $result['object'] =& CRM_Core_Payment
::singleton($mode, $result);
287 * get all payment processors as an array of objects.
289 * @param $isExcludeTest
292 * @throws CiviCRM_API3_Exception
295 static function getAllPaymentProcessors($isExcludeTest, $reset = FALSE) {
297 * $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . ($isExcludeTest ? 'test' : 'all');
299 $processors = CRM_Utils_Cache::singleton()->get($cacheKey);
300 if (!empty($processors)) {
305 $retrievalParameters = array('is_active' => TRUE, 'options' => array('sort' => 'is_default DESC, name'), 'api.payment_processor_type.getsingle' => 1);
306 if ($isExcludeTest) {
307 $retrievalParameters['is_test'] = 0;
309 $processors = civicrm_api3('payment_processor', 'get', $retrievalParameters);
310 foreach ($processors['values'] as $processor) {
311 $processors['values'][$processor['id']]['payment_processor_type'] = $processor['payment_processor_type'] = $processors['values'][$processor['id']]['api.payment_processor_type.getsingle']['name'];
312 $processors['values'][$processor['id']]['object'] = CRM_Core_Payment
::singleton(empty($processor['is_test']) ?
'live' : 'test', $processor);
315 CRM_Utils_Cache::singleton()->set($cacheKey, $processors);
317 return $processors['values'];
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
325 * @param array $capabilities
326 * @param bool $isIncludeTest
330 * @return array available processors
332 static function getPaymentProcessors($capabilities = array(), $isIncludeTest = FALSE, $ids = array()) {
333 $processors = self
::getAllPaymentProcessors(!$isIncludeTest);
335 foreach ($processors as $index => $processor) {
336 if (!empty($ids) && !in_array($processor['id'], $ids)) {
337 unset ($processors[$index]);
340 if (($error = $processor['object']->checkConfig()) != NULL) {
341 unset ($processors[$index]);
344 foreach ($capabilities as $capability) {
345 if (($processor['object']->supports($capability)) == FALSE) {
346 unset ($processors[$index]);
355 * Is there a processor on this site with the specified capability
356 * @param array $capabilities
357 * @param bool $isIncludeTest
361 static function hasPaymentProcessorSupporting($capabilities = array(), $isIncludeTest = FALSE) {
362 $result = self
::getPaymentProcessors($capabilities, $isIncludeTest);
363 return (!empty($result)) ?
TRUE : FALSE;
367 * Function to retrieve payment processor id / info/ object based on component-id.
370 * @param string $component component
371 * @param string $type type of payment information to be retrieved
373 * @internal param int $componentID id of a component
374 * @return id / array / object based on type
378 static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') {
380 if (!in_array($component, array(
381 'membership', 'contribute', 'recur'))) {
385 if ($component == 'membership') {
387 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
388 FROM civicrm_membership mem
389 INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id )
390 INNER 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";
395 elseif ($component == 'contribute') {
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 )
403 elseif ($component == 'recur') {
405 SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test
406 FROM civicrm_contribution_recur cr
410 //we are interesting in single record.
413 $params = array(1 => array($entityID, 'Integer'));
414 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
416 if (!$dao->fetch()) {
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') {
427 elseif ($type == 'info') {
428 $result = self
::getPayment($ppID, $mode);
430 elseif ($type == 'obj') {
431 $payment = self
::getPayment($ppID, $mode);
432 $result = CRM_Core_Payment
::singleton($mode, $payment);