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