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