Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | This file is a part of CiviCRM. | | |
7 | | | | |
8 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
9 | | under the terms of the GNU Affero General Public License | | |
10 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
11 | | | | |
12 | | CiviCRM is distributed in the hope that it will be useful, but | | |
13 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
14 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
15 | | See the GNU Affero General Public License for more details. | | |
16 | | | | |
17 | | You should have received a copy of the GNU Affero General Public | | |
18 | | License and the CiviCRM Licensing Exception along | | |
19 | | with this program; if not, contact CiviCRM LLC | | |
20 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
21 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
22 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
23 | +--------------------------------------------------------------------+ | |
24 | */ | |
25 | ||
26 | ||
27 | /* | |
28 | * PxPay Functionality Copyright (C) 2008 Lucas Baker, Logistic Information Systems Limited (Logis) | |
29 | * PxAccess Functionality Copyright (C) 2008 Eileen McNaughton | |
30 | * Licensed to CiviCRM under the Academic Free License version 3.0. | |
31 | * | |
32 | * Grateful acknowledgements go to Donald Lobo for invaluable assistance | |
33 | * in creating this payment processor module | |
34 | */ | |
35 | class CRM_Core_Payment_PaymentExpressIPN extends CRM_Core_Payment_BaseIPN { | |
36 | ||
37 | /** | |
38 | * We only need one instance of this object. So we use the singleton | |
39 | * pattern and cache the instance in this variable | |
40 | * | |
41 | * @var object | |
42 | * @static | |
43 | */ | |
44 | static private $_singleton = NULL; | |
45 | ||
46 | /** | |
47 | * mode of operation: live or test | |
48 | * | |
49 | * @var object | |
50 | */ | |
51 | protected $_mode = NULL; | |
52 | ||
53 | static function retrieve($name, $type, $object, $abort = TRUE) { | |
54 | $value = CRM_Utils_Array::value($name, $object); | |
55 | if ($abort && $value === NULL) { | |
56 | CRM_Core_Error::debug_log_message("Could not find an entry for $name"); | |
57 | echo "Failure: Missing Parameter - " . $name . "<p>"; | |
58 | exit(); | |
59 | } | |
60 | ||
61 | if ($value) { | |
62 | if (!CRM_Utils_Type::validate($value, $type)) { | |
63 | CRM_Core_Error::debug_log_message("Could not find a valid entry for $name"); | |
64 | echo "Failure: Invalid Parameter<p>"; | |
65 | exit(); | |
66 | } | |
67 | } | |
68 | ||
69 | return $value; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Constructor | |
74 | * | |
75 | * @param string $mode the mode of operation: live or test | |
76 | * | |
77b97be7 EM |
77 | * @param $paymentProcessor |
78 | * | |
79 | * @return \CRM_Core_Payment_PaymentExpressIPN | |
6a488035 TO |
80 | */ |
81 | function __construct($mode, &$paymentProcessor) { | |
82 | parent::__construct(); | |
83 | ||
84 | $this->_mode = $mode; | |
85 | $this->_paymentProcessor = $paymentProcessor; | |
86 | } | |
87 | ||
88 | /** | |
89 | * singleton function used to manage this object | |
90 | * | |
91 | * @param string $mode the mode of operation: live or test | |
92 | * | |
dd244018 EM |
93 | * @param $paymentProcessor |
94 | * @param null $paymentForm | |
95 | * @param bool $force | |
96 | * | |
6a488035 TO |
97 | * @return object |
98 | * @static | |
99 | */ | |
328b8a19 | 100 | static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) { |
6a488035 TO |
101 | if (self::$_singleton === NULL) { |
102 | self::$_singleton = new CRM_Core_Payment_PaymentExpressIPN($mode, $paymentProcessor); | |
103 | } | |
104 | return self::$_singleton; | |
105 | } | |
106 | ||
107 | /** | |
108 | * The function gets called when a new order takes place. | |
109 | * | |
dd244018 | 110 | * @param $success |
6a488035 TO |
111 | * @param array $privateData contains the name value pair of <merchant-private-data> |
112 | * | |
dd244018 EM |
113 | * @param $component |
114 | * @param $amount | |
115 | * @param $transactionReference | |
6a488035 | 116 | * |
dd244018 EM |
117 | * @internal param \xml $dataRoot response send by google in xml format |
118 | * @return void | |
6a488035 TO |
119 | */ |
120 | function newOrderNotify($success, $privateData, $component, $amount, $transactionReference) { | |
121 | $ids = $input = $params = array(); | |
122 | ||
123 | $input['component'] = strtolower($component); | |
124 | ||
125 | $ids['contact'] = self::retrieve('contactID', 'Integer', $privateData, TRUE); | |
126 | $ids['contribution'] = self::retrieve('contributionID', 'Integer', $privateData, TRUE); | |
127 | ||
128 | if ($input['component'] == "event") { | |
129 | $ids['event'] = self::retrieve('eventID', 'Integer', $privateData, TRUE); | |
130 | $ids['participant'] = self::retrieve('participantID', 'Integer', $privateData, TRUE); | |
131 | $ids['membership'] = NULL; | |
132 | } | |
133 | else { | |
134 | $ids['membership'] = self::retrieve('membershipID', 'Integer', $privateData, FALSE); | |
135 | } | |
136 | $ids['contributionRecur'] = $ids['contributionPage'] = NULL; | |
137 | ||
138 | $paymentProcessorID = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', | |
139 | 'PayPal_Express', 'id', 'name' | |
140 | ); | |
141 | ||
142 | if (!$this->validateData($input, $ids, $objects, TRUE, $paymentProcessorID)) { | |
143 | return FALSE; | |
144 | } | |
145 | ||
146 | // make sure the invoice is valid and matches what we have in the contribution record | |
147 | $input['invoice'] = $privateData['invoiceID']; | |
148 | $input['newInvoice'] = $transactionReference; | |
149 | $contribution = &$objects['contribution']; | |
150 | $input['trxn_id'] = $transactionReference; | |
151 | ||
152 | if ($contribution->invoice_id != $input['invoice']) { | |
153 | CRM_Core_Error::debug_log_message("Invoice values dont match between database and IPN request"); | |
154 | echo "Failure: Invoice values dont match between database and IPN request<p>"; | |
155 | return; | |
156 | } | |
157 | ||
158 | // lets replace invoice-id with Payment Processor -number because thats what is common and unique | |
159 | // in subsequent calls or notifications sent by google. | |
160 | $contribution->invoice_id = $input['newInvoice']; | |
161 | ||
162 | $input['amount'] = $amount; | |
163 | ||
164 | if ($contribution->total_amount != $input['amount']) { | |
165 | CRM_Core_Error::debug_log_message("Amount values dont match between database and IPN request"); | |
166 | echo "Failure: Amount values dont match between database and IPN request. " . $contribution->total_amount . "/" . $input['amount'] . "<p>"; | |
167 | return; | |
168 | } | |
169 | ||
170 | $transaction = new CRM_Core_Transaction(); | |
171 | ||
6a488035 TO |
172 | // check if contribution is already completed, if so we ignore this ipn |
173 | ||
174 | if ($contribution->contribution_status_id == 1) { | |
175 | CRM_Core_Error::debug_log_message("returning since contribution has already been handled"); | |
176 | echo "Success: Contribution has already been handled<p>"; | |
177 | return TRUE; | |
178 | } | |
179 | else { | |
180 | /* Since trxn_id hasn't got any use here, | |
181 | * lets make use of it by passing the eventID/membershipTypeID to next level. | |
182 | * And change trxn_id to the payment processor reference before finishing db update */ | |
183 | ||
184 | if ($ids['event']) { | |
185 | $contribution->trxn_id = $ids['event'] . CRM_Core_DAO::VALUE_SEPARATOR . $ids['participant']; | |
186 | } | |
187 | else { | |
188 | $contribution->trxn_id = $ids['membership']; | |
189 | } | |
190 | } | |
191 | $this->completeTransaction($input, $ids, $objects, $transaction); | |
192 | return TRUE; | |
193 | } | |
194 | ||
195 | /** | |
196 | ||
197 | /** | |
198 | * The function returns the component(Event/Contribute..)and whether it is Test or not | |
199 | * | |
200 | * @param array $privateData contains the name-value pairs of transaction related data | |
201 | * @param int $orderNo <order-total> send by google | |
202 | * | |
203 | * @return array context of this call (test, component, payment processor id) | |
204 | * @static | |
205 | */ | |
206 | static function getContext($privateData, $orderNo) { | |
207 | ||
208 | $component = NULL; | |
209 | $isTest = NULL; | |
210 | ||
211 | $contributionID = $privateData['contributionID']; | |
212 | $contribution = new CRM_Contribute_DAO_Contribution(); | |
213 | $contribution->id = $contributionID; | |
214 | ||
215 | if (!$contribution->find(TRUE)) { | |
216 | CRM_Core_Error::debug_log_message("Could not find contribution record: $contributionID"); | |
217 | echo "Failure: Could not find contribution record for $contributionID<p>"; | |
218 | exit(); | |
219 | } | |
220 | ||
221 | if (stristr($contribution->source, 'Online Contribution')) { | |
222 | $component = 'contribute'; | |
223 | } | |
224 | elseif (stristr($contribution->source, 'Online Event Registration')) { | |
225 | $component = 'event'; | |
226 | } | |
227 | $isTest = $contribution->is_test; | |
228 | ||
229 | $duplicateTransaction = 0; | |
230 | if ($contribution->contribution_status_id == 1) { | |
231 | //contribution already handled. (some processors do two notifications so this could be valid) | |
232 | $duplicateTransaction = 1; | |
233 | } | |
234 | ||
235 | if ($component == 'contribute') { | |
236 | if (!$contribution->contribution_page_id) { | |
237 | CRM_Core_Error::debug_log_message("Could not find contribution page for contribution record: $contributionID"); | |
238 | echo "Failure: Could not find contribution page for contribution record: $contributionID<p>"; | |
239 | exit(); | |
240 | } | |
241 | } | |
242 | else { | |
243 | ||
244 | $eventID = $privateData['eventID']; | |
245 | ||
246 | if (!$eventID) { | |
247 | CRM_Core_Error::debug_log_message("Could not find event ID"); | |
248 | echo "Failure: Could not find eventID<p>"; | |
249 | exit(); | |
250 | } | |
251 | ||
252 | // we are in event mode | |
253 | // make sure event exists and is valid | |
254 | $event = new CRM_Event_DAO_Event(); | |
255 | $event->id = $eventID; | |
256 | if (!$event->find(TRUE)) { | |
257 | CRM_Core_Error::debug_log_message("Could not find event: $eventID"); | |
258 | echo "Failure: Could not find event: $eventID<p>"; | |
259 | exit(); | |
260 | } | |
261 | } | |
262 | ||
263 | return array($isTest, $component, $duplicateTransaction); | |
264 | } | |
265 | ||
266 | /** | |
267 | * This method is handles the response that will be invoked by the | |
268 | * notification or request sent by the payment processor. | |
269 | *hex string from paymentexpress is passed to this function as hex string. Code based on googleIPN | |
270 | * mac_key is only passed if the processor is pxaccess as it is used for decryption | |
271 | * $dps_method is either pxaccess or pxpay | |
272 | */ | |
273 | static function main($dps_method, $rawPostData, $dps_url, $dps_user, $dps_key, $mac_key) { | |
274 | ||
275 | $config = CRM_Core_Config::singleton(); | |
276 | define('RESPONSE_HANDLER_LOG_FILE', $config->uploadDir . 'CiviCRM.PaymentExpress.log'); | |
277 | ||
278 | //Setup the log file | |
279 | if (!$message_log = fopen(RESPONSE_HANDLER_LOG_FILE, "a")) { | |
280 | error_func("Cannot open " . RESPONSE_HANDLER_LOG_FILE . " file.\n", 0); | |
281 | exit(1); | |
282 | } | |
283 | ||
284 | if ($dps_method == "pxpay") { | |
285 | $processResponse = CRM_Core_Payment_PaymentExpressUtils::_valueXml(array( | |
286 | 'PxPayUserId' => $dps_user, | |
287 | 'PxPayKey' => $dps_key, | |
288 | 'Response' => $_GET['result'], | |
289 | )); | |
290 | $processResponse = CRM_Core_Payment_PaymentExpressUtils::_valueXml('ProcessResponse', $processResponse); | |
291 | ||
292 | fwrite($message_log, sprintf("\n\r%s:- %s\n", date("D M j G:i:s T Y"), | |
293 | $processResponse | |
294 | )); | |
295 | ||
296 | // Send the XML-formatted validation request to DPS so that we can receive a decrypted XML response which contains the transaction results | |
297 | $curl = CRM_Core_Payment_PaymentExpressUtils::_initCURL($processResponse, $dps_url); | |
298 | ||
299 | fwrite($message_log, sprintf("\n\r%s:- %s\n", date("D M j G:i:s T Y"), | |
300 | $curl | |
301 | )); | |
302 | $success = FALSE; | |
303 | if ($response = curl_exec($curl)) { | |
304 | fwrite($message_log, sprintf("\n\r%s:- %s\n", date("D M j G:i:s T Y"), | |
305 | $response | |
306 | )); | |
307 | curl_close($curl); | |
308 | ||
309 | // Assign the returned XML values to variables | |
310 | $valid = CRM_Core_Payment_PaymentExpressUtils::_xmlAttribute($response, 'valid'); | |
311 | $success = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'Success'); | |
312 | $txnId = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'TxnId'); | |
313 | $responseText = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'ResponseText'); | |
314 | $authCode = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'AuthCode'); | |
315 | $DPStxnRef = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, 'DpsTxnRef'); | |
316 | $qfKey = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, "TxnData1"); | |
317 | $privateData = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, "TxnData2"); | |
318 | list($component,$paymentProcessorID,) =explode(',', CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, "TxnData3")); | |
319 | $amount = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, "AmountSettlement"); | |
320 | $merchantReference = CRM_Core_Payment_PaymentExpressUtils::_xmlElement($response, "MerchantReference"); | |
321 | } | |
322 | else { | |
323 | // calling DPS failed | |
324 | CRM_Core_Error::fatal(ts('Unable to establish connection to the payment gateway to verify transaction response.')); | |
325 | exit; | |
326 | } | |
327 | } | |
328 | elseif ($dps_method == "pxaccess") { | |
329 | ||
330 | require_once ('PaymentExpress/pxaccess.inc.php'); | |
331 | global $pxaccess; | |
332 | $pxaccess = new PxAccess($dps_url, $dps_user, $dps_key, $mac_key); | |
333 | #getResponse method in PxAccess object returns PxPayResponse object | |
334 | #which encapsulates all the response data | |
335 | $rsp = $pxaccess->getResponse($rawPostData); | |
336 | ||
337 | $qfKey = $rsp->getTxnData1(); | |
338 | $privateData = $rsp->getTxnData2(); | |
339 | list($component,$paymentProcessorID) = explode(',',$rsp->getTxnData3()); | |
340 | $success = $rsp->getSuccess(); | |
341 | $authCode = $rsp->getAuthCode(); | |
342 | $DPStxnRef = $rsp->getDpsTxnRef(); | |
343 | $amount = $rsp->getAmountSettlement(); | |
344 | $MerchantReference = $rsp->getMerchantReference(); | |
345 | } | |
346 | ||
347 | $privateData = $privateData ? self::stringToArray($privateData) : ''; | |
348 | ||
349 | // Record the current count in array, before we start adding things (for later checks) | |
350 | $countPrivateData = count($privateData); | |
351 | ||
352 | // Private Data consists of : a=contactID, b=contributionID,c=contributionTypeID,d=invoiceID,e=membershipID,f=participantID,g=eventID | |
353 | $privateData['contactID'] = $privateData['a']; | |
354 | $privateData['contributionID'] = $privateData['b']; | |
355 | $privateData['contributionTypeID'] = $privateData['c']; | |
356 | $privateData['invoiceID'] = $privateData['d']; | |
357 | ||
358 | if ($component == "event") { | |
359 | $privateData['participantID'] = $privateData['f']; | |
360 | $privateData['eventID'] = $privateData['g']; | |
361 | } | |
362 | elseif ($component == "contribute") { | |
363 | ||
364 | if ($countPrivateData == 5) { | |
365 | $privateData["membershipID"] = $privateData['e']; | |
366 | } | |
367 | } | |
368 | ||
369 | $transactionReference = $authCode . "-" . $DPStxnRef; | |
370 | ||
371 | list($mode, $component, $duplicateTransaction) = self::getContext($privateData, $transactionReference); | |
372 | $mode = $mode ? 'test' : 'live'; | |
373 | ||
374 | ||
375 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, | |
376 | $mode | |
377 | ); | |
378 | ||
379 | $ipn = self::singleton($mode, $component, $paymentProcessor); | |
380 | ||
381 | ||
382 | //Check status and take appropriate action | |
383 | ||
384 | if ($success == 1) { | |
385 | if ($duplicateTransaction == 0) { | |
386 | $ipn->newOrderNotify($success, $privateData, $component, $amount, $transactionReference); | |
387 | } | |
388 | ||
389 | if ($component == "event") { | |
390 | $finalURL = CRM_Utils_System::url('civicrm/event/register', | |
391 | "_qf_ThankYou_display=1&qfKey=$qfKey", | |
392 | FALSE, NULL, FALSE | |
393 | ); | |
394 | } | |
395 | elseif ($component == "contribute") { | |
396 | $finalURL = CRM_Utils_System::url('civicrm/contribute/transact', | |
397 | "_qf_ThankYou_display=1&qfKey=$qfKey", | |
398 | FALSE, NULL, FALSE | |
399 | ); | |
400 | } | |
401 | ||
402 | CRM_Utils_System::redirect($finalURL); | |
403 | } | |
404 | else { | |
405 | ||
406 | if ($component == "event") { | |
407 | $finalURL = CRM_Utils_System::url('civicrm/event/confirm', | |
408 | "reset=1&cc=fail&participantId=$privateData[participantID]", | |
409 | FALSE, NULL, FALSE | |
410 | ); | |
411 | } | |
412 | elseif ($component == "contribute") { | |
413 | $finalURL = CRM_Utils_System::url('civicrm/contribute/transact', | |
414 | "_qf_Main_display=1&cancel=1&qfKey=$qfKey", | |
415 | FALSE, NULL, FALSE | |
416 | ); | |
417 | } | |
418 | ||
419 | CRM_Utils_System::redirect($finalURL); | |
420 | } | |
421 | } | |
422 | ||
423 | /** | |
424 | * Converts the comma separated name-value pairs in <TxnData2> | |
425 | * to an array of values. | |
426 | */ | |
427 | static function stringToArray($str) { | |
428 | $vars = $labels = array(); | |
429 | $labels = explode(',', $str); | |
430 | foreach ($labels as $label) { | |
431 | $terms = explode('=', $label); | |
432 | $vars[$terms[0]] = $terms[1]; | |
433 | } | |
434 | return $vars; | |
435 | } | |
436 | } | |
437 |