dolibarr 21.0.3
paymentko.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2006-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
30if (!defined('NOLOGIN')) {
31 define("NOLOGIN", 1); // This means this output page does not require to be logged.
32}
33if (!defined('NOCSRFCHECK')) {
34 define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
35}
36if (!defined('NOIPCHECK')) {
37 define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
38}
39if (!defined('NOBROWSERNOTIF')) {
40 define('NOBROWSERNOTIF', '1');
41}
42
43if (!defined('XFRAMEOPTIONS_ALLOWALL')) {
44 define('XFRAMEOPTIONS_ALLOWALL', '1');
45}
46
47// For MultiCompany module.
48// Do not use GETPOST here, function is not defined and this test must be done before including main.inc.php
49// Because 2 entities can have the same ref.
50$entity = (!empty($_GET['e']) ? (int) $_GET['e'] : (!empty($_POST['e']) ? (int) $_POST['e'] : 1));
51if (is_numeric($entity)) {
52 define("DOLENTITY", $entity);
53}
54
55'@phan-var-force CommonObject $object';
56
57// Load Dolibarr environment
58require '../../main.inc.php';
59require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
60require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
61require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
62if (isModEnabled('paypal')) {
63 require_once DOL_DOCUMENT_ROOT.'/paypal/lib/paypal.lib.php';
64 require_once DOL_DOCUMENT_ROOT.'/paypal/lib/paypalfunctions.lib.php';
65}
66
77// Hook to be used by external payment modules (ie Payzen, ...)
78$hookmanager = new HookManager($db);
79
80$hookmanager->initHooks(array('newpayment'));
81
82$langs->loadLangs(array("main", "other", "dict", "bills", "companies", "paybox", "paypal", "stripe"));
83
84$PAYPALTOKEN = "";
85$PAYPALPAYERID = "";
86if (isModEnabled('paypal')) {
87 $PAYPALTOKEN = GETPOST('TOKEN');
88 if (empty($PAYPALTOKEN)) {
89 $PAYPALTOKEN = GETPOST('token');
90 }
91 $PAYPALPAYERID = GETPOST('PAYERID');
92 if (empty($PAYPALPAYERID)) {
93 $PAYPALPAYERID = GETPOST('PayerID');
94 }
95}
96if (isModEnabled('paybox')) {
97}
98if (isModEnabled('stripe')) {
99}
100
101$FULLTAG = GETPOST('FULLTAG');
102if (empty($FULLTAG)) {
103 $FULLTAG = GETPOST('fulltag');
104}
105
106$suffix = GETPOST("suffix", 'aZ09');
107
108
109// Detect $paymentmethod
110$paymentmethod = '';
111$reg = array();
112if (preg_match('/PM=([^\.]+)/', $FULLTAG, $reg)) {
113 $paymentmethod = $reg[1];
114}
115if (empty($paymentmethod)) {
116 dol_print_error(null, 'The back url does not contain a parameter fulltag that should help us to find the payment method used');
117 exit;
118} else {
119 dol_syslog("paymentko.php: paymentmethod=".$paymentmethod, LOG_DEBUG, 0, '_payment');
120}
121
122// Detect $ws
123$reg_ws = array();
124$ws = preg_match('/WS=([^\.]+)/', $FULLTAG, $reg_ws) ? $reg_ws[1] : 0;
125if ($ws) {
126 dol_syslog("paymentko.php: page is invoked from a website with ref ".$ws.". It performs actions and then redirects back to this website. A page with ref paymentko must be created for this website.", LOG_DEBUG, 0, '_payment');
127}
128
129
130$validpaymentmethod = getValidOnlinePaymentMethods($paymentmethod);
131
132// Security check
133if (empty($validpaymentmethod)) {
134 httponly_accessforbidden('No valid payment mode');
135}
136
137
138$object = new stdClass(); // For triggers
141$error = 0;
142
143// Check if we have redirtodomain to do.
144$ws_virtuelhost = null;
145$ws_id = 0;
146$doactionsthenredirect = 0;
147if ($ws) {
148 $doactionsthenredirect = 1;
149 include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
150 $website = new Website($db);
151 $result = $website->fetch(0, $ws);
152 if ($result > 0) {
153 $ws_virtuelhost = $website->virtualhost;
154 $ws_id = $website->id;
155 }
156}
157
158
159/*
160 * Actions
161 */
162
163// None
164
165
166/*
167 * View
168 */
169
170dol_syslog("Callback url when an online payment is refused or canceled. query_string=".(empty($_SERVER["QUERY_STRING"]) ? '' : $_SERVER["QUERY_STRING"])." script_uri=".(empty($_SERVER["SCRIPT_URI"]) ? '' : $_SERVER["SCRIPT_URI"]), LOG_DEBUG, 0, '_payment');
171
172$tracepost = "";
173foreach ($_POST as $k => $v) {
174 if (is_scalar($k) && is_scalar($v)) {
175 $tracepost .= "$k - $v\n";
176 }
177}
178dol_syslog("POST=".$tracepost, LOG_DEBUG, 0, '_payment');
179
180dol_syslog("paymentkosessioncode=".GETPOST('paymentkosessioncode')." SESSION['paymentkosessioncode']=".$_SESSION['paymentkosessioncode'], LOG_DEBUG, 0, '_payment');
181
182// Set $appli for emails title
183$appli = $mysoc->name;
184$error = 0;
185
186
187if (!empty($_SESSION['ipaddress'])) { // To avoid to make action twice
188 // Get on url call
189 $fulltag = $FULLTAG;
190 $onlinetoken = empty($PAYPALTOKEN) ? $_SESSION['onlinetoken'] : $PAYPALTOKEN;
191 $payerID = empty($PAYPALPAYERID) ? $_SESSION['payerID'] : $PAYPALPAYERID;
192 // Set by newpayment.php
193 $paymentType = $_SESSION['PaymentType'];
194 $currencyCodeType = $_SESSION['currencyCodeType'];
195 $FinalPaymentAmt = $_SESSION['FinalPaymentAmt'];
196 // From env
197 $ipaddress = $_SESSION['ipaddress'];
198 $errormessage = $_SESSION['errormessage'];
199
200 if (is_object($object) && method_exists($object, 'call_trigger')) {
201 // Call trigger @phan-suppress-next-line PhanUndeclaredMethod
202 $result = $object->call_trigger('PAYMENTONLINE_PAYMENT_KO', $user);
203 if ($result < 0) {
204 $error++;
205 }
206 // End call triggers
207 }
208
209 // Send an email
210 $sendemail = getDolGlobalString('ONLINE_PAYMENT_SENDEMAIL');
211
212 // Send warning of error to administrator
213 if ($sendemail) {
214 // Get default language to use for the company for supervision emails
215 $myCompanyDefaultLang = $mysoc->default_lang;
216 if (empty($myCompanyDefaultLang) || $myCompanyDefaultLang === 'auto') {
217 // We must guess the language from the company country. We must not use the language of the visitor. This is a technical email for supervision
218 // so it must always be into the same language.
219 $myCompanyDefaultLang = getLanguageCodeFromCountryCode($mysoc->country_code);
220 }
221
222 $companylangs = new Translate('', $conf);
223 $companylangs->setDefaultLang($myCompanyDefaultLang);
224 $companylangs->loadLangs(array('main', 'members', 'bills', 'paypal', 'paybox', 'stripe'));
225
226 $from = getDolGlobalString("MAIN_MAIL_EMAIL_FROM");
227 $sendto = $sendemail;
228
229 $urlback = $_SERVER["REQUEST_URI"];
230 $topic = '['.$appli.'] '.$companylangs->transnoentitiesnoconv("NewOnlinePaymentFailed");
231 $content = "";
232 $content .= '<span style="color: orange">'.$companylangs->transnoentitiesnoconv("ValidationOfOnlinePaymentFailed")."</span>\n";
233
234 $content .= "<br><br>\n";
235 $content .= '<u>'.$companylangs->transnoentitiesnoconv("TechnicalInformation").":</u><br>\n";
236 $content .= $companylangs->transnoentitiesnoconv("OnlinePaymentSystem").': <strong>'.$paymentmethod."</strong><br>\n";
237 $content .= $companylangs->transnoentitiesnoconv("ReturnURLAfterPayment").': '.$urlback."<br>\n";
238 $content .= $companylangs->transnoentitiesnoconv("Error").': '.$errormessage."<br>\n";
239 $content .= "<br>\n";
240 $content .= "tag=".$fulltag." token=".$onlinetoken." paymentType=".$paymentType." currencycodeType=".$currencyCodeType." payerId=".$payerID." ipaddress=".$ipaddress." FinalPaymentAmt=".$FinalPaymentAmt;
241
242 $ishtml = dol_textishtml($content); // May contain urls
243
244 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
245 $mailfile = new CMailFile($topic, $sendto, $from, $content, array(), array(), array(), '', '', 0, $ishtml ? 1 : 0);
246
247 $result = $mailfile->sendfile();
248 if ($result) {
249 dol_syslog("EMail sent to ".$sendto, LOG_DEBUG, 0, '_payment');
250 } else {
251 dol_syslog("Failed to send EMail to ".$sendto, LOG_ERR, 0, '_payment');
252 }
253 }
254
255 unset($_SESSION['ipaddress']);
256}
257
258// Show answer page
259if (empty($doactionsthenredirect)) {
260 $head = '';
261 if (getDolGlobalString('ONLINE_PAYMENT_CSS_URL')) {
262 $head = '<link rel="stylesheet" type="text/css" href="' . getDolGlobalString('ONLINE_PAYMENT_CSS_URL').'?lang='.$langs->defaultlang.'">'."\n";
263 }
264
265 $conf->dol_hide_topmenu = 1;
266 $conf->dol_hide_leftmenu = 1;
267
268 $replacemainarea = (empty($conf->dol_hide_leftmenu) ? '<div>' : '').'<div>';
269 llxHeader($head, $langs->trans("PaymentForm"), '', '', 0, 0, '', '', '', 'onlinepaymentbody', $replacemainarea);
270
271
272 // Show ko message
273 print '<span id="dolpaymentspan"></span>'."\n";
274 print '<div id="dolpaymentdiv" align="center">'."\n";
275
276 // Show logo (search order: logo defined by PAYMENT_LOGO_suffix, then PAYMENT_LOGO, then small company logo, large company logo, theme logo, common logo)
277 // Define logo and logosmall
278 $logosmall = $mysoc->logo_small;
279 $logo = $mysoc->logo;
280 $paramlogo = 'ONLINE_PAYMENT_LOGO_'.$suffix;
281 if (getDolGlobalString($paramlogo)) {
282 $logosmall = getDolGlobalString($paramlogo);
283 } elseif (getDolGlobalString('ONLINE_PAYMENT_LOGO')) {
284 $logosmall = getDolGlobalString('ONLINE_PAYMENT_LOGO');
285 }
286 //print '<!-- Show logo (logosmall='.$logosmall.' logo='.$logo.') -->'."\n";
287 // Define urllogo
288 $urllogo = '';
289 $urllogofull = '';
290 if (!empty($logosmall) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$logosmall)) {
291 $urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/thumbs/'.$logosmall);
292 $urllogofull = $dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/thumbs/'.$logosmall);
293 } elseif (!empty($logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$logo)) {
294 $urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/'.$logo);
295 $urllogofull = $dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/'.$logo);
296 }
297
298 // Output html code for logo
299 if ($urllogo) {
300 print '<div class="backgreypublicpayment">';
301 print '<div class="logopublicpayment">';
302 print '<img id="dolpaymentlogo" src="'.$urllogo.'"';
303 print '>';
304 print '</div>';
305 if (!getDolGlobalString('MAIN_HIDE_POWERED_BY')) {
306 print '<div class="poweredbypublicpayment opacitymedium right"><a class="poweredbyhref" href="https://www.dolibarr.org?utm_medium=website&utm_source=poweredby" target="dolibarr" rel="noopener">'.$langs->trans("PoweredBy").'<br><img class="poweredbyimg" src="'.DOL_URL_ROOT.'/theme/dolibarr_logo.svg" width="80px"></a></div>';
307 }
308 print '</div>';
309 }
310 if (getDolGlobalString('MAIN_IMAGE_PUBLIC_PAYMENT')) {
311 print '<div class="backimagepublicpayment">';
312 print '<img id="idMAIN_IMAGE_PUBLIC_PAYMENT" src="' . getDolGlobalString('MAIN_IMAGE_PUBLIC_PAYMENT').'">';
313 print '</div>';
314 }
315
316
317 print '<br><br>';
318
319
320 print $langs->trans("YourPaymentHasNotBeenRecorded")."<br><br>";
321
322 $key = 'ONLINE_PAYMENT_MESSAGE_KO';
323 if (getDolGlobalString($key)) {
324 print $conf->global->$key;
325 }
326
327 $type = GETPOST('s', 'alpha');
328 $ref = GETPOST('ref', 'alphanohtml');
329 $tag = GETPOST('tag', 'alpha');
330 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
331 if ($type || $tag) {
332 $urlsubscription = getOnlinePaymentUrl(0, ($type ? $type : 'free'), $ref, $FinalPaymentAmt, $tag);
333
334 print $langs->trans("ClickHereToTryAgain", $urlsubscription);
335 }
336
337 print "\n</div>\n";
338
339
340 htmlPrintOnlineFooter($mysoc, $langs, 0, $suffix);
341
342 llxFooter('', 'public');
343}
344
345
346$db->close();
347
348
349// If option to do a redirect somewhere else is defined.
350if (!empty($doactionsthenredirect)) {
351 // Redirect to an error page
352 $randomseckey = getRandomPassword(true, null, 20);
353 $_SESSION['paymentkosessionkey'] = $randomseckey; // key between paymentok.php to another page like a paymentko of the website.
354
355 // Paymentko page must be created for the specific website
356 if (!defined('USEDOLIBARRSERVER') && !empty($ws_virtuelhost)) {
357 $ext_urlko = $ws_virtuelhost . '/paymentko.php?paymentkosessioncode='.urlencode($randomseckey).'&fulltag='.$FULLTAG;
358 } else {
359 $ext_urlko = DOL_URL_ROOT.'/public/website/index.php?paymentkosessioncode='.urlencode($randomseckey).'&website='.urlencode($ws).'&pageref=paymentko&fulltag='.$FULLTAG;
360 }
361
362 dol_syslog("Now do a redirect using Location : ".$ext_urlko, LOG_DEBUG, 0, '_payment');
363 header("Location: ".$ext_urlko);
364 exit;
365}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:87
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to manage hooks.
Class to manage translations.
Class Website.
htmlPrintOnlineFooter($fromcompany, $langs, $addformmessage=0, $suffix='', $object=null)
Show footer of company in HTML public pages.
getLanguageCodeFromCountryCode($countrycode)
Return default language from country code.
dol_textishtml($msg, $option=0)
Return if a text is a html content.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
getRandomPassword($generic=false, $replaceambiguouschars=null, $length=32)
Return a generated password using default module.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.