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
51 static function create($params) {
52 // FIXME Reconcile with CRM_Admin_Form_PaymentProcessor::updatePaymentProcessor
53 $processor = new CRM_Financial_DAO_PaymentProcessor();
54 $processor->copyValues($params);
56 $ppTypeDAO = new CRM_Financial_DAO_PaymentProcessorType();
57 $ppTypeDAO->id
= $params['payment_processor_type_id'];
58 if (!$ppTypeDAO->find(TRUE)) {
59 CRM_Core_Error
::fatal(ts('Could not find payment processor meta information'));
62 // also copy meta fields from the info DAO
63 $processor->is_recur
= $ppTypeDAO->is_recur
;
64 $processor->billing_mode
= $ppTypeDAO->billing_mode
;
65 $processor->class_name
= $ppTypeDAO->class_name
;
66 $processor->payment_type
= $ppTypeDAO->payment_type
;
69 // CRM-11826, add entry in civicrm_entity_financial_account
70 // if financial_account_id is not NULL
71 if (!empty($params['financial_account_id'])) {
72 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
74 'entity_table' => 'civicrm_payment_processor',
75 'entity_id' => $processor->id
,
76 'account_relationship' => $relationTypeId,
77 'financial_account_id' => $params['financial_account_id']
79 CRM_Financial_BAO_FinancialTypeAccount
::add($values);
87 function __construct() {
88 parent
::__construct();
92 * Takes a bunch of params that are needed to match certain criteria and
93 * retrieves the relevant objects. It also stores all the retrieved
94 * values in the default array
96 * @param array $params (reference ) an assoc array of name/value pairs
97 * @param array $defaults (reference ) an assoc array to hold the flattened values
99 * @return object CRM_Financial_DAO_PaymentProcessor object on success, null otherwise
103 static function retrieve(&$params, &$defaults) {
104 $paymentProcessor = new CRM_Financial_DAO_PaymentProcessor();
105 $paymentProcessor->copyValues($params);
106 if ($paymentProcessor->find(TRUE)) {
107 CRM_Core_DAO
::storeValues($paymentProcessor, $defaults);
108 return $paymentProcessor;
114 * update the is_active flag in the db
116 * @param int $id id of the database record
117 * @param boolean $is_active value we want to set the is_active field
119 * @return Object DAO object on sucess, null otherwise
124 static function setIsActive($id, $is_active) {
125 return CRM_Core_DAO
::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active);
129 * retrieve the default payment processor
133 * @return object The default payment processor object on success,
138 static function &getDefault() {
139 if (self
::$_defaultPaymentProcessor == NULL) {
140 $params = array('is_default' => 1);
142 self
::$_defaultPaymentProcessor = self
::retrieve($params, $defaults);
144 return self
::$_defaultPaymentProcessor;
148 * Function to delete payment processor
150 * @param $paymentProcessorID
153 * @internal param int $paymentProcessorId ID of the processor to be deleted.
158 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 * Function to get the payment processor details
180 * @param int $paymentProcessorID payment processor id
181 * @param string $mode payment mode ie test or live
183 * @return array associated array with payment processor related fields
187 static function getPayment($paymentProcessorID, $mode) {
188 if (!$paymentProcessorID) {
189 CRM_Core_Error
::fatal(ts('Invalid value passed to getPayment function'));
192 $dao = new CRM_Financial_DAO_PaymentProcessor( );
193 $dao->id
= $paymentProcessorID;
195 if (!$dao->find(TRUE)) {
199 if ($mode == 'test') {
200 $testDAO = new CRM_Financial_DAO_PaymentProcessor( );
201 $testDAO->name
= $dao->name
;
202 $testDAO->is_active
= 1;
203 $testDAO->is_test
= 1;
204 if (!$testDAO->find(TRUE)) {
205 CRM_Core_Error
::fatal(ts('Could not retrieve payment processor details'));
207 return self
::buildPayment($testDAO, $mode);
210 return self
::buildPayment($dao, $mode);
214 static function getPayments($paymentProcessorIDs, $mode) {
215 if (!$paymentProcessorIDs) {
216 CRM_Core_Error
::fatal(ts('Invalid value passed to getPayment function'));
219 $payments = array( );
220 foreach ($paymentProcessorIDs as $paymentProcessorID) {
221 $payment = self
::getPayment($paymentProcessorID, $mode);
222 $payments[$payment['id']] = $payment;
225 uasort($payments, 'self::defaultComparison');
230 * compare 2 payment processors to see which should go first based on is_default
231 * (sort function for sortDefaultFirst)
232 * @param array $processor1
233 * @param array_type $processor2
236 static function defaultComparison($processor1, $processor2){
237 $p1 = CRM_Utils_Array
::value('is_default', $processor1);
238 $p2 = CRM_Utils_Array
::value('is_default', $processor2);
242 return ($p1 > $p2) ?
-1 : 1;
246 * Function to build payment processor details
248 * @param object $dao payment processor object
249 * @param string $mode payment mode ie test or live
251 * @return array associated array with payment processor related fields
255 static function buildPayment($dao, $mode) {
257 'id', 'name', 'payment_processor_type_id', 'user_name', 'password',
258 'signature', 'url_site', 'url_api', 'url_recur', 'url_button',
259 'subject', 'class_name', 'is_recur', 'billing_mode',
260 'payment_type', 'is_default',
263 foreach ($fields as $name) {
264 $result[$name] = $dao->$name;
266 $result['payment_processor_type'] = CRM_Core_PseudoConstant
::paymentProcessorType(FALSE, $dao->payment_processor_type_id
, 'name');
268 $result['instance'] =& CRM_Core_Payment
::singleton($mode, $result);
274 * Function to retrieve payment processor id / info/ object based on component-id.
277 * @param string $component component
278 * @param string $type type of payment information to be retrieved
280 * @internal param int $componentID id of a component
281 * @return id / array / object based on type
285 static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') {
287 if (!in_array($component, array(
288 'membership', 'contribute', 'recur'))) {
292 if ($component == 'membership') {
294 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
295 FROM civicrm_membership mem
296 INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id )
297 INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id )
298 LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id )
299 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
300 WHERE mp.membership_id = %1";
302 elseif ($component == 'contribute') {
304 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
305 FROM civicrm_contribution con
306 LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id )
307 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
310 elseif ($component == 'recur') {
312 SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test
313 FROM civicrm_contribution_recur cr
317 //we are interesting in single record.
320 $params = array(1 => array($entityID, 'Integer'));
321 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
323 if (!$dao->fetch()) {
329 $ppID = (isset($dao->ppID1
) && $dao->ppID1
) ?
$dao->ppID1
: (isset($dao->ppID2
) ?
$dao->ppID2
: NULL);
330 $mode = (isset($dao->is_test
) && $dao->is_test
) ?
'test' : 'live';
331 if (!$ppID ||
$type == 'id') {
334 elseif ($type == 'info') {
335 $result = self
::getPayment($ppID, $mode);
337 elseif ($type == 'obj') {
338 $payment = self
::getPayment($ppID, $mode);
339 $result = CRM_Core_Payment
::singleton($mode, $payment);