PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/paypal/lib/paypal.lib.php

https://github.com/asterix14/dolibarr
PHP | 722 lines | 408 code | 83 blank | 231 comment | 91 complexity | 570e38e8efec38877d7fee6935236613 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2008-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2011 Regis Houssin <regis@dolibarr.fr>
  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 2 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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/paypal/lib/paypal.lib.php
  20. * \ingroup paypal
  21. * \brief Library for common paypal functions
  22. */
  23. /**
  24. * Show header
  25. *
  26. * @param string $title
  27. * @param string $head
  28. * @return void
  29. */
  30. function llxHeaderPaypal($title, $head = "")
  31. {
  32. global $user, $conf, $langs;
  33. header("Content-type: text/html; charset=".$conf->file->character_set_client);
  34. print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
  35. //print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd>';
  36. print "\n";
  37. print "<html>\n";
  38. print "<head>\n";
  39. print '<meta name="robots" content="noindex,nofollow">'."\n";
  40. print '<meta name="keywords" content="dolibarr,payment,online">'."\n";
  41. print '<meta name="description" content="Welcome on Dolibarr online payment form">'."\n";
  42. print "<title>".$title."</title>\n";
  43. if ($head) print $head."\n";
  44. if ($conf->global->PAYPAL_CSS_URL) print '<link rel="stylesheet" type="text/css" href="'.$conf->global->PAYPAL_CSS_URL.'?lang='.$langs->defaultlang.'">'."\n";
  45. else
  46. {
  47. print '<link rel="stylesheet" type="text/css" href="'.DOL_URL_ROOT.$conf->css.'?lang='.$langs->defaultlang.'">'."\n";
  48. print '<style type="text/css">';
  49. print '.CTableRow1 { margin: 1px; padding: 3px; font: 12px verdana,arial; background: #e6E6eE; color: #000000; -moz-border-radius-topleft:6px; -moz-border-radius-topright:6px; -moz-border-radius-bottomleft:6px; -moz-border-radius-bottomright:6px;}';
  50. print '.CTableRow2 { margin: 1px; padding: 3px; font: 12px verdana,arial; background: #FFFFFF; color: #000000; -moz-border-radius-topleft:6px; -moz-border-radius-topright:6px; -moz-border-radius-bottomleft:6px; -moz-border-radius-bottomright:6px;}';
  51. print '</style>';
  52. }
  53. print "</head>\n";
  54. print '<body style="margin: 20px;">'."\n";
  55. }
  56. /**
  57. * Show footer
  58. *
  59. * @return void
  60. */
  61. function llxFooterPaypal()
  62. {
  63. print "</body>\n";
  64. print "</html>\n";
  65. }
  66. /**
  67. * Show footer of company in HTML pages
  68. *
  69. * @param Societe $fromcompany Third party
  70. * @param Translate $langs Output language
  71. * @return void
  72. */
  73. function html_print_paypal_footer($fromcompany,$langs)
  74. {
  75. global $conf;
  76. // Juridical status
  77. $line1="";
  78. if ($fromcompany->forme_juridique_code)
  79. {
  80. $line1.=($line1?" - ":"").$langs->convToOutputCharset(getFormeJuridiqueLabel($fromcompany->forme_juridique_code));
  81. }
  82. // Capital
  83. if ($fromcompany->capital)
  84. {
  85. $line1.=($line1?" - ":"").$langs->transnoentities("CapitalOf",$fromcompany->capital)." ".$langs->transnoentities("Currency".$conf->monnaie);
  86. }
  87. // Prof Id 1
  88. if ($fromcompany->idprof1 && ($fromcompany->pays_code != 'FR' || ! $fromcompany->idprof2))
  89. {
  90. $field=$langs->transcountrynoentities("ProfId1",$fromcompany->pays_code);
  91. if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
  92. $line1.=($line1?" - ":"").$field.": ".$langs->convToOutputCharset($fromcompany->idprof1);
  93. }
  94. // Prof Id 2
  95. if ($fromcompany->idprof2)
  96. {
  97. $field=$langs->transcountrynoentities("ProfId2",$fromcompany->pays_code);
  98. if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
  99. $line1.=($line1?" - ":"").$field.": ".$langs->convToOutputCharset($fromcompany->idprof2);
  100. }
  101. // Second line of company infos
  102. $line2="";
  103. // Prof Id 3
  104. if ($fromcompany->idprof3)
  105. {
  106. $field=$langs->transcountrynoentities("ProfId3",$fromcompany->pays_code);
  107. if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
  108. $line2.=($line2?" - ":"").$field.": ".$langs->convToOutputCharset($fromcompany->idprof3);
  109. }
  110. // Prof Id 4
  111. if ($fromcompany->idprof4)
  112. {
  113. $field=$langs->transcountrynoentities("ProfId4",$fromcompany->pays_code);
  114. if (preg_match('/\((.*)\)/i',$field,$reg)) $field=$reg[1];
  115. $line2.=($line2?" - ":"").$field.": ".$langs->convToOutputCharset($fromcompany->idprof4);
  116. }
  117. // IntraCommunautary VAT
  118. if ($fromcompany->tva_intra != '')
  119. {
  120. $line2.=($line2?" - ":"").$langs->transnoentities("VATIntraShort").": ".$langs->convToOutputCharset($fromcompany->tva_intra);
  121. }
  122. print '<br><br><hr>'."\n";
  123. print '<center><font style="font-size: 10px;">'."\n";
  124. print $fromcompany->nom.'<br>';
  125. print $line1.'<br>';
  126. print $line2;
  127. print '</font></center>'."\n";
  128. }
  129. /**
  130. * Define head array for tabs of paypal tools setup pages
  131. *
  132. * @return Array of head
  133. */
  134. function paypaladmin_prepare_head()
  135. {
  136. global $langs, $conf;
  137. $h = 0;
  138. $head = array();
  139. $head[$h][0] = DOL_URL_ROOT."/paypal/admin/paypal.php";
  140. $head[$h][1] = $langs->trans("Account");
  141. $head[$h][2] = 'paypalaccount';
  142. $h++;
  143. // Show more tabs from modules
  144. // Entries must be declared in modules descriptor with line
  145. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  146. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  147. complete_head_from_modules($conf,$langs,$object,$head,$h,'paypaladmin');
  148. return $head;
  149. }
  150. /**
  151. * Return string with full Url
  152. *
  153. * @param int $mode 0=True url, 1=Url formated with colors
  154. * @param string $type Type of URL ('free', 'order', 'invoice', 'contractline', 'membersubscription' ...)
  155. * @param string $ref Ref of object
  156. * @param int $amount Amount
  157. * @param string $freetag Free tag
  158. * @return string Url string
  159. */
  160. function getPaypalPaymentUrl($mode,$type,$ref='',$amount='9.99',$freetag='your_free_tag')
  161. {
  162. global $conf;
  163. if ($type == 'free')
  164. {
  165. $out=DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?amount='.($mode?'<font color="#666666">':'').$amount.($mode?'</font>':'').'&tag='.($mode?'<font color="#666666">':'').$freetag.($mode?'</font>':'');
  166. if (! empty($conf->global->PAYPAL_SECURITY_TOKEN)) $out.='&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN;
  167. }
  168. if ($type == 'order')
  169. {
  170. $out=DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?source=order&ref='.($mode?'<font color="#666666">':'');
  171. if ($mode == 1) $out.='order_ref';
  172. if ($mode == 0) $out.=urlencode($ref);
  173. $out.=($mode?'</font>':'');
  174. if (! empty($conf->global->PAYPAL_SECURITY_TOKEN))
  175. {
  176. if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN;
  177. else
  178. {
  179. $out.='&securekey='.($mode?'<font color="#666666">':'');
  180. if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + order + order_ref)";
  181. if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'order' . $ref, 2);
  182. $out.=($mode?'</font>':'');
  183. }
  184. }
  185. }
  186. if ($type == 'invoice')
  187. {
  188. $out=DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?source=invoice&ref='.($mode?'<font color="#666666">':'');
  189. if ($mode == 1) $out.='invoice_ref';
  190. if ($mode == 0) $out.=urlencode($ref);
  191. $out.=($mode?'</font>':'');
  192. if (! empty($conf->global->PAYPAL_SECURITY_TOKEN))
  193. {
  194. if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN;
  195. else
  196. {
  197. $out.='&securekey='.($mode?'<font color="#666666">':'');
  198. if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + invoice + invoice_ref)";
  199. if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'invoice' . $ref, 2);
  200. $out.=($mode?'</font>':'');
  201. }
  202. }
  203. }
  204. if ($type == 'contractline')
  205. {
  206. $out=DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?source=contractline&ref='.($mode?'<font color="#666666">':'');
  207. if ($mode == 1) $out.='contractline_ref';
  208. if ($mode == 0) $out.=urlencode($ref);
  209. $out.=($mode?'</font>':'');
  210. if (! empty($conf->global->PAYPAL_SECURITY_TOKEN))
  211. {
  212. if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN;
  213. else
  214. {
  215. $out.='&securekey='.($mode?'<font color="#666666">':'');
  216. if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + contactline + contractline_ref)";
  217. if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'contractline' . $ref, 2);
  218. $out.=($mode?'</font>':'');
  219. }
  220. }
  221. }
  222. if ($type == 'membersubscription')
  223. {
  224. $out=DOL_MAIN_URL_ROOT.'/public/paypal/newpayment.php?source=membersubscription&ref='.($mode?'<font color="#666666">':'');
  225. if ($mode == 1) $out.='member_ref';
  226. if ($mode == 0) $out.=urlencode($ref);
  227. $out.=($mode?'</font>':'');
  228. if (! empty($conf->global->PAYPAL_SECURITY_TOKEN))
  229. {
  230. if (empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) $out.='&securekey='.$conf->global->PAYPAL_SECURITY_TOKEN;
  231. else
  232. {
  233. $out.='&securekey='.($mode?'<font color="#666666">':'');
  234. if ($mode == 1) $out.="hash('".$conf->global->PAYPAL_SECURITY_TOKEN."' + membersubscription + member_ref)";
  235. if ($mode == 0) $out.= dol_hash($conf->global->PAYPAL_SECURITY_TOKEN . 'membersubscription' . $ref, 2);
  236. $out.=($mode?'</font>':'');
  237. }
  238. }
  239. }
  240. return $out;
  241. }
  242. /**
  243. * Send redirect to paypal to browser
  244. *
  245. * @param float $paymentAmount Amount
  246. * @param string $currencyCodeType Currency code
  247. * @param string $paymentType Payment type
  248. * @param string $returnURL Url to use if payment is OK
  249. * @param string $cancelURL Url to use if payment is KO
  250. * @param string $tag Tag
  251. * @return void
  252. */
  253. function print_paypal_redirect($paymentAmount,$currencyCodeType,$paymentType,$returnURL,$cancelURL,$tag)
  254. {
  255. //declaring of global variables
  256. global $conf, $langs;
  257. global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
  258. global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
  259. global $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum;
  260. //'------------------------------------
  261. //' Calls the SetExpressCheckout API call
  262. //'
  263. //'-------------------------------------------------
  264. if (empty($conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY)) $conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY='integral';
  265. $solutionType='Sole';
  266. $landingPage='Billing';
  267. // For payment with Paypal only
  268. if ($conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY == 'paypalonly')
  269. {
  270. $solutionType='Mark';
  271. $landingPage='Login';
  272. }
  273. // For payment with Credit card or Paypal
  274. if ($conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY == 'integral')
  275. {
  276. $solutionType='Sole';
  277. $landingPage='Billing';
  278. }
  279. // For payment with Credit card
  280. if ($conf->global->PAYPAL_API_INTEGRAL_OR_PAYPALONLY == 'cconly')
  281. {
  282. $solutionType='Sole';
  283. $landingPage='Billing';
  284. }
  285. dol_syslog("expresscheckout redirect with CallSetExpressCheckout $paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $tag, $landingPage, $solutionType, $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum");
  286. $resArray = CallSetExpressCheckout(
  287. $paymentAmount,
  288. $currencyCodeType,
  289. $paymentType,
  290. $returnURL,
  291. $cancelURL,
  292. $tag,
  293. $solutionType,
  294. $landingPage,
  295. $shipToName,
  296. $shipToStreet,
  297. $shipToCity,
  298. $shipToState,
  299. $shipToCountryCode,
  300. $shipToZip,
  301. $shipToStreet2,
  302. $phoneNum
  303. );
  304. /* For direct payment with credit card
  305. {
  306. //$resArray = DirectPayment (...);
  307. }
  308. */
  309. $ack = strtoupper($resArray["ACK"]);
  310. if($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING")
  311. {
  312. $token=$resArray["TOKEN"];
  313. // Redirect to paypal.com here
  314. $payPalURL = $API_Url . $token;
  315. header("Location: ".$payPalURL);
  316. exit;
  317. }
  318. else
  319. {
  320. //Display a user friendly Error on the page using any of the following error information returned by PayPal
  321. $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
  322. $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
  323. $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
  324. $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
  325. echo "SetExpressCheckout API call failed. <br>\n";
  326. echo "Detailed Error Message: " . $ErrorLongMsg." <br>\n";
  327. echo "Short Error Message: " . $ErrorShortMsg." <br>\n";
  328. echo "Error Code: " . $ErrorCode." <br>\n";
  329. echo "Error Severity Code: " . $ErrorSeverityCode." <br>\n";
  330. }
  331. }
  332. /*
  333. '-------------------------------------------------------------------------------------------------------------------------------------------
  334. ' Purpose: Prepares the parameters for the SetExpressCheckout API Call.
  335. ' Inputs:
  336. ' paymentAmount: Total value of the shopping cart
  337. ' currencyCodeType: Currency code value the PayPal API
  338. ' paymentType: paymentType has to be one of the following values: Sale or Order or Authorization
  339. ' returnURL: the page where buyers return to after they are done with the payment review on PayPal
  340. ' cancelURL: the page where buyers return to when they cancel the payment review on PayPal
  341. ' shipToName: the Ship to name entered on the merchant's site
  342. ' shipToStreet: the Ship to Street entered on the merchant's site
  343. ' shipToCity: the Ship to City entered on the merchant's site
  344. ' shipToState: the Ship to State entered on the merchant's site
  345. ' shipToCountryCode: the Code for Ship to Country entered on the merchant's site
  346. ' shipToZip: the Ship to ZipCode entered on the merchant's site
  347. ' shipToStreet2: the Ship to Street2 entered on the merchant's site
  348. ' phoneNum: the phoneNum entered on the merchant's site
  349. '--------------------------------------------------------------------------------------------------------------------------------------------
  350. */
  351. function CallSetExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL, $tag, $solutionType, $landingPage, $shipToName, $shipToStreet, $shipToCity, $shipToState, $shipToCountryCode, $shipToZip, $shipToStreet2, $phoneNum)
  352. {
  353. //------------------------------------------------------------------------------------------------------------------------------------
  354. // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
  355. //declaring of global variables
  356. global $conf, $langs;
  357. global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
  358. global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
  359. $nvpstr="&AMT=". urlencode($paymentAmount);
  360. $nvpstr = $nvpstr . "&PAYMENTACTION=" . urlencode($paymentType);
  361. $nvpstr = $nvpstr . "&RETURNURL=" . urlencode($returnURL);
  362. $nvpstr = $nvpstr . "&CANCELURL=" . urlencode($cancelURL);
  363. $nvpstr = $nvpstr . "&CURRENCYCODE=" . urlencode($currencyCodeType);
  364. $nvpstr = $nvpstr . "&ADDROVERRIDE=1";
  365. //$nvpstr = $nvpstr . "&ALLOWNOTE=0";
  366. $nvpstr = $nvpstr . "&SHIPTONAME=" . urlencode($shipToName);
  367. $nvpstr = $nvpstr . "&SHIPTOSTREET=" . urlencode($shipToStreet);
  368. $nvpstr = $nvpstr . "&SHIPTOSTREET2=" . urlencode($shipToStreet2);
  369. $nvpstr = $nvpstr . "&SHIPTOCITY=" . urlencode($shipToCity);
  370. $nvpstr = $nvpstr . "&SHIPTOSTATE=" . urlencode($shipToState);
  371. $nvpstr = $nvpstr . "&SHIPTOCOUNTRYCODE=" . urlencode($shipToCountryCode);
  372. $nvpstr = $nvpstr . "&SHIPTOZIP=" . urlencode($shipToZip);
  373. $nvpstr = $nvpstr . "&PHONENUM=" . urlencode($phoneNum);
  374. $nvpstr = $nvpstr . "&SOLUTIONTYPE=" . urlencode($solutionType);
  375. $nvpstr = $nvpstr . "&LANDINGPAGE=" . urlencode($landingPage);
  376. //$nvpstr = $nvpstr . "&CUSTOMERSERVICENUMBER=" . urlencode($tag);
  377. $nvpstr = $nvpstr . "&INVNUM=" . urlencode($tag);
  378. $_SESSION["currencyCodeType"] = $currencyCodeType;
  379. $_SESSION["PaymentType"] = $paymentType;
  380. //'---------------------------------------------------------------------------------------------------------------
  381. //' Make the API call to PayPal
  382. //' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
  383. //' If an error occured, show the resulting errors
  384. //'---------------------------------------------------------------------------------------------------------------
  385. $resArray=hash_call("SetExpressCheckout", $nvpstr);
  386. $ack = strtoupper($resArray["ACK"]);
  387. if($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING")
  388. {
  389. $token = urldecode($resArray["TOKEN"]);
  390. $_SESSION['TOKEN']=$token;
  391. $_SESSION['ipaddress']=$_SERVER['REMOTE_ADDR ']; // Payer ip
  392. }
  393. return $resArray;
  394. }
  395. /**
  396. * Prepares the parameters for the GetExpressCheckoutDetails API Call.
  397. *
  398. * @return array The NVP Collection object of the GetExpressCheckoutDetails Call Response.
  399. */
  400. function GetDetails($token)
  401. {
  402. //'--------------------------------------------------------------
  403. //' At this point, the buyer has completed authorizing the payment
  404. //' at PayPal. The function will call PayPal to obtain the details
  405. //' of the authorization, incuding any shipping information of the
  406. //' buyer. Remember, the authorization is not a completed transaction
  407. //' at this state - the buyer still needs an additional step to finalize
  408. //' the transaction
  409. //'--------------------------------------------------------------
  410. //declaring of global variables
  411. global $conf, $langs;
  412. global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
  413. global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
  414. //'---------------------------------------------------------------------------
  415. //' Build a second API request to PayPal, using the token as the
  416. //' ID to get the details on the payment authorization
  417. //'---------------------------------------------------------------------------
  418. $nvpstr="&TOKEN=" . $token;
  419. //'---------------------------------------------------------------------------
  420. //' Make the API call and store the results in an array.
  421. //' If the call was a success, show the authorization details, and provide
  422. //' an action to complete the payment.
  423. //' If failed, show the error
  424. //'---------------------------------------------------------------------------
  425. $resArray=hash_call("GetExpressCheckoutDetails",$nvpstr);
  426. $ack = strtoupper($resArray["ACK"]);
  427. if($ack == "SUCCESS" || $ack=="SUCCESSWITHWARNING")
  428. {
  429. $_SESSION['payer_id'] = $resArray['PAYERID'];
  430. }
  431. return $resArray;
  432. }
  433. /**
  434. * Validate payment
  435. *
  436. * @return void
  437. */
  438. function ConfirmPayment($token, $paymentType, $currencyCodeType, $payerID, $ipaddress, $FinalPaymentAmt, $tag)
  439. {
  440. /* Gather the information to make the final call to
  441. finalize the PayPal payment. The variable nvpstr
  442. holds the name value pairs
  443. */
  444. //declaring of global variables
  445. global $conf, $langs;
  446. global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
  447. global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
  448. $nvpstr = '&TOKEN=' . urlencode($token) . '&PAYERID=' . urlencode($payerID) . '&PAYMENTACTION=' . urlencode($paymentType) . '&AMT=' . urlencode($FinalPaymentAmt);
  449. $nvpstr .= '&CURRENCYCODE=' . urlencode($currencyCodeType) . '&IPADDRESS=' . urlencode($ipaddress);
  450. //$nvpstr .= '&CUSTOM=' . urlencode($tag);
  451. $nvpstr .= '&INVNUM=' . urlencode($tag);
  452. /* Make the call to PayPal to finalize payment
  453. If an error occured, show the resulting errors
  454. */
  455. $resArray=hash_call("DoExpressCheckoutPayment",$nvpstr);
  456. /* Display the API response back to the browser.
  457. If the response from PayPal was a success, display the response parameters'
  458. If the response was an error, display the errors received using APIError.php.
  459. */
  460. $ack = strtoupper($resArray["ACK"]);
  461. return $resArray;
  462. }
  463. /**
  464. * This function makes a DoDirectPayment API call
  465. *
  466. * paymentType: paymentType has to be one of the following values: Sale or Order or Authorization
  467. * paymentAmount: total value of the shopping cart
  468. * currencyCode: currency code value the PayPal API
  469. * firstName: first name as it appears on credit card
  470. * lastName: last name as it appears on credit card
  471. * street: buyer's street address line as it appears on credit card
  472. * city: buyer's city
  473. * state: buyer's state
  474. * countryCode: buyer's country code
  475. * zip: buyer's zip
  476. * creditCardType: buyer's credit card type (i.e. Visa, MasterCard ... )
  477. * creditCardNumber: buyers credit card number without any spaces, dashes or any other characters
  478. * expDate: credit card expiration date
  479. * cvv2: Card Verification Value
  480. * @return array The NVP Collection object of the DoDirectPayment Call Response.
  481. */
  482. function DirectPayment($paymentType, $paymentAmount, $creditCardType, $creditCardNumber, $expDate, $cvv2, $firstName, $lastName, $street, $city, $state, $zip, $countryCode, $currencyCode, $tag)
  483. {
  484. //declaring of global variables
  485. global $conf, $langs;
  486. global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
  487. global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
  488. //Construct the parameter string that describes DoDirectPayment
  489. $nvpstr = "&AMT=" . urlencode($paymentAmount);
  490. $nvpstr = $nvpstr . "&CURRENCYCODE=" . urlencode($currencyCode);
  491. $nvpstr = $nvpstr . "&PAYMENTACTION=" . urlencode($paymentType);
  492. $nvpstr = $nvpstr . "&CREDITCARDTYPE=" . urlencode($creditCardType);
  493. $nvpstr = $nvpstr . "&ACCT=" . urlencode($creditCardNumber);
  494. $nvpstr = $nvpstr . "&EXPDATE=" . urlencode($expDate);
  495. $nvpstr = $nvpstr . "&CVV2=" . urlencode($cvv2);
  496. $nvpstr = $nvpstr . "&FIRSTNAME=" . urlencode($firstName);
  497. $nvpstr = $nvpstr . "&LASTNAME=" . urlencode($lastName);
  498. $nvpstr = $nvpstr . "&STREET=" . urlencode($street);
  499. $nvpstr = $nvpstr . "&CITY=" . urlencode($city);
  500. $nvpstr = $nvpstr . "&STATE=" . urlencode($state);
  501. $nvpstr = $nvpstr . "&COUNTRYCODE=" . urlencode($countryCode);
  502. $nvpstr = $nvpstr . "&IPADDRESS=" . $_SERVER['REMOTE_ADDR'];
  503. $nvpstr = $nvpstr . "&INVNUM=" . urlencode($tag);
  504. $resArray=hash_call("DoDirectPayment", $nvpstr);
  505. return $resArray;
  506. }
  507. /**
  508. * hash_call: Function to perform the API call to PayPal using API signature
  509. *
  510. * @param string $methodName is name of API method.
  511. * @param string $nvpStr is nvp string.
  512. * @return array returns an associtive array containing the response from the server.
  513. */
  514. function hash_call($methodName,$nvpStr)
  515. {
  516. //declaring of global variables
  517. global $conf, $langs;
  518. global $API_Endpoint, $API_Url, $API_version, $USE_PROXY, $PROXY_HOST, $PROXY_PORT, $PROXY_USER, $PROXY_PASS;
  519. global $PAYPAL_API_USER, $PAYPAL_API_PASSWORD, $PAYPAL_API_SIGNATURE;
  520. // TODO problem with triggers
  521. $API_version="56";
  522. if ($conf->global->PAYPAL_API_SANDBOX)
  523. {
  524. $API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
  525. $API_Url = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
  526. }
  527. else
  528. {
  529. $API_Endpoint = "https://api-3t.paypal.com/nvp";
  530. $API_Url = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
  531. }
  532. // Clean parameters
  533. $PAYPAL_API_USER="";
  534. if ($conf->global->PAYPAL_API_USER) $PAYPAL_API_USER=$conf->global->PAYPAL_API_USER;
  535. $PAYPAL_API_PASSWORD="";
  536. if ($conf->global->PAYPAL_API_PASSWORD) $PAYPAL_API_PASSWORD=$conf->global->PAYPAL_API_PASSWORD;
  537. $PAYPAL_API_SIGNATURE="";
  538. if ($conf->global->PAYPAL_API_SIGNATURE) $PAYPAL_API_SIGNATURE=$conf->global->PAYPAL_API_SIGNATURE;
  539. $PAYPAL_API_SANDBOX="";
  540. if ($conf->global->PAYPAL_API_SANDBOX) $PAYPAL_API_SANDBOX=$conf->global->PAYPAL_API_SANDBOX;
  541. // TODO END problem with triggers
  542. dol_syslog("Paypal API endpoint ".$API_Endpoint);
  543. //setting the curl parameters.
  544. $ch = curl_init();
  545. /*print $API_Endpoint."-".$API_version."-".$PAYPAL_API_USER."-".$PAYPAL_API_PASSWORD."-".$PAYPAL_API_SIGNATURE."<br>";
  546. print $USE_PROXY."-".$gv_ApiErrorURL."<br>";
  547. print $nvpStr;
  548. exit;*/
  549. curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
  550. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  551. //turning off the server and peer verification(TrustManager Concept).
  552. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  553. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  554. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  555. curl_setopt($ch, CURLOPT_POST, 1);
  556. //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
  557. if ($USE_PROXY)
  558. {
  559. dol_syslog("Paypal API hash_call set proxy to ".$PROXY_HOST. ":" . $PROXY_PORT." - ".$PROXY_USER. ":" . $PROXY_PASS);
  560. //curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10
  561. curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);
  562. if ($PROXY_USER) curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER. ":" . $PROXY_PASS);
  563. }
  564. //NVPRequest for submitting to server
  565. $nvpreq ="METHOD=" . urlencode($methodName) . "&VERSION=" . urlencode($API_version) . "&PWD=" . urlencode($PAYPAL_API_PASSWORD) . "&USER=" . urlencode($PAYPAL_API_USER) . "&SIGNATURE=" . urlencode($PAYPAL_API_SIGNATURE) . $nvpStr;
  566. $nvpreq.="&LOCALE=".strtoupper($langs->getDefaultLang(1));
  567. //$nvpreq.="&BRANDNAME=".urlencode(); // Override merchant name
  568. //$nvpreq.="&NOTIFYURL=".urlencode(); // For Instant Payment Notification url
  569. dol_syslog("Paypal API hash_call nvpreq=".$nvpreq);
  570. //setting the nvpreq as POST FIELD to curl
  571. curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
  572. //getting response from server
  573. $response = curl_exec($ch);
  574. $nvpReqArray=deformatNVP($nvpreq);
  575. $_SESSION['nvpReqArray']=$nvpReqArray;
  576. //convrting NVPResponse to an Associative Array
  577. dol_syslog("Paypal API hash_call Response nvpresp=".$response);
  578. $nvpResArray=deformatNVP($response);
  579. if (curl_errno($ch))
  580. {
  581. // moving to display page to display curl errors
  582. $_SESSION['curl_error_no']=curl_errno($ch);
  583. $_SESSION['curl_error_msg']=curl_error($ch);
  584. //Execute the Error handling module to display errors.
  585. }
  586. else
  587. {
  588. //closing the curl
  589. curl_close($ch);
  590. }
  591. return $nvpResArray;
  592. }
  593. /**
  594. * Get API errors
  595. *
  596. * @return array Array of errors
  597. */
  598. function GetApiError()
  599. {
  600. $errors=array();
  601. $resArray=$_SESSION['reshash'];
  602. if(isset($_SESSION['curl_error_no']))
  603. {
  604. $errors[] = $_SESSION['curl_error_no'].'-'.$_SESSION['curl_error_msg'];
  605. }
  606. foreach($resArray as $key => $value)
  607. {
  608. $errors[] = $key.'-'.$value;
  609. }
  610. return $errors;
  611. }
  612. /**
  613. * This function will take NVPString and convert it to an Associative Array and it will decode the response.
  614. * It is usefull to search for a particular key and displaying arrays.
  615. *
  616. * @param string $nvpstr NVPString
  617. * @return array nvpArray = Associative Array
  618. * ----------------------------------------------------------------------------------
  619. */
  620. function deformatNVP($nvpstr)
  621. {
  622. $intial=0;
  623. $nvpArray = array();
  624. while(strlen($nvpstr))
  625. {
  626. //postion of Key
  627. $keypos= strpos($nvpstr,'=');
  628. //position of value
  629. $valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr);
  630. /*getting the Key and Value values and storing in a Associative Array*/
  631. $keyval=substr($nvpstr,$intial,$keypos);
  632. $valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
  633. //decoding the respose
  634. $nvpArray[urldecode($keyval)] =urldecode($valval);
  635. $nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
  636. }
  637. return $nvpArray;
  638. }
  639. ?>