19 if (!defined(
'NOLOGIN')) {
22 if (!defined(
'NOCSRFCHECK')) {
23 define(
"NOCSRFCHECK", 1);
25 if (!defined(
'NOIPCHECK')) {
26 define(
'NOIPCHECK',
'1');
28 if (!defined(
'NOBROWSERNOTIF')) {
29 define(
'NOBROWSERNOTIF',
'1');
32 $entity = (!empty($_GET[
'entity']) ? (int) $_GET[
'entity'] : (!empty($_POST[
'entity']) ? (int) $_POST[
'entity'] : 1));
33 if (is_numeric($entity)) {
34 define(
"DOLENTITY", $entity);
38 require
'../../main.inc.php';
39 require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
40 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/core/class/ccountry.class.php';
42 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
43 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
44 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
45 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
46 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
47 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
49 require_once DOL_DOCUMENT_ROOT.
'/includes/stripe/stripe-php/init.php';
50 require_once DOL_DOCUMENT_ROOT.
'/stripe/class/stripe.class.php';
54 if (isset($_GET[
'connect'])) {
55 if (isset($_GET[
'test'])) {
56 $endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_CONNECT_KEY;
57 $service =
'StripeTest';
60 $endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_CONNECT_KEY;
61 $service =
'StripeLive';
65 if (isset($_GET[
'test'])) {
66 $endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_KEY;
67 $service =
'StripeTest';
70 $endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_KEY;
71 $service =
'StripeLive';
76 if (empty($conf->stripe->enabled)) {
80 if (empty($endpoint_secret)) {
84 if (!empty($conf->global->STRIPE_USER_ACCOUNT_FOR_ACTIONS)) {
86 $user =
new User($db);
87 $user->fetch($conf->global->STRIPE_USER_ACCOUNT_FOR_ACTIONS);
102 $payload = @file_get_contents(
"php://input");
103 $sig_header = $_SERVER[
"HTTP_STRIPE_SIGNATURE"];
109 $event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
110 }
catch (\UnexpectedValueException $e) {
113 }
catch (\
Stripe\Error\SignatureVerification $e) {
119 $langs->load(
"main");
122 if (
isModEnabled(
'multicompany') && !empty($conf->stripeconnect->enabled) && is_object($mc)) {
123 $sql =
"SELECT entity";
124 $sql .=
" FROM ".MAIN_DB_PREFIX.
"oauth_token";
125 $sql .=
" WHERE service = '".$db->escape($service).
"' and tokenstring LIKE '%".$db->escape($event->account).
"%'";
127 dol_syslog(get_class($db).
"::fetch", LOG_DEBUG);
128 $result = $db->query($sql);
130 if ($db->num_rows($result)) {
131 $obj = $db->fetch_object($result);
139 $ret = $mc->switchEntity($key);
143 $stripe =
new Stripe($db);
146 $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
147 if (!empty($conf->global->MAIN_APPLICATION_TITLE)) {
148 $societeName = $conf->global->MAIN_APPLICATION_TITLE;
153 dol_syslog(
"***** Stripe IPN was called with event->type = ".$event->type);
156 if ($event->type ==
'payout.created') {
159 $result =
dolibarr_set_const($db, $service.
"_NEXTPAYOUT", date(
'Y-m-d H:i:s', $event->data->object->arrival_date),
'chaine', 0,
'', $conf->entity);
162 $subject = $societeName.
' - [NOTIFICATION] Stripe payout scheduled';
163 if (!empty($user->email)) {
166 $sendto = $conf->global->MAIN_INFO_SOCIETE_MAIL.
'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.
'>';
170 if (!empty($conf->global->ONLINE_PAYMENT_SENDEMAIL)) {
171 $sendtocc = $conf->global->ONLINE_PAYMENT_SENDEMAIL.
'" <'.$conf->global->ONLINE_PAYMENT_SENDEMAIL.
'>';
174 $message =
"A bank transfer of ".price2num($event->data->object->amount / 100).
" ".$event->data->object->currency.
" should arrive in your account the ".
dol_print_date($event->data->object->arrival_date,
'dayhour');
190 $ret = $mailfile->sendfile();
195 http_response_code(500);
198 } elseif ($event->type ==
'payout.paid') {
201 $result =
dolibarr_set_const($db, $service.
"_NEXTPAYOUT",
null,
'chaine', 0,
'', $conf->entity);
203 $langs->load(
"errors");
206 $label = $event->data->object->description;
207 $amount = $event->data->object->amount / 100;
208 $amount_to = $event->data->object->amount / 100;
209 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
211 $accountfrom =
new Account($db);
212 $accountfrom->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS);
215 $accountto->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_BANKTRANSFERS);
217 if (($accountto->id != $accountfrom->id) && empty($error)) {
218 $bank_line_id_from = 0;
219 $bank_line_id_to = 0;
227 $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1 *
price2num($amount),
'',
'', $user);
229 if (!($bank_line_id_from > 0)) {
233 $bank_line_id_to = $accountto->addline($dateo, $typeto, $label,
price2num($amount),
'',
'', $user);
235 if (!($bank_line_id_to > 0)) {
240 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.
'/compta/bank/line.php?rowid=',
'(banktransfert)',
'banktransfert');
242 if (!($result > 0)) {
246 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.
'/compta/bank/line.php?rowid=',
'(banktransfert)',
'banktransfert');
248 if (!($result > 0)) {
253 $subject = $societeName.
' - [NOTIFICATION] Stripe payout done';
254 if (!empty($user->email)) {
257 $sendto = $conf->global->MAIN_INFO_SOCIETE_MAIL.
'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.
'>';
261 if (!empty($conf->global->ONLINE_PAYMENT_SENDEMAIL)) {
262 $sendtocc = $conf->global->ONLINE_PAYMENT_SENDEMAIL.
'" <'.$conf->global->ONLINE_PAYMENT_SENDEMAIL.
'>';
265 $message =
"A bank transfer of ".price2num($event->data->object->amount / 100).
" ".$event->data->object->currency.
" has been done to your account the ".
dol_print_date($event->data->object->arrival_date,
'dayhour');
281 $ret = $mailfile->sendfile();
286 http_response_code(500);
289 } elseif ($event->type ==
'customer.source.created') {
291 } elseif ($event->type ==
'customer.source.updated') {
293 } elseif ($event->type ==
'customer.source.delete') {
295 } elseif ($event->type ==
'customer.deleted') {
297 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"societe_account WHERE key_account = '".$db->escape($event->data->object->id).
"' and site='stripe'";
300 } elseif ($event->type ==
'payment_intent.succeeded') {
303 } elseif ($event->type ==
'payment_intent.payment_failed') {
305 } elseif ($event->type ==
'checkout.session.completed') {
308 } elseif ($event->type ==
'payment_method.attached') {
309 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companypaymentmode.class.php';
310 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
315 $idthirdparty = $societeaccount->getThirdPartyID($db->escape($event->data->object->customer),
'stripe', $servicestatus);
316 if ($idthirdparty > 0) {
317 $companypaymentmode->stripe_card_ref = $db->escape($event->data->object->id);
318 $companypaymentmode->fk_soc = $idthirdparty;
319 $companypaymentmode->bank =
null;
320 $companypaymentmode->label =
null;
321 $companypaymentmode->number = $db->escape($event->data->object->id);
322 $companypaymentmode->last_four = $db->escape($event->data->object->card->last4);
323 $companypaymentmode->card_type = $db->escape($event->data->object->card->branding);
324 $companypaymentmode->proprio = $db->escape($event->data->object->billing_details->name);
325 $companypaymentmode->exp_date_month = $db->escape($event->data->object->card->exp_month);
326 $companypaymentmode->exp_date_year = $db->escape($event->data->object->card->exp_year);
327 $companypaymentmode->cvn =
null;
328 $companypaymentmode->datec = $db->escape($event->data->object->created);
329 $companypaymentmode->default_rib = 0;
330 $companypaymentmode->type = $db->escape($event->data->object->type);
331 $companypaymentmode->country_code = $db->escape($event->data->object->card->country);
332 $companypaymentmode->status = $servicestatus;
336 $result = $companypaymentmode->create($user);
347 } elseif ($event->type ==
'payment_method.updated') {
348 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companypaymentmode.class.php';
350 $companypaymentmode->fetch(0,
'', 0,
'',
" AND stripe_card_ref = '".$db->escape($event->data->object->id).
"'");
351 $companypaymentmode->bank =
null;
352 $companypaymentmode->label =
null;
353 $companypaymentmode->number = $db->escape($event->data->object->id);
354 $companypaymentmode->last_four = $db->escape($event->data->object->card->last4);
355 $companypaymentmode->proprio = $db->escape($event->data->object->billing_details->name);
356 $companypaymentmode->exp_date_month = $db->escape($event->data->object->card->exp_month);
357 $companypaymentmode->exp_date_year = $db->escape($event->data->object->card->exp_year);
358 $companypaymentmode->cvn =
null;
359 $companypaymentmode->datec = $db->escape($event->data->object->created);
360 $companypaymentmode->default_rib = 0;
361 $companypaymentmode->type = $db->escape($event->data->object->type);
362 $companypaymentmode->country_code = $db->escape($event->data->object->card->country);
363 $companypaymentmode->status = $servicestatus;
367 $result = $companypaymentmode->update($user);
377 } elseif ($event->type ==
'payment_method.detached') {
379 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"societe_rib WHERE number = '".$db->escape($event->data->object->id).
"' and status = ".((int) $servicestatus);
382 } elseif ($event->type ==
'charge.succeeded') {
385 } elseif ($event->type ==
'charge.failed') {
387 } elseif (($event->type ==
'source.chargeable') && ($event->data->object->type ==
'three_d_secure') && ($event->data->object->three_d_secure->authenticated ==
true)) {
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
Class to manage bank accounts.
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class for CompanyPaymentMode.
Class for SocieteAccount.
Class to manage Dolibarr users.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
httponly_accessforbidden($message=1, $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.