Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
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 | ||
81 | protected $_paymentForm = NULL; | |
82 | ||
83 | /** | |
84 | * singleton function used to manage this object | |
85 | * | |
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 | |
90 | * | |
91 | * @return object | |
92 | * @static | |
93 | * | |
94 | */ | |
95 | static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) { | |
96 | // make sure paymentProcessor is not empty | |
97 | // CRM-7424 | |
98 | if (empty($paymentProcessor)) { | |
99 | return CRM_Core_DAO::$_nullObject; | |
100 | } | |
101 | ||
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)); | |
109 | } | |
110 | else { | |
111 | $paymentClass = 'CRM_Core_' . $paymentProcessor['class_name']; | |
112 | require_once (str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php'); | |
113 | } | |
114 | ||
115 | //load the object. | |
116 | self::$_singleton[$cacheKey] = eval('return ' . $paymentClass . '::singleton( $mode, $paymentProcessor );'); | |
117 | } | |
118 | ||
119 | //load the payment form for required processor. | |
120 | if ($paymentForm !== NULL) { | |
121 | self::$_singleton[$cacheKey]->setForm($paymentForm); | |
122 | } | |
123 | ||
124 | return self::$_singleton[$cacheKey]; | |
125 | } | |
126 | ||
127 | /** | |
128 | * Setter for the payment form that wants to use the processor | |
129 | * | |
130 | * @param obj $paymentForm | |
131 | * | |
132 | */ | |
133 | function setForm(&$paymentForm) { | |
134 | $this->_paymentForm = $paymentForm; | |
135 | } | |
136 | ||
137 | /** | |
138 | * Getter for payment form that is using the processor | |
139 | * | |
140 | * @return obj A form object | |
141 | */ | |
142 | function getForm() { | |
143 | return $this->_paymentForm; | |
144 | } | |
145 | ||
146 | /** | |
147 | * Getter for accessing member vars | |
148 | * | |
149 | */ | |
150 | function getVar($name) { | |
151 | return isset($this->$name) ? $this->$name : NULL; | |
152 | } | |
153 | ||
154 | /** | |
155 | * This function collects all the information from a web/api form and invokes | |
156 | * the relevant payment processor specific functions to perform the transaction | |
157 | * | |
158 | * @param array $params assoc array of input parameters for this transaction | |
159 | * | |
160 | * @return array the result in an nice formatted array (or an error object) | |
161 | * @abstract | |
162 | */ | |
163 | abstract function doDirectPayment(&$params); | |
164 | ||
165 | /** | |
166 | * This function checks to see if we have the right config values | |
167 | * | |
168 | * @param string $mode the mode we are operating in (live or test) | |
169 | * | |
170 | * @return string the error message if any | |
171 | * @public | |
172 | */ | |
173 | abstract function checkConfig(); | |
174 | ||
175 | static function paypalRedirect(&$paymentProcessor) { | |
176 | if (!$paymentProcessor) { | |
177 | return FALSE; | |
178 | } | |
179 | ||
180 | if (isset($_GET['payment_date']) && | |
181 | isset($_GET['merchant_return_link']) && | |
182 | CRM_Utils_Array::value('payment_status', $_GET) == 'Completed' && | |
183 | $paymentProcessor['payment_processor_type'] == "PayPal_Standard" | |
184 | ) { | |
185 | return TRUE; | |
186 | } | |
187 | ||
188 | return FALSE; | |
189 | } | |
190 | ||
191 | /** | |
192 | * Page callback for civicrm/payment/ipn | |
193 | * @public | |
194 | */ | |
195 | static function handleIPN() { | |
196 | self::handlePaymentMethod( | |
197 | 'PaymentNotification', | |
198 | array( | |
199 | 'processor_name' => @$_GET['processor_name'], | |
200 | 'mode' => @$_GET['mode'], | |
201 | ) | |
202 | ); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Payment callback handler | |
207 | * Load requested payment processor and call that processor's handle<$method> method | |
208 | * | |
209 | * @public | |
210 | */ | |
211 | static function handlePaymentMethod($method, $params = array( )) { | |
212 | ||
213 | if (!isset($params['processor_name'])) { | |
214 | CRM_Core_Error::fatal("Missing 'processor_name' param for payment callback"); | |
215 | } | |
216 | ||
217 | // Query db for processor .. | |
218 | $mode = @$params['mode']; | |
219 | ||
220 | $dao = CRM_Core_DAO::executeQuery(" | |
221 | SELECT ppt.class_name, ppt.name as processor_name, pp.id AS processor_id | |
222 | FROM civicrm_payment_processor_type ppt | |
223 | INNER JOIN civicrm_payment_processor pp | |
224 | ON pp.payment_processor_type_id = ppt.id | |
225 | AND pp.is_active | |
226 | AND pp.is_test = %1 | |
227 | WHERE ppt.name = %2 | |
228 | ", | |
229 | array( | |
230 | 1 => array($mode == 'test' ? 1 : 0, 'Integer'), | |
231 | 2 => array($params['processor_name'], 'String'), | |
232 | ) | |
233 | ); | |
234 | ||
235 | // Check whether we found anything at all .. | |
236 | if (!$dao->N) { | |
237 | CRM_Core_Error::fatal("No active instances of the '{$params['processor_name']}' payment processor were found."); | |
238 | } | |
239 | ||
240 | $method = 'handle' . $method; | |
241 | $extension_instance_found = FALSE; | |
242 | ||
243 | // In all likelihood, we'll just end up with the one instance returned here. But it's | |
244 | // possible we may get more. Hence, iterate through all instances .. | |
245 | ||
246 | while ($dao->fetch()) { | |
247 | // Check pp is extension | |
248 | $ext = CRM_Extension_System::singleton()->getMapper(); | |
249 | if ($ext->isExtensionKey($dao->class_name)) { | |
250 | $extension_instance_found = TRUE; | |
251 | $paymentClass = $ext->keyToClass($dao->class_name, 'payment'); | |
252 | require_once $ext->classToPath($paymentClass); | |
253 | } | |
254 | else { | |
255 | // Legacy instance - but there may also be an extension instance, so | |
256 | // continue on to the next instance and check that one. We'll raise an | |
257 | // error later on if none are found. | |
258 | continue; | |
259 | } | |
260 | ||
261 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($dao->processor_id, $mode); | |
262 | ||
263 | // Should never be empty - we already established this processor_id exists and is active. | |
264 | if (empty($paymentProcessor)) { | |
265 | continue; | |
266 | } | |
267 | ||
268 | // Instantiate PP | |
269 | eval('$processorInstance = ' . $paymentClass . '::singleton( $mode, $paymentProcessor );'); | |
270 | ||
271 | // Does PP implement this method, and can we call it? | |
272 | if (!method_exists($processorInstance, $method) || | |
273 | !is_callable(array($processorInstance, $method)) | |
274 | ) { | |
275 | // No? This will be the case in all instances, so let's just die now | |
276 | // and not prolong the agony. | |
277 | CRM_Core_Error::fatal("Payment processor does not implement a '$method' method"); | |
278 | } | |
279 | ||
280 | // Everything, it seems, is ok - execute pp callback handler | |
281 | $processorInstance->$method(); | |
282 | } | |
283 | ||
284 | if (!$extension_instance_found) CRM_Core_Error::fatal( | |
285 | "No extension instances of the '{$params['processor_name']}' payment processor were found.<br />" . | |
286 | "$method method is unsupported in legacy payment processors." | |
287 | ); | |
288 | ||
289 | // Exit here on web requests, allowing just the plain text response to be echoed | |
290 | if ($method == 'handlePaymentNotification') { | |
291 | CRM_Utils_System::civiExit(); | |
292 | } | |
293 | } | |
294 | ||
295 | /** | |
296 | * Function to check whether a method is present ( & supported ) by the payment processor object. | |
297 | * | |
298 | * @param string $method method to check for. | |
299 | * | |
300 | * @return boolean | |
301 | * @public | |
302 | */ | |
303 | function isSupported($method = 'cancelSubscription') { | |
304 | return method_exists(CRM_Utils_System::getClassName($this), $method); | |
305 | } | |
306 | ||
307 | function subscriptionURL($entityID = NULL, $entity = NULL, $action = 'cancel') { | |
308 | if ($action == 'cancel') { | |
309 | $url = 'civicrm/contribute/unsubscribe'; | |
310 | } | |
311 | elseif ($action == 'billing') { | |
1a9f6d0d PJ |
312 | //in notify mode don't return the update billing url |
313 | if ($this->_paymentProcessor['billing_mode'] == self::BILLING_MODE_NOTIFY) { | |
314 | return NULL; | |
315 | } | |
6a488035 TO |
316 | $url = 'civicrm/contribute/updatebilling'; |
317 | } | |
318 | elseif ($action == 'update') { | |
319 | $url = 'civicrm/contribute/updaterecur'; | |
320 | } | |
321 | $session = CRM_Core_Session::singleton(); | |
322 | $userId = $session->get('userID'); | |
323 | $checksumValue = ""; | |
324 | ||
325 | if ($entityID && $entity == 'membership') { | |
326 | if (!$userId) { | |
327 | $contactID = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_Membership", $entityID, "contact_id"); | |
328 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf'); | |
329 | $checksumValue = "&cs={$checksumValue}"; | |
330 | } | |
6a30bb95 | 331 | return CRM_Utils_System::url($url, "reset=1&mid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE); |
6a488035 TO |
332 | } |
333 | ||
334 | if ($entityID && $entity == 'contribution') { | |
335 | if (!$userId) { | |
336 | $contactID = CRM_Core_DAO::getFieldValue("CRM_Contribute_DAO_Contribution", $entityID, "contact_id"); | |
337 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf'); | |
338 | $checksumValue = "&cs={$checksumValue}"; | |
339 | } | |
6a30bb95 | 340 | return CRM_Utils_System::url($url, "reset=1&coid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE); |
6a488035 TO |
341 | } |
342 | ||
343 | if ($entityID && $entity == 'recur') { | |
344 | if (!$userId) { | |
345 | $sql = " | |
346 | SELECT con.contact_id | |
347 | FROM civicrm_contribution_recur rec | |
348 | INNER JOIN civicrm_contribution con ON ( con.contribution_recur_id = rec.id ) | |
349 | WHERE rec.id = %1 | |
350 | GROUP BY rec.id"; | |
351 | $contactID = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($entityID, 'Integer'))); | |
352 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, NULL, 'inf'); | |
353 | $checksumValue = "&cs={$checksumValue}"; | |
354 | } | |
6a30bb95 | 355 | return CRM_Utils_System::url($url, "reset=1&crid={$entityID}{$checksumValue}", TRUE, NULL, FALSE, TRUE); |
6a488035 TO |
356 | } |
357 | ||
358 | if ($this->isSupported('accountLoginURL')) { | |
359 | return $this->accountLoginURL(); | |
360 | } | |
361 | return $this->_paymentProcessor['url_recur']; | |
362 | } | |
363 | ||
364 | /** | |
365 | * Check for presence of type 1 or type 3 enabled processors (means we can do back-office submit credit/debit card trxns) | |
366 | * @public | |
367 | */ | |
368 | static function allowBackofficeCreditCard($template = NULL, $variableName = 'newCredit') { | |
369 | $newCredit = FALSE; | |
370 | $processors = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, | |
371 | "billing_mode IN ( 1, 3 )" | |
372 | ); | |
373 | if (count($processors) > 0) { | |
374 | $newCredit = TRUE; | |
375 | } | |
376 | if ($template) { | |
377 | $template->assign($variableName, $newCredit); | |
378 | } | |
379 | return $newCredit; | |
380 | } | |
381 | ||
382 | } | |
383 |