3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
46 static private $_singleton = NULL;
52 * The mode of operation: live or test.
54 * @param $paymentProcessor
56 * @return \CRM_Core_Payment_PayPalImpl
58 public 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 * Are back office payments supported - e.g paypal standard won't permit you to enter a credit card associated with someone else's login
81 protected function supportsBackOffice() {
82 if ($this->_processorName
== ts('PayPal Pro')) {
89 * Express checkout code. Check PayPal documentation for more information
91 * @param array $params
92 * Assoc array of input parameters for this transaction.
95 * the result in an nice formatted array (or an error object)
97 public function setExpressCheckOut(&$params) {
100 $this->initialize($args, 'SetExpressCheckout');
102 $args['paymentAction'] = $params['payment_action'];
103 $args['amt'] = $params['amount'];
104 $args['currencyCode'] = $params['currencyID'];
105 $args['desc'] = CRM_Utils_Array
::value('description', $params);
106 $args['invnum'] = $params['invoiceID'];
107 $args['returnURL'] = $params['returnURL'];
108 $args['cancelURL'] = $params['cancelURL'];
109 $args['version'] = '56.0';
111 //LCD if recurring, collect additional data and set some values
112 if (!empty($params['is_recur'])) {
113 $args['L_BILLINGTYPE0'] = 'RecurringPayments';
114 //$args['L_BILLINGAGREEMENTDESCRIPTION0'] = 'Recurring Contribution';
115 $args['L_BILLINGAGREEMENTDESCRIPTION0'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit'];
116 $args['L_PAYMENTTYPE0'] = 'Any';
119 // Allow further manipulation of the arguments via custom hooks ..
120 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $args);
122 $result = $this->invokeAPI($args);
124 if (is_a($result, 'CRM_Core_Error')) {
130 return $result['token'];
134 * Get details from paypal. Check PayPal documentation for more information
136 * @param string $token
137 * The key associated with this transaction.
140 * the result in an nice formatted array (or an error object)
142 public function getExpressCheckoutDetails($token) {
145 $this->initialize($args, 'GetExpressCheckoutDetails');
146 $args['token'] = $token;
148 $args['method'] = 'GetExpressCheckoutDetails';
150 $result = $this->invokeAPI($args);
152 if (is_a($result, 'CRM_Core_Error')) {
159 $params['token'] = $result['token'];
160 $params['payer_id'] = $result['payerid'];
161 $params['payer_status'] = $result['payerstatus'];
162 $params['first_name'] = $result['firstname'];
163 $params['middle_name'] = CRM_Utils_Array
::value('middlename', $result);
164 $params['last_name'] = $result['lastname'];
165 $params['street_address'] = $result['shiptostreet'];
166 $params['supplemental_address_1'] = CRM_Utils_Array
::value('shiptostreet2', $result);
167 $params['city'] = $result['shiptocity'];
168 $params['state_province'] = $result['shiptostate'];
169 $params['postal_code'] = $result['shiptozip'];
170 $params['country'] = $result['shiptocountrycode'];
176 * Do the express checkout at paypal. Check PayPal documentation for more information
178 * @param array $params
180 * @internal param string $token the key associated with this transaction
183 * the result in an nice formatted array (or an error object)
185 public function doExpressCheckout(&$params) {
188 $this->initialize($args, 'DoExpressCheckoutPayment');
190 $args['token'] = $params['token'];
191 $args['paymentAction'] = $params['payment_action'];
192 $args['amt'] = $params['amount'];
193 $args['currencyCode'] = $params['currencyID'];
194 $args['payerID'] = $params['payer_id'];
195 $args['invnum'] = $params['invoiceID'];
196 $args['returnURL'] = CRM_Utils_Array
::value('returnURL', $params);
197 $args['cancelURL'] = CRM_Utils_Array
::value('cancelURL', $params);
198 $args['desc'] = $params['description'];
200 // add CiviCRM BN code
201 $args['BUTTONSOURCE'] = 'CiviCRM_SP';
203 $result = $this->invokeAPI($args);
205 if (is_a($result, 'CRM_Core_Error')) {
211 $params['trxn_id'] = $result['transactionid'];
212 $params['gross_amount'] = $result['amt'];
213 $params['fee_amount'] = $result['feeamt'];
214 $params['net_amount'] = CRM_Utils_Array
::value('settleamt', $result);
215 if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) {
216 $params['net_amount'] = $params['gross_amount'] - $params['fee_amount'];
218 $params['payment_status'] = $result['paymentstatus'];
219 $params['pending_reason'] = $result['pendingreason'];
224 //LCD add new function for handling recurring payments for PayPal Express
226 * @param array $params
230 public function createRecurringPayments(&$params) {
233 $this->initialize($args, 'CreateRecurringPaymentsProfile');
235 $start_time = strtotime(date('m/d/Y'));
236 $start_date = date('Y-m-d\T00:00:00\Z', $start_time);
238 $args['token'] = $params['token'];
239 $args['paymentAction'] = $params['payment_action'];
240 $args['amt'] = $params['amount'];
241 $args['currencyCode'] = $params['currencyID'];
242 $args['payerID'] = $params['payer_id'];
243 $args['invnum'] = $params['invoiceID'];
244 $args['returnURL'] = $params['returnURL'];
245 $args['cancelURL'] = $params['cancelURL'];
246 $args['profilestartdate'] = $start_date;
247 $args['method'] = 'CreateRecurringPaymentsProfile';
248 $args['billingfrequency'] = $params['frequency_interval'];
249 $args['billingperiod'] = ucwords($params['frequency_unit']);
250 $args['desc'] = $params['amount'] . " Per " . $params['frequency_interval'] . " " . $params['frequency_unit'];
251 //$args['desc'] = 'Recurring Contribution';
252 $args['totalbillingcycles'] = $params['installments'];
253 $args['version'] = '56.0';
254 $args['profilereference'] = "i={$params['invoiceID']}" .
256 "&c={$params['contactID']}" .
257 "&r={$params['contributionRecurID']}" .
258 "&b={$params['contributionID']}" .
259 "&p={$params['contributionPageID']}";
261 // add CiviCRM BN code
262 $args['BUTTONSOURCE'] = 'CiviCRM_SP';
264 $result = $this->invokeAPI($args);
266 if (is_a($result, 'CRM_Core_Error')) {
271 $params['trxn_id'] = $result['transactionid'];
272 $params['gross_amount'] = $result['amt'];
273 $params['fee_amount'] = $result['feeamt'];
274 $params['net_amount'] = $result['settleamt'];
275 if ($params['net_amount'] == 0 && $params['fee_amount'] != 0) {
276 $params['net_amount'] = $params['gross_amount'] - $params['fee_amount'];
278 $params['payment_status'] = $result['paymentstatus'];
279 $params['pending_reason'] = $result['pendingreason'];
288 public function initialize(&$args, $method) {
289 $args['user'] = $this->_paymentProcessor
['user_name'];
290 $args['pwd'] = $this->_paymentProcessor
['password'];
291 $args['version'] = 3.0;
292 $args['signature'] = $this->_paymentProcessor
['signature'];
293 $args['subject'] = CRM_Utils_Array
::value('subject', $this->_paymentProcessor
);
294 $args['method'] = $method;
298 * This function collects all the information from a web/api form and invokes
299 * the relevant payment processor specific functions to perform the transaction
301 * @param array $params
302 * Assoc array of input parameters for this transaction.
304 * @param string $component
306 * the result in an nice formatted array (or an error object)
308 public function doDirectPayment(&$params, $component = 'contribute') {
311 $this->initialize($args, 'DoDirectPayment');
313 $args['paymentAction'] = $params['payment_action'];
314 $args['amt'] = $params['amount'];
315 $args['currencyCode'] = $params['currencyID'];
316 $args['invnum'] = $params['invoiceID'];
317 $args['ipaddress'] = $params['ip_address'];
318 $args['creditCardType'] = $params['credit_card_type'];
319 $args['acct'] = $params['credit_card_number'];
320 $args['expDate'] = sprintf('%02d', $params['month']) . $params['year'];
321 $args['cvv2'] = $params['cvv2'];
322 $args['firstName'] = $params['first_name'];
323 $args['lastName'] = $params['last_name'];
324 $args['email'] = CRM_Utils_Array
::value('email', $params);
325 $args['street'] = $params['street_address'];
326 $args['city'] = $params['city'];
327 $args['state'] = $params['state_province'];
328 $args['countryCode'] = $params['country'];
329 $args['zip'] = $params['postal_code'];
330 $args['desc'] = substr(CRM_Utils_Array
::value('description', $params), 0, 127);
331 $args['custom'] = CRM_Utils_Array
::value('accountingCode', $params);
333 // add CiviCRM BN code
334 $args['BUTTONSOURCE'] = 'CiviCRM_SP';
336 if (CRM_Utils_Array
::value('is_recur', $params) == 1) {
337 $start_time = strtotime(date('m/d/Y'));
338 $start_date = date('Y-m-d\T00:00:00\Z', $start_time);
340 $args['PaymentAction'] = 'Sale';
341 $args['billingperiod'] = ucwords($params['frequency_unit']);
342 $args['billingfrequency'] = $params['frequency_interval'];
343 $args['method'] = "CreateRecurringPaymentsProfile";
344 $args['profilestartdate'] = $start_date;
346 $params['description'] . ": " .
347 $params['amount'] . " Per " .
348 $params['frequency_interval'] . " " .
349 $params['frequency_unit'];
350 $args['amt'] = $params['amount'];
351 $args['totalbillingcycles'] = $params['installments'];
352 $args['version'] = 56.0;
353 $args['PROFILEREFERENCE'] = "" .
354 "i=" . $params['invoiceID'] . "&m=" . $component .
355 "&c=" . $params['contactID'] . "&r=" . $params['contributionRecurID'] .
356 "&b=" . $params['contributionID'] . "&p=" . $params['contributionPageID'];
359 // Allow further manipulation of the arguments via custom hooks ..
360 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $args);
362 $result = $this->invokeAPI($args);
365 if (is_a($result, 'CRM_Core_Error')) {
369 $params['recurr_profile_id'] = NULL;
371 if (CRM_Utils_Array
::value('is_recur', $params) == 1) {
372 $params['recurr_profile_id'] = $result['profileid'];
377 $params['trxn_id'] = CRM_Utils_Array
::value('transactionid', $result);
378 $params['gross_amount'] = CRM_Utils_Array
::value('amt', $result);
383 * This function checks to see if we have the right config values.
386 * the error message if any
388 public function checkConfig() {
390 $paymentProcessorType = CRM_Core_PseudoConstant
::paymentProcessorType(FALSE, NULL, 'name');
392 $this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal_Standard', $paymentProcessorType) ||
393 $this->_paymentProcessor
['payment_processor_type_id'] == CRM_Utils_Array
::key('PayPal', $paymentProcessorType)
395 if (empty($this->_paymentProcessor
['user_name'])) {
396 $error[] = ts('User Name is not set in the Administer » System Settings » Payment Processors.');
400 if ($this->_paymentProcessor
['payment_processor_type_id'] != CRM_Utils_Array
::key('PayPal_Standard', $paymentProcessorType)) {
401 if (empty($this->_paymentProcessor
['signature'])) {
402 $error[] = ts('Signature is not set in the Administer » System Settings » Payment Processors.');
405 if (empty($this->_paymentProcessor
['password'])) {
406 $error[] = ts('Password is not set in the Administer » System Settings » Payment Processors.');
410 if (!empty($error)) {
411 return implode('<p>', $error);
419 * @return null|string
421 public function cancelSubscriptionURL() {
422 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal_Standard') {
423 return "{$this->_paymentProcessor['url_site']}cgi-bin/webscr?cmd=_subscr-find&alias=" . urlencode($this->_paymentProcessor
['user_name']);
431 * Check whether a method is present ( & supported ) by the payment processor object.
433 * @param string $method
434 * Method to check for.
438 public function isSupported($method = 'cancelSubscription') {
439 if ($this->_paymentProcessor
['payment_processor_type'] != 'PayPal') {
440 // since subscription methods like cancelSubscription or updateBilling is not yet implemented / supported
441 // by standard or express.
444 return parent
::isSupported($method);
448 * @param string $message
449 * @param array $params
451 * @return array|bool|object
453 public function cancelSubscription(&$message = '', $params = array()) {
454 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal') {
456 $this->initialize($args, 'ManageRecurringPaymentsProfileStatus');
458 $args['PROFILEID'] = CRM_Utils_Array
::value('subscriptionId', $params);
459 $args['ACTION'] = 'Cancel';
460 $args['NOTE'] = CRM_Utils_Array
::value('reason', $params);
462 $result = $this->invokeAPI($args);
463 if (is_a($result, 'CRM_Core_Error')) {
466 $message = "{$result['ack']}: profileid={$result['profileid']}";
473 * @param string $message
474 * @param array $params
476 * @return array|bool|object
478 public function updateSubscriptionBillingInfo(&$message = '', $params = array()) {
479 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal') {
480 $config = CRM_Core_Config
::singleton();
482 $this->initialize($args, 'UpdateRecurringPaymentsProfile');
484 $args['PROFILEID'] = $params['subscriptionId'];
485 $args['AMT'] = $params['amount'];
486 $args['CURRENCYCODE'] = $config->defaultCurrency
;
487 $args['CREDITCARDTYPE'] = $params['credit_card_type'];
488 $args['ACCT'] = $params['credit_card_number'];
489 $args['EXPDATE'] = sprintf('%02d', $params['month']) . $params['year'];
490 $args['CVV2'] = $params['cvv2'];
492 $args['FIRSTNAME'] = $params['first_name'];
493 $args['LASTNAME'] = $params['last_name'];
494 $args['STREET'] = $params['street_address'];
495 $args['CITY'] = $params['city'];
496 $args['STATE'] = $params['state_province'];
497 $args['COUNTRYCODE'] = $params['postal_code'];
498 $args['ZIP'] = $params['country'];
500 $result = $this->invokeAPI($args);
501 if (is_a($result, 'CRM_Core_Error')) {
504 $message = "{$result['ack']}: profileid={$result['profileid']}";
511 * @param string $message
512 * @param array $params
514 * @return array|bool|object
516 public function changeSubscriptionAmount(&$message = '', $params = array()) {
517 if ($this->_paymentProcessor
['payment_processor_type'] == 'PayPal') {
518 $config = CRM_Core_Config
::singleton();
520 $this->initialize($args, 'UpdateRecurringPaymentsProfile');
522 $args['PROFILEID'] = $params['subscriptionId'];
523 $args['AMT'] = $params['amount'];
524 $args['CURRENCYCODE'] = $config->defaultCurrency
;
525 $args['BILLINGFREQUENCY'] = $params['installments'];
527 $result = $this->invokeAPI($args);
528 CRM_Core_Error
::debug_var('$result', $result);
529 if (is_a($result, 'CRM_Core_Error')) {
532 $message = "{$result['ack']}: profileid={$result['profileid']}";
539 * @param array $params
540 * @param string $component
544 public function doTransferCheckout(&$params, $component = 'contribute') {
545 $config = CRM_Core_Config
::singleton();
547 if ($component != 'contribute' && $component != 'event') {
548 CRM_Core_Error
::fatal(ts('Component is invalid'));
551 $notifyURL = $config->userFrameworkResourceURL
. "extern/ipn.php?reset=1&contactID={$params['contactID']}" . "&contributionID={$params['contributionID']}" . "&module={$component}";
553 if ($component == 'event') {
554 $notifyURL .= "&eventID={$params['eventID']}&participantID={$params['participantID']}";
557 $membershipID = CRM_Utils_Array
::value('membershipID', $params);
559 $notifyURL .= "&membershipID=$membershipID";
561 $relatedContactID = CRM_Utils_Array
::value('related_contact', $params);
562 if ($relatedContactID) {
563 $notifyURL .= "&relatedContactID=$relatedContactID";
565 $onBehalfDupeAlert = CRM_Utils_Array
::value('onbehalf_dupe_alert', $params);
566 if ($onBehalfDupeAlert) {
567 $notifyURL .= "&onBehalfDupeAlert=$onBehalfDupeAlert";
572 $url = ($component == 'event') ?
'civicrm/event/register' : 'civicrm/contribute/transact';
573 $cancel = ($component == 'event') ?
'_qf_Register_display' : '_qf_Main_display';
574 $returnURL = CRM_Utils_System
::url($url,
575 "_qf_ThankYou_display=1&qfKey={$params['qfKey']}",
579 $cancelUrlString = "$cancel=1&cancel=1&qfKey={$params['qfKey']}";
580 if (!empty($params['is_recur'])) {
581 $cancelUrlString .= "&isRecur=1&recurId={$params['contributionRecurID']}&contribId={$params['contributionID']}";
584 $cancelURL = CRM_Utils_System
::url(
590 // ensure that the returnURL is absolute.
591 if (substr($returnURL, 0, 4) != 'http') {
592 $fixUrl = CRM_Utils_System
::url("civicrm/admin/setting/url", '&reset=1');
593 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)));
596 $paypalParams = array(
597 'business' => $this->_paymentProcessor
['user_name'],
598 'notify_url' => $notifyURL,
599 'item_name' => $params['item_name'],
601 'undefined_quantity' => 0,
602 'cancel_return' => $cancelURL,
605 'return' => $returnURL,
607 'currency_code' => $params['currencyID'],
608 'invoice' => $params['invoiceID'],
609 'lc' => substr($config->lcMessages
, -2),
610 'charset' => function_exists('mb_internal_encoding') ?
mb_internal_encoding() : 'UTF-8',
611 'custom' => CRM_Utils_Array
::value('accountingCode', $params),
612 'bn' => 'CiviCRM_SP',
615 // add name and address if available, CRM-3130
617 'first_name' => 'first_name',
618 'last_name' => 'last_name',
619 'street_address' => 'address1',
620 'country' => 'country',
621 'preferred_language' => 'lc',
623 'state_province' => 'state',
624 'postal_code' => 'zip',
628 foreach (array_keys($params) as $p) {
629 // get the base name without the location type suffixed to it
630 $parts = explode('-', $p);
631 $name = count($parts) > 1 ?
$parts[0] : $p;
632 if (isset($otherVars[$name])) {
633 $value = $params[$p];
635 if ($name == 'state_province') {
636 $stateName = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($value);
639 if ($name == 'country') {
640 $countryName = CRM_Core_PseudoConstant
::countryIsoCode($value);
641 $value = $countryName;
643 // ensure value is not an array
645 if (!is_array($value)) {
646 $paypalParams[$otherVars[$name]] = $value;
652 // if recurring donations, add a few more items
653 if (!empty($params['is_recur'])) {
654 if ($params['contributionRecurID']) {
655 $notifyURL .= "&contributionRecurID={$params['contributionRecurID']}&contributionPageID={$params['contributionPageID']}";
656 $paypalParams['notify_url'] = $notifyURL;
659 CRM_Core_Error
::fatal(ts('Recurring contribution, but no database id'));
662 $paypalParams +
= array(
663 'cmd' => '_xclick-subscriptions',
664 'a3' => $params['amount'],
665 'p3' => $params['frequency_interval'],
666 't3' => ucfirst(substr($params['frequency_unit'], 0, 1)),
669 'srt' => CRM_Utils_Array
::value('installments', $params),
675 $paypalParams +
= array(
677 'amount' => $params['amount'],
681 // Allow further manipulation of the arguments via custom hooks ..
682 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $paypalParams);
685 foreach ($paypalParams as $key => $value) {
686 if ($value === NULL) {
690 $value = urlencode($value);
691 if ($key == 'return' ||
692 $key == 'cancel_return' ||
695 $value = str_replace('%2F', '/', $value);
697 $uri .= "&{$key}={$value}";
700 $uri = substr($uri, 1);
701 $url = $this->_paymentProcessor
['url_site'];
702 $sub = empty($params['is_recur']) ?
'cgi-bin/webscr' : 'subscriptions';
703 $paypalURL = "{$url}{$sub}?$uri";
705 CRM_Utils_System
::redirect($paypalURL);
709 * Hash_call: Function to perform the API call to PayPal using API signature
710 * @methodName is name of API method.
711 * @nvpStr is nvp string.
712 * returns an associtive array containing the response from the server.
714 public function invokeAPI($args, $url = NULL) {
717 if (empty($this->_paymentProcessor
['url_api'])) {
718 CRM_Core_Error
::fatal(ts('Please set the API URL. Please refer to the documentation for more details'));
721 $url = $this->_paymentProcessor
['url_api'] . 'nvp';
724 if (!function_exists('curl_init')) {
725 CRM_Core_Error
::fatal("curl functions NOT available.");
728 //setting the curl parameters.
730 curl_setopt($ch, CURLOPT_URL
, $url);
731 curl_setopt($ch, CURLOPT_VERBOSE
, 1);
733 //turning off the server and peer verification(TrustManager Concept).
734 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL'));
735 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL') ?
2 : 0);
737 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
738 curl_setopt($ch, CURLOPT_POST
, 1);
741 foreach ($args as $n => $v) {
742 $p[] = "$n=" . urlencode($v);
745 //NVPRequest for submitting to server
746 $nvpreq = implode('&', $p);
748 //setting the nvpreq as POST FIELD to curl
749 curl_setopt($ch, CURLOPT_POSTFIELDS
, $nvpreq);
751 //getting response from server
752 $response = curl_exec($ch);
754 //converting NVPResponse to an Associative Array
755 $result = self
::deformat($response);
757 if (curl_errno($ch)) {
758 $e = CRM_Core_Error
::singleton();
759 $e->push(curl_errno($ch),
769 if (strtolower($result['ack']) != 'success' &&
770 strtolower($result['ack']) != 'successwithwarning'
772 $e = CRM_Core_Error
::singleton();
773 $e->push($result['l_errorcode0'],
775 "{$result['l_shortmessage0']} {$result['l_longmessage0']}"
784 * This function will take NVPString and convert it to an Associative Array and it will decode the response.
785 * It is usefull to search for a particular key and displaying arrays.
786 * @nvpstr is NVPString.
787 * @nvpArray is Associative Array.
789 public static function deformat($str) {
792 while (strlen($str)) {
794 $keyPos = strpos($str, '=');
797 $valPos = strpos($str, '&') ?
strpos($str, '&') : strlen($str);
799 /*getting the Key and Value values and storing in a Associative Array*/
801 $key = substr($str, 0, $keyPos);
802 $val = substr($str, $keyPos +
1, $valPos - $keyPos - 1);
804 //decoding the respose
805 $result[strtolower(urldecode($key))] = urldecode($val);
806 $str = substr($str, $valPos +
1, strlen($str));