dolibarr 25.0.0-alpha
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-2025 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
78// Hook to be used by external payment modules (ie Payzen, ...)
79$hookmanager = new HookManager($db);
80
81$hookmanager->initHooks(array('newpayment'));
82
83$langs->loadLangs(array("main", "other", "dict", "bills", "companies", "paypal", "stripe"));
84
85$PAYPALTOKEN = "";
86$PAYPALPAYERID = "";
87if (isModEnabled('paypal')) {
88 $PAYPALTOKEN = GETPOST('TOKEN');
89 if (empty($PAYPALTOKEN)) {
90 $PAYPALTOKEN = GETPOST('token');
91 }
92 $PAYPALPAYERID = GETPOST('PAYERID');
93 if (empty($PAYPALPAYERID)) {
94 $PAYPALPAYERID = GETPOST('PayerID');
95 }
96}
97
98$FULLTAG = GETPOST('FULLTAG');
99if (empty($FULLTAG)) {
100 $FULLTAG = GETPOST('fulltag');
101}
102
103$suffix = GETPOST("suffix", 'aZ09');
104
105
106// Detect $paymentmethod
107$paymentmethod = '';
108$reg = array();
109if (preg_match('/PM=([^\.]+)/', $FULLTAG, $reg)) {
110 $paymentmethod = $reg[1];
111}
112if (empty($paymentmethod)) {
113 // Missing/invalid fulltag is a malformed client request, not a server failure: answer 400
114 // with a plain message instead of dol_print_error (which implies an internal error and
115 // returns a misleading 202 status).
116 dol_syslog("***** paymentko.php was called with a non valid parameter FULLTAG=".$FULLTAG, LOG_WARNING, 0, '_payment');
117 http_response_code(400);
118 print 'Bad request: the back url does not contain a valid fulltag parameter, required to find the payment method used.';
119 exit;
120} else {
121 dol_syslog("paymentko.php: paymentmethod=".$paymentmethod, LOG_DEBUG, 0, '_payment');
122}
123
124// Detect $ws
125$reg_ws = array();
126$ws = preg_match('/WS=([^\.]+)/', $FULLTAG, $reg_ws) ? $reg_ws[1] : 0;
127if ($ws) {
128 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');
129}
130
131
132$validpaymentmethod = getValidOnlinePaymentMethods($paymentmethod);
133
134// Security check
135if (empty($validpaymentmethod)) {
136 httponly_accessforbidden('No valid payment mode');
137}
138
139
140$object = new stdClass(); // For triggers
141
142$error = 0;
143
144// Check if we have redirtodomain to do.
145$ws_virtuelhost = null;
146$ws_id = 0;
147$doactionsthenredirect = 0;
148if ($ws) {
149 $doactionsthenredirect = 1;
150 include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
151 $website = new Website($db);
152 $result = $website->fetch(0, $ws);
153 if ($result > 0) {
154 $ws_virtuelhost = $website->virtualhost;
155 $ws_id = $website->id;
156 }
157}
158
159
160/*
161 * Actions
162 */
163
164// None
165
166
167/*
168 * View
169 */
170
171dol_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');
172
173$tracepost = "";
174foreach ($_POST as $k => $v) {
175 if (is_scalar($k) && is_scalar($v)) {
176 $tracepost .= "$k - $v\n";
177 }
178}
179dol_syslog("POST=".$tracepost, LOG_DEBUG, 0, '_payment');
180
181dol_syslog("paymentkosessioncode=".GETPOST('paymentkosessioncode')." SESSION['paymentkosessioncode']=".$_SESSION['paymentkosessioncode'], LOG_DEBUG, 0, '_payment');
182
183// Set $appli for emails title
184$appli = $mysoc->name;
185$error = 0;
186$FinalPaymentAmt = 0;
187
188
189if (!empty($_SESSION['ipaddress'])) { // To avoid to make action twice
190 // Get on url call
191 $fulltag = $FULLTAG;
192 $onlinetoken = empty($PAYPALTOKEN) ? $_SESSION['onlinetoken'] : $PAYPALTOKEN;
193 $payerID = empty($PAYPALPAYERID) ? $_SESSION['payerID'] : $PAYPALPAYERID;
194 // Set by newpayment.php
195 $paymentType = $_SESSION['PaymentType'] ?? '';
196 $currencyCodeType = $_SESSION['currencyCodeType'];
197 $FinalPaymentAmt = $_SESSION['FinalPaymentAmt'];
198 // From env
199 $ipaddress = $_SESSION['ipaddress'];
200 $errormessage = $_SESSION['errormessage'];
201
202 // @phpstan-ignore-next-line
203 if (is_object($object) && method_exists($object, 'call_trigger')) {
204 // Call trigger @phan-suppress-next-line PhanUndeclaredMethod
205 $result = $object->call_trigger('PAYMENTONLINE_PAYMENT_KO', $user);
206 if ($result < 0) {
207 $error++;
208 }
209 // End call triggers
210 }
211
212 // Send an email
213 $sendemail = getDolGlobalString('ONLINE_PAYMENT_SENDEMAIL');
214
215 // Send warning of error to administrator
216 if ($sendemail) {
217 // Get default language to use for the company for supervision emails
218 $myCompanyDefaultLang = (string) $mysoc->default_lang;
219 if (empty($myCompanyDefaultLang) || $myCompanyDefaultLang === 'auto') {
220 // 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
221 // so it must always be into the same language.
222 $myCompanyDefaultLang = (string) getLanguageCodeFromCountryCode($mysoc->country_code);
223 }
224
225 $companylangs = new Translate('', $conf);
226 $companylangs->setDefaultLang($myCompanyDefaultLang);
227 $companylangs->loadLangs(array('main', 'members', 'bills', 'paypal', 'stripe'));
228
229 $from = getDolGlobalString("MAIN_MAIL_EMAIL_FROM");
230 $sendto = $sendemail;
231
232 $urlback = $_SERVER["REQUEST_URI"];
233 $topic = '['.$appli.'] '.$companylangs->transnoentitiesnoconv("NewOnlinePaymentFailed");
234 $content = "";
235 $content .= '<span style="color: orange">'.$companylangs->transnoentitiesnoconv("ValidationOfOnlinePaymentFailed")."</span>\n";
236
237 $content .= "<br><br>\n";
238 $content .= '<u>'.$companylangs->transnoentitiesnoconv("TechnicalInformation").":</u><br>\n";
239 $content .= $companylangs->transnoentitiesnoconv("OnlinePaymentSystem").': <strong>'.$paymentmethod."</strong><br>\n";
240 $content .= $companylangs->transnoentitiesnoconv("ReturnURLAfterPayment").': '.$urlback."<br>\n";
241 $content .= $companylangs->transnoentitiesnoconv("Error").': '.$errormessage."<br>\n";
242 $content .= "<br>\n";
243 $content .= "tag=".$fulltag." token=".$onlinetoken." paymentType=".$paymentType." currencycodeType=".$currencyCodeType." payerId=".$payerID." ipaddress=".$ipaddress." FinalPaymentAmt=".$FinalPaymentAmt;
244
245 $ishtml = dol_textishtml($content); // May contain urls
246
247 require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
248 $mailfile = new CMailFile($topic, $sendto, $from, $content, array(), array(), array(), '', '', 0, $ishtml ? 1 : 0);
249
250 $result = $mailfile->sendfile();
251 if ($result) {
252 dol_syslog("EMail sent to ".$sendto, LOG_DEBUG, 0, '_payment');
253 } else {
254 dol_syslog("Failed to send EMail to ".$sendto, LOG_ERR, 0, '_payment');
255 }
256 }
257
258 unset($_SESSION['ipaddress']);
259}
260
261// Show answer page
262if (empty($doactionsthenredirect)) {
263 $head = '';
264 if (getDolGlobalString('ONLINE_PAYMENT_CSS_URL')) {
265 $head = '<link rel="stylesheet" type="text/css" href="' . getDolGlobalString('ONLINE_PAYMENT_CSS_URL').'?lang='.$langs->defaultlang.'">'."\n";
266 }
267
268 $conf->dol_hide_topmenu = 1;
269 $conf->dol_hide_leftmenu = 1;
270
271 $replacemainarea = (empty($conf->dol_hide_leftmenu) ? '<div>' : '').'<div>';
272 llxHeader($head, $langs->trans("PaymentForm"), '', '', 0, 0, '', '', '', 'onlinepaymentbody', $replacemainarea);
273
274
275 // Show ko message
276 print '<span id="dolpaymentspan"></span>'."\n";
277 print '<div id="dolpaymentdiv" align="center">'."\n";
278
279 // Show logo (search order: logo defined by PAYMENT_LOGO_suffix, then PAYMENT_LOGO, then small company logo, large company logo, theme logo, common logo)
280 // Define logo and logosmall
281 $logosmall = $mysoc->logo_small;
282 $logo = $mysoc->logo;
283 $paramlogo = 'ONLINE_PAYMENT_LOGO_'.$suffix;
284 if (getDolGlobalString($paramlogo)) {
285 $logosmall = getDolGlobalString($paramlogo);
286 } elseif (getDolGlobalString('ONLINE_PAYMENT_LOGO')) {
287 $logosmall = getDolGlobalString('ONLINE_PAYMENT_LOGO');
288 }
289 //print '<!-- Show logo (logosmall='.$logosmall.' logo='.$logo.') -->'."\n";
290 // Define urllogo
291 $urllogo = '';
292 $urllogofull = '';
293 if (!empty($logosmall) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$logosmall)) {
294 $urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/thumbs/'.$logosmall);
295 $urllogofull = $dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/thumbs/'.$logosmall);
296 } elseif (!empty($logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$logo)) {
297 $urllogo = DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/'.$logo);
298 $urllogofull = $dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/'.$logo);
299 }
300
301 // Output html code for logo
302 if ($urllogo) {
303 print '<div class="backgreypublicpayment">';
304 print '<div class="logopublicpayment">';
305 print '<img id="dolpaymentlogo" src="'.$urllogo.'"';
306 print '>';
307 print '</div>';
308 if (!getDolGlobalString('MAIN_HIDE_POWERED_BY')) {
309 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>';
310 }
311 print '</div>';
312 }
313 if (getDolGlobalString('MAIN_IMAGE_PUBLIC_PAYMENT')) {
314 print '<div class="backimagepublicpayment">';
315 print '<img id="idMAIN_IMAGE_PUBLIC_PAYMENT" src="' . getDolGlobalString('MAIN_IMAGE_PUBLIC_PAYMENT').'">';
316 print '</div>';
317 }
318
319
320 print '<br><br>';
321
322
323 print $langs->trans("YourPaymentHasNotBeenRecorded")."<br><br>";
324
325 $key = 'ONLINE_PAYMENT_MESSAGE_KO';
326 if (getDolGlobalString($key)) {
327 print $conf->global->$key;
328 }
329
330 $type = GETPOST('s', 'alpha');
331 $ref = GETPOST('ref', 'alphanohtml');
332 $tag = GETPOST('tag', 'alpha');
333 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
334 if ($type || $tag) {
335 $urlsubscription = getOnlinePaymentUrl(0, ($type ? $type : 'free'), $ref, $FinalPaymentAmt, $tag);
336
337 print $langs->trans("ClickHereToTryAgain", $urlsubscription);
338 }
339
340 print "\n</div>\n";
341
342
343 htmlPrintOnlineFooter($mysoc, $langs, 0, $suffix);
344
345 llxFooter('', 'public');
346}
347
348
349$db->close();
350
351
352// If option to do a redirect somewhere else is defined.
353if (!empty($doactionsthenredirect)) {
354 // Redirect to an error page
355 $randomseckey = getRandomPassword(true, null, 20);
356 $_SESSION['paymentkosessionkey'] = $randomseckey; // key between paymentok.php to another page like a paymentko of the website.
357
358 // Paymentko page must be created for the specific website
359 if (!defined('USEDOLIBARRSERVER') && !empty($ws_virtuelhost)) {
360 $ext_urlko = $ws_virtuelhost . '/paymentko.php?paymentkosessioncode='.urlencode($randomseckey).'&fulltag='.$FULLTAG;
361 } else {
362 $ext_urlko = DOL_URL_ROOT.'/public/website/index.php?paymentkosessioncode='.urlencode($randomseckey).'&website='.urlencode($ws).'&pageref=paymentko&fulltag='.$FULLTAG;
363 }
364
365 dol_syslog("Now do a redirect using Location : ".$ext_urlko, LOG_DEBUG, 0, '_payment');
366 header("Location: ".$ext_urlko);
367 exit;
368}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
global $dolibarr_main_url_root
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
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:73
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.
global $mysoc
getLanguageCodeFromCountryCode($countrycode)
Return default language from country code.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_textishtml($msg, $option=0)
Return if a text is a html content.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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.