Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class contains payment processor related functions. | |
36 | */ | |
d3e86119 | 37 | class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProcessor { |
6a488035 | 38 | /** |
100fef9d | 39 | * Static holder for the default payment processor |
6a488035 TO |
40 | */ |
41 | static $_defaultPaymentProcessor = NULL; | |
42 | ||
c490a46a | 43 | /** |
fe482240 | 44 | * Create Payment Processor. |
6a488035 | 45 | * |
ed5dd492 TO |
46 | * @param array $params |
47 | * Parameters for Processor entity. | |
e0ef6999 EM |
48 | * |
49 | * @return CRM_Financial_DAO_PaymentProcessor | |
50 | * @throws Exception | |
51 | */ | |
00be9182 | 52 | public static function create($params) { |
6a488035 TO |
53 | $processor = new CRM_Financial_DAO_PaymentProcessor(); |
54 | $processor->copyValues($params); | |
55 | ||
29dd4ada JP |
56 | if (empty($params['id'])) { |
57 | $ppTypeDAO = new CRM_Financial_DAO_PaymentProcessorType(); | |
58 | $ppTypeDAO->id = $params['payment_processor_type_id']; | |
59 | if (!$ppTypeDAO->find(TRUE)) { | |
60 | CRM_Core_Error::fatal(ts('Could not find payment processor meta information')); | |
61 | } | |
6a488035 | 62 | |
29dd4ada JP |
63 | // also copy meta fields from the info DAO |
64 | $processor->is_recur = $ppTypeDAO->is_recur; | |
65 | $processor->billing_mode = $ppTypeDAO->billing_mode; | |
66 | $processor->class_name = $ppTypeDAO->class_name; | |
67 | $processor->payment_type = $ppTypeDAO->payment_type; | |
68 | } | |
6a488035 TO |
69 | |
70 | $processor->save(); | |
03e04002 | 71 | // CRM-11826, add entry in civicrm_entity_financial_account |
6a488035 | 72 | // if financial_account_id is not NULL |
a7488080 | 73 | if (!empty($params['financial_account_id'])) { |
f743a6eb | 74 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); |
6a488035 TO |
75 | $values = array( |
76 | 'entity_table' => 'civicrm_payment_processor', | |
77 | 'entity_id' => $processor->id, | |
78 | 'account_relationship' => $relationTypeId, | |
21dfd5f5 | 79 | 'financial_account_id' => $params['financial_account_id'], |
6a488035 | 80 | ); |
03e04002 | 81 | CRM_Financial_BAO_FinancialTypeAccount::add($values); |
6a488035 | 82 | } |
b7e7f943 | 83 | |
46168cb8 MW |
84 | if (isset($params['id']) && isset($params['is_active']) && !isset($params['is_test'])) { |
85 | // check if is_active has changed & if so update test instance is_active too. | |
86 | $test_id = self::getTestProcessorId($params['id']); | |
87 | $testDAO = new CRM_Financial_DAO_PaymentProcessor(); | |
88 | $testDAO->id = $test_id; | |
68483a20 | 89 | if ($testDAO->find(TRUE)) { |
46168cb8 MW |
90 | $testDAO->is_active = $params['is_active']; |
91 | $testDAO->save(); | |
92 | } | |
93 | } | |
94 | ||
d6944518 | 95 | Civi\Payment\System::singleton()->flushProcessors(); |
6a488035 TO |
96 | return $processor; |
97 | } | |
98 | ||
99 | /** | |
fe482240 | 100 | * Class constructor. |
6a488035 | 101 | */ |
00be9182 | 102 | public function __construct() { |
6a488035 TO |
103 | parent::__construct(); |
104 | } | |
105 | ||
cb5962bd | 106 | /** |
c294dbbc | 107 | * Retrieve array of allowed credit cards for this payment processor. |
cb5962bd SL |
108 | * @param interger|null $paymentProcessorID id of processor. |
109 | * @return array | |
110 | */ | |
111 | public static function getCreditCards($paymentProcessorID = NULL) { | |
112 | if (!empty($paymentProcessorID)) { | |
113 | $processor = new CRM_Financial_DAO_PaymentProcessor(); | |
114 | $processor->id = $paymentProcessorID; | |
115 | $processor->find(TRUE); | |
116 | $cards = json_decode($processor->accepted_credit_cards, TRUE); | |
117 | return $cards; | |
118 | } | |
119 | return array(); | |
120 | } | |
121 | ||
6a488035 | 122 | /** |
fe482240 EM |
123 | * Retrieve DB object based on input parameters. |
124 | * | |
125 | * It also stores all the retrieved values in the default array. | |
6a488035 | 126 | * |
ed5dd492 TO |
127 | * @param array $params |
128 | * (reference ) an assoc array of name/value pairs. | |
129 | * @param array $defaults | |
130 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 131 | * |
16b10e64 CW |
132 | * @return CRM_Financial_DAO_PaymentProcessor|null |
133 | * object on success, null otherwise | |
6a488035 | 134 | */ |
00be9182 | 135 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
136 | $paymentProcessor = new CRM_Financial_DAO_PaymentProcessor(); |
137 | $paymentProcessor->copyValues($params); | |
138 | if ($paymentProcessor->find(TRUE)) { | |
139 | CRM_Core_DAO::storeValues($paymentProcessor, $defaults); | |
140 | return $paymentProcessor; | |
141 | } | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | /** | |
fe482240 | 146 | * Update the is_active flag in the db. |
6a488035 | 147 | * |
ed5dd492 TO |
148 | * @param int $id |
149 | * Id of the database record. | |
150 | * @param bool $is_active | |
151 | * Value we want to set the is_active field. | |
6a488035 | 152 | * |
16b10e64 CW |
153 | * @return CRM_Financial_DAO_PaymentProcessor|null |
154 | * DAO object on success, null otherwise | |
6a488035 | 155 | * |
6a488035 | 156 | */ |
00be9182 | 157 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
158 | return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active); |
159 | } | |
160 | ||
161 | /** | |
fe482240 | 162 | * Retrieve the default payment processor. |
6a488035 | 163 | * |
16b10e64 | 164 | * @return CRM_Financial_DAO_PaymentProcessor|null |
a6c01b45 | 165 | * The default payment processor object on success, |
16b10e64 | 166 | * null otherwise |
6a488035 | 167 | */ |
00be9182 | 168 | public static function &getDefault() { |
6a488035 TO |
169 | if (self::$_defaultPaymentProcessor == NULL) { |
170 | $params = array('is_default' => 1); | |
171 | $defaults = array(); | |
172 | self::$_defaultPaymentProcessor = self::retrieve($params, $defaults); | |
173 | } | |
174 | return self::$_defaultPaymentProcessor; | |
175 | } | |
176 | ||
177 | /** | |
fe482240 | 178 | * Delete payment processor. |
6a488035 | 179 | * |
c490a46a | 180 | * @param int $paymentProcessorID |
77b97be7 EM |
181 | * |
182 | * @return null | |
6a488035 | 183 | */ |
00be9182 | 184 | public static function del($paymentProcessorID) { |
6a488035 | 185 | if (!$paymentProcessorID) { |
1f21f2cf | 186 | CRM_Core_Error::fatal(ts('Invalid value passed to delete function.')); |
6a488035 TO |
187 | } |
188 | ||
189 | $dao = new CRM_Financial_DAO_PaymentProcessor(); | |
190 | $dao->id = $paymentProcessorID; | |
191 | if (!$dao->find(TRUE)) { | |
192 | return NULL; | |
193 | } | |
194 | ||
195 | $testDAO = new CRM_Financial_DAO_PaymentProcessor(); | |
196 | $testDAO->name = $dao->name; | |
197 | $testDAO->is_test = 1; | |
198 | $testDAO->delete(); | |
199 | ||
200 | $dao->delete(); | |
d6944518 | 201 | Civi\Payment\System::singleton()->flushProcessors(); |
6a488035 TO |
202 | } |
203 | ||
204 | /** | |
fe482240 | 205 | * Get the payment processor details. |
6a488035 | 206 | * |
9d8f43b1 EM |
207 | * This returns an array whereas Civi\Payment\System::singleton->getByID() returns an object. |
208 | * The object is a key in the array. | |
2c5311b9 | 209 | * |
ed5dd492 TO |
210 | * @param int $paymentProcessorID |
211 | * Payment processor id. | |
212 | * @param string $mode | |
213 | * Payment mode ie test or live. | |
6a488035 | 214 | * |
a6c01b45 CW |
215 | * @return array |
216 | * associated array with payment processor related fields | |
6a488035 | 217 | */ |
aaff4c69 | 218 | public static function getPayment($paymentProcessorID, $mode = 'based_on_id') { |
9d8f43b1 EM |
219 | $capabilities = ($mode == 'test') ? array('TestMode') : array(); |
220 | $processors = self::getPaymentProcessors($capabilities, array($paymentProcessorID)); | |
221 | $processor = $processors[$paymentProcessorID]; | |
222 | $fields = array( | |
223 | 'id', | |
224 | 'name', | |
225 | 'payment_processor_type_id', | |
226 | 'user_name', | |
227 | 'password', | |
228 | 'signature', | |
229 | 'url_site', | |
230 | 'url_api', | |
231 | 'url_recur', | |
232 | 'url_button', | |
233 | 'subject', | |
234 | 'class_name', | |
235 | 'is_recur', | |
236 | 'billing_mode', | |
237 | 'is_test', | |
238 | 'payment_type', | |
239 | 'is_default', | |
240 | ); | |
241 | // Just to prevent e-Notices elsewhere we set all fields. | |
242 | foreach ($fields as $name) { | |
243 | if (!isset($processor)) { | |
244 | $processor[$name] = NULL; | |
6a488035 | 245 | } |
6a488035 | 246 | } |
9d8f43b1 EM |
247 | $processor['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, |
248 | $processor['payment_processor_type_id'], 'name'); | |
249 | return $processors[$paymentProcessorID]; | |
6a488035 TO |
250 | } |
251 | ||
428e38a4 EM |
252 | /** |
253 | * Given a live processor ID get the test id. | |
254 | * | |
255 | * @param int $id | |
256 | * | |
257 | * @return int | |
258 | * Test payment processor ID. | |
259 | */ | |
260 | public static function getTestProcessorId($id) { | |
261 | $liveProcessorName = civicrm_api3('payment_processor', 'getvalue', array( | |
262 | 'id' => $id, | |
263 | 'return' => 'name', | |
264 | )); | |
265 | return civicrm_api3('payment_processor', 'getvalue', array( | |
266 | 'return' => 'id', | |
267 | 'name' => $liveProcessorName, | |
46168cb8 | 268 | 'is_test' => 1, |
428e38a4 EM |
269 | 'domain_id' => CRM_Core_Config::domainID(), |
270 | )); | |
271 | } | |
272 | ||
13ac605f | 273 | /** |
100fef9d | 274 | * Compare 2 payment processors to see which should go first based on is_default |
13ac605f DG |
275 | * (sort function for sortDefaultFirst) |
276 | * @param array $processor1 | |
16b10e64 | 277 | * @param array $processor2 |
79d7553f | 278 | * |
279 | * @return int | |
13ac605f | 280 | */ |
9b873358 | 281 | public static function defaultComparison($processor1, $processor2) { |
045f52a3 TO |
282 | $p1 = CRM_Utils_Array::value('is_default', $processor1); |
283 | $p2 = CRM_Utils_Array::value('is_default', $processor2); | |
284 | if ($p1 == $p2) { | |
285 | return 0; | |
13ac605f | 286 | } |
045f52a3 TO |
287 | return ($p1 > $p2) ? -1 : 1; |
288 | } | |
13ac605f | 289 | |
fbcb6fba | 290 | /** |
100fef9d | 291 | * Get all payment processors as an array of objects. |
fbcb6fba | 292 | * |
52767de0 EM |
293 | * @param string|NULL $mode |
294 | * only return this mode - test|live or NULL for all | |
fbcb6fba | 295 | * @param bool $reset |
4199eb7e SL |
296 | * @param bool $isCurrentDomainOnly |
297 | * Do we only want to load payment processors associated with the current domain. | |
fbcb6fba EM |
298 | * |
299 | * @throws CiviCRM_API3_Exception | |
300 | * @return array | |
301 | */ | |
4199eb7e | 302 | public static function getAllPaymentProcessors($mode = 'all', $reset = FALSE, $isCurrentDomainOnly = TRUE) { |
7036a6d0 | 303 | |
a160bb08 | 304 | $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . $mode . '_' . CRM_Core_Config::domainID(); |
7036a6d0 EM |
305 | if (!$reset) { |
306 | $processors = CRM_Utils_Cache::singleton()->get($cacheKey); | |
307 | if (!empty($processors)) { | |
308 | return $processors; | |
309 | } | |
310 | } | |
311 | ||
353ffa53 TO |
312 | $retrievalParameters = array( |
313 | 'is_active' => TRUE, | |
5391207b | 314 | 'options' => array('sort' => 'is_default DESC, name', 'limit' => 0), |
79d7553f | 315 | 'api.payment_processor_type.getsingle' => 1, |
353ffa53 | 316 | ); |
4199eb7e | 317 | if ($isCurrentDomainOnly) { |
5391207b SL |
318 | $retrievalParameters['domain_id'] = CRM_Core_Config::domainID(); |
319 | } | |
52767de0 EM |
320 | if ($mode == 'test') { |
321 | $retrievalParameters['is_test'] = 1; | |
322 | } | |
323 | elseif ($mode == 'live') { | |
fbcb6fba EM |
324 | $retrievalParameters['is_test'] = 0; |
325 | } | |
7036a6d0 | 326 | |
fbcb6fba EM |
327 | $processors = civicrm_api3('payment_processor', 'get', $retrievalParameters); |
328 | foreach ($processors['values'] as $processor) { | |
7036a6d0 | 329 | $fieldsToProvide = array('user_name', 'password', 'signature', 'subject', 'is_recur'); |
52767de0 | 330 | foreach ($fieldsToProvide as $field) { |
7036a6d0 | 331 | // Prevent e-notices in processor classes when not configured. |
52767de0 | 332 | if (!isset($processor[$field])) { |
be74ba34 | 333 | $processors['values'][$processor['id']][$field] = NULL; |
52767de0 EM |
334 | } |
335 | } | |
9d91a9c6 | 336 | $processors['values'][$processor['id']]['payment_processor_type'] = $processor['payment_processor_type'] = $processors['values'][$processor['id']]['api.payment_processor_type.getsingle']['name']; |
7036a6d0 | 337 | $processors['values'][$processor['id']]['object'] = Civi\Payment\System::singleton()->getByProcessor($processor); |
fbcb6fba | 338 | } |
7036a6d0 | 339 | |
1d1fee72 | 340 | // Add the pay-later pseudo-processor. |
341 | $processors['values'][0] = array( | |
f48e6cf7 | 342 | 'object' => new CRM_Core_Payment_Manual(), |
1d1fee72 | 343 | 'id' => 0, |
344 | 'payment_processor_type_id' => 0, | |
f48e6cf7 | 345 | // This shouldn't be required but there are still some processors hacked into core with nasty 'if's. |
346 | 'payment_processor_type' => 'Manual', | |
1d1fee72 | 347 | 'class_name' => 'Payment_Manual', |
348 | 'name' => 'pay_later', | |
349 | 'billing_mode' => '', | |
fdf4616e | 350 | 'is_default' => 0, |
18135422 | 351 | 'payment_instrument_id' => key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')), |
1d1fee72 | 352 | // Making this optionally recur would give lots of options -but it should |
353 | // be a row in the payment processor table before we do that. | |
354 | 'is_recur' => FALSE, | |
18135422 | 355 | 'is_test' => FALSE, |
1d1fee72 | 356 | ); |
357 | ||
7036a6d0 EM |
358 | CRM_Utils_Cache::singleton()->set($cacheKey, $processors['values']); |
359 | ||
fbcb6fba EM |
360 | return $processors['values']; |
361 | } | |
362 | ||
363 | /** | |
100fef9d | 364 | * Get Payment processors with specified capabilities. |
fbcb6fba EM |
365 | * Note that both the singleton & the pseudoconstant function have caching so we don't add |
366 | * arguably this could go on the pseudoconstant class | |
367 | * | |
368 | * @param array $capabilities | |
16b10e64 CW |
369 | * capabilities of processor e.g |
370 | * - BackOffice | |
371 | * - TestMode | |
372 | * - LiveMode | |
373 | * - FutureStartDate | |
fbcb6fba | 374 | * |
93e11927 | 375 | * @param array|bool $ids |
dc913073 | 376 | * |
a6c01b45 CW |
377 | * @return array |
378 | * available processors | |
fbcb6fba | 379 | */ |
93e11927 | 380 | public static function getPaymentProcessors($capabilities = array(), $ids = FALSE) { |
e082c947 | 381 | $testProcessors = in_array('TestMode', $capabilities) ? self::getAllPaymentProcessors('test') : array(); |
5391207b SL |
382 | if (is_array($ids)) { |
383 | $processors = self::getAllPaymentProcessors('all', TRUE, FALSE); | |
384 | } | |
385 | else { | |
797515ef | 386 | $processors = self::getAllPaymentProcessors('all', TRUE); |
5391207b | 387 | } |
e082c947 | 388 | |
99459756 | 389 | if (in_array('TestMode', $capabilities) && is_array($ids)) { |
74b91f33 | 390 | $possibleLiveIDs = array_diff($ids, array_keys($testProcessors)); |
391 | foreach ($possibleLiveIDs as $possibleLiveID) { | |
392 | if (isset($processors[$possibleLiveID]) && ($liveProcessorName = $processors[$possibleLiveID]['name']) != FALSE) { | |
393 | foreach ($testProcessors as $index => $testProcessor) { | |
394 | if ($testProcessor['name'] == $liveProcessorName) { | |
395 | $ids[] = $testProcessor['id']; | |
e082c947 | 396 | } |
397 | } | |
398 | } | |
399 | } | |
400 | $processors = $testProcessors; | |
52767de0 | 401 | } |
7036a6d0 | 402 | |
1b9f9ca3 | 403 | foreach ($processors as $index => $processor) { |
74b91f33 | 404 | if (is_array($ids) && !in_array($processor['id'], $ids)) { |
5391207b | 405 | unset($processors[$index]); |
1b9f9ca3 EM |
406 | continue; |
407 | } | |
408 | // Invalid processors will store a null value in 'object' (e.g. if not all required config fields are present). | |
409 | // This is determined by calling when loading the processor via the $processorObject->checkConfig() function. | |
410 | if (!is_a($processor['object'], 'CRM_Core_Payment')) { | |
5391207b | 411 | unset($processors[$index]); |
1b9f9ca3 EM |
412 | continue; |
413 | } | |
414 | foreach ($capabilities as $capability) { | |
415 | if (($processor['object']->supports($capability)) == FALSE) { | |
5391207b | 416 | unset($processors[$index]); |
1b9f9ca3 | 417 | continue 1; |
fbcb6fba EM |
418 | } |
419 | } | |
420 | } | |
1b9f9ca3 | 421 | |
fbcb6fba EM |
422 | return $processors; |
423 | } | |
424 | ||
425 | /** | |
fe482240 | 426 | * Is there a processor on this site with the specified capability. |
c1f95567 | 427 | * |
428 | * The capabilities are defined on CRM_Core_Payment and can be extended by | |
429 | * processors. | |
430 | * | |
431 | * examples are | |
432 | * - supportsBackOffice | |
433 | * - supportsLiveMode | |
434 | * - supportsFutureRecurDate | |
435 | * - supportsCancelRecurring | |
436 | * - supportsRecurContributionsForPledges | |
437 | * | |
438 | * They are passed as array('BackOffice'); | |
439 | * | |
440 | * Details of specific functions are in the docblocks on the CRM_Core_Payment class. | |
441 | * | |
fbcb6fba | 442 | * @param array $capabilities |
fbcb6fba EM |
443 | * |
444 | * @return bool | |
445 | */ | |
fb674ca9 | 446 | public static function hasPaymentProcessorSupporting($capabilities = array()) { |
d09e593c | 447 | $capabilitiesString = implode('', $capabilities); |
448 | if (!isset(\Civi::$statics[__CLASS__]['supported_capabilities'][$capabilitiesString])) { | |
449 | $result = self::getPaymentProcessors($capabilities); | |
450 | \Civi::$statics[__CLASS__]['supported_capabilities'][$capabilitiesString] = (!empty($result)) ? TRUE : FALSE; | |
451 | } | |
452 | return \Civi::$statics[__CLASS__]['supported_capabilities'][$capabilitiesString]; | |
fbcb6fba EM |
453 | } |
454 | ||
6a488035 | 455 | /** |
100fef9d | 456 | * Retrieve payment processor id / info/ object based on component-id. |
6a488035 | 457 | * |
0bd096e8 | 458 | * @todo function needs revisiting. The whole 'info / obj' thing is an overload. Recommend creating new functions |
459 | * that are entity specific as there is little shared code specific to obj or info | |
460 | * | |
461 | * Also, it does not accurately derive the processor - for a completed contribution the best place to look is in the | |
462 | * relevant financial_trxn record. For a recurring contribution it is in the contribution_recur table. | |
463 | * | |
464 | * For a membership the relevant contribution_recur should be derived & then resolved as above. The contribution page | |
465 | * is never a reliable place to look as there can be more than one configured. For a pending contribution there is | |
466 | * no way to derive the processor - but hey - what processor? it didn't go through! | |
467 | * | |
468 | * Query for membership might look something like: | |
469 | * SELECT fte.payment_processor_id | |
470 | * FROM civicrm_membership mem | |
471 | * INNER JOIN civicrm_line_item li ON ( mem.id = li.entity_id AND li.entity_table = 'civicrm_membership') | |
472 | * INNER JOIN civicrm_contribution con ON ( li.contribution_id = con.id ) | |
473 | * LEFT JOIN civicrm_entity_financial_trxn ft ON ft.entity_id = con.id AND ft.entity_table = | |
474 | * 'civicrm_contribution' | |
475 | * LEFT JOIN civicrm_financial_trxn fte ON fte.id = ft.financial_trxn_id | |
476 | * | |
100fef9d | 477 | * @param int $entityID |
ed5dd492 TO |
478 | * @param string $component |
479 | * Component. | |
480 | * @param string $type | |
481 | * Type of payment information to be retrieved. | |
6a488035 | 482 | * |
16b10e64 | 483 | * @return int|array|object |
6a488035 | 484 | */ |
00be9182 | 485 | public static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') { |
6a488035 TO |
486 | $result = NULL; |
487 | if (!in_array($component, array( | |
353ffa53 TO |
488 | 'membership', |
489 | 'contribute', | |
79d7553f | 490 | 'recur', |
353ffa53 TO |
491 | )) |
492 | ) { | |
6a488035 TO |
493 | return $result; |
494 | } | |
cded2ebf | 495 | |
6a488035 TO |
496 | if ($component == 'membership') { |
497 | $sql = " | |
498 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
499 | FROM civicrm_membership mem | |
500 | INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id ) | |
501 | INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id ) | |
502 | LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id ) | |
503 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
504 | WHERE mp.membership_id = %1"; | |
505 | } | |
506 | elseif ($component == 'contribute') { | |
507 | $sql = " | |
508 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
509 | FROM civicrm_contribution con | |
510 | LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id ) | |
511 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
512 | WHERE con.id = %1"; | |
513 | } | |
514 | elseif ($component == 'recur') { | |
b3e340d9 | 515 | // @deprecated - use getPaymentProcessorForRecurringContribution. |
6a488035 TO |
516 | $sql = " |
517 | SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test | |
518 | FROM civicrm_contribution_recur cr | |
519 | WHERE cr.id = %1"; | |
520 | } | |
521 | ||
cded2ebf | 522 | // We are interested in a single record. |
6a488035 TO |
523 | $sql .= ' LIMIT 1'; |
524 | ||
525 | $params = array(1 => array($entityID, 'Integer')); | |
526 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
527 | ||
528 | if (!$dao->fetch()) { | |
529 | ||
530 | return $result; | |
531 | ||
532 | } | |
533 | ||
534 | $ppID = (isset($dao->ppID1) && $dao->ppID1) ? $dao->ppID1 : (isset($dao->ppID2) ? $dao->ppID2 : NULL); | |
535 | $mode = (isset($dao->is_test) && $dao->is_test) ? 'test' : 'live'; | |
536 | if (!$ppID || $type == 'id') { | |
537 | $result = $ppID; | |
538 | } | |
539 | elseif ($type == 'info') { | |
540 | $result = self::getPayment($ppID, $mode); | |
541 | } | |
0bd096e8 | 542 | elseif ($type == 'obj' && is_numeric($ppID)) { |
543 | try { | |
544 | $paymentProcessor = civicrm_api3('PaymentProcessor', 'getsingle', array('id' => $ppID)); | |
545 | } | |
546 | catch (API_Exception $e) { | |
547 | // Unable to load the processor because this function uses an unreliable method to derive it. | |
548 | // The function looks to load the payment processor ID from the contribution page, which | |
549 | // can support multiple processors. | |
550 | } | |
788e24b7 | 551 | $paymentProcessor['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, $paymentProcessor['payment_processor_type_id'], 'name'); |
0bd096e8 | 552 | $result = Civi\Payment\System::singleton()->getByProcessor($paymentProcessor); |
6a488035 | 553 | } |
6a488035 TO |
554 | return $result; |
555 | } | |
96025800 | 556 | |
b3e340d9 | 557 | /** |
558 | * Get the payment processor associated with a recurring contribution series. | |
559 | * | |
560 | * @param int $contributionRecurID | |
561 | * | |
562 | * @return \CRM_Core_Payment | |
563 | */ | |
564 | public static function getPaymentProcessorForRecurringContribution($contributionRecurID) { | |
565 | $paymentProcessorId = civicrm_api3('ContributionRecur', 'getvalue', array( | |
566 | 'id' => $contributionRecurID, | |
567 | 'return' => 'payment_processor_id', | |
568 | )); | |
569 | return Civi\Payment\System::singleton()->getById($paymentProcessorId); | |
570 | } | |
571 | ||
84df6a33 | 572 | /** |
573 | * Generate and assign an arbitrary value to a field of a test object. | |
574 | * | |
575 | * @param string $fieldName | |
576 | * @param array $fieldDef | |
577 | * @param int $counter | |
578 | * The globally-unique ID of the test object. | |
579 | */ | |
580 | protected function assignTestValue($fieldName, &$fieldDef, $counter) { | |
581 | if ($fieldName === 'class_name') { | |
582 | $this->class_name = 'Payment_Dummy'; | |
583 | } | |
584 | else { | |
585 | parent::assignTestValue($fieldName, $fieldDef, $counter); | |
586 | } | |
587 | } | |
588 | ||
6a488035 | 589 | } |