Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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 | * |
8a4fede3 | 153 | * @return bool |
154 | * true if we found and updated the object, else false | |
6a488035 | 155 | */ |
00be9182 | 156 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
157 | return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active); |
158 | } | |
159 | ||
160 | /** | |
fe482240 | 161 | * Retrieve the default payment processor. |
6a488035 | 162 | * |
16b10e64 | 163 | * @return CRM_Financial_DAO_PaymentProcessor|null |
a6c01b45 | 164 | * The default payment processor object on success, |
16b10e64 | 165 | * null otherwise |
6a488035 | 166 | */ |
00be9182 | 167 | public static function &getDefault() { |
6a488035 TO |
168 | if (self::$_defaultPaymentProcessor == NULL) { |
169 | $params = array('is_default' => 1); | |
170 | $defaults = array(); | |
171 | self::$_defaultPaymentProcessor = self::retrieve($params, $defaults); | |
172 | } | |
173 | return self::$_defaultPaymentProcessor; | |
174 | } | |
175 | ||
176 | /** | |
fe482240 | 177 | * Delete payment processor. |
6a488035 | 178 | * |
c490a46a | 179 | * @param int $paymentProcessorID |
77b97be7 EM |
180 | * |
181 | * @return null | |
6a488035 | 182 | */ |
00be9182 | 183 | public static function del($paymentProcessorID) { |
6a488035 | 184 | if (!$paymentProcessorID) { |
1f21f2cf | 185 | CRM_Core_Error::fatal(ts('Invalid value passed to delete function.')); |
6a488035 TO |
186 | } |
187 | ||
188 | $dao = new CRM_Financial_DAO_PaymentProcessor(); | |
189 | $dao->id = $paymentProcessorID; | |
190 | if (!$dao->find(TRUE)) { | |
191 | return NULL; | |
192 | } | |
193 | ||
194 | $testDAO = new CRM_Financial_DAO_PaymentProcessor(); | |
195 | $testDAO->name = $dao->name; | |
196 | $testDAO->is_test = 1; | |
197 | $testDAO->delete(); | |
198 | ||
199 | $dao->delete(); | |
d6944518 | 200 | Civi\Payment\System::singleton()->flushProcessors(); |
6a488035 TO |
201 | } |
202 | ||
203 | /** | |
fe482240 | 204 | * Get the payment processor details. |
6a488035 | 205 | * |
9d8f43b1 EM |
206 | * This returns an array whereas Civi\Payment\System::singleton->getByID() returns an object. |
207 | * The object is a key in the array. | |
2c5311b9 | 208 | * |
ed5dd492 TO |
209 | * @param int $paymentProcessorID |
210 | * Payment processor id. | |
211 | * @param string $mode | |
212 | * Payment mode ie test or live. | |
6a488035 | 213 | * |
a6c01b45 CW |
214 | * @return array |
215 | * associated array with payment processor related fields | |
6a488035 | 216 | */ |
aaff4c69 | 217 | public static function getPayment($paymentProcessorID, $mode = 'based_on_id') { |
9d8f43b1 EM |
218 | $capabilities = ($mode == 'test') ? array('TestMode') : array(); |
219 | $processors = self::getPaymentProcessors($capabilities, array($paymentProcessorID)); | |
9d8f43b1 | 220 | return $processors[$paymentProcessorID]; |
6a488035 TO |
221 | } |
222 | ||
428e38a4 EM |
223 | /** |
224 | * Given a live processor ID get the test id. | |
225 | * | |
226 | * @param int $id | |
227 | * | |
228 | * @return int | |
229 | * Test payment processor ID. | |
230 | */ | |
231 | public static function getTestProcessorId($id) { | |
232 | $liveProcessorName = civicrm_api3('payment_processor', 'getvalue', array( | |
233 | 'id' => $id, | |
234 | 'return' => 'name', | |
235 | )); | |
236 | return civicrm_api3('payment_processor', 'getvalue', array( | |
237 | 'return' => 'id', | |
238 | 'name' => $liveProcessorName, | |
46168cb8 | 239 | 'is_test' => 1, |
428e38a4 EM |
240 | 'domain_id' => CRM_Core_Config::domainID(), |
241 | )); | |
242 | } | |
243 | ||
13ac605f | 244 | /** |
100fef9d | 245 | * Compare 2 payment processors to see which should go first based on is_default |
13ac605f DG |
246 | * (sort function for sortDefaultFirst) |
247 | * @param array $processor1 | |
16b10e64 | 248 | * @param array $processor2 |
79d7553f | 249 | * |
250 | * @return int | |
13ac605f | 251 | */ |
9b873358 | 252 | public static function defaultComparison($processor1, $processor2) { |
045f52a3 TO |
253 | $p1 = CRM_Utils_Array::value('is_default', $processor1); |
254 | $p2 = CRM_Utils_Array::value('is_default', $processor2); | |
255 | if ($p1 == $p2) { | |
256 | return 0; | |
13ac605f | 257 | } |
045f52a3 TO |
258 | return ($p1 > $p2) ? -1 : 1; |
259 | } | |
13ac605f | 260 | |
fbcb6fba | 261 | /** |
100fef9d | 262 | * Get all payment processors as an array of objects. |
fbcb6fba | 263 | * |
52767de0 EM |
264 | * @param string|NULL $mode |
265 | * only return this mode - test|live or NULL for all | |
fbcb6fba | 266 | * @param bool $reset |
4199eb7e SL |
267 | * @param bool $isCurrentDomainOnly |
268 | * Do we only want to load payment processors associated with the current domain. | |
fbcb6fba EM |
269 | * |
270 | * @throws CiviCRM_API3_Exception | |
271 | * @return array | |
272 | */ | |
4199eb7e | 273 | public static function getAllPaymentProcessors($mode = 'all', $reset = FALSE, $isCurrentDomainOnly = TRUE) { |
7036a6d0 | 274 | |
6689745a | 275 | $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . $mode . '_' . $isCurrentDomainOnly . '_' . CRM_Core_Config::domainID(); |
7036a6d0 EM |
276 | if (!$reset) { |
277 | $processors = CRM_Utils_Cache::singleton()->get($cacheKey); | |
278 | if (!empty($processors)) { | |
279 | return $processors; | |
280 | } | |
281 | } | |
282 | ||
353ffa53 TO |
283 | $retrievalParameters = array( |
284 | 'is_active' => TRUE, | |
5391207b | 285 | 'options' => array('sort' => 'is_default DESC, name', 'limit' => 0), |
79d7553f | 286 | 'api.payment_processor_type.getsingle' => 1, |
353ffa53 | 287 | ); |
4199eb7e | 288 | if ($isCurrentDomainOnly) { |
5391207b SL |
289 | $retrievalParameters['domain_id'] = CRM_Core_Config::domainID(); |
290 | } | |
52767de0 EM |
291 | if ($mode == 'test') { |
292 | $retrievalParameters['is_test'] = 1; | |
293 | } | |
294 | elseif ($mode == 'live') { | |
fbcb6fba EM |
295 | $retrievalParameters['is_test'] = 0; |
296 | } | |
7036a6d0 | 297 | |
fbcb6fba EM |
298 | $processors = civicrm_api3('payment_processor', 'get', $retrievalParameters); |
299 | foreach ($processors['values'] as $processor) { | |
742c1119 MW |
300 | $fieldsToProvide = array( |
301 | 'id', | |
302 | 'name', | |
303 | 'payment_processor_type_id', | |
304 | 'user_name', | |
305 | 'password', | |
306 | 'signature', | |
307 | 'url_site', | |
308 | 'url_api', | |
309 | 'url_recur', | |
310 | 'url_button', | |
311 | 'subject', | |
312 | 'class_name', | |
313 | 'is_recur', | |
314 | 'billing_mode', | |
315 | 'is_test', | |
316 | 'payment_type', | |
317 | 'is_default', | |
318 | ); | |
52767de0 | 319 | foreach ($fieldsToProvide as $field) { |
7036a6d0 | 320 | // Prevent e-notices in processor classes when not configured. |
52767de0 | 321 | if (!isset($processor[$field])) { |
be74ba34 | 322 | $processors['values'][$processor['id']][$field] = NULL; |
52767de0 EM |
323 | } |
324 | } | |
9d91a9c6 | 325 | $processors['values'][$processor['id']]['payment_processor_type'] = $processor['payment_processor_type'] = $processors['values'][$processor['id']]['api.payment_processor_type.getsingle']['name']; |
7036a6d0 | 326 | $processors['values'][$processor['id']]['object'] = Civi\Payment\System::singleton()->getByProcessor($processor); |
fbcb6fba | 327 | } |
7036a6d0 | 328 | |
1d1fee72 | 329 | // Add the pay-later pseudo-processor. |
330 | $processors['values'][0] = array( | |
f48e6cf7 | 331 | 'object' => new CRM_Core_Payment_Manual(), |
1d1fee72 | 332 | 'id' => 0, |
333 | 'payment_processor_type_id' => 0, | |
f48e6cf7 | 334 | // This shouldn't be required but there are still some processors hacked into core with nasty 'if's. |
335 | 'payment_processor_type' => 'Manual', | |
1d1fee72 | 336 | 'class_name' => 'Payment_Manual', |
337 | 'name' => 'pay_later', | |
338 | 'billing_mode' => '', | |
fdf4616e | 339 | 'is_default' => 0, |
18135422 | 340 | 'payment_instrument_id' => key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')), |
1d1fee72 | 341 | // Making this optionally recur would give lots of options -but it should |
342 | // be a row in the payment processor table before we do that. | |
343 | 'is_recur' => FALSE, | |
18135422 | 344 | 'is_test' => FALSE, |
1d1fee72 | 345 | ); |
346 | ||
7036a6d0 EM |
347 | CRM_Utils_Cache::singleton()->set($cacheKey, $processors['values']); |
348 | ||
fbcb6fba EM |
349 | return $processors['values']; |
350 | } | |
351 | ||
352 | /** | |
100fef9d | 353 | * Get Payment processors with specified capabilities. |
fbcb6fba EM |
354 | * Note that both the singleton & the pseudoconstant function have caching so we don't add |
355 | * arguably this could go on the pseudoconstant class | |
356 | * | |
357 | * @param array $capabilities | |
16b10e64 CW |
358 | * capabilities of processor e.g |
359 | * - BackOffice | |
360 | * - TestMode | |
361 | * - LiveMode | |
362 | * - FutureStartDate | |
fbcb6fba | 363 | * |
93e11927 | 364 | * @param array|bool $ids |
dc913073 | 365 | * |
a6c01b45 CW |
366 | * @return array |
367 | * available processors | |
fbcb6fba | 368 | */ |
93e11927 | 369 | public static function getPaymentProcessors($capabilities = array(), $ids = FALSE) { |
e082c947 | 370 | $testProcessors = in_array('TestMode', $capabilities) ? self::getAllPaymentProcessors('test') : array(); |
5391207b | 371 | if (is_array($ids)) { |
6689745a | 372 | $processors = self::getAllPaymentProcessors('all', FALSE, FALSE); |
5391207b SL |
373 | } |
374 | else { | |
6689745a | 375 | $processors = self::getAllPaymentProcessors('all'); |
5391207b | 376 | } |
e082c947 | 377 | |
99459756 | 378 | if (in_array('TestMode', $capabilities) && is_array($ids)) { |
74b91f33 | 379 | $possibleLiveIDs = array_diff($ids, array_keys($testProcessors)); |
380 | foreach ($possibleLiveIDs as $possibleLiveID) { | |
381 | if (isset($processors[$possibleLiveID]) && ($liveProcessorName = $processors[$possibleLiveID]['name']) != FALSE) { | |
382 | foreach ($testProcessors as $index => $testProcessor) { | |
383 | if ($testProcessor['name'] == $liveProcessorName) { | |
384 | $ids[] = $testProcessor['id']; | |
e082c947 | 385 | } |
386 | } | |
387 | } | |
388 | } | |
389 | $processors = $testProcessors; | |
52767de0 | 390 | } |
7036a6d0 | 391 | |
1b9f9ca3 | 392 | foreach ($processors as $index => $processor) { |
74b91f33 | 393 | if (is_array($ids) && !in_array($processor['id'], $ids)) { |
5391207b | 394 | unset($processors[$index]); |
1b9f9ca3 EM |
395 | continue; |
396 | } | |
397 | // Invalid processors will store a null value in 'object' (e.g. if not all required config fields are present). | |
398 | // This is determined by calling when loading the processor via the $processorObject->checkConfig() function. | |
399 | if (!is_a($processor['object'], 'CRM_Core_Payment')) { | |
5391207b | 400 | unset($processors[$index]); |
1b9f9ca3 EM |
401 | continue; |
402 | } | |
403 | foreach ($capabilities as $capability) { | |
404 | if (($processor['object']->supports($capability)) == FALSE) { | |
5391207b | 405 | unset($processors[$index]); |
1b9f9ca3 | 406 | continue 1; |
fbcb6fba EM |
407 | } |
408 | } | |
409 | } | |
1b9f9ca3 | 410 | |
fbcb6fba EM |
411 | return $processors; |
412 | } | |
413 | ||
414 | /** | |
fe482240 | 415 | * Is there a processor on this site with the specified capability. |
c1f95567 | 416 | * |
417 | * The capabilities are defined on CRM_Core_Payment and can be extended by | |
418 | * processors. | |
419 | * | |
420 | * examples are | |
421 | * - supportsBackOffice | |
422 | * - supportsLiveMode | |
423 | * - supportsFutureRecurDate | |
54afd8a6 | 424 | * - supportsRecurring |
c1f95567 | 425 | * - supportsCancelRecurring |
426 | * - supportsRecurContributionsForPledges | |
427 | * | |
428 | * They are passed as array('BackOffice'); | |
429 | * | |
430 | * Details of specific functions are in the docblocks on the CRM_Core_Payment class. | |
431 | * | |
fbcb6fba | 432 | * @param array $capabilities |
fbcb6fba EM |
433 | * |
434 | * @return bool | |
435 | */ | |
fb674ca9 | 436 | public static function hasPaymentProcessorSupporting($capabilities = array()) { |
d09e593c | 437 | $capabilitiesString = implode('', $capabilities); |
438 | if (!isset(\Civi::$statics[__CLASS__]['supported_capabilities'][$capabilitiesString])) { | |
439 | $result = self::getPaymentProcessors($capabilities); | |
b74cf8c5 | 440 | \Civi::$statics[__CLASS__]['supported_capabilities'][$capabilitiesString] = (!empty($result) && array_keys($result) !== array(0)) ? TRUE : FALSE; |
d09e593c | 441 | } |
442 | return \Civi::$statics[__CLASS__]['supported_capabilities'][$capabilitiesString]; | |
fbcb6fba EM |
443 | } |
444 | ||
6a488035 | 445 | /** |
100fef9d | 446 | * Retrieve payment processor id / info/ object based on component-id. |
6a488035 | 447 | * |
0bd096e8 | 448 | * @todo function needs revisiting. The whole 'info / obj' thing is an overload. Recommend creating new functions |
449 | * that are entity specific as there is little shared code specific to obj or info | |
450 | * | |
451 | * Also, it does not accurately derive the processor - for a completed contribution the best place to look is in the | |
452 | * relevant financial_trxn record. For a recurring contribution it is in the contribution_recur table. | |
453 | * | |
454 | * For a membership the relevant contribution_recur should be derived & then resolved as above. The contribution page | |
455 | * is never a reliable place to look as there can be more than one configured. For a pending contribution there is | |
456 | * no way to derive the processor - but hey - what processor? it didn't go through! | |
457 | * | |
458 | * Query for membership might look something like: | |
459 | * SELECT fte.payment_processor_id | |
460 | * FROM civicrm_membership mem | |
461 | * INNER JOIN civicrm_line_item li ON ( mem.id = li.entity_id AND li.entity_table = 'civicrm_membership') | |
462 | * INNER JOIN civicrm_contribution con ON ( li.contribution_id = con.id ) | |
463 | * LEFT JOIN civicrm_entity_financial_trxn ft ON ft.entity_id = con.id AND ft.entity_table = | |
464 | * 'civicrm_contribution' | |
465 | * LEFT JOIN civicrm_financial_trxn fte ON fte.id = ft.financial_trxn_id | |
466 | * | |
100fef9d | 467 | * @param int $entityID |
ed5dd492 TO |
468 | * @param string $component |
469 | * Component. | |
470 | * @param string $type | |
471 | * Type of payment information to be retrieved. | |
6a488035 | 472 | * |
16b10e64 | 473 | * @return int|array|object |
6a488035 | 474 | */ |
00be9182 | 475 | public static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') { |
6a488035 TO |
476 | $result = NULL; |
477 | if (!in_array($component, array( | |
353ffa53 TO |
478 | 'membership', |
479 | 'contribute', | |
79d7553f | 480 | 'recur', |
353ffa53 TO |
481 | )) |
482 | ) { | |
6a488035 TO |
483 | return $result; |
484 | } | |
cded2ebf | 485 | |
6a488035 TO |
486 | if ($component == 'membership') { |
487 | $sql = " | |
488 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
489 | FROM civicrm_membership mem | |
490 | INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id ) | |
491 | INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id ) | |
492 | LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id ) | |
493 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
494 | WHERE mp.membership_id = %1"; | |
495 | } | |
496 | elseif ($component == 'contribute') { | |
497 | $sql = " | |
498 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
499 | FROM civicrm_contribution con | |
500 | LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id ) | |
501 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
502 | WHERE con.id = %1"; | |
503 | } | |
504 | elseif ($component == 'recur') { | |
b3e340d9 | 505 | // @deprecated - use getPaymentProcessorForRecurringContribution. |
6a488035 TO |
506 | $sql = " |
507 | SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test | |
508 | FROM civicrm_contribution_recur cr | |
509 | WHERE cr.id = %1"; | |
510 | } | |
511 | ||
cded2ebf | 512 | // We are interested in a single record. |
6a488035 TO |
513 | $sql .= ' LIMIT 1'; |
514 | ||
515 | $params = array(1 => array($entityID, 'Integer')); | |
516 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
517 | ||
518 | if (!$dao->fetch()) { | |
519 | ||
520 | return $result; | |
521 | ||
522 | } | |
523 | ||
524 | $ppID = (isset($dao->ppID1) && $dao->ppID1) ? $dao->ppID1 : (isset($dao->ppID2) ? $dao->ppID2 : NULL); | |
525 | $mode = (isset($dao->is_test) && $dao->is_test) ? 'test' : 'live'; | |
526 | if (!$ppID || $type == 'id') { | |
527 | $result = $ppID; | |
528 | } | |
529 | elseif ($type == 'info') { | |
530 | $result = self::getPayment($ppID, $mode); | |
531 | } | |
0bd096e8 | 532 | elseif ($type == 'obj' && is_numeric($ppID)) { |
533 | try { | |
534 | $paymentProcessor = civicrm_api3('PaymentProcessor', 'getsingle', array('id' => $ppID)); | |
535 | } | |
536 | catch (API_Exception $e) { | |
537 | // Unable to load the processor because this function uses an unreliable method to derive it. | |
538 | // The function looks to load the payment processor ID from the contribution page, which | |
539 | // can support multiple processors. | |
540 | } | |
742c1119 MW |
541 | |
542 | $paymentProcessor['payment_processor_type'] = CRM_Core_PseudoConstant::getName('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', $paymentProcessor['payment_processor_type_id']); | |
0bd096e8 | 543 | $result = Civi\Payment\System::singleton()->getByProcessor($paymentProcessor); |
6a488035 | 544 | } |
6a488035 TO |
545 | return $result; |
546 | } | |
96025800 | 547 | |
b3e340d9 | 548 | /** |
549 | * Get the payment processor associated with a recurring contribution series. | |
550 | * | |
551 | * @param int $contributionRecurID | |
552 | * | |
553 | * @return \CRM_Core_Payment | |
554 | */ | |
555 | public static function getPaymentProcessorForRecurringContribution($contributionRecurID) { | |
556 | $paymentProcessorId = civicrm_api3('ContributionRecur', 'getvalue', array( | |
557 | 'id' => $contributionRecurID, | |
558 | 'return' => 'payment_processor_id', | |
559 | )); | |
560 | return Civi\Payment\System::singleton()->getById($paymentProcessorId); | |
561 | } | |
562 | ||
46097c9c MW |
563 | /** |
564 | * Get the name of the payment processor | |
565 | * | |
566 | * @param $paymentProcessorId | |
567 | * | |
568 | * @return null|string | |
569 | */ | |
570 | public static function getPaymentProcessorName($paymentProcessorId) { | |
571 | try { | |
572 | $paymentProcessor = civicrm_api3('PaymentProcessor', 'getsingle', array( | |
573 | 'return' => array('name'), | |
574 | 'id' => $paymentProcessorId, | |
575 | )); | |
576 | return $paymentProcessor['name']; | |
577 | } | |
578 | catch (Exception $e) { | |
579 | return ts('Unknown') . ' (' . $paymentProcessorId . ')'; | |
580 | } | |
581 | } | |
582 | ||
84df6a33 | 583 | /** |
584 | * Generate and assign an arbitrary value to a field of a test object. | |
585 | * | |
586 | * @param string $fieldName | |
587 | * @param array $fieldDef | |
588 | * @param int $counter | |
589 | * The globally-unique ID of the test object. | |
590 | */ | |
591 | protected function assignTestValue($fieldName, &$fieldDef, $counter) { | |
592 | if ($fieldName === 'class_name') { | |
593 | $this->class_name = 'Payment_Dummy'; | |
594 | } | |
595 | else { | |
596 | parent::assignTestValue($fieldName, $fieldDef, $counter); | |
597 | } | |
598 | } | |
599 | ||
8563c1ce | 600 | /** |
601 | * Get the default financial account id for payment processor accounts. | |
602 | * | |
603 | * Note that there is only a 'name' field & no label field. If people customise | |
604 | * name then this won't work. This is new best-effort functionality so that's non-regressive. | |
605 | * | |
606 | * The fix for that is to add a label value to the financial account table. | |
607 | */ | |
608 | public static function getDefaultFinancialAccountID() { | |
609 | return CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_EntityFinancialAccount', 'financial_account_id', 'Payment Processor Account'); | |
610 | } | |
611 | ||
6a488035 | 612 | } |