CRM-17066 - Specify api permissions for messageTemplates
[civicrm-core.git] / CRM / Core / Payment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 27
914a49bf 28use Civi\Payment\System;
353ffa53 29
6a488035 30/**
3782df3e 31 * Class CRM_Core_Payment.
6a488035 32 *
3782df3e 33 * This class is the main class for the payment processor subsystem.
6a488035 34 *
3782df3e
EM
35 * It is the parent class for payment processors. It also holds some IPN related functions
36 * that need to be moved. In particular handlePaymentMethod should be moved to a factory class.
6a488035 37 */
6a488035
TO
38abstract class CRM_Core_Payment {
39
40 /**
100fef9d 41 * How are we getting billing information?
6a488035
TO
42 *
43 * FORM - we collect it on the same page
44 * BUTTON - the processor collects it and sends it back to us via some protocol
45 */
7da04cde 46 const
6a488035
TO
47 BILLING_MODE_FORM = 1,
48 BILLING_MODE_BUTTON = 2,
49 BILLING_MODE_NOTIFY = 4;
50
51 /**
100fef9d 52 * Which payment type(s) are we using?
6a488035
TO
53 *
54 * credit card
55 * direct debit
56 * or both
43e5f0f6 57 * @todo create option group - nb omnipay uses a 3rd type - transparent redirect cc
6a488035 58 */
7da04cde 59 const
6a488035
TO
60 PAYMENT_TYPE_CREDIT_CARD = 1,
61 PAYMENT_TYPE_DIRECT_DEBIT = 2;
62
63 /**
64 * Subscription / Recurring payment Status
65 * START, END
6a488035 66 */
7da04cde 67 const
6a488035
TO
68 RECURRING_PAYMENT_START = 'START',
69 RECURRING_PAYMENT_END = 'END';
70
353ffa53 71 protected $_paymentProcessor;
6a488035
TO
72
73 /**
d09edf64 74 * Singleton function used to manage this object.
3782df3e 75 *
914a49bf
EM
76 * We will migrate to calling Civi\Payment\System::singleton()->getByProcessor($paymentProcessor)
77 * & Civi\Payment\System::singleton()->getById($paymentProcessor) directly as the main access methods & work
78 * to remove this function all together
6a488035 79 *
6a0b768e
TO
80 * @param string $mode
81 * The mode of operation: live or test.
82 * @param array $paymentProcessor
83 * The details of the payment processor being invoked.
84 * @param object $paymentForm
85 * Deprecated - avoid referring to this if possible. If you have to use it document why as this is scary interaction.
86 * @param bool $force
87 * Should we force a reload of this payment object.
88 *
89 * @return CRM_Core_Payment
52767de0 90 * @throws \CRM_Core_Exception
6a488035 91 */
00be9182 92 public static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
6a488035
TO
93 // make sure paymentProcessor is not empty
94 // CRM-7424
95 if (empty($paymentProcessor)) {
96 return CRM_Core_DAO::$_nullObject;
97 }
ef1c2283
EM
98 //we use two lines because we can't remove the '&singleton' without risking breakage
99 //of extension classes that extend this one
353ffa53 100 $object = Civi\Payment\System::singleton()->getByProcessor($paymentProcessor);
ef1c2283 101 return $object;
6a488035
TO
102 }
103
e2bef985 104 /**
3782df3e
EM
105 * Log payment notification message to forensic system log.
106 *
43e5f0f6 107 * @todo move to factory class \Civi\Payment\System (or similar)
3782df3e
EM
108 *
109 * @param array $params
110 *
e2bef985 111 * @return mixed
112 */
113 public static function logPaymentNotification($params) {
414e3596 114 $message = 'payment_notification ';
e2bef985 115 if (!empty($params['processor_name'])) {
414e3596 116 $message .= 'processor_name=' . $params['processor_name'];
e2bef985 117 }
118 if (!empty($params['processor_id'])) {
119 $message .= 'processor_id=' . $params['processor_id'];
120 }
414e3596 121
122 $log = new CRM_Utils_SystemLogger();
123 $log->alert($message, $_REQUEST);
e2bef985 124 }
125
fbcb6fba 126 /**
d09edf64 127 * Check if capability is supported.
3782df3e
EM
128 *
129 * Capabilities have a one to one relationship with capability-related functions on this class.
130 *
131 * Payment processor classes should over-ride the capability-specific function rather than this one.
132 *
6a0b768e
TO
133 * @param string $capability
134 * E.g BackOffice, LiveMode, FutureRecurStartDate.
fbcb6fba
EM
135 *
136 * @return bool
137 */
138 public function supports($capability) {
139 $function = 'supports' . ucfirst($capability);
140 if (method_exists($this, $function)) {
141 return $this->$function();
142 }
143 return FALSE;
144 }
145
146 /**
3782df3e
EM
147 * Are back office payments supported.
148 *
149 * e.g paypal standard won't permit you to enter a credit card associated
150 * with someone else's login.
151 * The intention is to support off-site (other than paypal) & direct debit but that is not all working yet so to
152 * reach a 'stable' point we disable.
153 *
fbcb6fba
EM
154 * @return bool
155 */
d8ce0d68 156 protected function supportsBackOffice() {
9c39fb25
EM
157 if ($this->_paymentProcessor['billing_mode'] == 4 || $this->_paymentProcessor['payment_type'] != 1) {
158 return FALSE;
159 }
160 else {
161 return TRUE;
162 }
fbcb6fba
EM
163 }
164
5c3744ff 165 /**
166 * Can more than one transaction be processed at once?
167 *
168 * In general processors that process payment by server to server communication support this while others do not.
169 *
170 * In future we are likely to hit an issue where this depends on whether a token already exists.
171 *
172 * @return bool
173 */
174 protected function supportsMultipleConcurrentPayments() {
175 if ($this->_paymentProcessor['billing_mode'] == 4 || $this->_paymentProcessor['payment_type'] != 1) {
176 return FALSE;
177 }
178 else {
179 return TRUE;
180 }
181 }
182
fbcb6fba 183 /**
3782df3e
EM
184 * Are live payments supported - e.g dummy doesn't support this.
185 *
fbcb6fba
EM
186 * @return bool
187 */
d8ce0d68 188 protected function supportsLiveMode() {
fbcb6fba
EM
189 return TRUE;
190 }
191
52767de0 192 /**
d09edf64 193 * Are test payments supported.
3782df3e 194 *
52767de0
EM
195 * @return bool
196 */
197 protected function supportsTestMode() {
198 return TRUE;
199 }
200
fbcb6fba 201 /**
d09edf64 202 * Should the first payment date be configurable when setting up back office recurring payments.
3782df3e 203 *
fbcb6fba 204 * We set this to false for historical consistency but in fact most new processors use tokens for recurring and can support this
3782df3e 205 *
fbcb6fba
EM
206 * @return bool
207 */
d8ce0d68 208 protected function supportsFutureRecurStartDate() {
fbcb6fba
EM
209 return FALSE;
210 }
211
677fe56c
EM
212 /**
213 * Can recurring contributions be set against pledges.
214 *
215 * In practice all processors that use the baseIPN function to finish transactions or
216 * call the completetransaction api support this by looking up previous contributions in the
217 * series and, if there is a prior contribution against a pledge, and the pledge is not complete,
218 * adding the new payment to the pledge.
219 *
220 * However, only enabling for processors it has been tested against.
221 *
222 * @return bool
223 */
224 protected function supportsRecurContributionsForPledges() {
225 return FALSE;
226 }
227
6a488035 228 /**
3782df3e
EM
229 * Default payment instrument validation.
230 *
a479fe60 231 * Implement the usual Luhn algorithm via a static function in the CRM_Core_Payment_Form if it's a credit card
3782df3e
EM
232 * Not a static function, because I need to check for payment_type.
233 *
234 * @param array $values
235 * @param array $errors
a479fe60 236 */
237 public function validatePaymentInstrument($values, &$errors) {
238 if ($this->_paymentProcessor['payment_type'] == 1) {
239 CRM_Core_Payment_Form::validateCreditCard($values, $errors);
240 }
241 }
242
80bcd255
EM
243 /**
244 * Getter for the payment processor.
245 *
246 * The payment processor array is based on the civicrm_payment_processor table entry.
247 *
248 * @return array
249 * Payment processor array.
250 */
251 public function getPaymentProcessor() {
252 return $this->_paymentProcessor;
253 }
254
255 /**
256 * Setter for the payment processor.
257 *
258 * @param array $processor
259 */
260 public function setPaymentProcessor($processor) {
261 $this->_paymentProcessor = $processor;
262 }
263
6a488035 264 /**
3782df3e
EM
265 * Setter for the payment form that wants to use the processor.
266 *
43e5f0f6 267 * @deprecated
3782df3e 268 *
ac32ed13 269 * @param CRM_Core_Form $paymentForm
6a488035 270 */
00be9182 271 public function setForm(&$paymentForm) {
6a488035
TO
272 $this->_paymentForm = $paymentForm;
273 }
274
275 /**
d09edf64 276 * Getter for payment form that is using the processor.
43e5f0f6 277 * @deprecated
16b10e64
CW
278 * @return CRM_Core_Form
279 * A form object
6a488035 280 */
00be9182 281 public function getForm() {
6a488035
TO
282 return $this->_paymentForm;
283 }
284
285 /**
d09edf64 286 * Getter for accessing member vars.
6c99ada1 287 *
43e5f0f6 288 * @todo believe this is unused
6c99ada1 289 *
100fef9d 290 * @param string $name
dc913073
EM
291 *
292 * @return null
6a488035 293 */
00be9182 294 public function getVar($name) {
6a488035
TO
295 return isset($this->$name) ? $this->$name : NULL;
296 }
297
dc913073 298 /**
d09edf64 299 * Get name for the payment information type.
43e5f0f6 300 * @todo - use option group + name field (like Omnipay does)
dc913073
EM
301 * @return string
302 */
303 public function getPaymentTypeName() {
459091e1 304 return $this->_paymentProcessor['payment_type'] == 1 ? 'credit_card' : 'direct_debit';
dc913073
EM
305 }
306
307 /**
d09edf64 308 * Get label for the payment information type.
43e5f0f6 309 * @todo - use option group + labels (like Omnipay does)
dc913073
EM
310 * @return string
311 */
312 public function getPaymentTypeLabel() {
459091e1 313 return $this->_paymentProcessor['payment_type'] == 1 ? 'Credit Card' : 'Direct Debit';
dc913073
EM
314 }
315
44b6505d 316 /**
d09edf64 317 * Get array of fields that should be displayed on the payment form.
44b6505d
EM
318 * @todo make payment type an option value & use it in the function name - currently on debit & credit card work
319 * @return array
320 * @throws CiviCRM_API3_Exception
321 */
322 public function getPaymentFormFields() {
dc913073 323 if ($this->_paymentProcessor['billing_mode'] == 4) {
44b6505d
EM
324 return array();
325 }
326 return $this->_paymentProcessor['payment_type'] == 1 ? $this->getCreditCardFormFields() : $this->getDirectDebitFormFields();
327 }
328
329 /**
d09edf64 330 * Get array of fields that should be displayed on the payment form for credit cards.
dc913073 331 *
44b6505d
EM
332 * @return array
333 */
334 protected function getCreditCardFormFields() {
335 return array(
336 'credit_card_type',
337 'credit_card_number',
338 'cvv2',
339 'credit_card_exp_date',
340 );
341 }
342
343 /**
d09edf64 344 * Get array of fields that should be displayed on the payment form for direct debits.
dc913073 345 *
44b6505d
EM
346 * @return array
347 */
348 protected function getDirectDebitFormFields() {
349 return array(
350 'account_holder',
351 'bank_account_number',
352 'bank_identification_number',
353 'bank_name',
354 );
355 }
356
dc913073 357 /**
d09edf64 358 * Return an array of all the details about the fields potentially required for payment fields.
3782df3e 359 *
dc913073
EM
360 * Only those determined by getPaymentFormFields will actually be assigned to the form
361 *
a6c01b45
CW
362 * @return array
363 * field metadata
dc913073
EM
364 */
365 public function getPaymentFormFieldsMetadata() {
366 //@todo convert credit card type into an option value
367 $creditCardType = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::creditCard();
368 return array(
369 'credit_card_number' => array(
370 'htmlType' => 'text',
371 'name' => 'credit_card_number',
372 'title' => ts('Card Number'),
373 'cc_field' => TRUE,
374 'attributes' => array(
375 'size' => 20,
376 'maxlength' => 20,
21dfd5f5 377 'autocomplete' => 'off',
f803aacb 378 'class' => 'creditcard',
dc913073
EM
379 ),
380 'is_required' => TRUE,
381 ),
382 'cvv2' => array(
383 'htmlType' => 'text',
384 'name' => 'cvv2',
385 'title' => ts('Security Code'),
386 'cc_field' => TRUE,
387 'attributes' => array(
388 'size' => 5,
389 'maxlength' => 10,
21dfd5f5 390 'autocomplete' => 'off',
dc913073
EM
391 ),
392 'is_required' => CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
393 'cvv_backoffice_required',
394 NULL,
395 1
396 ),
397 'rules' => array(
398 array(
399 '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.'),
400 'rule_name' => 'integer',
401 'rule_parameters' => NULL,
7c550ca0 402 ),
353ffa53 403 ),
dc913073
EM
404 ),
405 'credit_card_exp_date' => array(
406 'htmlType' => 'date',
407 'name' => 'credit_card_exp_date',
408 'title' => ts('Expiration Date'),
409 'cc_field' => TRUE,
410 'attributes' => CRM_Core_SelectValues::date('creditCard'),
411 'is_required' => TRUE,
412 'rules' => array(
413 array(
414 'rule_message' => ts('Card expiration date cannot be a past date.'),
415 'rule_name' => 'currentDate',
416 'rule_parameters' => TRUE,
7c550ca0 417 ),
353ffa53 418 ),
dc913073
EM
419 ),
420 'credit_card_type' => array(
421 'htmlType' => 'select',
422 'name' => 'credit_card_type',
423 'title' => ts('Card Type'),
424 'cc_field' => TRUE,
425 'attributes' => $creditCardType,
426 'is_required' => FALSE,
427 ),
428 'account_holder' => array(
429 'htmlType' => 'text',
430 'name' => 'account_holder',
431 'title' => ts('Account Holder'),
432 'cc_field' => TRUE,
433 'attributes' => array(
434 'size' => 20,
435 'maxlength' => 34,
21dfd5f5 436 'autocomplete' => 'on',
dc913073
EM
437 ),
438 'is_required' => TRUE,
439 ),
440 //e.g. IBAN can have maxlength of 34 digits
441 'bank_account_number' => array(
442 'htmlType' => 'text',
443 'name' => 'bank_account_number',
444 'title' => ts('Bank Account Number'),
445 'cc_field' => TRUE,
446 'attributes' => array(
447 'size' => 20,
448 'maxlength' => 34,
21dfd5f5 449 'autocomplete' => 'off',
dc913073
EM
450 ),
451 'rules' => array(
452 array(
453 'rule_message' => ts('Please enter a valid Bank Identification Number (value must not contain punctuation characters).'),
454 'rule_name' => 'nopunctuation',
455 'rule_parameters' => NULL,
7c550ca0 456 ),
353ffa53 457 ),
dc913073
EM
458 'is_required' => TRUE,
459 ),
460 //e.g. SWIFT-BIC can have maxlength of 11 digits
461 'bank_identification_number' => array(
462 'htmlType' => 'text',
463 'name' => 'bank_identification_number',
464 'title' => ts('Bank Identification Number'),
465 'cc_field' => TRUE,
466 'attributes' => array(
467 'size' => 20,
468 'maxlength' => 11,
21dfd5f5 469 'autocomplete' => 'off',
dc913073
EM
470 ),
471 'is_required' => TRUE,
472 'rules' => array(
473 array(
474 'rule_message' => ts('Please enter a valid Bank Identification Number (value must not contain punctuation characters).'),
475 'rule_name' => 'nopunctuation',
476 'rule_parameters' => NULL,
7c550ca0 477 ),
353ffa53 478 ),
dc913073
EM
479 ),
480 'bank_name' => array(
481 'htmlType' => 'text',
482 'name' => 'bank_name',
483 'title' => ts('Bank Name'),
484 'cc_field' => TRUE,
485 'attributes' => array(
486 'size' => 20,
487 'maxlength' => 64,
21dfd5f5 488 'autocomplete' => 'off',
dc913073
EM
489 ),
490 'is_required' => TRUE,
491
21dfd5f5 492 ),
dc913073
EM
493 );
494 }
44b6505d 495
6a488035 496 /**
8319cf11
EM
497 * Calling this from outside the payment subsystem is deprecated - use doPayment.
498 *
499 * Does a server to server payment transaction.
500 *
501 * Note that doPayment will throw an exception so the code may need to be modified
6a488035 502 *
6a0b768e
TO
503 * @param array $params
504 * Assoc array of input parameters for this transaction.
6a488035 505 *
a6c01b45
CW
506 * @return array
507 * the result in an nice formatted array (or an error object)
6a488035
TO
508 * @abstract
509 */
7c550ca0 510 abstract protected function doDirectPayment(&$params);
6a488035 511
c1cc3e0c 512 /**
6c99ada1
EM
513 * Process payment - this function wraps around both doTransferPayment and doDirectPayment.
514 *
515 * The function ensures an exception is thrown & moves some of this logic out of the form layer and makes the forms
516 * more agnostic.
c1cc3e0c
EM
517 *
518 * @param array $params
519 *
520 * @param $component
521 *
a6c01b45
CW
522 * @return array
523 * (modified)
c1cc3e0c
EM
524 * @throws CRM_Core_Exception
525 */
8319cf11 526 public function doPayment(&$params, $component = 'contribute') {
c1cc3e0c
EM
527 if ($this->_paymentProcessor['billing_mode'] == 4) {
528 $result = $this->doTransferCheckout($params, $component);
529 }
530 else {
531 $result = $this->doDirectPayment($params, $component);
532 }
533 if (is_a($result, 'CRM_Core_Error')) {
534 throw new CRM_Core_Exception(CRM_Core_Error::getMessages($result));
535 }
a9cf9972 536 //CRM-15767 - Submit Credit Card Contribution not being saved
537 return $result;
c1cc3e0c
EM
538 }
539
6a488035 540 /**
d09edf64 541 * This function checks to see if we have the right config values.
6a488035 542 *
a6c01b45
CW
543 * @return string
544 * the error message if any
6a488035 545 */
7c550ca0 546 abstract protected function checkConfig();
6a488035 547
a0ee3941 548 /**
6c99ada1
EM
549 * Redirect for paypal.
550 *
551 * @todo move to paypal class or remove
552 *
a0ee3941 553 * @param $paymentProcessor
6c99ada1 554 *
a0ee3941
EM
555 * @return bool
556 */
00be9182 557 public static function paypalRedirect(&$paymentProcessor) {
6a488035
TO
558 if (!$paymentProcessor) {
559 return FALSE;
560 }
561
562 if (isset($_GET['payment_date']) &&
563 isset($_GET['merchant_return_link']) &&
564 CRM_Utils_Array::value('payment_status', $_GET) == 'Completed' &&
565 $paymentProcessor['payment_processor_type'] == "PayPal_Standard"
566 ) {
567 return TRUE;
568 }
569
570 return FALSE;
571 }
572
573 /**
6c99ada1
EM
574 * Handle incoming payment notification.
575 *
576 * IPNs, also called silent posts are notifications of payment outcomes or activity on an external site.
577 *
43e5f0f6 578 * @todo move to0 \Civi\Payment\System factory method
6a488035 579 * Page callback for civicrm/payment/ipn
6a488035 580 */
00be9182 581 public static function handleIPN() {
6a488035
TO
582 self::handlePaymentMethod(
583 'PaymentNotification',
584 array(
585 'processor_name' => @$_GET['processor_name'],
42b90e8f 586 'processor_id' => @$_GET['processor_id'],
6a488035 587 'mode' => @$_GET['mode'],
4164f4e1 588 'q' => @$_GET['q'],
6a488035
TO
589 )
590 );
160c9df2 591 CRM_Utils_System::civiExit();
6a488035
TO
592 }
593
594 /**
3782df3e
EM
595 * Payment callback handler.
596 *
597 * The processor_name or processor_id is passed in.
43d1ae00
EM
598 * Note that processor_id is more reliable as one site may have more than one instance of a
599 * processor & ideally the processor will be validating the results
6a488035
TO
600 * Load requested payment processor and call that processor's handle<$method> method
601 *
3782df3e
EM
602 * @todo move to \Civi\Payment\System factory method
603 *
604 * @param string $method
605 * 'PaymentNotification' or 'PaymentCron'
4691b077 606 * @param array $params
23de1ac0
EM
607 *
608 * @throws \CRM_Core_Exception
609 * @throws \Exception
6a488035 610 */
00be9182 611 public static function handlePaymentMethod($method, $params = array()) {
42b90e8f 612 if (!isset($params['processor_id']) && !isset($params['processor_name'])) {
4164f4e1
EM
613 $q = explode('/', CRM_Utils_Array::value('q', $params, ''));
614 $lastParam = array_pop($q);
615 if (is_numeric($lastParam)) {
616 $params['processor_id'] = $_GET['processor_id'] = $lastParam;
617 }
618 else {
0ac9fd52 619 throw new CRM_Core_Exception("Either 'processor_id' (recommended) or 'processor_name' (deprecated) is required for payment callback.");
4164f4e1 620 }
6a488035 621 }
4164f4e1 622
e2bef985 623 self::logPaymentNotification($params);
6a488035 624
42b90e8f
CB
625 $sql = "SELECT ppt.class_name, ppt.name as processor_name, pp.id AS processor_id
626 FROM civicrm_payment_processor_type ppt
627 INNER JOIN civicrm_payment_processor pp
628 ON pp.payment_processor_type_id = ppt.id
9ff0c7a1 629 AND pp.is_active";
42b90e8f
CB
630
631 if (isset($params['processor_id'])) {
632 $sql .= " WHERE pp.id = %2";
633 $args[2] = array($params['processor_id'], 'Integer');
0ac9fd52 634 $notFound = ts("No active instances of payment processor %1 were found.", array(1 => $params['processor_id']));
42b90e8f
CB
635 }
636 else {
9ff0c7a1
EM
637 // This is called when processor_name is passed - passing processor_id instead is recommended.
638 $sql .= " WHERE ppt.name = %2 AND pp.is_test = %1";
6c99ada1
EM
639 $args[1] = array(
640 (CRM_Utils_Array::value('mode', $params) == 'test') ? 1 : 0,
641 'Integer',
642 );
42b90e8f 643 $args[2] = array($params['processor_name'], 'String');
0ac9fd52 644 $notFound = ts("No active instances of payment processor '%1' were found.", array(1 => $params['processor_name']));
42b90e8f
CB
645 }
646
647 $dao = CRM_Core_DAO::executeQuery($sql, $args);
6a488035 648
3782df3e 649 // Check whether we found anything at all.
6a488035 650 if (!$dao->N) {
3782df3e 651 CRM_Core_Error::fatal($notFound);
6a488035
TO
652 }
653
654 $method = 'handle' . $method;
655 $extension_instance_found = FALSE;
656
657 // In all likelihood, we'll just end up with the one instance returned here. But it's
658 // possible we may get more. Hence, iterate through all instances ..
659
660 while ($dao->fetch()) {
5495e78a 661 // Check pp is extension - is this still required - surely the singleton below handles it.
6a488035
TO
662 $ext = CRM_Extension_System::singleton()->getMapper();
663 if ($ext->isExtensionKey($dao->class_name)) {
6a488035
TO
664 $paymentClass = $ext->keyToClass($dao->class_name, 'payment');
665 require_once $ext->classToPath($paymentClass);
666 }
6a488035 667
b47b1631 668 $processorInstance = Civi\Payment\System::singleton()->getById($dao->processor_id);
6a488035
TO
669
670 // Should never be empty - we already established this processor_id exists and is active.
81ebda7b 671 if (empty($processorInstance)) {
6a488035
TO
672 continue;
673 }
674
6a488035
TO
675 // Does PP implement this method, and can we call it?
676 if (!method_exists($processorInstance, $method) ||
677 !is_callable(array($processorInstance, $method))
678 ) {
43d1ae00
EM
679 // on the off chance there is a double implementation of this processor we should keep looking for another
680 // note that passing processor_id is more reliable & we should work to deprecate processor_name
681 continue;
6a488035
TO
682 }
683
684 // Everything, it seems, is ok - execute pp callback handler
685 $processorInstance->$method();
a5ef96f6 686 $extension_instance_found = TRUE;
6a488035
TO
687 }
688
4f99ca55 689 if (!$extension_instance_found) {
0ac9fd52
CB
690 $message = "No extension instances of the '%1' payment processor were found.<br />" .
691 "%2 method is unsupported in legacy payment processors.";
692 CRM_Core_Error::fatal(ts($message, array(1 => $params['processor_name'], 2 => $method)));
2aa397bc 693 }
6a488035
TO
694 }
695
696 /**
100fef9d 697 * Check whether a method is present ( & supported ) by the payment processor object.
6a488035 698 *
6a0b768e
TO
699 * @param string $method
700 * Method to check for.
6a488035 701 *
7c550ca0 702 * @return bool
6a488035 703 */
00be9182 704 public function isSupported($method = 'cancelSubscription') {
6a488035
TO
705 return method_exists(CRM_Utils_System::getClassName($this), $method);
706 }
707
a0ee3941 708 /**
3782df3e
EM
709 * Get url for users to manage this recurring contribution for this processor.
710 *
100fef9d 711 * @param int $entityID
a0ee3941
EM
712 * @param null $entity
713 * @param string $action
714 *
715 * @return string
716 */
00be9182 717 public function subscriptionURL($entityID = NULL, $entity = NULL, $action = 'cancel') {
03cfff4c
KW
718 // Set URL
719 switch ($action) {
2aa397bc 720 case 'cancel':
03cfff4c
KW
721 $url = 'civicrm/contribute/unsubscribe';
722 break;
2aa397bc
TO
723
724 case 'billing':
03cfff4c 725 //in notify mode don't return the update billing url
68acd6ae 726 if (!$this->isSupported('updateSubscriptionBillingInfo')) {
03cfff4c
KW
727 return NULL;
728 }
68acd6ae 729 $url = 'civicrm/contribute/updatebilling';
03cfff4c 730 break;
2aa397bc
TO
731
732 case 'update':
03cfff4c
KW
733 $url = 'civicrm/contribute/updaterecur';
734 break;
6a488035
TO
735 }
736
353ffa53
TO
737 $session = CRM_Core_Session::singleton();
738 $userId = $session->get('userID');
739 $contactID = 0;
03cfff4c 740 $checksumValue = '';
353ffa53 741 $entityArg = '';
03cfff4c
KW
742
743 // Find related Contact
744 if ($entityID) {
745 switch ($entity) {
2aa397bc 746 case 'membership':
03cfff4c
KW
747 $contactID = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $entityID, "contact_id");
748 $entityArg = 'mid';
749 break;
750
2aa397bc 751 case 'contribution':
03cfff4c
KW
752 $contactID = CRM_Core_DAO::getFieldValue("CRM_Contribute_DAO_Contribution", $entityID, "contact_id");
753 $entityArg = 'coid';
754 break;
755
2aa397bc 756 case 'recur':
03cfff4c 757 $sql = "
6a488035
TO
758 SELECT con.contact_id
759 FROM civicrm_contribution_recur rec
760INNER JOIN civicrm_contribution con ON ( con.contribution_recur_id = rec.id )
761 WHERE rec.id = %1
762 GROUP BY rec.id";
03cfff4c
KW
763 $contactID = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($entityID, 'Integer')));
764 $entityArg = 'crid';
765 break;
6a488035 766 }
6a488035
TO
767 }
768
03cfff4c
KW
769 // Add entity arguments
770 if ($entityArg != '') {
771 // Add checksum argument
772 if ($contactID != 0 && $userId != $contactID) {
773 $checksumValue = '&cs=' . CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf');
774 }
775 return CRM_Utils_System::url($url, "reset=1&{$entityArg}={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
776 }
777
778 // Else login URL
6a488035
TO
779 if ($this->isSupported('accountLoginURL')) {
780 return $this->accountLoginURL();
781 }
03cfff4c
KW
782
783 // Else default
5c3744ff 784 return isset($this->_paymentProcessor['url_recur']) ? $this->_paymentProcessor['url_recur'] : '';
6a488035 785 }
96025800 786
6a488035 787}