Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class contains payment processor related functions. | |
38 | */ | |
39 | class 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 | */ | |
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); | |
55 | ||
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')); | |
60 | } | |
61 | ||
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; | |
67 | ||
68 | $processor->save(); | |
03e04002 | 69 | // CRM-11826, add entry in civicrm_entity_financial_account |
6a488035 TO |
70 | // if financial_account_id is not NULL |
71 | if (CRM_Utils_Array::value('financial_account_id', $params)) { | |
f743a6eb | 72 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); |
6a488035 TO |
73 | $values = array( |
74 | 'entity_table' => 'civicrm_payment_processor', | |
75 | 'entity_id' => $processor->id, | |
76 | 'account_relationship' => $relationTypeId, | |
77 | 'financial_account_id' => $params['financial_account_id'] | |
78 | ); | |
03e04002 | 79 | CRM_Financial_BAO_FinancialTypeAccount::add($values); |
6a488035 TO |
80 | } |
81 | return $processor; | |
82 | } | |
83 | ||
84 | /** | |
85 | * class constructor | |
86 | */ | |
87 | function __construct() { | |
88 | parent::__construct(); | |
89 | } | |
90 | ||
91 | /** | |
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 | |
95 | * | |
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 | |
98 | * | |
99 | * @return object CRM_Financial_DAO_PaymentProcessor object on success, null otherwise | |
100 | * @access public | |
101 | * @static | |
102 | */ | |
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; | |
109 | } | |
110 | return NULL; | |
111 | } | |
112 | ||
113 | /** | |
114 | * update the is_active flag in the db | |
115 | * | |
116 | * @param int $id id of the database record | |
117 | * @param boolean $is_active value we want to set the is_active field | |
118 | * | |
119 | * @return Object DAO object on sucess, null otherwise | |
120 | * | |
121 | * @access public | |
122 | * @static | |
123 | */ | |
124 | static function setIsActive($id, $is_active) { | |
125 | return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active); | |
126 | } | |
127 | ||
128 | /** | |
129 | * retrieve the default payment processor | |
130 | * | |
131 | * @param NULL | |
132 | * | |
133 | * @return object The default payment processor object on success, | |
134 | * null otherwise | |
135 | * @static | |
136 | * @access public | |
137 | */ | |
138 | static function &getDefault() { | |
139 | if (self::$_defaultPaymentProcessor == NULL) { | |
140 | $params = array('is_default' => 1); | |
141 | $defaults = array(); | |
142 | self::$_defaultPaymentProcessor = self::retrieve($params, $defaults); | |
143 | } | |
144 | return self::$_defaultPaymentProcessor; | |
145 | } | |
146 | ||
147 | /** | |
148 | * Function to delete payment processor | |
149 | * | |
150 | * @param int $paymentProcessorId ID of the processor to be deleted. | |
151 | * | |
152 | * @access public | |
153 | * @static | |
154 | */ | |
155 | static function del($paymentProcessorID) { | |
156 | if (!$paymentProcessorID) { | |
157 | CRM_Core_Error::fatal(ts('Invalid value passed to delete function')); | |
158 | } | |
159 | ||
160 | $dao = new CRM_Financial_DAO_PaymentProcessor(); | |
161 | $dao->id = $paymentProcessorID; | |
162 | if (!$dao->find(TRUE)) { | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | $testDAO = new CRM_Financial_DAO_PaymentProcessor(); | |
167 | $testDAO->name = $dao->name; | |
168 | $testDAO->is_test = 1; | |
169 | $testDAO->delete(); | |
170 | ||
171 | $dao->delete(); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Function to get the payment processor details | |
176 | * | |
177 | * @param int $paymentProcessorID payment processor id | |
178 | * @param string $mode payment mode ie test or live | |
179 | * | |
180 | * @return array associated array with payment processor related fields | |
181 | * @static | |
182 | * @access public | |
183 | */ | |
184 | static function getPayment($paymentProcessorID, $mode) { | |
185 | if (!$paymentProcessorID) { | |
186 | CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function')); | |
187 | } | |
188 | ||
189 | $dao = new CRM_Financial_DAO_PaymentProcessor( ); | |
190 | $dao->id = $paymentProcessorID; | |
191 | $dao->is_active = 1; | |
192 | if (!$dao->find(TRUE)) { | |
193 | return NULL; | |
194 | } | |
195 | ||
196 | if ($mode == 'test') { | |
197 | $testDAO = new CRM_Financial_DAO_PaymentProcessor( ); | |
198 | $testDAO->name = $dao->name; | |
199 | $testDAO->is_active = 1; | |
200 | $testDAO->is_test = 1; | |
201 | if (!$testDAO->find(TRUE)) { | |
202 | CRM_Core_Error::fatal(ts('Could not retrieve payment processor details')); | |
203 | } | |
204 | return self::buildPayment($testDAO, $mode); | |
205 | } | |
206 | else { | |
207 | return self::buildPayment($dao, $mode); | |
208 | } | |
209 | } | |
210 | ||
211 | static function getPayments($paymentProcessorIDs, $mode) { | |
212 | if (!$paymentProcessorIDs) { | |
213 | CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function')); | |
214 | } | |
215 | ||
216 | $payments = array( ); | |
217 | foreach ($paymentProcessorIDs as $paymentProcessorID) { | |
218 | $payment = self::getPayment($paymentProcessorID, $mode); | |
219 | $payments[$payment['id']] = $payment; | |
220 | } | |
221 | ||
13ac605f | 222 | uasort($payments, 'self::defaultComparison'); |
6a488035 TO |
223 | return $payments; |
224 | } | |
225 | ||
13ac605f DG |
226 | /** |
227 | * compare 2 payment processors to see which should go first based on is_default | |
228 | * (sort function for sortDefaultFirst) | |
229 | * @param array $processor1 | |
230 | * @param array_type $processor2 | |
231 | * @return number | |
232 | */ | |
233 | static function defaultComparison($processor1, $processor2){ | |
234 | $p1 = CRM_Utils_Array::value('is_default', $processor1); | |
235 | $p2 = CRM_Utils_Array::value('is_default', $processor2); | |
236 | if ($p1 == $p2) { | |
237 | return 0; | |
238 | } | |
239 | return ($p1 > $p2) ? -1 : 1; | |
240 | } | |
241 | ||
6a488035 TO |
242 | /** |
243 | * Function to build payment processor details | |
244 | * | |
245 | * @param object $dao payment processor object | |
246 | * @param string $mode payment mode ie test or live | |
247 | * | |
248 | * @return array associated array with payment processor related fields | |
249 | * @static | |
250 | * @access public | |
251 | */ | |
252 | static function buildPayment($dao, $mode) { | |
253 | $fields = array( | |
254 | 'id', 'name', 'payment_processor_type_id', 'user_name', 'password', | |
255 | 'signature', 'url_site', 'url_api', 'url_recur', 'url_button', | |
256 | 'subject', 'class_name', 'is_recur', 'billing_mode', | |
257 | 'payment_type', 'is_default', | |
258 | ); | |
259 | $result = array(); | |
260 | foreach ($fields as $name) { | |
261 | $result[$name] = $dao->$name; | |
262 | } | |
263 | $result['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, $dao->payment_processor_type_id, 'name'); | |
264 | ||
265 | $result['instance'] =& CRM_Core_Payment::singleton($mode, $result); | |
266 | ||
267 | return $result; | |
268 | } | |
269 | ||
270 | /** | |
271 | * Function to retrieve payment processor id / info/ object based on component-id. | |
272 | * | |
273 | * @param int $componentID id of a component | |
274 | * @param string $component component | |
275 | * @param string $type type of payment information to be retrieved | |
276 | * | |
277 | * @return id / array / object based on type | |
278 | * @static | |
279 | * @access public | |
280 | */ | |
281 | static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') { | |
282 | $result = NULL; | |
283 | if (!in_array($component, array( | |
284 | 'membership', 'contribute', 'recur'))) { | |
285 | return $result; | |
286 | } | |
287 | //FIXME: | |
288 | if ($component == 'membership') { | |
289 | $sql = " | |
290 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
291 | FROM civicrm_membership mem | |
292 | INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id ) | |
293 | INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id ) | |
294 | LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id ) | |
295 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
296 | WHERE mp.membership_id = %1"; | |
297 | } | |
298 | elseif ($component == 'contribute') { | |
299 | $sql = " | |
300 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
301 | FROM civicrm_contribution con | |
302 | LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id ) | |
303 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
304 | WHERE con.id = %1"; | |
305 | } | |
306 | elseif ($component == 'recur') { | |
307 | $sql = " | |
308 | SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test | |
309 | FROM civicrm_contribution_recur cr | |
310 | WHERE cr.id = %1"; | |
311 | } | |
312 | ||
313 | //we are interesting in single record. | |
314 | $sql .= ' LIMIT 1'; | |
315 | ||
316 | $params = array(1 => array($entityID, 'Integer')); | |
317 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
318 | ||
319 | if (!$dao->fetch()) { | |
320 | ||
321 | return $result; | |
322 | ||
323 | } | |
324 | ||
325 | $ppID = (isset($dao->ppID1) && $dao->ppID1) ? $dao->ppID1 : (isset($dao->ppID2) ? $dao->ppID2 : NULL); | |
326 | $mode = (isset($dao->is_test) && $dao->is_test) ? 'test' : 'live'; | |
327 | if (!$ppID || $type == 'id') { | |
328 | $result = $ppID; | |
329 | } | |
330 | elseif ($type == 'info') { | |
331 | $result = self::getPayment($ppID, $mode); | |
332 | } | |
333 | elseif ($type == 'obj') { | |
334 | $payment = self::getPayment($ppID, $mode); | |
335 | $result = CRM_Core_Payment::singleton($mode, $payment); | |
336 | } | |
337 | ||
338 | return $result; | |
339 | } | |
340 | } | |
341 |