fix a few comment blocks, minor tidy ups
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessor.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class contains payment processor related functions.
38 */
39class 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 * @params array parameters for Processor entity
50 */
e0ef6999
EM
51 /**
52 * @param $params
53 *
54 * @return CRM_Financial_DAO_PaymentProcessor
55 * @throws Exception
56 */
6a488035
TO
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);
61
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'));
66 }
67
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;
73
74 $processor->save();
03e04002 75 // CRM-11826, add entry in civicrm_entity_financial_account
6a488035 76 // if financial_account_id is not NULL
a7488080 77 if (!empty($params['financial_account_id'])) {
f743a6eb 78 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
6a488035
TO
79 $values = array(
80 'entity_table' => 'civicrm_payment_processor',
81 'entity_id' => $processor->id,
82 'account_relationship' => $relationTypeId,
83 'financial_account_id' => $params['financial_account_id']
84 );
03e04002 85 CRM_Financial_BAO_FinancialTypeAccount::add($values);
6a488035
TO
86 }
87 return $processor;
88 }
89
90 /**
91 * class constructor
92 */
93 function __construct() {
94 parent::__construct();
95 }
96
97 /**
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
101 *
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
104 *
105 * @return object CRM_Financial_DAO_PaymentProcessor object on success, null otherwise
106 * @access public
107 * @static
108 */
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;
115 }
116 return NULL;
117 }
118
119 /**
120 * update the is_active flag in the db
121 *
122 * @param int $id id of the database record
123 * @param boolean $is_active value we want to set the is_active field
124 *
125 * @return Object DAO object on sucess, null otherwise
126 *
127 * @access public
128 * @static
129 */
130 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 * @access public
143 */
144 static function &getDefault() {
145 if (self::$_defaultPaymentProcessor == NULL) {
146 $params = array('is_default' => 1);
147 $defaults = array();
148 self::$_defaultPaymentProcessor = self::retrieve($params, $defaults);
149 }
150 return self::$_defaultPaymentProcessor;
151 }
152
153 /**
154 * Function to delete payment processor
155 *
77b97be7
EM
156 * @param $paymentProcessorID
157 *
158 * @return null
159 * @internal param int $paymentProcessorId ID of the processor to be deleted.
6a488035
TO
160 *
161 * @access public
162 * @static
163 */
164 static function del($paymentProcessorID) {
165 if (!$paymentProcessorID) {
166 CRM_Core_Error::fatal(ts('Invalid value passed to delete function'));
167 }
168
169 $dao = new CRM_Financial_DAO_PaymentProcessor();
170 $dao->id = $paymentProcessorID;
171 if (!$dao->find(TRUE)) {
172 return NULL;
173 }
174
175 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
176 $testDAO->name = $dao->name;
177 $testDAO->is_test = 1;
178 $testDAO->delete();
179
180 $dao->delete();
181 }
182
183 /**
184 * Function to get the payment processor details
185 *
186 * @param int $paymentProcessorID payment processor id
187 * @param string $mode payment mode ie test or live
188 *
189 * @return array associated array with payment processor related fields
190 * @static
191 * @access public
192 */
193 static function getPayment($paymentProcessorID, $mode) {
194 if (!$paymentProcessorID) {
195 CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function'));
196 }
197
198 $dao = new CRM_Financial_DAO_PaymentProcessor( );
199 $dao->id = $paymentProcessorID;
200 $dao->is_active = 1;
201 if (!$dao->find(TRUE)) {
202 return NULL;
203 }
204
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'));
212 }
213 return self::buildPayment($testDAO, $mode);
214 }
215 else {
216 return self::buildPayment($dao, $mode);
217 }
218 }
219
e0ef6999
EM
220 /**
221 * @param $paymentProcessorIDs
222 * @param $mode
223 *
224 * @return array
225 * @throws Exception
226 */
6a488035
TO
227 static function getPayments($paymentProcessorIDs, $mode) {
228 if (!$paymentProcessorIDs) {
229 CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function'));
230 }
231
232 $payments = array( );
233 foreach ($paymentProcessorIDs as $paymentProcessorID) {
234 $payment = self::getPayment($paymentProcessorID, $mode);
235 $payments[$payment['id']] = $payment;
236 }
237
13ac605f 238 uasort($payments, 'self::defaultComparison');
6a488035
TO
239 return $payments;
240 }
241
13ac605f
DG
242 /**
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
247 * @return number
248 */
249 static function defaultComparison($processor1, $processor2){
250 $p1 = CRM_Utils_Array::value('is_default', $processor1);
251 $p2 = CRM_Utils_Array::value('is_default', $processor2);
252 if ($p1 == $p2) {
253 return 0;
254 }
255 return ($p1 > $p2) ? -1 : 1;
256 }
257
6a488035
TO
258 /**
259 * Function to build payment processor details
260 *
261 * @param object $dao payment processor object
262 * @param string $mode payment mode ie test or live
263 *
264 * @return array associated array with payment processor related fields
265 * @static
266 * @access public
267 */
268 static function buildPayment($dao, $mode) {
269 $fields = array(
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',
274 );
275 $result = array();
276 foreach ($fields as $name) {
277 $result[$name] = $dao->$name;
278 }
279 $result['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, $dao->payment_processor_type_id, 'name');
280
281 $result['instance'] =& CRM_Core_Payment::singleton($mode, $result);
282
283 return $result;
284 }
285
286 /**
287 * Function to retrieve payment processor id / info/ object based on component-id.
288 *
da6b46f4
EM
289 * @param $entityID
290 * @param string $component component
291 * @param string $type type of payment information to be retrieved
6a488035 292 *
da6b46f4 293 * @internal param int $componentID id of a component
6a488035
TO
294 * @return id / array / object based on type
295 * @static
296 * @access public
297 */
298 static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') {
299 $result = NULL;
300 if (!in_array($component, array(
301 'membership', 'contribute', 'recur'))) {
302 return $result;
303 }
304 //FIXME:
305 if ($component == 'membership') {
306 $sql = "
307 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
308 FROM civicrm_membership mem
309INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id )
310INNER 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";
314 }
315 elseif ($component == 'contribute') {
316 $sql = "
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 )
321 WHERE con.id = %1";
322 }
323 elseif ($component == 'recur') {
324 $sql = "
325 SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test
326 FROM civicrm_contribution_recur cr
327 WHERE cr.id = %1";
328 }
329
330 //we are interesting in single record.
331 $sql .= ' LIMIT 1';
332
333 $params = array(1 => array($entityID, 'Integer'));
334 $dao = CRM_Core_DAO::executeQuery($sql, $params);
335
336 if (!$dao->fetch()) {
337
338 return $result;
339
340 }
341
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') {
345 $result = $ppID;
346 }
347 elseif ($type == 'info') {
348 $result = self::getPayment($ppID, $mode);
349 }
350 elseif ($type == 'obj') {
351 $payment = self::getPayment($ppID, $mode);
352 $result = CRM_Core_Payment::singleton($mode, $payment);
353 }
354
355 return $result;
356 }
357}
358