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
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
54 * @param $paymentProcessor
56 * @return \CRM_Core_Payment_PayPalImpl
58 function __construct($mode, &$paymentProcessor) {
60 $this->_paymentProcessor
= $paymentProcessor;
61 $this->_processorName
= ts('PayPal Pro');
62 $paymentProcessorType = CRM_Core_PseudoConstant
::paymentProcessorType(false, null, 'name');
64 if ($this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal_Standard', $paymentProcessorType)) {
65 $this->_processorName
= ts('PayPal Standard');
68 elseif ($this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal_Express', $paymentProcessorType)) {
69 $this->_processorName
= ts('PayPal Express');
72 if (!$this->_paymentProcessor
['user_name']) {
73 CRM_Core_Error
::fatal(ts('Could not find user name for payment processor'));
78 * singleton function used to manage this object
80 * @param string $mode the mode of operation: live or test
82 * @param object $paymentProcessor
83 * @param null $paymentForm
89 static function &singleton($mode, &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
90 $processorName = $paymentProcessor['name'];
91 if (!isset(self
::$_singleton[$processorName]) || self
::$_singleton[$processorName] === NULL) {
92 self
::$_singleton[$processorName] = new CRM_Core_Payment_PaypalImpl($mode, $paymentProcessor);
94 return self
::$_singleton[$processorName];
98 * are back office payments supported - e.g paypal standard won't permit you to enter a credit card associated with someone else's login
101 protected function supportsBackOffice() {
102 if ($this->_processorName
== ts('PayPal Pro')) {
108 * express checkout code. Check PayPal documentation for more information
110 * @param array $params assoc array of input parameters for this transaction
112 * @return array the result in an nice formatted array (or an error object)
115 function setExpressCheckOut(&$params) {
118 $this->initialize($args, 'SetExpressCheckout');
120 $args['paymentAction'] = $params['payment_action'];
121 $args['amt'] = $params['amount'];
122 $args['currencyCode'] = $params['currencyID'];
123 $args['desc'] = $params['description'];
124 $args['invnum'] = $params['invoiceID'];
125 $args['returnURL'] = $params['returnURL'];
126 $args['cancelURL'] = $params['cancelURL'];
127 $args['version'] = '56.0';
129 //LCD if recurring, collect additional data and set some values
130 if (!empty($params['is_recur'])) {
131 $args['L_BILLINGTYPE0'] = 'RecurringPayments';
132 //$args['L_BILLINGAGREEMENTDESCRIPTION0'] = 'Recurring Contribution';
133 $args['L_BILLINGAGREEMENTDESCRIPTION0'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit'];
134 $args['L_PAYMENTTYPE0'] = 'Any';
137 // Allow further manipulation of the arguments via custom hooks ..
138 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $args);
140 $result = $this->invokeAPI($args);
142 if (is_a($result, 'CRM_Core_Error')) {
148 return $result['token'];
152 * get details from paypal. Check PayPal documentation for more information
154 * @param string $token the key associated with this transaction
156 * @return array the result in an nice formatted array (or an error object)
159 function getExpressCheckoutDetails($token) {
162 $this->initialize($args, 'GetExpressCheckoutDetails');
163 $args['token'] = $token;
165 $args['method'] = 'GetExpressCheckoutDetails';
167 $result = $this->invokeAPI($args);
169 if (is_a($result, 'CRM_Core_Error')) {
176 $params['token'] = $result['token'];
177 $params['payer_id'] = $result['payerid'];
178 $params['payer_status'] = $result['payerstatus'];
179 $params['first_name'] = $result['firstname'];
180 $params['middle_name'] = $result['middlename'];
181 $params['last_name'] = $result['lastname'];
182 $params['street_address'] = $result['shiptostreet'];
183 $params['supplemental_address_1'] = $result['shiptostreet2'];
184 $params['city'] = $result['shiptocity'];
185 $params['state_province'] = $result['shiptostate'];
186 $params['postal_code'] = $result['shiptozip'];
187 $params['country'] = $result['shiptocountrycode'];
193 * do the express checkout at paypal. Check PayPal documentation for more information
195 * @param array $params
197 * @internal param string $token the key associated with this transaction
199 * @return array the result in an nice formatted array (or an error object)
202 function doExpressCheckout(&$params) {
205 $this->initialize($args, 'DoExpressCheckoutPayment');
207 $args['token'] = $params['token'];
208 $args['paymentAction'] = $params['payment_action'];
209 $args['amt'] = $params['amount'];
210 $args['currencyCode'] = $params['currencyID'];
211 $args['payerID'] = $params['payer_id'];
212 $args['invnum'] = $params['invoiceID'];
213 $args['returnURL'] = $params['returnURL'];
214 $args['cancelURL'] = $params['cancelURL'];
215 $args['desc'] = $params['description'];
217 $result = $this->invokeAPI($args);
219 if (is_a($result, 'CRM_Core_Error')) {
225 $params['trxn_id'] = $result['transactionid'];
226 $params['gross_amount'] = $result['amt'];
227 $params['fee_amount'] = $result['feeamt'];
228 $params['net_amount'] = $result['settleamt'];
229 if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) {
230 $params['net_amount'] = $params['gross_amount'] - $params['fee_amount'];
232 $params['payment_status'] = $result['paymentstatus'];
233 $params['pending_reason'] = $result['pendingreason'];
238 //LCD add new function for handling recurring payments for PayPal Express
240 * @param array $params
244 function createRecurringPayments(&$params) {
247 $this->initialize($args, 'CreateRecurringPaymentsProfile');
249 $start_time = strtotime(date('m/d/Y'));
250 $start_date = date('Y-m-d\T00:00:00\Z', $start_time);
252 $args['token'] = $params['token'];
253 $args['paymentAction'] = $params['payment_action'];
254 $args['amt'] = $params['amount'];
255 $args['currencyCode'] = $params['currencyID'];
256 $args['payerID'] = $params['payer_id'];
257 $args['invnum'] = $params['invoiceID'];
258 $args['returnURL'] = $params['returnURL'];
259 $args['cancelURL'] = $params['cancelURL'];
260 $args['profilestartdate'] = $start_date;
261 $args['method'] = 'CreateRecurringPaymentsProfile';
262 $args['billingfrequency'] = $params['frequency_interval'];
263 $args['billingperiod'] = ucwords($params['frequency_unit']);
264 $args['desc'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit'];
265 //$args['desc'] = 'Recurring Contribution';
266 $args['totalbillingcycles'] = $params['installments'];
267 $args['version'] = '56.0';
268 $args['profilereference'] =
269 "i={$params['invoiceID']}" .
271 "&c={$params['contactID']}" .
272 "&r={$params['contributionRecurID']}" .
273 "&b={$params['contributionID']}" .
274 "&p={$params['contributionPageID']}";
276 $result = $this->invokeAPI($args);
278 if (is_a($result, 'CRM_Core_Error')) {
284 $params['trxn_id'] = $result['transactionid'];
285 $params['gross_amount'] = $result['amt'];
286 $params['fee_amount'] = $result['feeamt'];
287 $params['net_amount'] = $result['settleamt'];
288 if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) {
289 $params['net_amount'] = $params['gross_amount'] - $params['fee_amount'];
291 $params['payment_status'] = $result['paymentstatus'];
292 $params['pending_reason'] = $result['pendingreason'];
301 function initialize(&$args, $method) {
302 $args['user'] = $this->_paymentProcessor
['user_name'];
303 $args['pwd'] = $this->_paymentProcessor
['password'];
304 $args['version'] = 3.0;
305 $args['signature'] = $this->_paymentProcessor
['signature'];
306 $args['subject'] = $this->_paymentProcessor
['subject'];
307 $args['method'] = $method;
311 * This function collects all the information from a web/api form and invokes
312 * the relevant payment processor specific functions to perform the transaction
314 * @param array $params assoc array of input parameters for this transaction
316 * @param string $component
317 * @return array the result in an nice formatted array (or an error object)
320 function doDirectPayment(&$params, $component = 'contribute') {
323 $this->initialize($args, 'DoDirectPayment');
325 $args['paymentAction'] = $params['payment_action'];
326 $args['amt'] = $params['amount'];
327 $args['currencyCode'] = $params['currencyID'];
328 $args['invnum'] = $params['invoiceID'];
329 $args['ipaddress'] = $params['ip_address'];
330 $args['creditCardType'] = $params['credit_card_type'];
331 $args['acct'] = $params['credit_card_number'];
332 $args['expDate'] = sprintf('%02d', $params['month']) . $params['year'];
333 $args['cvv2'] = $params['cvv2'];
334 $args['firstName'] = $params['first_name'];
335 $args['lastName'] = $params['last_name'];
336 $args['email'] = CRM_Utils_Array
::value('email', $params);
337 $args['street'] = $params['street_address'];
338 $args['city'] = $params['city'];
339 $args['state'] = $params['state_province'];
340 $args['countryCode'] = $params['country'];
341 $args['zip'] = $params['postal_code'];
342 $args['desc'] = substr(CRM_Utils_Array
::value('description', $params), 0, 127);
343 $args['custom'] = CRM_Utils_Array
::value('accountingCode', $params);
345 if (CRM_Utils_Array
::value('is_recur', $params) == 1) {
346 $start_time = strtotime(date('m/d/Y'));
347 $start_date = date('Y-m-d\T00:00:00\Z', $start_time);
349 $args['PaymentAction'] = 'Sale';
350 $args['billingperiod'] = ucwords($params['frequency_unit']);
351 $args['billingfrequency'] = $params['frequency_interval'];
352 $args['method'] = "CreateRecurringPaymentsProfile";
353 $args['profilestartdate'] = $start_date;
355 $params['description'] . ": " .
356 $params['amount'] . " Per " .
357 $params['frequency_interval'] . " " .
358 $params['frequency_unit'];
359 $args['amt'] = $params['amount'];
360 $args['totalbillingcycles'] = $params['installments'];
361 $args['version'] = 56.0;
362 $args['PROFILEREFERENCE'] =
363 "i=" . $params['invoiceID'] . "&m=" . $component .
364 "&c=" . $params['contactID'] . "&r=" . $params['contributionRecurID'] .
365 "&b=" . $params['contributionID'] . "&p=" . $params['contributionPageID'];
368 // Allow further manipulation of the arguments via custom hooks ..
369 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $args);
371 $result = $this->invokeAPI($args);
374 if (is_a($result, 'CRM_Core_Error')) {
378 $params['recurr_profile_id'] = NULL;
380 if (CRM_Utils_Array
::value('is_recur', $params) == 1) {
381 $params['recurr_profile_id'] = $result['profileid'];
386 $params['trxn_id'] = CRM_Utils_Array
::value('transactionid', $result);
387 $params['gross_amount'] = CRM_Utils_Array
::value('amt', $result);
392 * This function checks to see if we have the right config values
394 * @return string the error message if any
397 function checkConfig() {
399 $paymentProcessorType = CRM_Core_PseudoConstant
::paymentProcessorType(false, null, 'name');
401 $this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal_Standard', $paymentProcessorType) ||
402 $this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal', $paymentProcessorType)
404 if (empty($this->_paymentProcessor
['user_name'])) {
405 $error[] = ts('User Name is not set in the Administer » System Settings » Payment Processors.');
409 if ($this->_paymentProcessor
['payment_processor_type_id'] != CRM_Utils_Array
::key('PayPal_Standard', $paymentProcessorType)) {
410 if (empty($this->_paymentProcessor
['signature'])) {
411 $error[] = ts('Signature is not set in the Administer » System Settings » Payment Processors.');
414 if (empty($this->_paymentProcessor
['password'])) {
415 $error[] = ts('Password is not set in the Administer » System Settings » Payment Processors.');
419 if (!empty($error)) {
420 return implode('<p>', $error);
428 * @return null|string
430 function cancelSubscriptionURL() {
431 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal_Standard') {
432 return "{$this->_paymentProcessor['url_site']}cgi-bin/webscr?cmd=_subscr-find&alias=" . urlencode($this->_paymentProcessor
['user_name']);
440 * check whether a method is present ( & supported ) by the payment processor object.
442 * @param string $method method to check for.
447 function isSupported($method = 'cancelSubscription') {
448 if ($this->_paymentProcessor
['payment_processor_type'] != 'PayPal') {
449 // since subscription methods like cancelSubscription or updateBilling is not yet implemented / supported
450 // by standard or express.
453 return parent
::isSupported($method);
457 * @param string $message
458 * @param array $params
460 * @return array|bool|object
462 function cancelSubscription(&$message = '', $params = array()) {
463 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal') {
465 $this->initialize($args, 'ManageRecurringPaymentsProfileStatus');
467 $args['PROFILEID'] = CRM_Utils_Array
::value('subscriptionId', $params);
468 $args['ACTION'] = 'Cancel';
469 $args['NOTE'] = CRM_Utils_Array
::value('reason', $params);
471 $result = $this->invokeAPI($args);
472 if (is_a($result, 'CRM_Core_Error')) {
475 $message = "{$result['ack']}: profileid={$result['profileid']}";
482 * @param string $message
483 * @param array $params
485 * @return array|bool|object
487 function updateSubscriptionBillingInfo(&$message = '', $params = array()) {
488 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal') {
489 $config = CRM_Core_Config
::singleton();
491 $this->initialize($args, 'UpdateRecurringPaymentsProfile');
493 $args['PROFILEID'] = $params['subscriptionId'];
494 $args['AMT'] = $params['amount'];
495 $args['CURRENCYCODE'] = $config->defaultCurrency
;
496 $args['CREDITCARDTYPE'] = $params['credit_card_type'];
497 $args['ACCT'] = $params['credit_card_number'];
498 $args['EXPDATE'] = sprintf('%02d', $params['month']) . $params['year'];
499 $args['CVV2'] = $params['cvv2'];
501 $args['FIRSTNAME'] = $params['first_name'];
502 $args['LASTNAME'] = $params['last_name'];
503 $args['STREET'] = $params['street_address'];
504 $args['CITY'] = $params['city'];
505 $args['STATE'] = $params['state_province'];
506 $args['COUNTRYCODE'] = $params['postal_code'];
507 $args['ZIP'] = $params['country'];
509 $result = $this->invokeAPI($args);
510 if (is_a($result, 'CRM_Core_Error')) {
513 $message = "{$result['ack']}: profileid={$result['profileid']}";
520 * @param string $message
521 * @param array $params
523 * @return array|bool|object
525 function changeSubscriptionAmount(&$message = '', $params = array()) {
526 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal') {
527 $config = CRM_Core_Config
::singleton();
529 $this->initialize($args, 'UpdateRecurringPaymentsProfile');
531 $args['PROFILEID'] = $params['subscriptionId'];
532 $args['AMT'] = $params['amount'];
533 $args['CURRENCYCODE'] = $config->defaultCurrency
;
534 $args['BILLINGFREQUENCY'] = $params['installments'];
536 $result = $this->invokeAPI($args);
537 CRM_Core_Error
::debug_var('$result', $result);
538 if (is_a($result, 'CRM_Core_Error')) {
541 $message = "{$result['ack']}: profileid={$result['profileid']}";
548 * @param array $params
549 * @param string $component
553 function doTransferCheckout(&$params, $component = 'contribute') {
554 $config = CRM_Core_Config
::singleton();
556 if ($component != 'contribute' && $component != 'event') {
557 CRM_Core_Error
::fatal(ts('Component is invalid'));
560 $notifyURL = $config->userFrameworkResourceURL
. "extern/ipn.php?reset=1&contactID={$params['contactID']}" . "&contributionID={$params['contributionID']}" . "&module={$component}";
562 if ($component == 'event') {
563 $notifyURL .= "&eventID={$params['eventID']}&participantID={$params['participantID']}";
566 $membershipID = CRM_Utils_Array
::value('membershipID', $params);
568 $notifyURL .= "&membershipID=$membershipID";
570 $relatedContactID = CRM_Utils_Array
::value('related_contact', $params);
571 if ($relatedContactID) {
572 $notifyURL .= "&relatedContactID=$relatedContactID";
574 $onBehalfDupeAlert = CRM_Utils_Array
::value('onbehalf_dupe_alert', $params);
575 if ($onBehalfDupeAlert) {
576 $notifyURL .= "&onBehalfDupeAlert=$onBehalfDupeAlert";
581 $url = ($component == 'event') ?
'civicrm/event/register' : 'civicrm/contribute/transact';
582 $cancel = ($component == 'event') ?
'_qf_Register_display' : '_qf_Main_display';
583 $returnURL = CRM_Utils_System
::url($url,
584 "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
588 $cancelUrlString = "$cancel=1&cancel=1&qfKey={$params['qfKey']}";
589 if (!empty($params['is_recur'])) {
590 $cancelUrlString .= "&isRecur=1&recurId={$params['contributionRecurID']}&contribId={$params['contributionID']}";
593 $cancelURL = CRM_Utils_System
::url(
599 // ensure that the returnURL is absolute.
600 if (substr($returnURL, 0, 4) != 'http') {
601 $fixUrl = CRM_Utils_System
::url("civicrm/admin/setting/url", '&reset=1');
602 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)));
605 $paypalParams = array(
606 'business' => $this->_paymentProcessor
['user_name'],
607 'notify_url' => $notifyURL,
608 'item_name' => $params['item_name'],
610 'undefined_quantity' => 0,
611 'cancel_return' => $cancelURL,
614 'return' => $returnURL,
616 'currency_code' => $params['currencyID'],
617 'invoice' => $params['invoiceID'],
618 'lc' => substr($config->lcMessages
, -2),
619 'charset' => function_exists('mb_internal_encoding') ?
mb_internal_encoding() : 'UTF-8',
620 'custom' => CRM_Utils_Array
::value('accountingCode',
625 // add name and address if available, CRM-3130
627 'first_name' => 'first_name',
628 'last_name' => 'last_name',
629 'street_address' => 'address1',
630 'country' => 'country',
631 'preferred_language' => 'lc',
633 'state_province' => 'state',
634 'postal_code' => 'zip',
638 foreach (array_keys($params) as $p) {
639 // get the base name without the location type suffixed to it
640 $parts = explode('-', $p);
641 $name = count($parts) > 1 ?
$parts[0] : $p;
642 if (isset($otherVars[$name])) {
643 $value = $params[$p];
645 if ($name == 'state_province') {
646 $stateName = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value);
649 if ($name == 'country') {
650 $countryName = CRM_Core_PseudoConstant
::countryIsoCode($value);
651 $value = $countryName;
653 // ensure value is not an array
655 if (!is_array($value)) {
656 $paypalParams[$otherVars[$name]] = $value;
662 // if recurring donations, add a few more items
663 if (!empty($params['is_recur'])) {
664 if ($params['contributionRecurID']) {
665 $notifyURL .= "&contributionRecurID={$params['contributionRecurID']}&contributionPageID={$params['contributionPageID']}";
666 $paypalParams['notify_url'] = $notifyURL;
669 CRM_Core_Error
::fatal(ts('Recurring contribution, but no database id'));
672 $paypalParams +
= array(
673 'cmd' => '_xclick-subscriptions',
674 'a3' => $params['amount'],
675 'p3' => $params['frequency_interval'],
676 't3' => ucfirst(substr($params['frequency_unit'], 0, 1)),
679 'srt' => CRM_Utils_Array
::value('installments', $params),
685 $paypalParams +
= array(
687 'amount' => $params['amount'],
691 // Allow further manipulation of the arguments via custom hooks ..
692 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $paypalParams);
695 foreach ($paypalParams as $key => $value) {
696 if ($value === NULL) {
700 $value = urlencode($value);
701 if ($key == 'return' ||
702 $key == 'cancel_return' ||
705 $value = str_replace('%2F', '/', $value);
707 $uri .= "&{$key}={$value}";
710 $uri = substr($uri, 1);
711 $url = $this->_paymentProcessor
['url_site'];
712 $sub = empty($params['is_recur']) ?
'cgi-bin/webscr' : 'subscriptions';
713 $paypalURL = "{$url}{$sub}?$uri";
715 CRM_Utils_System
::redirect($paypalURL);
719 * hash_call: Function to perform the API call to PayPal using API signature
720 * @methodName is name of API method.
721 * @nvpStr is nvp string.
722 * returns an associtive array containing the response from the server.
724 function invokeAPI($args, $url = NULL) {
727 if (empty($this->_paymentProcessor
['url_api'])) {
728 CRM_Core_Error
::fatal(ts('Please set the API URL. Please refer to the documentation for more details'));
731 $url = $this->_paymentProcessor
['url_api'] . 'nvp';
734 if (!function_exists('curl_init')) {
735 CRM_Core_Error
::fatal("curl functions NOT available.");
738 //setting the curl parameters.
740 curl_setopt($ch, CURLOPT_URL
, $url);
741 curl_setopt($ch, CURLOPT_VERBOSE
, 1);
743 //turning off the server and peer verification(TrustManager Concept).
744 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL'));
745 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL') ?
2 : 0);
747 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
748 curl_setopt($ch, CURLOPT_POST
, 1);
751 foreach ($args as $n => $v) {
752 $p[] = "$n=" . urlencode($v);
755 //NVPRequest for submitting to server
756 $nvpreq = implode('&', $p);
758 //setting the nvpreq as POST FIELD to curl
759 curl_setopt($ch, CURLOPT_POSTFIELDS
, $nvpreq);
761 //getting response from server
762 $response = curl_exec($ch);
764 //converting NVPResponse to an Associative Array
765 $result = self
::deformat($response);
767 if (curl_errno($ch)) {
768 $e = CRM_Core_Error
::singleton();
769 $e->push(curl_errno($ch),
779 if (strtolower($result['ack']) != 'success' &&
780 strtolower($result['ack']) != 'successwithwarning'
782 $e = CRM_Core_Error
::singleton();
783 $e->push($result['l_errorcode0'],
785 "{$result['l_shortmessage0']} {$result['l_longmessage0']}"
793 /** This function will take NVPString and convert it to an Associative Array and it will decode the response.
794 * It is usefull to search for a particular key and displaying arrays.
795 * @nvpstr is NVPString.
796 * @nvpArray is Associative Array.
798 static function deformat($str) {
801 while (strlen($str)) {
803 $keyPos = strpos($str, '=');
806 $valPos = strpos($str, '&') ?
strpos($str, '&') : strlen($str);
808 /*getting the Key and Value values and storing in a Associative Array*/
810 $key = substr($str, 0, $keyPos);
811 $val = substr($str, $keyPos +
1, $valPos - $keyPos - 1);
813 //decoding the respose
814 $result[strtolower(urldecode($key))] = urldecode($val);
815 $str = substr($str, $valPos +
1, strlen($str));