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',
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'] =& CRM_Core_Payment
::singleton($mode, $result);
287 * Function to retrieve payment processor id / info/ object based on component-id.
290 * @param string $component component
291 * @param string $type type of payment information to be retrieved
293 * @internal param int $componentID id of a component
294 * @return id / array / object based on type
298 static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') {
300 if (!in_array($component, array(
301 'membership', 'contribute', 'recur'))) {
305 if ($component == 'membership') {
307 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
308 FROM civicrm_membership mem
309 INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id )
310 INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id )
311 LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id )
312 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
313 WHERE mp.membership_id = %1";
315 elseif ($component == 'contribute') {
317 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
318 FROM civicrm_contribution con
319 LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id )
320 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
323 elseif ($component == 'recur') {
325 SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test
326 FROM civicrm_contribution_recur cr
330 //we are interesting in single record.
333 $params = array(1 => array($entityID, 'Integer'));
334 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
336 if (!$dao->fetch()) {
342 $ppID = (isset($dao->ppID1
) && $dao->ppID1
) ?
$dao->ppID1
: (isset($dao->ppID2
) ?
$dao->ppID2
: NULL);
343 $mode = (isset($dao->is_test
) && $dao->is_test
) ?
'test' : 'live';
344 if (!$ppID ||
$type == 'id') {
347 elseif ($type == 'info') {
348 $result = self
::getPayment($ppID, $mode);
350 elseif ($type == 'obj') {
351 $payment = self
::getPayment($ppID, $mode);
352 $result = CRM_Core_Payment
::singleton($mode, $payment);