3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 abstract class CRM_Core_Payment
{
39 * how are we getting billing information?
41 * FORM - we collect it on the same page
42 * BUTTON - the processor collects it and sends it back to us via some protocol
45 BILLING_MODE_FORM
= 1,
46 BILLING_MODE_BUTTON
= 2,
47 BILLING_MODE_NOTIFY
= 4;
50 * which payment type(s) are we using?
58 PAYMENT_TYPE_CREDIT_CARD
= 1,
59 PAYMENT_TYPE_DIRECT_DEBIT
= 2;
62 * Subscription / Recurring payment Status
67 RECURRING_PAYMENT_START
= 'START',
68 RECURRING_PAYMENT_END
= 'END';
71 * We only need one instance of this object. So we use the singleton
72 * pattern and cache the instance in this variable
77 static private $_singleton = NULL;
79 protected $_paymentProcessor;
81 protected $_paymentForm = NULL;
84 * singleton function used to manage this object
86 * @param string $mode the mode of operation: live or test
87 * @param object $paymentProcessor the details of the payment processor being invoked
88 * @param object $paymentForm reference to the form object if available
89 * @param boolean $force should we force a reload of this payment object
95 static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
96 // make sure paymentProcessor is not empty
98 if (empty($paymentProcessor)) {
99 return CRM_Core_DAO
::$_nullObject;
102 $cacheKey = "{$mode}_{$paymentProcessor['id']}_" . (int)isset($paymentForm);
103 if (!isset(self
::$_singleton[$cacheKey]) ||
$force) {
104 $config = CRM_Core_Config
::singleton();
105 $ext = CRM_Extension_System
::singleton()->getMapper();
106 if ($ext->isExtensionKey($paymentProcessor['class_name'])) {
107 $paymentClass = $ext->keyToClass($paymentProcessor['class_name'], 'payment');
108 require_once ($ext->classToPath($paymentClass));
111 $paymentClass = 'CRM_Core_' . $paymentProcessor['class_name'];
112 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $paymentClass) . '.php');
116 self
::$_singleton[$cacheKey] = $paymentClass::singleton($mode, $paymentProcessor);
119 //load the payment form for required processor.
120 if ($paymentForm !== NULL) {
121 self
::$_singleton[$cacheKey]->setForm($paymentForm);
124 return self
::$_singleton[$cacheKey];
132 public static function logPaymentNotification($params) {
133 $message = 'payment_notification ';
134 if (!empty($params['processor_name'])) {
135 $message .= 'processor_name=' . $params['processor_name'];
137 if (!empty($params['processor_id'])) {
138 $message .= 'processor_id=' . $params['processor_id'];
141 $log = new CRM_Utils_SystemLogger();
142 $log->alert($message, $_REQUEST);
146 * Setter for the payment form that wants to use the processor
148 * @param obj $paymentForm
151 function setForm(&$paymentForm) {
152 $this->_paymentForm
= $paymentForm;
156 * Getter for payment form that is using the processor
158 * @return obj A form object
161 return $this->_paymentForm
;
165 * Getter for accessing member vars
168 function getVar($name) {
169 return isset($this->$name) ?
$this->$name : NULL;
173 * This function collects all the information from a web/api form and invokes
174 * the relevant payment processor specific functions to perform the transaction
176 * @param array $params assoc array of input parameters for this transaction
178 * @return array the result in an nice formatted array (or an error object)
181 abstract function doDirectPayment(&$params);
184 * This function checks to see if we have the right config values
186 * @internal param string $mode the mode we are operating in (live or test)
188 * @return string the error message if any
191 abstract function checkConfig();
193 static function paypalRedirect(&$paymentProcessor) {
194 if (!$paymentProcessor) {
198 if (isset($_GET['payment_date']) &&
199 isset($_GET['merchant_return_link']) &&
200 CRM_Utils_Array
::value('payment_status', $_GET) == 'Completed' &&
201 $paymentProcessor['payment_processor_type'] == "PayPal_Standard"
210 * Page callback for civicrm/payment/ipn
213 static function handleIPN() {
214 self
::handlePaymentMethod(
215 'PaymentNotification',
217 'processor_name' => @$_GET['processor_name'],
218 'processor_id' => @$_GET['processor_id'],
219 'mode' => @$_GET['mode'],
225 * Payment callback handler. The processor_name or processor_id is passed in.
226 * Note that processor_id is more reliable as one site may have more than one instance of a
227 * processor & ideally the processor will be validating the results
228 * Load requested payment processor and call that processor's handle<$method> method
232 static function handlePaymentMethod($method, $params = array( )) {
233 if (!isset($params['processor_id']) && !isset($params['processor_name'])) {
234 CRM_Core_Error
::fatal("Either 'processor_id' or 'processor_name' param is required for payment callback");
236 self
::logPaymentNotification($params);
238 // Query db for processor ..
239 $mode = @$params['mode'];
241 $sql = "SELECT ppt.class_name, ppt.name as processor_name, pp.id AS processor_id
242 FROM civicrm_payment_processor_type ppt
243 INNER JOIN civicrm_payment_processor pp
244 ON pp.payment_processor_type_id = ppt.id
246 AND pp.is_test = %1";
247 $args[1] = array($mode == 'test' ?
1 : 0, 'Integer');
249 if (isset($params['processor_id'])) {
250 $sql .= " WHERE pp.id = %2";
251 $args[2] = array($params['processor_id'], 'Integer');
252 $notfound = "No active instances of payment processor ID#'{$params['processor_id']}' were found.";
255 $sql .= " WHERE ppt.name = %2";
256 $args[2] = array($params['processor_name'], 'String');
257 $notfound = "No active instances of the '{$params['processor_name']}' payment processor were found.";
260 $dao = CRM_Core_DAO
::executeQuery($sql, $args);
262 // Check whether we found anything at all ..
264 CRM_Core_Error
::fatal($notfound);
267 $method = 'handle' . $method;
268 $extension_instance_found = FALSE;
270 // In all likelihood, we'll just end up with the one instance returned here. But it's
271 // possible we may get more. Hence, iterate through all instances ..
273 while ($dao->fetch()) {
274 // Check pp is extension
275 $ext = CRM_Extension_System
::singleton()->getMapper();
276 if ($ext->isExtensionKey($dao->class_name
)) {
277 $paymentClass = $ext->keyToClass($dao->class_name
, 'payment');
278 require_once $ext->classToPath($paymentClass);
281 // Legacy or extension as module instance
282 if (empty($paymentClass)) {
283 $paymentClass = 'CRM_Core_' . $dao->class_name
;
288 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($dao->processor_id
, $mode);
290 // Should never be empty - we already established this processor_id exists and is active.
291 if (empty($paymentProcessor)) {
296 $processorInstance = $paymentClass::singleton($mode, $paymentProcessor);
298 // Does PP implement this method, and can we call it?
299 if (!method_exists($processorInstance, $method) ||
300 !is_callable(array($processorInstance, $method))
302 // on the off chance there is a double implementation of this processor we should keep looking for another
303 // note that passing processor_id is more reliable & we should work to deprecate processor_name
307 // Everything, it seems, is ok - execute pp callback handler
308 $processorInstance->$method();
309 $extension_instance_found = TRUE;
312 if (!$extension_instance_found) CRM_Core_Error
::fatal(
313 "No extension instances of the '{$params['processor_name']}' payment processor were found.<br />" .
314 "$method method is unsupported in legacy payment processors."
317 // Exit here on web requests, allowing just the plain text response to be echoed
318 if ($method == 'handlePaymentNotification') {
319 CRM_Utils_System
::civiExit();
324 * Function to check whether a method is present ( & supported ) by the payment processor object.
326 * @param string $method method to check for.
331 function isSupported($method = 'cancelSubscription') {
332 return method_exists(CRM_Utils_System
::getClassName($this), $method);
335 function subscriptionURL($entityID = NULL, $entity = NULL, $action = 'cancel') {
336 if ($action == 'cancel') {
337 $url = 'civicrm/contribute/unsubscribe';
339 elseif ($action == 'billing') {
340 //in notify mode don't return the update billing url
341 if ($this->_paymentProcessor
['billing_mode'] == self
::BILLING_MODE_NOTIFY
) {
344 $url = 'civicrm/contribute/updatebilling';
346 elseif ($action == 'update') {
347 $url = 'civicrm/contribute/updaterecur';
349 $session = CRM_Core_Session
::singleton();
350 $userId = $session->get('userID');
353 if ($entityID && $entity == 'membership') {
355 $contactID = CRM_Core_DAO
::getFieldValue("CRM_Member_DAO_Membership", $entityID, "contact_id");
356 $checksumValue = CRM_Contact_BAO_Contact_Utils
::generateChecksum($contactID, NULL, 'inf');
357 $checksumValue = "&cs={$checksumValue}";
359 return CRM_Utils_System
::url($url, "reset=1&mid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
362 if ($entityID && $entity == 'contribution') {
364 $contactID = CRM_Core_DAO
::getFieldValue("CRM_Contribute_DAO_Contribution", $entityID, "contact_id");
365 $checksumValue = CRM_Contact_BAO_Contact_Utils
::generateChecksum($contactID, NULL, 'inf');
366 $checksumValue = "&cs={$checksumValue}";
368 return CRM_Utils_System
::url($url, "reset=1&coid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
371 if ($entityID && $entity == 'recur') {
374 SELECT con.contact_id
375 FROM civicrm_contribution_recur rec
376 INNER JOIN civicrm_contribution con ON ( con.contribution_recur_id = rec.id )
379 $contactID = CRM_Core_DAO
::singleValueQuery($sql, array(1 => array($entityID, 'Integer')));
380 $checksumValue = CRM_Contact_BAO_Contact_Utils
::generateChecksum($contactID, NULL, 'inf');
381 $checksumValue = "&cs={$checksumValue}";
383 return CRM_Utils_System
::url($url, "reset=1&crid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
386 if ($this->isSupported('accountLoginURL')) {
387 return $this->accountLoginURL();
389 return $this->_paymentProcessor
['url_recur'];
393 * Check for presence of type 1 or type 3 enabled processors (means we can do back-office submit credit/debit card trxns)
396 static function allowBackofficeCreditCard($template = NULL, $variableName = 'newCredit') {
398 // restrict to type=1 (credit card) payment processor payment_types and only include billing mode types 1 and 3
399 $processors = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE,
400 "billing_mode IN ( 1, 3 ) AND payment_type = 1"
402 if (count($processors) > 0) {
406 $template->assign($variableName, $newCredit);