3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Core_Payment_PayPalImpl
extends CRM_Core_Payment
{
36 CONST CHARSET
= 'iso-8859-1';
38 protected $_mode = NULL;
41 * We only need one instance of this object. So we use the singleton
42 * pattern and cache the instance in this variable
47 static private $_singleton = NULL;
52 * @param string $mode the mode of operation: live or test
56 function __construct($mode, &$paymentProcessor) {
58 $this->_paymentProcessor
= $paymentProcessor;
59 $this->_processorName
= ts('PayPal Pro');
60 $paymentProcessorType = CRM_Core_PseudoConstant
::paymentProcessorType(false, null, 'name');
62 if ($this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal_Standard', $paymentProcessorType)) {
63 $this->_processorName
= ts('PayPal Standard');
66 elseif ($this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal_Express', $paymentProcessorType)) {
67 $this->_processorName
= ts('PayPal Express');
70 if (!$this->_paymentProcessor
['user_name']) {
71 CRM_Core_Error
::fatal(ts('Could not find user name for payment processor'));
76 * singleton function used to manage this object
78 * @param string $mode the mode of operation: live or test
84 static function &singleton($mode, &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
85 $processorName = $paymentProcessor['name'];
86 if (!isset(self
::$_singleton[$processorName]) || self
::$_singleton[$processorName] === NULL) {
87 self
::$_singleton[$processorName] = new CRM_Core_Payment_PaypalImpl($mode, $paymentProcessor);
89 return self
::$_singleton[$processorName];
93 * express checkout code. Check PayPal documentation for more information
95 * @param array $params assoc array of input parameters for this transaction
97 * @return array the result in an nice formatted array (or an error object)
100 function setExpressCheckOut(&$params) {
103 $this->initialize($args, 'SetExpressCheckout');
105 $args['paymentAction'] = $params['payment_action'];
106 $args['amt'] = $params['amount'];
107 $args['currencyCode'] = $params['currencyID'];
108 $args['invnum'] = $params['invoiceID'];
109 $args['returnURL'] = $params['returnURL'];
110 $args['cancelURL'] = $params['cancelURL'];
111 $args['version'] = '56.0';
113 //LCD if recurring, collect additional data and set some values
114 if (!empty($params['is_recur'])) {
115 $args['L_BILLINGTYPE0'] = 'RecurringPayments';
116 //$args['L_BILLINGAGREEMENTDESCRIPTION0'] = 'Recurring Contribution';
117 $args['L_BILLINGAGREEMENTDESCRIPTION0'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit'];
118 $args['L_PAYMENTTYPE0'] = 'Any';
121 // Allow further manipulation of the arguments via custom hooks ..
122 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $args);
124 $result = $this->invokeAPI($args);
126 if (is_a($result, 'CRM_Core_Error')) {
132 return $result['token'];
136 * get details from paypal. Check PayPal documentation for more information
138 * @param string $token the key associated with this transaction
140 * @return array the result in an nice formatted array (or an error object)
143 function getExpressCheckoutDetails($token) {
146 $this->initialize($args, 'GetExpressCheckoutDetails');
147 $args['token'] = $token;
149 $args['method'] = 'GetExpressCheckoutDetails';
151 $result = $this->invokeAPI($args);
153 if (is_a($result, 'CRM_Core_Error')) {
160 $params['token'] = $result['token'];
161 $params['payer_id'] = $result['payerid'];
162 $params['payer_status'] = $result['payerstatus'];
163 $params['first_name'] = $result['firstname'];
164 $params['middle_name'] = $result['middlename'];
165 $params['last_name'] = $result['lastname'];
166 $params['street_address'] = $result['shiptostreet'];
167 $params['supplemental_address_1'] = $result['shiptostreet2'];
168 $params['city'] = $result['shiptocity'];
169 $params['state_province'] = $result['shiptostate'];
170 $params['postal_code'] = $result['shiptozip'];
171 $params['country'] = $result['shiptocountrycode'];
177 * do the express checkout at paypal. Check PayPal documentation for more information
179 * @param string $token the key associated with this transaction
181 * @return array the result in an nice formatted array (or an error object)
184 function doExpressCheckout(&$params) {
187 $this->initialize($args, 'DoExpressCheckoutPayment');
189 $args['token'] = $params['token'];
190 $args['paymentAction'] = $params['payment_action'];
191 $args['amt'] = $params['amount'];
192 $args['currencyCode'] = $params['currencyID'];
193 $args['payerID'] = $params['payer_id'];
194 $args['invnum'] = $params['invoiceID'];
195 $args['returnURL'] = $params['returnURL'];
196 $args['cancelURL'] = $params['cancelURL'];
198 $result = $this->invokeAPI($args);
200 if (is_a($result, 'CRM_Core_Error')) {
206 $params['trxn_id'] = $result['transactionid'];
207 $params['gross_amount'] = $result['amt'];
208 $params['fee_amount'] = $result['feeamt'];
209 $params['net_amount'] = $result['settleamt'];
210 if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) {
211 $params['net_amount'] = $params['gross_amount'] - $params['fee_amount'];
213 $params['payment_status'] = $result['paymentstatus'];
214 $params['pending_reason'] = $result['pendingreason'];
219 //LCD add new function for handling recurring payments for PayPal Express
220 function createRecurringPayments(&$params) {
223 $this->initialize($args, 'CreateRecurringPaymentsProfile');
225 $start_time = strtotime(date('m/d/Y'));
226 $start_date = date('Y-m-d\T00:00:00\Z', $start_time);
228 $args['token'] = $params['token'];
229 $args['paymentAction'] = $params['payment_action'];
230 $args['amt'] = $params['amount'];
231 $args['currencyCode'] = $params['currencyID'];
232 $args['payerID'] = $params['payer_id'];
233 $args['invnum'] = $params['invoiceID'];
234 $args['returnURL'] = $params['returnURL'];
235 $args['cancelURL'] = $params['cancelURL'];
236 $args['profilestartdate'] = $start_date;
237 $args['method'] = 'CreateRecurringPaymentsProfile';
238 $args['billingfrequency'] = $params['frequency_interval'];
239 $args['billingperiod'] = ucwords($params['frequency_unit']);
240 $args['desc'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit'];
241 //$args['desc'] = 'Recurring Contribution';
242 $args['totalbillingcycles'] = $params['installments'];
243 $args['version'] = '56.0';
244 $args['profilereference'] =
245 "i={$params['invoiceID']}" .
247 "&c={$params['contactID']}" .
248 "&r={$params['contributionRecurID']}" .
249 "&b={$params['contributionID']}" .
250 "&p={$params['contributionPageID']}";
252 $result = $this->invokeAPI($args);
254 if (is_a($result, 'CRM_Core_Error')) {
260 $params['trxn_id'] = $result['transactionid'];
261 $params['gross_amount'] = $result['amt'];
262 $params['fee_amount'] = $result['feeamt'];
263 $params['net_amount'] = $result['settleamt'];
264 if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) {
265 $params['net_amount'] = $params['gross_amount'] - $params['fee_amount'];
267 $params['payment_status'] = $result['paymentstatus'];
268 $params['pending_reason'] = $result['pendingreason'];
273 function initialize(&$args, $method) {
274 $args['user'] = $this->_paymentProcessor
['user_name'];
275 $args['pwd'] = $this->_paymentProcessor
['password'];
276 $args['version'] = 3.0;
277 $args['signature'] = $this->_paymentProcessor
['signature'];
278 $args['subject'] = $this->_paymentProcessor
['subject'];
279 $args['method'] = $method;
283 * This function collects all the information from a web/api form and invokes
284 * the relevant payment processor specific functions to perform the transaction
286 * @param array $params assoc array of input parameters for this transaction
288 * @return array the result in an nice formatted array (or an error object)
291 function doDirectPayment(&$params, $component = 'contribute') {
294 $this->initialize($args, 'DoDirectPayment');
296 $args['paymentAction'] = $params['payment_action'];
297 $args['amt'] = $params['amount'];
298 $args['currencyCode'] = $params['currencyID'];
299 $args['invnum'] = $params['invoiceID'];
300 $args['ipaddress'] = $params['ip_address'];
301 $args['creditCardType'] = $params['credit_card_type'];
302 $args['acct'] = $params['credit_card_number'];
303 $args['expDate'] = sprintf('%02d', $params['month']) . $params['year'];
304 $args['cvv2'] = $params['cvv2'];
305 $args['firstName'] = $params['first_name'];
306 $args['lastName'] = $params['last_name'];
307 $args['email'] = CRM_Utils_Array
::value('email', $params);
308 $args['street'] = $params['street_address'];
309 $args['city'] = $params['city'];
310 $args['state'] = $params['state_province'];
311 $args['countryCode'] = $params['country'];
312 $args['zip'] = $params['postal_code'];
313 $args['desc'] = substr(CRM_Utils_Array
::value('description', $params), 0, 127);
314 $args['custom'] = CRM_Utils_Array
::value('accountingCode', $params);
316 if (CRM_Utils_Array
::value('is_recur', $params) == 1) {
317 $start_time = strtotime(date('m/d/Y'));
318 $start_date = date('Y-m-d\T00:00:00\Z', $start_time);
320 $args['PaymentAction'] = 'Sale';
321 $args['billingperiod'] = ucwords($params['frequency_unit']);
322 $args['billingfrequency'] = $params['frequency_interval'];
323 $args['method'] = "CreateRecurringPaymentsProfile";
324 $args['profilestartdate'] = $start_date;
326 $params['description'] . ": " .
327 $params['amount'] . " Per " .
328 $params['frequency_interval'] . " " .
329 $params['frequency_unit'];
330 $args['amt'] = $params['amount'];
331 $args['totalbillingcycles'] = $params['installments'];
332 $args['version'] = 56.0;
333 $args['PROFILEREFERENCE'] =
334 "i=" . $params['invoiceID'] . "&m=" . $component .
335 "&c=" . $params['contactID'] . "&r=" . $params['contributionRecurID'] .
336 "&b=" . $params['contributionID'] . "&p=" . $params['contributionPageID'];
339 // Allow further manipulation of the arguments via custom hooks ..
340 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $args);
342 $result = $this->invokeAPI($args);
345 if (is_a($result, 'CRM_Core_Error')) {
349 $params['recurr_profile_id'] = NULL;
351 if (CRM_Utils_Array
::value('is_recur', $params) == 1) {
352 $params['recurr_profile_id'] = $result['profileid'];
357 $params['trxn_id'] = CRM_Utils_Array
::value('transactionid', $result);
358 $params['gross_amount'] = CRM_Utils_Array
::value('amt', $result);
363 * This function checks to see if we have the right config values
365 * @return string the error message if any
368 function checkConfig() {
370 $paymentProcessorType = CRM_Core_PseudoConstant
::paymentProcessorType(false, null, 'name');
372 $this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal_Standard', $paymentProcessorType) ||
373 $this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal', $paymentProcessorType)
375 if (empty($this->_paymentProcessor
['user_name'])) {
376 $error[] = ts('User Name is not set in the Administer » System Settings » Payment Processors.');
380 if ($this->_paymentProcessor
['payment_processor_type_id'] != CRM_Utils_Array
::key('PayPal_Standard', $paymentProcessorType)) {
381 if (empty($this->_paymentProcessor
['signature'])) {
382 $error[] = ts('Signature is not set in the Administer » System Settings » Payment Processors.');
385 if (empty($this->_paymentProcessor
['password'])) {
386 $error[] = ts('Password is not set in the Administer » System Settings » Payment Processors.');
390 if (!empty($error)) {
391 return implode('<p>', $error);
398 function cancelSubscriptionURL() {
399 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal_Standard') {
400 return "{$this->_paymentProcessor['url_site']}cgi-bin/webscr?cmd=_subscr-find&alias=" . urlencode($this->_paymentProcessor
['user_name']);
407 function isSupported($method = 'cancelSubscription') {
408 if ($this->_paymentProcessor
['payment_processor_type'] != 'PayPal') {
409 // since subscription methods like cancelSubscription or updateBilling is not yet implemented / supported
410 // by standard or express.
413 return parent
::isSupported($method);
416 function cancelSubscription(&$message = '', $params = array(
418 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal') {
420 $this->initialize($args, 'ManageRecurringPaymentsProfileStatus');
422 $args['PROFILEID'] = CRM_Utils_Array
::value('subscriptionId', $params);
423 $args['ACTION'] = 'Cancel';
424 $args['NOTE'] = CRM_Utils_Array
::value('reason', $params);
426 $result = $this->invokeAPI($args);
427 if (is_a($result, 'CRM_Core_Error')) {
430 $message = "{$result['ack']}: profileid={$result['profileid']}";
436 function updateSubscriptionBillingInfo(&$message = '', $params = array(
438 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal') {
439 $config = CRM_Core_Config
::singleton();
441 $this->initialize($args, 'UpdateRecurringPaymentsProfile');
443 $args['PROFILEID'] = $params['subscriptionId'];
444 $args['AMT'] = $params['amount'];
445 $args['CURRENCYCODE'] = $config->defaultCurrency
;
446 $args['CREDITCARDTYPE'] = $params['credit_card_type'];
447 $args['ACCT'] = $params['credit_card_number'];
448 $args['EXPDATE'] = sprintf('%02d', $params['month']) . $params['year'];
449 $args['CVV2'] = $params['cvv2'];
451 $args['FIRSTNAME'] = $params['first_name'];
452 $args['LASTNAME'] = $params['last_name'];
453 $args['STREET'] = $params['street_address'];
454 $args['CITY'] = $params['city'];
455 $args['STATE'] = $params['state_province'];
456 $args['COUNTRYCODE'] = $params['postal_code'];
457 $args['ZIP'] = $params['country'];
459 $result = $this->invokeAPI($args);
460 if (is_a($result, 'CRM_Core_Error')) {
463 $message = "{$result['ack']}: profileid={$result['profileid']}";
469 function changeSubscriptionAmount(&$message = '', $params = array()) {
470 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal') {
471 $config = CRM_Core_Config
::singleton();
473 $this->initialize($args, 'UpdateRecurringPaymentsProfile');
475 $args['PROFILEID'] = $params['subscriptionId'];
476 $args['AMT'] = $params['amount'];
477 $args['CURRENCYCODE'] = $config->defaultCurrency
;
478 $args['BILLINGFREQUENCY'] = $params['installments'];
480 $result = $this->invokeAPI($args);
481 CRM_Core_Error
::debug_var('$result', $result);
482 if (is_a($result, 'CRM_Core_Error')) {
485 $message = "{$result['ack']}: profileid={$result['profileid']}";
491 function doTransferCheckout(&$params, $component = 'contribute') {
492 $config = CRM_Core_Config
::singleton();
494 if ($component != 'contribute' && $component != 'event') {
495 CRM_Core_Error
::fatal(ts('Component is invalid'));
498 $notifyURL = $config->userFrameworkResourceURL
. "extern/ipn.php?reset=1&contactID={$params['contactID']}" . "&contributionID={$params['contributionID']}" . "&module={$component}";
500 if ($component == 'event') {
501 $notifyURL .= "&eventID={$params['eventID']}&participantID={$params['participantID']}";
504 $membershipID = CRM_Utils_Array
::value('membershipID', $params);
506 $notifyURL .= "&membershipID=$membershipID";
508 $relatedContactID = CRM_Utils_Array
::value('related_contact', $params);
509 if ($relatedContactID) {
510 $notifyURL .= "&relatedContactID=$relatedContactID";
512 $onBehalfDupeAlert = CRM_Utils_Array
::value('onbehalf_dupe_alert', $params);
513 if ($onBehalfDupeAlert) {
514 $notifyURL .= "&onBehalfDupeAlert=$onBehalfDupeAlert";
519 $url = ($component == 'event') ?
'civicrm/event/register' : 'civicrm/contribute/transact';
520 $cancel = ($component == 'event') ?
'_qf_Register_display' : '_qf_Main_display';
521 $returnURL = CRM_Utils_System
::url($url,
522 "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
526 $cancelUrlString = "$cancel=1&cancel=1&qfKey={$params['qfKey']}";
527 if (!empty($params['is_recur'])) {
528 $cancelUrlString .= "&isRecur=1&recurId={$params['contributionRecurID']}&contribId={$params['contributionID']}";
531 $cancelURL = CRM_Utils_System
::url(
537 // ensure that the returnURL is absolute.
538 if (substr($returnURL, 0, 4) != 'http') {
539 $fixUrl = CRM_Utils_System
::url("civicrm/admin/setting/url", '&reset=1');
540 CRM_Core_Error
::fatal(ts('Sending a relative URL to PayPalIPN is erroneous. Please make your resource URL (in <a href="%1">Administer » System Settings » Resource URLs</a> ) complete.', array(1 => $fixUrl)));
543 $paypalParams = array(
544 'business' => $this->_paymentProcessor
['user_name'],
545 'notify_url' => $notifyURL,
546 'item_name' => $params['item_name'],
548 'undefined_quantity' => 0,
549 'cancel_return' => $cancelURL,
552 'return' => $returnURL,
554 'currency_code' => $params['currencyID'],
555 'invoice' => $params['invoiceID'],
556 'lc' => substr($config->lcMessages
, -2),
557 'charset' => function_exists('mb_internal_encoding') ?
mb_internal_encoding() : 'UTF-8',
558 'custom' => CRM_Utils_Array
::value('accountingCode',
563 // add name and address if available, CRM-3130
565 'first_name' => 'first_name',
566 'last_name' => 'last_name',
567 'street_address' => 'address1',
568 'country' => 'country',
569 'preferred_language' => 'lc',
571 'state_province' => 'state',
572 'postal_code' => 'zip',
576 foreach (array_keys($params) as $p) {
577 // get the base name without the location type suffixed to it
578 $parts = explode('-', $p);
579 $name = count($parts) > 1 ?
$parts[0] : $p;
580 if (isset($otherVars[$name])) {
581 $value = $params[$p];
583 if ($name == 'state_province') {
584 $stateName = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value);
587 if ($name == 'country') {
588 $countryName = CRM_Core_PseudoConstant
::countryIsoCode($value);
589 $value = $countryName;
591 // ensure value is not an array
593 if (!is_array($value)) {
594 $paypalParams[$otherVars[$name]] = $value;
600 // if recurring donations, add a few more items
601 if (!empty($params['is_recur'])) {
602 if ($params['contributionRecurID']) {
603 $notifyURL .= "&contributionRecurID={$params['contributionRecurID']}&contributionPageID={$params['contributionPageID']}";
604 $paypalParams['notify_url'] = $notifyURL;
607 CRM_Core_Error
::fatal(ts('Recurring contribution, but no database id'));
610 $paypalParams +
= array(
611 'cmd' => '_xclick-subscriptions',
612 'a3' => $params['amount'],
613 'p3' => $params['frequency_interval'],
614 't3' => ucfirst(substr($params['frequency_unit'], 0, 1)),
617 'srt' => CRM_Utils_Array
::value('installments', $params),
623 $paypalParams +
= array(
625 'amount' => $params['amount'],
629 // Allow further manipulation of the arguments via custom hooks ..
630 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $paypalParams);
633 foreach ($paypalParams as $key => $value) {
634 if ($value === NULL) {
638 $value = urlencode($value);
639 if ($key == 'return' ||
640 $key == 'cancel_return' ||
643 $value = str_replace('%2F', '/', $value);
645 $uri .= "&{$key}={$value}";
648 $uri = substr($uri, 1);
649 $url = $this->_paymentProcessor
['url_site'];
650 $sub = empty($params['is_recur']) ?
'cgi-bin/webscr' : 'subscriptions';
651 $paypalURL = "{$url}{$sub}?$uri";
653 CRM_Utils_System
::redirect($paypalURL);
657 * hash_call: Function to perform the API call to PayPal using API signature
658 * @methodName is name of API method.
659 * @nvpStr is nvp string.
660 * returns an associtive array containing the response from the server.
662 function invokeAPI($args, $url = NULL) {
665 if (empty($this->_paymentProcessor
['url_api'])) {
666 CRM_Core_Error
::fatal(ts('Please set the API URL. Please refer to the documentation for more details'));
669 $url = $this->_paymentProcessor
['url_api'] . 'nvp';
672 if (!function_exists('curl_init')) {
673 CRM_Core_Error
::fatal("curl functions NOT available.");
676 //setting the curl parameters.
678 curl_setopt($ch, CURLOPT_URL
, $url);
679 curl_setopt($ch, CURLOPT_VERBOSE
, 1);
681 //turning off the server and peer verification(TrustManager Concept).
682 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL'));
683 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL') ?
2 : 0);
685 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
686 curl_setopt($ch, CURLOPT_POST
, 1);
689 foreach ($args as $n => $v) {
690 $p[] = "$n=" . urlencode($v);
693 //NVPRequest for submitting to server
694 $nvpreq = implode('&', $p);
696 //setting the nvpreq as POST FIELD to curl
697 curl_setopt($ch, CURLOPT_POSTFIELDS
, $nvpreq);
699 //getting response from server
700 $response = curl_exec($ch);
702 //converting NVPResponse to an Associative Array
703 $result = self
::deformat($response);
705 if (curl_errno($ch)) {
706 $e = CRM_Core_Error
::singleton();
707 $e->push(curl_errno($ch),
717 if (strtolower($result['ack']) != 'success' &&
718 strtolower($result['ack']) != 'successwithwarning'
720 $e = CRM_Core_Error
::singleton();
721 $e->push($result['l_errorcode0'],
723 "{$result['l_shortmessage0']} {$result['l_longmessage0']}"
731 /** This function will take NVPString and convert it to an Associative Array and it will decode the response.
732 * It is usefull to search for a particular key and displaying arrays.
733 * @nvpstr is NVPString.
734 * @nvpArray is Associative Array.
736 static function deformat($str) {
739 while (strlen($str)) {
741 $keyPos = strpos($str, '=');
744 $valPos = strpos($str, '&') ?
strpos($str, '&') : strlen($str);
746 /*getting the Key and Value values and storing in a Associative Array*/
748 $key = substr($str, 0, $keyPos);
749 $val = substr($str, $keyPos +
1, $valPos - $keyPos - 1);
751 //decoding the respose
752 $result[strtolower(urldecode($key))] = urldecode($val);
753 $str = substr($str, $valPos +
1, strlen($str));