paymentok.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /* Copyright (C) 2001-2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2006-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/public/paypal/paymentok.php
  21. * \ingroup paypal
  22. * \brief File to show page after a successful payment
  23. * This page is called by paypal with url provided to payal competed with parameter TOKEN=xxx
  24. * This token can be used to get more informations.
  25. * \author Laurent Destailleur
  26. */
  27. define("NOLOGIN",1); // This means this output page does not require to be logged.
  28. define("NOCSRFCHECK",1); // We accept to go on this page from external web site.
  29. // For MultiCompany module
  30. $entity=(! empty($_GET['entity']) ? (int) $_GET['entity'] : (! empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  31. if (is_int($entity))
  32. {
  33. define("DOLENTITY", $entity);
  34. }
  35. require '../../main.inc.php';
  36. require_once DOL_DOCUMENT_ROOT.'/paypal/lib/paypal.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/paypal/lib/paypalfunctions.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  39. // Security check
  40. if (empty($conf->paypal->enabled)) accessforbidden('',1,1,1);
  41. $langs->load("main");
  42. $langs->load("other");
  43. $langs->load("dict");
  44. $langs->load("bills");
  45. $langs->load("companies");
  46. $langs->load("paybox");
  47. $langs->load("paypal");
  48. // Clean parameters
  49. $PAYPAL_API_USER="";
  50. if (! empty($conf->global->PAYPAL_API_USER)) $PAYPAL_API_USER=$conf->global->PAYPAL_API_USER;
  51. $PAYPAL_API_PASSWORD="";
  52. if (! empty($conf->global->PAYPAL_API_PASSWORD)) $PAYPAL_API_PASSWORD=$conf->global->PAYPAL_API_PASSWORD;
  53. $PAYPAL_API_SIGNATURE="";
  54. if (! empty($conf->global->PAYPAL_API_SIGNATURE)) $PAYPAL_API_SIGNATURE=$conf->global->PAYPAL_API_SIGNATURE;
  55. $PAYPAL_API_SANDBOX="";
  56. if (! empty($conf->global->PAYPAL_API_SANDBOX)) $PAYPAL_API_SANDBOX=$conf->global->PAYPAL_API_SANDBOX;
  57. $PAYPAL_API_OK="";
  58. if ($urlok) $PAYPAL_API_OK=$urlok;
  59. $PAYPAL_API_KO="";
  60. if ($urlko) $PAYPAL_API_KO=$urlko;
  61. if (empty($PAYPAL_API_USER))
  62. {
  63. dol_print_error('',"Paypal setup param PAYPAL_API_USER not defined");
  64. return -1;
  65. }
  66. if (empty($PAYPAL_API_PASSWORD))
  67. {
  68. dol_print_error('',"Paypal setup param PAYPAL_API_PASSWORD not defined");
  69. return -1;
  70. }
  71. if (empty($PAYPAL_API_SIGNATURE))
  72. {
  73. dol_print_error('',"Paypal setup param PAYPAL_API_SIGNATURE not defined");
  74. return -1;
  75. }
  76. $source=GETPOST('source');
  77. $ref=GETPOST('ref');
  78. $PAYPALTOKEN=GETPOST('TOKEN');
  79. if (empty($PAYPALTOKEN)) $PAYPALTOKEN=GETPOST('token');
  80. $PAYPALPAYERID=GETPOST('PAYERID');
  81. if (empty($PAYPALPAYERID)) $PAYPALPAYERID=GETPOST('PayerID');
  82. $PAYPALFULLTAG=GETPOST('FULLTAG');
  83. if (empty($PAYPALFULLTAG)) $PAYPALFULLTAG=GETPOST('fulltag');
  84. /*
  85. * Actions
  86. */
  87. /*
  88. * View
  89. */
  90. dol_syslog("Callback url when a PayPal payment was done. query_string=".(empty($_SERVER["QUERY_STRING"])?'':$_SERVER["QUERY_STRING"])." script_uri=".(empty($_SERVER["SCRIPT_URI"])?'':$_SERVER["SCRIPT_URI"]), LOG_DEBUG, 0, '_paypal');
  91. $tracepost = "";
  92. foreach($_POST as $k => $v) $tracepost .= "{$k} - {$v}\n";
  93. dol_syslog("POST=".$tracepost, LOG_DEBUG, 0, '_paypal');
  94. llxHeaderPaypal($langs->trans("PaymentForm"));
  95. print '<span id="dolpaymentspan"></span>'."\n";
  96. print '<div id="dolpaymentdiv" align="center">'."\n";
  97. if ($PAYPALTOKEN)
  98. {
  99. // Get on url call
  100. $token = $PAYPALTOKEN;
  101. $fulltag = $PAYPALFULLTAG;
  102. $payerID = $PAYPALPAYERID;
  103. // Set by newpayment.php
  104. $paymentType = $_SESSION['PaymentType'];
  105. $currencyCodeType = $_SESSION['currencyCodeType'];
  106. $FinalPaymentAmt = $_SESSION["Payment_Amount"];
  107. // From env
  108. $ipaddress = $_SESSION['ipaddress'];
  109. dol_syslog("Call newpaymentok with token=".$token." paymentType=".$paymentType." currencyCodeType=".$currencyCodeType." payerID=".$payerID." ipaddress=".$ipaddress." FinalPaymentAmt=".$FinalPaymentAmt." fulltag=".$fulltag, LOG_DEBUG, 0, '_paypal');
  110. // Send an email
  111. if (! empty($conf->global->MEMBER_PAYONLINE_SENDEMAIL) && preg_match('/MEM=/',$fulltag))
  112. {
  113. $sendto=$conf->global->MEMBER_PAYONLINE_SENDEMAIL;
  114. $from=$conf->global->MAILING_EMAIL_FROM;
  115. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  116. $mailfile = new CMailFile(
  117. 'New subscription payed',
  118. $sendto,
  119. $from,
  120. 'New subscription payed '.$fulltag
  121. );
  122. $result=$mailfile->sendfile();
  123. if ($result)
  124. {
  125. dol_syslog("EMail sent to ".$sendto, LOG_DEBUG, 0, '_paypal');
  126. }
  127. else
  128. {
  129. dol_syslog("Failed to send EMail to ".$sendto, LOG_ERR, 0, '_paypal');
  130. }
  131. }
  132. // Validate record
  133. if (! empty($paymentType))
  134. {
  135. dol_syslog("We call GetExpressCheckoutDetails", LOG_DEBUG, 0, '_paypal');
  136. $resArray=getDetails($token);
  137. //var_dump($resarray);
  138. dol_syslog("We call DoExpressCheckoutPayment token=".$token." paymentType=".$paymentType." currencyCodeType=".$currencyCodeType." payerID=".$payerID." ipaddress=".$ipaddress." FinalPaymentAmt=".$FinalPaymentAmt." fulltag=".$fulltag, LOG_DEBUG, 0, '_paypal');
  139. $resArray=confirmPayment($token, $paymentType, $currencyCodeType, $payerID, $ipaddress, $FinalPaymentAmt, $fulltag);
  140. $ack = strtoupper($resArray["ACK"]);
  141. if($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING")
  142. {
  143. $object = new stdClass();
  144. $object->source = $source;
  145. $object->ref = $ref;
  146. $object->payerID = $payerID;
  147. $object->fulltag = $fulltag;
  148. $object->resArray = $resArray;
  149. // resArray was built from a string like that
  150. // TOKEN=EC%2d1NJ057703V9359028&TIMESTAMP=2010%2d11%2d01T11%3a40%3a13Z&CORRELATIONID=1efa8c6a36bd8&ACK=Success&VERSION=56&BUILD=1553277&TRANSACTIONID=9B994597K9921420R&TRANSACTIONTYPE=expresscheckout&PAYMENTTYPE=instant&ORDERTIME=2010%2d11%2d01T11%3a40%3a12Z&AMT=155%2e57&FEEAMT=5%2e54&TAXAMT=0%2e00&CURRENCYCODE=EUR&PAYMENTSTATUS=Completed&PENDINGREASON=None&REASONCODE=None
  151. $PAYMENTSTATUS=urldecode($resArray["PAYMENTSTATUS"]); // Should contains 'Completed'
  152. $TRANSACTIONID=urldecode($resArray["TRANSACTIONID"]);
  153. $NOTE=urldecode($resArray["NOTE"]);
  154. print $langs->trans("YourPaymentHasBeenRecorded")."<br>\n";
  155. print $langs->trans("ThisIsTransactionId",$TRANSACTIONID)."<br>\n";
  156. if (! empty($conf->global->PAYPAL_MESSAGE_OK)) print $conf->global->PAYPAL_MESSAGE_OK;
  157. // Appel des triggers
  158. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  159. $interface=new Interfaces($db);
  160. $result=$interface->run_triggers('PAYPAL_PAYMENT_OK',$object,$user,$langs,$conf);
  161. if ($result < 0) { $error++; $errors=$interface->errors; }
  162. // Fin appel triggers
  163. }
  164. else
  165. {
  166. //Display a user friendly Error on the page using any of the following error information returned by PayPal
  167. $ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
  168. $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
  169. $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
  170. $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
  171. echo "DoExpressCheckoutPayment API call failed. ";
  172. echo "Detailed Error Message: " . $ErrorLongMsg;
  173. echo "Short Error Message: " . $ErrorShortMsg;
  174. echo "Error Code: " . $ErrorCode;
  175. echo "Error Severity Code: " . $ErrorSeverityCode;
  176. if ($mysoc->email) echo "\nPlease, send a screenshot of this page to ".$mysoc->email;
  177. }
  178. }
  179. else
  180. {
  181. dol_print_error('','Session expired');
  182. }
  183. }
  184. else
  185. {
  186. // No TOKEN parameter in URL
  187. dol_print_error('','No TOKEN parameter in URL');
  188. }
  189. print "\n</div>\n";
  190. html_print_paypal_footer($mysoc,$langs);
  191. llxFooterPaypal();
  192. $db->close();
  193. ?>