Commit | Line | Data |
---|---|---|
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 | abstract class CRM_Core_Payment { | |
37 | ||
38 | /** | |
39 | * how are we getting billing information? | |
40 | * | |
41 | * FORM - we collect it on the same page | |
42 | * BUTTON - the processor collects it and sends it back to us via some protocol | |
43 | */ | |
44 | CONST | |
45 | BILLING_MODE_FORM = 1, | |
46 | BILLING_MODE_BUTTON = 2, | |
47 | BILLING_MODE_NOTIFY = 4; | |
48 | ||
49 | /** | |
50 | * which payment type(s) are we using? | |
51 | * | |
52 | * credit card | |
53 | * direct debit | |
54 | * or both | |
55 | * | |
56 | */ | |
57 | CONST | |
58 | PAYMENT_TYPE_CREDIT_CARD = 1, | |
59 | PAYMENT_TYPE_DIRECT_DEBIT = 2; | |
60 | ||
61 | /** | |
62 | * Subscription / Recurring payment Status | |
63 | * START, END | |
64 | * | |
65 | */ | |
66 | CONST | |
67 | RECURRING_PAYMENT_START = 'START', | |
68 | RECURRING_PAYMENT_END = 'END'; | |
69 | ||
70 | /** | |
71 | * We only need one instance of this object. So we use the singleton | |
72 | * pattern and cache the instance in this variable | |
73 | * | |
74 | * @var object | |
75 | * @static | |
76 | */ | |
77 | static private $_singleton = NULL; | |
78 | ||
79 | protected $_paymentProcessor; | |
80 | ||
ac32ed13 EM |
81 | /** |
82 | * @var CRM_Core_Form | |
83 | */ | |
6a488035 TO |
84 | protected $_paymentForm = NULL; |
85 | ||
86 | /** | |
87 | * singleton function used to manage this object | |
88 | * | |
89 | * @param string $mode the mode of operation: live or test | |
d77a0a58 | 90 | * @param array $paymentProcessor the details of the payment processor being invoked |
459091e1 | 91 | * @param object $paymentForm deprecated - avoid referring to this if possible. If you have to use it document why as this is scary interaction |
6a488035 TO |
92 | * @param boolean $force should we force a reload of this payment object |
93 | * | |
d85ec2d1 | 94 | * @return CRM_Core_Payment |
6a488035 TO |
95 | * @static |
96 | * | |
97 | */ | |
98 | static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) { | |
99 | // make sure paymentProcessor is not empty | |
100 | // CRM-7424 | |
101 | if (empty($paymentProcessor)) { | |
102 | return CRM_Core_DAO::$_nullObject; | |
103 | } | |
104 | ||
105 | $cacheKey = "{$mode}_{$paymentProcessor['id']}_" . (int)isset($paymentForm); | |
459091e1 | 106 | |
6a488035 TO |
107 | if (!isset(self::$_singleton[$cacheKey]) || $force) { |
108 | $config = CRM_Core_Config::singleton(); | |
109 | $ext = CRM_Extension_System::singleton()->getMapper(); | |
110 | if ($ext->isExtensionKey($paymentProcessor['class_name'])) { | |
111 | $paymentClass = $ext->keyToClass($paymentProcessor['class_name'], 'payment'); | |
112 | require_once ($ext->classToPath($paymentClass)); | |
113 | } | |
114 | else { | |
115 | $paymentClass = 'CRM_Core_' . $paymentProcessor['class_name']; | |
459091e1 EM |
116 | if (empty($paymentClass)) { |
117 | throw new CRM_Core_Exception('no class provided'); | |
118 | } | |
6a488035 TO |
119 | require_once (str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php'); |
120 | } | |
121 | ||
122 | //load the object. | |
0e6e8724 | 123 | self::$_singleton[$cacheKey] = $paymentClass::singleton($mode, $paymentProcessor); |
6a488035 TO |
124 | } |
125 | ||
126 | //load the payment form for required processor. | |
127 | if ($paymentForm !== NULL) { | |
128 | self::$_singleton[$cacheKey]->setForm($paymentForm); | |
129 | } | |
130 | ||
131 | return self::$_singleton[$cacheKey]; | |
132 | } | |
133 | ||
e2bef985 | 134 | /** |
135 | * @param $params | |
136 | * | |
137 | * @return mixed | |
138 | */ | |
139 | public static function logPaymentNotification($params) { | |
414e3596 | 140 | $message = 'payment_notification '; |
e2bef985 | 141 | if (!empty($params['processor_name'])) { |
414e3596 | 142 | $message .= 'processor_name=' . $params['processor_name']; |
e2bef985 | 143 | } |
144 | if (!empty($params['processor_id'])) { | |
145 | $message .= 'processor_id=' . $params['processor_id']; | |
146 | } | |
414e3596 | 147 | |
148 | $log = new CRM_Utils_SystemLogger(); | |
149 | $log->alert($message, $_REQUEST); | |
e2bef985 | 150 | } |
151 | ||
fbcb6fba EM |
152 | /** |
153 | * check if capability is supported | |
154 | * @param string $capability e.g BackOffice, LiveMode, FutureRecurStartDate | |
155 | * | |
156 | * @return bool | |
157 | */ | |
158 | public function supports($capability) { | |
159 | $function = 'supports' . ucfirst($capability); | |
160 | if (method_exists($this, $function)) { | |
161 | return $this->$function(); | |
162 | } | |
163 | return FALSE; | |
164 | } | |
165 | ||
166 | /** | |
167 | * are back office payments supported - e.g paypal standard won't permit you to enter a credit card associated with someone else's login | |
9c39fb25 | 168 | * The intention is to support off-site (other than paypal) & direct debit but that is not all working yet so to reach a 'stable' point we disable |
fbcb6fba EM |
169 | * @return bool |
170 | */ | |
d8ce0d68 | 171 | protected function supportsBackOffice() { |
9c39fb25 EM |
172 | if ($this->_paymentProcessor['billing_mode'] == 4 || $this->_paymentProcessor['payment_type'] != 1) { |
173 | return FALSE; | |
174 | } | |
175 | else { | |
176 | return TRUE; | |
177 | } | |
fbcb6fba EM |
178 | } |
179 | ||
180 | /** | |
181 | * are back office payments supported - e.g paypal standard won't permit you to enter a credit card associated with someone else's login | |
182 | * @return bool | |
183 | */ | |
d8ce0d68 | 184 | protected function supportsLiveMode() { |
fbcb6fba EM |
185 | return TRUE; |
186 | } | |
187 | ||
188 | /** | |
189 | * should the first payment date be configurable when setting up back office recurring payments | |
190 | * We set this to false for historical consistency but in fact most new processors use tokens for recurring and can support this | |
191 | * @return bool | |
192 | */ | |
d8ce0d68 | 193 | protected function supportsFutureRecurStartDate() { |
fbcb6fba EM |
194 | return FALSE; |
195 | } | |
196 | ||
6a488035 TO |
197 | /** |
198 | * Setter for the payment form that wants to use the processor | |
199 | * | |
ac32ed13 | 200 | * @param CRM_Core_Form $paymentForm |
6a488035 TO |
201 | * |
202 | */ | |
203 | function setForm(&$paymentForm) { | |
204 | $this->_paymentForm = $paymentForm; | |
205 | } | |
206 | ||
207 | /** | |
208 | * Getter for payment form that is using the processor | |
209 | * | |
ac32ed13 | 210 | * @return CRM_Core_Form A form object |
6a488035 TO |
211 | */ |
212 | function getForm() { | |
213 | return $this->_paymentForm; | |
214 | } | |
215 | ||
216 | /** | |
217 | * Getter for accessing member vars | |
218 | * | |
dc913073 EM |
219 | * @param $name |
220 | * | |
221 | * @return null | |
6a488035 TO |
222 | */ |
223 | function getVar($name) { | |
224 | return isset($this->$name) ? $this->$name : NULL; | |
225 | } | |
226 | ||
dc913073 EM |
227 | /** |
228 | * get name for the payment information type | |
229 | * | |
230 | * @return string | |
231 | */ | |
232 | public function getPaymentTypeName() { | |
459091e1 | 233 | return $this->_paymentProcessor['payment_type'] == 1 ? 'credit_card' : 'direct_debit'; |
dc913073 EM |
234 | } |
235 | ||
236 | /** | |
237 | * get label for the payment information type | |
238 | * | |
239 | * @return string | |
240 | */ | |
241 | public function getPaymentTypeLabel() { | |
459091e1 | 242 | return $this->_paymentProcessor['payment_type'] == 1 ? 'Credit Card' : 'Direct Debit'; |
dc913073 EM |
243 | } |
244 | ||
44b6505d EM |
245 | /** |
246 | * get array of fields that should be displayed on the payment form | |
247 | * @todo make payment type an option value & use it in the function name - currently on debit & credit card work | |
248 | * @return array | |
249 | * @throws CiviCRM_API3_Exception | |
250 | */ | |
251 | public function getPaymentFormFields() { | |
dc913073 | 252 | if ($this->_paymentProcessor['billing_mode'] == 4) { |
44b6505d EM |
253 | return array(); |
254 | } | |
255 | return $this->_paymentProcessor['payment_type'] == 1 ? $this->getCreditCardFormFields() : $this->getDirectDebitFormFields(); | |
256 | } | |
257 | ||
258 | /** | |
259 | * get array of fields that should be displayed on the payment form for credit cards | |
dc913073 | 260 | * |
44b6505d EM |
261 | * @return array |
262 | */ | |
263 | protected function getCreditCardFormFields() { | |
264 | return array( | |
265 | 'credit_card_type', | |
266 | 'credit_card_number', | |
267 | 'cvv2', | |
268 | 'credit_card_exp_date', | |
269 | ); | |
270 | } | |
271 | ||
272 | /** | |
273 | * get array of fields that should be displayed on the payment form for direct debits | |
dc913073 | 274 | * |
44b6505d EM |
275 | * @return array |
276 | */ | |
277 | protected function getDirectDebitFormFields() { | |
278 | return array( | |
279 | 'account_holder', | |
280 | 'bank_account_number', | |
281 | 'bank_identification_number', | |
282 | 'bank_name', | |
283 | ); | |
284 | } | |
285 | ||
dc913073 EM |
286 | /** |
287 | * return an array of all the details about the fields potentially required for payment fields | |
288 | * Only those determined by getPaymentFormFields will actually be assigned to the form | |
289 | * | |
290 | * @return array field metadata | |
291 | */ | |
292 | public function getPaymentFormFieldsMetadata() { | |
293 | //@todo convert credit card type into an option value | |
294 | $creditCardType = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::creditCard(); | |
295 | return array( | |
296 | 'credit_card_number' => array( | |
297 | 'htmlType' => 'text', | |
298 | 'name' => 'credit_card_number', | |
299 | 'title' => ts('Card Number'), | |
300 | 'cc_field' => TRUE, | |
301 | 'attributes' => array( | |
302 | 'size' => 20, | |
303 | 'maxlength' => 20, | |
304 | 'autocomplete' => 'off' | |
305 | ), | |
306 | 'is_required' => TRUE, | |
307 | ), | |
308 | 'cvv2' => array( | |
309 | 'htmlType' => 'text', | |
310 | 'name' => 'cvv2', | |
311 | 'title' => ts('Security Code'), | |
312 | 'cc_field' => TRUE, | |
313 | 'attributes' => array( | |
314 | 'size' => 5, | |
315 | 'maxlength' => 10, | |
316 | 'autocomplete' => 'off' | |
317 | ), | |
318 | 'is_required' => CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, | |
319 | 'cvv_backoffice_required', | |
320 | NULL, | |
321 | 1 | |
322 | ), | |
323 | 'rules' => array( | |
324 | array( | |
325 | 'rule_message' => ts('Please enter a valid value for your card security code. This is usually the last 3-4 digits on the card\'s signature panel.'), | |
326 | 'rule_name' => 'integer', | |
327 | 'rule_parameters' => NULL, | |
328 | )), | |
329 | ), | |
330 | 'credit_card_exp_date' => array( | |
331 | 'htmlType' => 'date', | |
332 | 'name' => 'credit_card_exp_date', | |
333 | 'title' => ts('Expiration Date'), | |
334 | 'cc_field' => TRUE, | |
335 | 'attributes' => CRM_Core_SelectValues::date('creditCard'), | |
336 | 'is_required' => TRUE, | |
337 | 'rules' => array( | |
338 | array( | |
339 | 'rule_message' => ts('Card expiration date cannot be a past date.'), | |
340 | 'rule_name' => 'currentDate', | |
341 | 'rule_parameters' => TRUE, | |
342 | )), | |
343 | ), | |
344 | 'credit_card_type' => array( | |
345 | 'htmlType' => 'select', | |
346 | 'name' => 'credit_card_type', | |
347 | 'title' => ts('Card Type'), | |
348 | 'cc_field' => TRUE, | |
349 | 'attributes' => $creditCardType, | |
350 | 'is_required' => FALSE, | |
351 | ), | |
352 | 'account_holder' => array( | |
353 | 'htmlType' => 'text', | |
354 | 'name' => 'account_holder', | |
355 | 'title' => ts('Account Holder'), | |
356 | 'cc_field' => TRUE, | |
357 | 'attributes' => array( | |
358 | 'size' => 20, | |
359 | 'maxlength' => 34, | |
360 | 'autocomplete' => 'on' | |
361 | ), | |
362 | 'is_required' => TRUE, | |
363 | ), | |
364 | //e.g. IBAN can have maxlength of 34 digits | |
365 | 'bank_account_number' => array( | |
366 | 'htmlType' => 'text', | |
367 | 'name' => 'bank_account_number', | |
368 | 'title' => ts('Bank Account Number'), | |
369 | 'cc_field' => TRUE, | |
370 | 'attributes' => array( | |
371 | 'size' => 20, | |
372 | 'maxlength' => 34, | |
373 | 'autocomplete' => 'off' | |
374 | ), | |
375 | 'rules' => array( | |
376 | array( | |
377 | 'rule_message' => ts('Please enter a valid Bank Identification Number (value must not contain punctuation characters).'), | |
378 | 'rule_name' => 'nopunctuation', | |
379 | 'rule_parameters' => NULL, | |
380 | )), | |
381 | 'is_required' => TRUE, | |
382 | ), | |
383 | //e.g. SWIFT-BIC can have maxlength of 11 digits | |
384 | 'bank_identification_number' => array( | |
385 | 'htmlType' => 'text', | |
386 | 'name' => 'bank_identification_number', | |
387 | 'title' => ts('Bank Identification Number'), | |
388 | 'cc_field' => TRUE, | |
389 | 'attributes' => array( | |
390 | 'size' => 20, | |
391 | 'maxlength' => 11, | |
392 | 'autocomplete' => 'off' | |
393 | ), | |
394 | 'is_required' => TRUE, | |
395 | 'rules' => array( | |
396 | array( | |
397 | 'rule_message' => ts('Please enter a valid Bank Identification Number (value must not contain punctuation characters).'), | |
398 | 'rule_name' => 'nopunctuation', | |
399 | 'rule_parameters' => NULL, | |
400 | )), | |
401 | ), | |
402 | 'bank_name' => array( | |
403 | 'htmlType' => 'text', | |
404 | 'name' => 'bank_name', | |
405 | 'title' => ts('Bank Name'), | |
406 | 'cc_field' => TRUE, | |
407 | 'attributes' => array( | |
408 | 'size' => 20, | |
409 | 'maxlength' => 64, | |
410 | 'autocomplete' => 'off' | |
411 | ), | |
412 | 'is_required' => TRUE, | |
413 | ||
414 | ) | |
415 | ); | |
416 | } | |
44b6505d | 417 | |
6a488035 TO |
418 | /** |
419 | * This function collects all the information from a web/api form and invokes | |
420 | * the relevant payment processor specific functions to perform the transaction | |
421 | * | |
422 | * @param array $params assoc array of input parameters for this transaction | |
423 | * | |
424 | * @return array the result in an nice formatted array (or an error object) | |
425 | * @abstract | |
426 | */ | |
427 | abstract function doDirectPayment(&$params); | |
428 | ||
c1cc3e0c EM |
429 | /** |
430 | * process payment - this function wraps around both doTransferPayment and doDirectPayment | |
431 | * it ensures an exception is thrown & moves some of this logic out of the form layer and makes the forms more agnostic | |
432 | * | |
433 | * @param array $params | |
434 | * | |
435 | * @param $component | |
436 | * | |
437 | * @throws CRM_Core_Exception | |
438 | */ | |
439 | public function doPayment(&$params, $component) { | |
440 | if ($this->_paymentProcessor['billing_mode'] == 4) { | |
441 | $result = $this->doTransferCheckout($params, $component); | |
442 | } | |
443 | else { | |
444 | $result = $this->doDirectPayment($params, $component); | |
445 | } | |
446 | if (is_a($result, 'CRM_Core_Error')) { | |
447 | throw new CRM_Core_Exception(CRM_Core_Error::getMessages($result)); | |
448 | } | |
449 | } | |
450 | ||
6a488035 TO |
451 | /** |
452 | * This function checks to see if we have the right config values | |
453 | * | |
414e3596 | 454 | * @internal param string $mode the mode we are operating in (live or test) |
6a488035 TO |
455 | * |
456 | * @return string the error message if any | |
457 | * @public | |
458 | */ | |
459 | abstract function checkConfig(); | |
460 | ||
a0ee3941 EM |
461 | /** |
462 | * @param $paymentProcessor | |
463 | * | |
464 | * @return bool | |
465 | */ | |
6a488035 TO |
466 | static function paypalRedirect(&$paymentProcessor) { |
467 | if (!$paymentProcessor) { | |
468 | return FALSE; | |
469 | } | |
470 | ||
471 | if (isset($_GET['payment_date']) && | |
472 | isset($_GET['merchant_return_link']) && | |
473 | CRM_Utils_Array::value('payment_status', $_GET) == 'Completed' && | |
474 | $paymentProcessor['payment_processor_type'] == "PayPal_Standard" | |
475 | ) { | |
476 | return TRUE; | |
477 | } | |
478 | ||
479 | return FALSE; | |
480 | } | |
481 | ||
482 | /** | |
483 | * Page callback for civicrm/payment/ipn | |
484 | * @public | |
485 | */ | |
486 | static function handleIPN() { | |
487 | self::handlePaymentMethod( | |
488 | 'PaymentNotification', | |
489 | array( | |
490 | 'processor_name' => @$_GET['processor_name'], | |
42b90e8f | 491 | 'processor_id' => @$_GET['processor_id'], |
6a488035 TO |
492 | 'mode' => @$_GET['mode'], |
493 | ) | |
494 | ); | |
495 | } | |
496 | ||
497 | /** | |
43d1ae00 EM |
498 | * Payment callback handler. The processor_name or processor_id is passed in. |
499 | * Note that processor_id is more reliable as one site may have more than one instance of a | |
500 | * processor & ideally the processor will be validating the results | |
6a488035 TO |
501 | * Load requested payment processor and call that processor's handle<$method> method |
502 | * | |
503 | * @public | |
4691b077 EM |
504 | * @param $method |
505 | * @param array $params | |
6a488035 | 506 | */ |
4691b077 | 507 | static function handlePaymentMethod($method, $params = array()) { |
42b90e8f CB |
508 | if (!isset($params['processor_id']) && !isset($params['processor_name'])) { |
509 | CRM_Core_Error::fatal("Either 'processor_id' or 'processor_name' param is required for payment callback"); | |
6a488035 | 510 | } |
e2bef985 | 511 | self::logPaymentNotification($params); |
6a488035 TO |
512 | |
513 | // Query db for processor .. | |
514 | $mode = @$params['mode']; | |
515 | ||
42b90e8f CB |
516 | $sql = "SELECT ppt.class_name, ppt.name as processor_name, pp.id AS processor_id |
517 | FROM civicrm_payment_processor_type ppt | |
518 | INNER JOIN civicrm_payment_processor pp | |
519 | ON pp.payment_processor_type_id = ppt.id | |
520 | AND pp.is_active | |
521 | AND pp.is_test = %1"; | |
522 | $args[1] = array($mode == 'test' ? 1 : 0, 'Integer'); | |
523 | ||
524 | if (isset($params['processor_id'])) { | |
525 | $sql .= " WHERE pp.id = %2"; | |
526 | $args[2] = array($params['processor_id'], 'Integer'); | |
527 | $notfound = "No active instances of payment processor ID#'{$params['processor_id']}' were found."; | |
528 | } | |
529 | else { | |
530 | $sql .= " WHERE ppt.name = %2"; | |
531 | $args[2] = array($params['processor_name'], 'String'); | |
532 | $notfound = "No active instances of the '{$params['processor_name']}' payment processor were found."; | |
533 | } | |
534 | ||
535 | $dao = CRM_Core_DAO::executeQuery($sql, $args); | |
6a488035 TO |
536 | |
537 | // Check whether we found anything at all .. | |
538 | if (!$dao->N) { | |
42b90e8f | 539 | CRM_Core_Error::fatal($notfound); |
6a488035 TO |
540 | } |
541 | ||
542 | $method = 'handle' . $method; | |
543 | $extension_instance_found = FALSE; | |
544 | ||
545 | // In all likelihood, we'll just end up with the one instance returned here. But it's | |
546 | // possible we may get more. Hence, iterate through all instances .. | |
547 | ||
548 | while ($dao->fetch()) { | |
549 | // Check pp is extension | |
550 | $ext = CRM_Extension_System::singleton()->getMapper(); | |
551 | if ($ext->isExtensionKey($dao->class_name)) { | |
6a488035 TO |
552 | $paymentClass = $ext->keyToClass($dao->class_name, 'payment'); |
553 | require_once $ext->classToPath($paymentClass); | |
554 | } | |
555 | else { | |
43d1ae00 | 556 | // Legacy or extension as module instance |
d031c654 | 557 | if (empty($paymentClass)) { |
43d1ae00 EM |
558 | $paymentClass = 'CRM_Core_' . $dao->class_name; |
559 | ||
560 | } | |
6a488035 TO |
561 | } |
562 | ||
563 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($dao->processor_id, $mode); | |
564 | ||
565 | // Should never be empty - we already established this processor_id exists and is active. | |
566 | if (empty($paymentProcessor)) { | |
567 | continue; | |
568 | } | |
569 | ||
570 | // Instantiate PP | |
0e6e8724 | 571 | $processorInstance = $paymentClass::singleton($mode, $paymentProcessor); |
6a488035 TO |
572 | |
573 | // Does PP implement this method, and can we call it? | |
574 | if (!method_exists($processorInstance, $method) || | |
575 | !is_callable(array($processorInstance, $method)) | |
576 | ) { | |
43d1ae00 EM |
577 | // on the off chance there is a double implementation of this processor we should keep looking for another |
578 | // note that passing processor_id is more reliable & we should work to deprecate processor_name | |
579 | continue; | |
6a488035 TO |
580 | } |
581 | ||
582 | // Everything, it seems, is ok - execute pp callback handler | |
583 | $processorInstance->$method(); | |
a5ef96f6 | 584 | $extension_instance_found = TRUE; |
6a488035 TO |
585 | } |
586 | ||
587 | if (!$extension_instance_found) CRM_Core_Error::fatal( | |
588 | "No extension instances of the '{$params['processor_name']}' payment processor were found.<br />" . | |
589 | "$method method is unsupported in legacy payment processors." | |
590 | ); | |
591 | ||
592 | // Exit here on web requests, allowing just the plain text response to be echoed | |
593 | if ($method == 'handlePaymentNotification') { | |
594 | CRM_Utils_System::civiExit(); | |
595 | } | |
596 | } | |
597 | ||
598 | /** | |
599 | * Function to check whether a method is present ( & supported ) by the payment processor object. | |
600 | * | |
601 | * @param string $method method to check for. | |
602 | * | |
603 | * @return boolean | |
604 | * @public | |
605 | */ | |
606 | function isSupported($method = 'cancelSubscription') { | |
607 | return method_exists(CRM_Utils_System::getClassName($this), $method); | |
608 | } | |
609 | ||
a0ee3941 EM |
610 | /** |
611 | * @param null $entityID | |
612 | * @param null $entity | |
613 | * @param string $action | |
614 | * | |
615 | * @return string | |
616 | */ | |
6a488035 | 617 | function subscriptionURL($entityID = NULL, $entity = NULL, $action = 'cancel') { |
03cfff4c KW |
618 | // Set URL |
619 | switch ($action) { | |
620 | case 'cancel' : | |
621 | $url = 'civicrm/contribute/unsubscribe'; | |
622 | break; | |
03cfff4c KW |
623 | case 'billing' : |
624 | //in notify mode don't return the update billing url | |
68acd6ae | 625 | if (!$this->isSupported('updateSubscriptionBillingInfo')) { |
03cfff4c KW |
626 | return NULL; |
627 | } | |
68acd6ae | 628 | $url = 'civicrm/contribute/updatebilling'; |
03cfff4c | 629 | break; |
03cfff4c KW |
630 | case 'update' : |
631 | $url = 'civicrm/contribute/updaterecur'; | |
632 | break; | |
6a488035 TO |
633 | } |
634 | ||
03cfff4c KW |
635 | $session = CRM_Core_Session::singleton(); |
636 | $userId = $session->get('userID'); | |
637 | $contactID = 0; | |
638 | $checksumValue = ''; | |
639 | $entityArg = ''; | |
640 | ||
641 | // Find related Contact | |
642 | if ($entityID) { | |
643 | switch ($entity) { | |
644 | case 'membership' : | |
645 | $contactID = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $entityID, "contact_id"); | |
646 | $entityArg = 'mid'; | |
647 | break; | |
648 | ||
649 | case 'contribution' : | |
650 | $contactID = CRM_Core_DAO::getFieldValue("CRM_Contribute_DAO_Contribution", $entityID, "contact_id"); | |
651 | $entityArg = 'coid'; | |
652 | break; | |
653 | ||
654 | case 'recur' : | |
655 | $sql = " | |
6a488035 TO |
656 | SELECT con.contact_id |
657 | FROM civicrm_contribution_recur rec | |
658 | INNER JOIN civicrm_contribution con ON ( con.contribution_recur_id = rec.id ) | |
659 | WHERE rec.id = %1 | |
660 | GROUP BY rec.id"; | |
03cfff4c KW |
661 | $contactID = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($entityID, 'Integer'))); |
662 | $entityArg = 'crid'; | |
663 | break; | |
6a488035 | 664 | } |
6a488035 TO |
665 | } |
666 | ||
03cfff4c KW |
667 | // Add entity arguments |
668 | if ($entityArg != '') { | |
669 | // Add checksum argument | |
670 | if ($contactID != 0 && $userId != $contactID) { | |
671 | $checksumValue = '&cs=' . CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf'); | |
672 | } | |
673 | return CRM_Utils_System::url($url, "reset=1&{$entityArg}={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE); | |
674 | } | |
675 | ||
676 | // Else login URL | |
6a488035 TO |
677 | if ($this->isSupported('accountLoginURL')) { |
678 | return $this->accountLoginURL(); | |
679 | } | |
03cfff4c KW |
680 | |
681 | // Else default | |
6a488035 TO |
682 | return $this->_paymentProcessor['url_recur']; |
683 | } | |
6a488035 | 684 | } |