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