dolibarr  19.0.0-dev
paypal.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2011-2012 Regis Houssin <regis.houssin@inodbox.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
26 
27 
34 {
35  global $langs, $conf;
36 
37  $h = 0;
38  $head = array();
39 
40  $head[$h][0] = DOL_URL_ROOT."/paypal/admin/paypal.php";
41  $head[$h][1] = $langs->trans("PayPal");
42  $head[$h][2] = 'paypalaccount';
43  $h++;
44 
45  $object = new stdClass();
46 
47  // Show more tabs from modules
48  // Entries must be declared in modules descriptor with line
49  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
50  // $this->tabs = array('entity:-tabname); to remove a tab
51  complete_head_from_modules($conf, $langs, $object, $head, $h, 'paypaladmin');
52 
53  complete_head_from_modules($conf, $langs, $object, $head, $h, 'paypaladmin', 'remove');
54 
55  return $head;
56 }
57 
58 
70 function print_paypal_redirect($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $tag)
71 {
72  //declaring of global variables
73  global $conf, $langs;
74  global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
75  global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
76 
77  global $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum;
78  global $email, $desc;
79 
80  //'------------------------------------
81  //' Calls the SetExpressCheckout API call
82  //'
83  //'-------------------------------------------------
84 
85  if (empty($conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY)) {
86  $conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY = 'integral';
87  }
88 
89  $solutionType = 'Sole';
90  $landingPage = 'Billing';
91  // For payment with Paypal only
92  if ($conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY == 'paypalonly') {
93  $solutionType = 'Mark';
94  $landingPage = 'Login';
95  }
96  // For payment with Credit card or Paypal
97  if ($conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY == 'integral') {
98  $solutionType = 'Sole';
99  $landingPage = 'Billing';
100  }
101  // For payment with Credit card
102  if ($conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY == 'cconly') {
103  $solutionType = 'Sole';
104  $landingPage = 'Billing';
105  }
106 
107  dol_syslog("print_paypal_redirect expresscheckout redirect with callSetExpressCheckout $paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $tag, $solutionType, $landingPage, $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum");
108  $resArray = callSetExpressCheckout(
109  $paymentAmount,
110  $currencyCodeType,
111  $paymentType,
112  $returnURL,
113  $cancelURL,
114  $tag,
115  $solutionType,
116  $landingPage,
117  $shipToName,
118  $shipToStreet,
119  $shipToCity,
120  $shipToState,
121  $shipToCountryCode,
122  $shipToZip,
123  $shipToStreet2,
124  $phoneNum,
125  $email,
126  $desc
127  );
128 
129  dol_syslog("print_paypal_redirect resArray=".var_export($resArray, true), LOG_DEBUG);
130 
131  $ack = strtoupper($resArray["ACK"]);
132  if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
133  $token = $resArray["TOKEN"];
134 
135  // Redirect to paypal.com here
136  $payPalURL = $API_Url.$token;
137  dol_syslog("Redirect to ".$payPalURL, LOG_INFO);
138  header("Location: ".$payPalURL);
139  exit;
140  } else {
141  //Display a user friendly Error on the page using any of the following error information returned by PayPal
142  $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
143  $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
144  $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
145  $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
146 
147  if ($ErrorCode == 10729) {
148  $mesg .= "PayPal can't accept payments for this thirdparty. An address is defined but is not complete (missing State).<br>Ask system administrator to fix address or to setup Paypal module to accept payments even on not complete addresses (remove option PAYPAL_REQUIRE_VALID_SHIPPING_ADDRESS).<br>\n";
149  } else {
150  $mesg = $langs->trans('SetExpressCheckoutAPICallFailed')."<br>\n";
151  $mesg .= $langs->trans('DetailedErrorMessage').": ".$ErrorLongMsg."<br>\n";
152  $mesg .= $langs->trans('ShortErrorMessage').": ".$ErrorShortMsg."<br>\n";
153  $mesg .= $langs->trans('ErrorCode').": ".$ErrorCode."<br>\n";
154  $mesg .= $langs->trans('ErrorSeverityCode').": ".$ErrorSeverityCode."<br>\n";
155  }
156 
157  return $mesg;
158  }
159 }
160 
202 function callSetExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $tag, $solutionType, $landingPage, $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum, $email = '', $desc = '')
203 {
204  //------------------------------------------------------------------------------------------------------------------------------------
205  // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
206 
207  //declaring of global variables
208  global $conf, $langs, $mysoc;
209  global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
210  global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
211 
212  $nvpstr = '';
213  //$nvpstr = $nvpstr . "&VERSION=".$API_version; // Already added by hash_call
214  $nvpstr = $nvpstr."&RETURNURL=".urlencode($returnURL);
215  $nvpstr = $nvpstr."&CANCELURL=".urlencode($cancelURL);
216  if (!empty($conf->global->PAYPAL_ALLOW_NOTES)) {
217  $nvpstr = $nvpstr."&ALLOWNOTE=0";
218  }
219  if (empty($conf->global->PAYPAL_REQUIRE_VALID_SHIPPING_ADDRESS)) {
220  $nvpstr = $nvpstr."&NOSHIPPING=1"; // An empty or not complete shipping address will be accepted
221  } else {
222  $nvpstr = $nvpstr."&NOSHIPPING=0"; // A valid shipping address is required (full required fields mandatory)
223  }
224  $nvpstr = $nvpstr."&SOLUTIONTYPE=".urlencode($solutionType);
225  $nvpstr = $nvpstr."&LANDINGPAGE=".urlencode($landingPage);
226  if (!empty($conf->global->PAYPAL_CUSTOMER_SERVICE_NUMBER)) {
227  $nvpstr = $nvpstr."&CUSTOMERSERVICENUMBER=".urlencode($conf->global->PAYPAL_CUSTOMER_SERVICE_NUMBER); // Hotline phone number
228  }
229 
230  $paypalprefix = 'PAYMENTREQUEST_0_';
231  //$paypalprefix = '';
232  if (!empty($paypalprefix) && $paymentType == 'Sole') {
233  $paymentType = 'Sale';
234  }
235 
236  $nvpstr = $nvpstr."&AMT=".urlencode($paymentAmount); // Total for all elements
237 
238  $nvpstr = $nvpstr."&".$paypalprefix."INVNUM=".urlencode($tag);
239  $nvpstr = $nvpstr."&".$paypalprefix."AMT=".urlencode($paymentAmount); // AMT deprecated by paypal -> PAYMENTREQUEST_n_AMT
240  $nvpstr = $nvpstr."&".$paypalprefix."ITEMAMT=".urlencode($paymentAmount); // AMT deprecated by paypal -> PAYMENTREQUEST_n_AMT
241  $nvpstr = $nvpstr."&".$paypalprefix."PAYMENTACTION=".urlencode($paymentType); // PAYMENTACTION deprecated by paypal -> PAYMENTREQUEST_n_PAYMENTACTION
242  $nvpstr = $nvpstr."&".$paypalprefix."CURRENCYCODE=".urlencode($currencyCodeType); // CURRENCYCODE deprecated by paypal -> PAYMENTREQUEST_n_CURRENCYCODE
243 
244  $nvpstr = $nvpstr."&".$paypalprefix."L_PAYMENTREQUEST_0_QTY0=1";
245  $nvpstr = $nvpstr."&".$paypalprefix."L_PAYMENTREQUEST_0_AMT0=".urlencode($paymentAmount);
246  $nvpstr = $nvpstr."&".$paypalprefix."L_PAYMENTREQUEST_0_NAME0=".urlencode($desc);
247  $nvpstr = $nvpstr."&".$paypalprefix."L_PAYMENTREQUEST_0_NUMBER0=0";
248 
249  $nvpstr = $nvpstr."&".$paypalprefix."SHIPTONAME=".urlencode($shipToName); // SHIPTONAME deprecated by paypal -> PAYMENTREQUEST_n_SHIPTONAME
250  $nvpstr = $nvpstr."&".$paypalprefix."SHIPTOSTREET=".urlencode($shipToStreet); //
251  $nvpstr = $nvpstr."&".$paypalprefix."SHIPTOSTREET2=".urlencode($shipToStreet2);
252  $nvpstr = $nvpstr."&".$paypalprefix."SHIPTOCITY=".urlencode($shipToCity);
253  $nvpstr = $nvpstr."&".$paypalprefix."SHIPTOSTATE=".urlencode($shipToState);
254  $nvpstr = $nvpstr."&".$paypalprefix."SHIPTOCOUNTRYCODE=".urlencode($shipToCountryCode);
255  $nvpstr = $nvpstr."&".$paypalprefix."SHIPTOZIP=".urlencode($shipToZip);
256  $nvpstr = $nvpstr."&".$paypalprefix."PHONENUM=".urlencode($phoneNum);
257  if (!empty($email)) {
258  $nvpstr = $nvpstr."&".$paypalprefix."EMAIL=".urlencode($email); // EMAIL deprecated by paypal -> PAYMENTREQUEST_n_EMAIL
259  }
260  if (!empty($desc)) {
261  $nvpstr = $nvpstr."&".$paypalprefix."DESC=".urlencode($desc); // DESC deprecated by paypal -> PAYMENTREQUEST_n_DESC
262  }
263 
264  if (!empty($conf->global->PAYPAL_LOGOIMG) && $mysoc->logo) {
265  global $dolibarr_main_url_root;
266 
267  // Define $urlwithroot
268  $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
269  $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
270  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
271 
272  $urllogo = $urlwithroot."/viewimage.php?modulepart=mycompany&file=".urlencode('logos/'.$mysoc->logo);
273  $nvpstr = $nvpstr."&LOGOIMG=".urlencode($urllogo);
274  }
275  if (!empty($conf->global->PAYPAL_BRANDNAME)) {
276  $nvpstr = $nvpstr."&BRANDNAME=".urlencode($conf->global->PAYPAL_BRANDNAME); // BRANDNAME
277  }
278  if (!empty($conf->global->PAYPAL_NOTETOBUYER)) {
279  $nvpstr = $nvpstr."&NOTETOBUYER=".urlencode($conf->global->PAYPAL_NOTETOBUYER); // PAYPAL_NOTETOBUYER
280  }
281 
282  $_SESSION["FinalPaymentAmt"] = $paymentAmount;
283  $_SESSION["currencyCodeType"] = $currencyCodeType;
284  $_SESSION["PaymentType"] = $paymentType; // 'Mark', 'Sole'
285  $_SESSION['ipaddress'] = getUserRemoteIP(); // Payer ip
286 
287  //'---------------------------------------------------------------------------------------------------------------
288  //' Make the API call to PayPal
289  //' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
290  //' If an error occured, show the resulting errors
291  //'---------------------------------------------------------------------------------------------------------------
292  $resArray = hash_call("SetExpressCheckout", $nvpstr);
293  $ack = strtoupper($resArray["ACK"]);
294  if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
295  $token = urldecode($resArray["TOKEN"]);
296  $_SESSION['TOKEN'] = $token;
297  }
298 
299  return $resArray;
300 }
301 
308 function getDetails($token)
309 {
310  //'--------------------------------------------------------------
311  //' At this point, the buyer has completed authorizing the payment
312  //' at PayPal. The function will call PayPal to obtain the details
313  //' of the authorization, incuding any shipping information of the
314  //' buyer. Remember, the authorization is not a completed transaction
315  //' at this state - the buyer still needs an additional step to finalize
316  //' the transaction
317  //'--------------------------------------------------------------
318 
319  //declaring of global variables
320  global $conf, $langs;
321  global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
322  global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
323 
324  //'---------------------------------------------------------------------------
325  //' Build a second API request to PayPal, using the token as the
326  //' ID to get the details on the payment authorization
327  //'---------------------------------------------------------------------------
328  $nvpstr = "&TOKEN=".$token;
329 
330  //'---------------------------------------------------------------------------
331  //' Make the API call and store the results in an array.
332  //' If the call was a success, show the authorization details, and provide
333  //' an action to complete the payment.
334  //' If failed, show the error
335  //'---------------------------------------------------------------------------
336  $resArray = hash_call("GetExpressCheckoutDetails", $nvpstr);
337  $ack = strtoupper($resArray["ACK"]);
338  if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") {
339  $_SESSION['payer_id'] = $resArray['PAYERID'];
340  }
341  return $resArray;
342 }
343 
344 
357 function confirmPayment($token, $paymentType, $currencyCodeType, $payerID, $ipaddress, $FinalPaymentAmt, $tag)
358 {
359  /* Gather the information to make the final call to
360  finalize the PayPal payment. The variable nvpstr
361  holds the name value pairs
362  */
363 
364  //declaring of global variables
365  global $conf, $langs;
366  global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
367  global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
368 
369  $nvpstr = '';
370  $nvpstr .= '&TOKEN='.urlencode($token);
371  $nvpstr .= '&PAYERID='.urlencode($payerID);
372  $nvpstr .= '&PAYMENTACTION='.urlencode($paymentType);
373  $nvpstr .= '&AMT='.urlencode($FinalPaymentAmt);
374  $nvpstr .= '&CURRENCYCODE='.urlencode($currencyCodeType);
375  $nvpstr .= '&IPADDRESS='.urlencode($ipaddress);
376  $nvpstr .= '&INVNUM='.urlencode($tag);
377 
378  /* Make the call to PayPal to finalize payment
379  If an error occured, show the resulting errors
380  */
381  $resArray = hash_call("DoExpressCheckoutPayment", $nvpstr);
382 
383  /* Display the API response back to the browser.
384  If the response from PayPal was a success, display the response parameters'
385  If the response was an error, display the errors received using APIError.php.
386  */
387  $ack = strtoupper($resArray["ACK"]);
388 
389  return $resArray;
390 }
391 
411 /*
412 function DirectPayment($paymentType, $paymentAmount, $creditCardType, $creditCardNumber, $expDate, $cvv2, $firstName, $lastName, $street, $city, $state, $zip, $countryCode, $currencyCode, $tag)
413 {
414  //declaring of global variables
415  global $conf, $langs;
416  global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
417  global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
418 
419  //Construct the parameter string that describes DoDirectPayment
420  $nvpstr = '';
421  $nvpstr = $nvpstr . "&AMT=" . urlencode($paymentAmount); // deprecated by paypal
422  $nvpstr = $nvpstr . "&CURRENCYCODE=" . urlencode($currencyCode);
423  $nvpstr = $nvpstr . "&PAYMENTACTION=" . urlencode($paymentType); // deprecated by paypal
424  $nvpstr = $nvpstr . "&CREDITCARDTYPE=" . urlencode($creditCardType);
425  $nvpstr = $nvpstr . "&ACCT=" . urlencode($creditCardNumber);
426  $nvpstr = $nvpstr . "&EXPDATE=" . urlencode($expDate);
427  $nvpstr = $nvpstr . "&CVV2=" . urlencode($cvv2);
428  $nvpstr = $nvpstr . "&FIRSTNAME=" . urlencode($firstName);
429  $nvpstr = $nvpstr . "&LASTNAME=" . urlencode($lastName);
430  $nvpstr = $nvpstr . "&STREET=" . urlencode($street);
431  $nvpstr = $nvpstr . "&CITY=" . urlencode($city);
432  $nvpstr = $nvpstr . "&STATE=" . urlencode($state);
433  $nvpstr = $nvpstr . "&COUNTRYCODE=" . urlencode($countryCode);
434  $nvpstr = $nvpstr . "&IPADDRESS=" . getUserRemotIP();
435  $nvpstr = $nvpstr . "&INVNUM=" . urlencode($tag);
436 
437  $resArray=hash_call("DoDirectPayment", $nvpstr);
438 
439  return $resArray;
440 }
441 */
442 
443 
451 function hash_call($methodName, $nvpStr)
452 {
453  //declaring of global variables
454  global $conf, $langs;
455  global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT, $PROXY_USER, $PROXY_PASS;
456  global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
457 
458  // TODO problem with triggers
459  $API_version = "98.0";
460  if (!empty($conf->global->PAYPAL_API_SANDBOX) || GETPOST('forcesandbox', 'alpha')) { // We can force sand box with param 'forcesandbox'
461  $API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
462  $API_Url = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
463  } else {
464  $API_Endpoint = "https://api-3t.paypal.com/nvp";
465  $API_Url = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
466  }
467 
468  // Clean parameters
469  $PAYPAL_API_USER = "";
470  if (!empty($conf->global->PAYPAL_API_USER)) {
471  $PAYPAL_API_USER = $conf->global->PAYPAL_API_USER;
472  }
473  $PAYPAL_API_PASSWORD = "";
474  if (!empty($conf->global->PAYPAL_API_PASSWORD)) {
475  $PAYPAL_API_PASSWORD = $conf->global->PAYPAL_API_PASSWORD;
476  }
477  $PAYPAL_API_SIGNATURE = "";
478  if (!empty($conf->global->PAYPAL_API_SIGNATURE)) {
479  $PAYPAL_API_SIGNATURE = $conf->global->PAYPAL_API_SIGNATURE;
480  }
481  $PAYPAL_API_SANDBOX = "";
482  if (!empty($conf->global->PAYPAL_API_SANDBOX)) {
483  $PAYPAL_API_SANDBOX = $conf->global->PAYPAL_API_SANDBOX;
484  }
485  // TODO END problem with triggers
486 
487  dol_syslog("Paypal API endpoint ".$API_Endpoint);
488 
489  //setting the curl parameters.
490  $ch = curl_init();
491 
492  /*print $API_Endpoint."-".$API_version."-".$PAYPAL_API_USER."-".$PAYPAL_API_PASSWORD."-".$PAYPAL_API_SIGNATURE."<br>";
493  print $USE_PROXY."-".$gv_ApiErrorURL."<br>";
494  print $nvpStr;
495  exit;*/
496  curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
497  curl_setopt($ch, CURLOPT_VERBOSE, 1);
498  // TLSv1 by default or change to TLSv1.2 in module configuration
499  curl_setopt($ch, CURLOPT_SSLVERSION, (empty($conf->global->PAYPAL_SSLVERSION) ? 1 : $conf->global->PAYPAL_SSLVERSION));
500 
501  $ssl_verifypeer = -1;
502 
503  // Turning on or off the ssl target certificate
504  if ($ssl_verifypeer < 0) {
505  global $dolibarr_main_prod;
506  $ssl_verifypeer = ($dolibarr_main_prod ? true : false);
507  }
508  if (!empty($conf->global->MAIN_CURL_DISABLE_VERIFYPEER)) {
509  $ssl_verifypeer = 0;
510  }
511 
512  //turning off the server and peer verification(TrustManager Concept).
513  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, ($ssl_verifypeer ? true : false));
514  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, ($ssl_verifypeer ? true : false));
515 
516  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, empty($conf->global->MAIN_USE_CONNECT_TIMEOUT) ? 5 : $conf->global->MAIN_USE_CONNECT_TIMEOUT);
517  curl_setopt($ch, CURLOPT_TIMEOUT, empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT) ? 30 : $conf->global->MAIN_USE_RESPONSE_TIMEOUT);
518 
519  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
520  curl_setopt($ch, CURLOPT_POST, 1);
521 
522  //if USE_PROXY constant set to true in Constants.php, then only proxy will be enabled.
523  if ($USE_PROXY) {
524  dol_syslog("Paypal API hash_call set proxy to ".$PROXY_HOST.":".$PROXY_PORT." - ".$PROXY_USER.":".$PROXY_PASS);
525  //curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10
526  curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST.":".$PROXY_PORT);
527  if ($PROXY_USER) {
528  curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER.":".$PROXY_PASS);
529  }
530  }
531 
532  //NVPRequest for submitting to server
533  $nvpreq = "METHOD=".urlencode($methodName)."&VERSION=".urlencode($API_version)."&PWD=".urlencode($PAYPAL_API_PASSWORD)."&USER=".urlencode($PAYPAL_API_USER)."&SIGNATURE=".urlencode($PAYPAL_API_SIGNATURE).$nvpStr;
534  $nvpreq .= "&LOCALECODE=".strtoupper($langs->getDefaultLang(1));
535  //$nvpreq.="&BRANDNAME=".urlencode(); // Override merchant name
536  //$nvpreq.="&NOTIFYURL=".urlencode(); // For Instant Payment Notification url
537 
538 
539  dol_syslog("Paypal API hash_call nvpreq=".$nvpreq);
540 
541  //setting the nvpreq as POST FIELD to curl
542  curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
543 
544  //getting response from server
545  $response = curl_exec($ch);
546 
547  $nvpReqArray = deformatNVP($nvpreq);
548  $_SESSION['nvpReqArray'] = $nvpReqArray;
549 
550  //convrting NVPResponse to an Associative Array
551  dol_syslog("Paypal API hash_call Response nvpresp=".$response);
552  $nvpResArray = deformatNVP($response);
553 
554  if (curl_errno($ch)) {
555  // moving to display page to display curl errors
556  $_SESSION['curl_error_no'] = curl_errno($ch);
557  $_SESSION['curl_error_msg'] = curl_error($ch);
558 
559  //Execute the Error handling module to display errors.
560  } else {
561  //closing the curl
562  curl_close($ch);
563  }
564 
565  return $nvpResArray;
566 }
567 
568 
576 function deformatNVP($nvpstr)
577 {
578  $intial = 0;
579  $nvpArray = array();
580 
581  while (strlen($nvpstr)) {
582  //postion of Key
583  $keypos = strpos($nvpstr, '=');
584  //position of value
585  $valuepos = strpos($nvpstr, '&') ? strpos($nvpstr, '&') : strlen($nvpstr);
586 
587  /*getting the Key and Value values and storing in a Associative Array*/
588  $keyval = substr($nvpstr, $intial, $keypos);
589  $valval = substr($nvpstr, $keypos + 1, $valuepos - $keypos - 1);
590  //decoding the respose
591  $nvpArray[urldecode($keyval)] = urldecode($valval);
592  $nvpstr = substr($nvpstr, $valuepos + 1, strlen($nvpstr));
593  }
594  return $nvpArray;
595 }
596 
602 function getApiError()
603 {
604  $errors = array();
605 
606  $resArray = $_SESSION['reshash'];
607 
608  if (isset($_SESSION['curl_error_no'])) {
609  $errors[] = $_SESSION['curl_error_no'].'-'.$_SESSION['curl_error_msg'];
610  }
611 
612  foreach ($resArray as $key => $value) {
613  $errors[] = $key.'-'.$value;
614  }
615 
616  return $errors;
617 }
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
getUserRemoteIP()
Return the IP of remote user.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getApiError()
Get API errors.
Definition: paypal.lib.php:602
confirmPayment($token, $paymentType, $currencyCodeType, $payerID, $ipaddress, $FinalPaymentAmt, $tag)
Validate payment.
Definition: paypal.lib.php:357
deformatNVP($nvpstr)
This function will take NVPString and convert it to an Associative Array and it will decode the respo...
Definition: paypal.lib.php:576
callSetExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $tag, $solutionType, $landingPage, $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum, $email='', $desc='')
Definition: paypal.lib.php:202
hash_call($methodName, $nvpStr)
This function makes a DoDirectPayment API call.
Definition: paypal.lib.php:451
getDetails($token)
Prepares the parameters for the GetExpressCheckoutDetails API Call.
Definition: paypal.lib.php:308
print_paypal_redirect($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $tag)
Send redirect to paypal to browser.
Definition: paypal.lib.php:70
paypaladmin_prepare_head()
Define head array for tabs of paypal tools setup pages.
Definition: paypal.lib.php:33