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;
84 protected $_paymentForm = NULL;
87 * singleton function used to manage this object
89 * @param string $mode the mode of operation: live or test
90 * @param array $paymentProcessor the details of the payment processor being invoked
91 * @param object $paymentForm reference to the form object if available
92 * @param boolean $force should we force a reload of this payment object
98 static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
99 // make sure paymentProcessor is not empty
101 if (empty($paymentProcessor)) {
102 return CRM_Core_DAO
::$_nullObject;
105 $cacheKey = "{$mode}_{$paymentProcessor['id']}_" . (int)isset($paymentForm);
106 if (!isset(self
::$_singleton[$cacheKey]) ||
$force) {
107 $config = CRM_Core_Config
::singleton();
108 $ext = CRM_Extension_System
::singleton()->getMapper();
109 if ($ext->isExtensionKey($paymentProcessor['class_name'])) {
110 $paymentClass = $ext->keyToClass($paymentProcessor['class_name'], 'payment');
111 require_once ($ext->classToPath($paymentClass));
114 $paymentClass = 'CRM_Core_' . $paymentProcessor['class_name'];
115 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $paymentClass) . '.php');
119 self
::$_singleton[$cacheKey] = $paymentClass::singleton($mode, $paymentProcessor);
122 //load the payment form for required processor.
123 if ($paymentForm !== NULL) {
124 self
::$_singleton[$cacheKey]->setForm($paymentForm);
127 return self
::$_singleton[$cacheKey];
135 public static function logPaymentNotification($params) {
136 $message = 'payment_notification ';
137 if (!empty($params['processor_name'])) {
138 $message .= 'processor_name=' . $params['processor_name'];
140 if (!empty($params['processor_id'])) {
141 $message .= 'processor_id=' . $params['processor_id'];
144 $log = new CRM_Utils_SystemLogger();
145 $log->alert($message, $_REQUEST);
149 * Setter for the payment form that wants to use the processor
151 * @param CRM_Core_Form $paymentForm
154 function setForm(&$paymentForm) {
155 $this->_paymentForm
= $paymentForm;
159 * Getter for payment form that is using the processor
161 * @return CRM_Core_Form A form object
164 return $this->_paymentForm
;
168 * Getter for accessing member vars
171 function getVar($name) {
172 return isset($this->$name) ?
$this->$name : NULL;
176 * This function collects all the information from a web/api form and invokes
177 * the relevant payment processor specific functions to perform the transaction
179 * @param array $params assoc array of input parameters for this transaction
181 * @return array the result in an nice formatted array (or an error object)
184 abstract function doDirectPayment(&$params);
187 * This function checks to see if we have the right config values
189 * @internal param string $mode the mode we are operating in (live or test)
191 * @return string the error message if any
194 abstract function checkConfig();
196 static function paypalRedirect(&$paymentProcessor) {
197 if (!$paymentProcessor) {
201 if (isset($_GET['payment_date']) &&
202 isset($_GET['merchant_return_link']) &&
203 CRM_Utils_Array
::value('payment_status', $_GET) == 'Completed' &&
204 $paymentProcessor['payment_processor_type'] == "PayPal_Standard"
213 * Page callback for civicrm/payment/ipn
216 static function handleIPN() {
217 self
::handlePaymentMethod(
218 'PaymentNotification',
220 'processor_name' => @$_GET['processor_name'],
221 'processor_id' => @$_GET['processor_id'],
222 'mode' => @$_GET['mode'],
228 * Payment callback handler. The processor_name or processor_id is passed in.
229 * Note that processor_id is more reliable as one site may have more than one instance of a
230 * processor & ideally the processor will be validating the results
231 * Load requested payment processor and call that processor's handle<$method> method
235 * @param array $params
237 static function handlePaymentMethod($method, $params = array()) {
238 if (!isset($params['processor_id']) && !isset($params['processor_name'])) {
239 CRM_Core_Error
::fatal("Either 'processor_id' or 'processor_name' param is required for payment callback");
241 self
::logPaymentNotification($params);
243 // Query db for processor ..
244 $mode = @$params['mode'];
246 $sql = "SELECT ppt.class_name, ppt.name as processor_name, pp.id AS processor_id
247 FROM civicrm_payment_processor_type ppt
248 INNER JOIN civicrm_payment_processor pp
249 ON pp.payment_processor_type_id = ppt.id
251 AND pp.is_test = %1";
252 $args[1] = array($mode == 'test' ?
1 : 0, 'Integer');
254 if (isset($params['processor_id'])) {
255 $sql .= " WHERE pp.id = %2";
256 $args[2] = array($params['processor_id'], 'Integer');
257 $notfound = "No active instances of payment processor ID#'{$params['processor_id']}' were found.";
260 $sql .= " WHERE ppt.name = %2";
261 $args[2] = array($params['processor_name'], 'String');
262 $notfound = "No active instances of the '{$params['processor_name']}' payment processor were found.";
265 $dao = CRM_Core_DAO
::executeQuery($sql, $args);
267 // Check whether we found anything at all ..
269 CRM_Core_Error
::fatal($notfound);
272 $method = 'handle' . $method;
273 $extension_instance_found = FALSE;
275 // In all likelihood, we'll just end up with the one instance returned here. But it's
276 // possible we may get more. Hence, iterate through all instances ..
278 while ($dao->fetch()) {
279 // Check pp is extension
280 $ext = CRM_Extension_System
::singleton()->getMapper();
281 if ($ext->isExtensionKey($dao->class_name
)) {
282 $paymentClass = $ext->keyToClass($dao->class_name
, 'payment');
283 require_once $ext->classToPath($paymentClass);
286 // Legacy or extension as module instance
287 if (empty($paymentClass)) {
288 $paymentClass = 'CRM_Core_' . $dao->class_name
;
293 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::getPayment($dao->processor_id
, $mode);
295 // Should never be empty - we already established this processor_id exists and is active.
296 if (empty($paymentProcessor)) {
301 $processorInstance = $paymentClass::singleton($mode, $paymentProcessor);
303 // Does PP implement this method, and can we call it?
304 if (!method_exists($processorInstance, $method) ||
305 !is_callable(array($processorInstance, $method))
307 // on the off chance there is a double implementation of this processor we should keep looking for another
308 // note that passing processor_id is more reliable & we should work to deprecate processor_name
312 // Everything, it seems, is ok - execute pp callback handler
313 $processorInstance->$method();
314 $extension_instance_found = TRUE;
317 if (!$extension_instance_found) CRM_Core_Error
::fatal(
318 "No extension instances of the '{$params['processor_name']}' payment processor were found.<br />" .
319 "$method method is unsupported in legacy payment processors."
322 // Exit here on web requests, allowing just the plain text response to be echoed
323 if ($method == 'handlePaymentNotification') {
324 CRM_Utils_System
::civiExit();
329 * Function to check whether a method is present ( & supported ) by the payment processor object.
331 * @param string $method method to check for.
336 function isSupported($method = 'cancelSubscription') {
337 return method_exists(CRM_Utils_System
::getClassName($this), $method);
340 function subscriptionURL($entityID = NULL, $entity = NULL, $action = 'cancel') {
341 if ($action == 'cancel') {
342 $url = 'civicrm/contribute/unsubscribe';
344 elseif ($action == 'billing') {
345 //in notify mode don't return the update billing url
346 if ($this->_paymentProcessor
['billing_mode'] == self
::BILLING_MODE_NOTIFY
) {
349 $url = 'civicrm/contribute/updatebilling';
351 elseif ($action == 'update') {
352 $url = 'civicrm/contribute/updaterecur';
354 $session = CRM_Core_Session
::singleton();
355 $userId = $session->get('userID');
358 if ($entityID && $entity == 'membership') {
360 $contactID = CRM_Core_DAO
::getFieldValue("CRM_Member_DAO_Membership", $entityID, "contact_id");
361 $checksumValue = CRM_Contact_BAO_Contact_Utils
::generateChecksum($contactID, NULL, 'inf');
362 $checksumValue = "&cs={$checksumValue}";
364 return CRM_Utils_System
::url($url, "reset=1&mid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
367 if ($entityID && $entity == 'contribution') {
369 $contactID = CRM_Core_DAO
::getFieldValue("CRM_Contribute_DAO_Contribution", $entityID, "contact_id");
370 $checksumValue = CRM_Contact_BAO_Contact_Utils
::generateChecksum($contactID, NULL, 'inf');
371 $checksumValue = "&cs={$checksumValue}";
373 return CRM_Utils_System
::url($url, "reset=1&coid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
376 if ($entityID && $entity == 'recur') {
379 SELECT con.contact_id
380 FROM civicrm_contribution_recur rec
381 INNER JOIN civicrm_contribution con ON ( con.contribution_recur_id = rec.id )
384 $contactID = CRM_Core_DAO
::singleValueQuery($sql, array(1 => array($entityID, 'Integer')));
385 $checksumValue = CRM_Contact_BAO_Contact_Utils
::generateChecksum($contactID, NULL, 'inf');
386 $checksumValue = "&cs={$checksumValue}";
388 return CRM_Utils_System
::url($url, "reset=1&crid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE);
391 if ($this->isSupported('accountLoginURL')) {
392 return $this->accountLoginURL();
394 return $this->_paymentProcessor
['url_recur'];