19 if (!defined(
'NOLOGIN')) define(
"NOLOGIN", 1);
20 if (!defined(
'NOCSRFCHECK')) define(
"NOCSRFCHECK", 1);
21 if (!defined(
'NOIPCHECK')) define(
'NOIPCHECK',
'1');
22 if (!defined(
'NOBROWSERNOTIF')) define(
'NOBROWSERNOTIF',
'1');
24 $entity = (!empty($_GET[
'entity']) ? (int) $_GET[
'entity'] : (!empty($_POST[
'entity']) ? (int) $_POST[
'entity'] : 1));
25 if (is_numeric($entity)) define(
"DOLENTITY", $entity);
27 require
'../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/class/ccountry.class.php';
31 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
34 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
35 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
36 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/includes/stripe/stripe-php/init.php';
39 require_once DOL_DOCUMENT_ROOT.
'/stripe/class/stripe.class.php';
46 if (isset($_GET[
'connect']))
48 if (isset($_GET[
'test']))
50 $endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_CONNECT_KEY;
51 $service =
'StripeTest';
54 $endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_CONNECT_KEY;
55 $service =
'StripeLive';
59 if (isset($_GET[
'test']))
61 $endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_KEY;
62 $service =
'StripeTest';
65 $endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_KEY;
66 $service =
'StripeLive';
71 if (empty($endpoint_secret))
73 print 'Error: Setup of module Stripe not complete for mode '.$service.
'. The WEBHOOK_KEY is not defined.';
74 http_response_code(400);
83 $payload = @file_get_contents(
"php://input");
84 $sig_header =
$_SERVER[
"HTTP_STRIPE_SIGNATURE"];
90 $event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
91 }
catch (\UnexpectedValueException $e) {
93 http_response_code(400);
95 }
catch (\
Stripe\Error\SignatureVerification $e) {
97 http_response_code(400);
103 $langs->load(
"main");
106 $user =
new User($db);
107 $user->fetch($conf->global->STRIPE_USER_ACCOUNT_FOR_ACTIONS);
110 if (!empty($conf->multicompany->enabled) && !empty($conf->stripeconnect->enabled) && is_object($mc))
112 $sql =
"SELECT entity";
113 $sql .=
" FROM ".MAIN_DB_PREFIX.
"oauth_token";
114 $sql .=
" WHERE service = '".$db->escape($service).
"' and tokenstring LIKE '%".$db->escape($event->account).
"%'";
116 dol_syslog(get_class($db).
"::fetch", LOG_DEBUG);
117 $result = $db->query($sql);
120 if ($db->num_rows($result))
122 $obj = $db->fetch_object($result);
130 $ret = $mc->switchEntity($key);
134 $stripe =
new Stripe($db);
137 $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
138 if (!empty($conf->global->MAIN_APPLICATION_TITLE)) $societeName = $conf->global->MAIN_APPLICATION_TITLE;
141 dol_syslog(
"***** Stripe IPN was called with event->type = ".$event->type);
144 if ($event->type ==
'payout.created') {
147 $result =
dolibarr_set_const($db, $service.
"_NEXTPAYOUT", date(
'Y-m-d H:i:s', $event->data->object->arrival_date),
'chaine', 0,
'', $conf->entity);
151 $subject = $societeName.
' - [NOTIFICATION] Stripe payout scheduled';
152 if (!empty($user->email)) {
155 $sendto = $conf->global->MAIN_INFO_SOCIETE_MAIL.
'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.
'>';
159 if (!empty($conf->global->ONLINE_PAYMENT_SENDEMAIL)) {
160 $sendtocc = $conf->global->ONLINE_PAYMENT_SENDEMAIL.
'" <'.$conf->global->ONLINE_PAYMENT_SENDEMAIL.
'>';
163 $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');
179 $ret = $mailfile->sendfile();
181 http_response_code(200);
185 http_response_code(500);
188 } elseif ($event->type ==
'payout.paid') {
191 $result =
dolibarr_set_const($db, $service.
"_NEXTPAYOUT", null,
'chaine', 0,
'', $conf->entity);
194 $langs->load(
"errors");
197 $label = $event->data->object->description;
198 $amount = $event->data->object->amount / 100;
199 $amount_to = $event->data->object->amount / 100;
200 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
202 $accountfrom =
new Account($db);
203 $accountfrom->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS);
206 $accountto->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_BANKTRANSFERS);
208 if (($accountto->id != $accountfrom->id) && empty($error))
210 $bank_line_id_from = 0;
211 $bank_line_id_to = 0;
218 if (!$error) $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1 *
price2num($amount),
'',
'', $user);
219 if (!($bank_line_id_from > 0)) $error++;
220 if (!$error) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label,
price2num($amount),
'',
'', $user);
221 if (!($bank_line_id_to > 0)) $error++;
223 if (!$error) $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.
'/compta/bank/line.php?rowid=',
'(banktransfert)',
'banktransfert');
224 if (!($result > 0)) $error++;
225 if (!$error) $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.
'/compta/bank/line.php?rowid=',
'(banktransfert)',
'banktransfert');
226 if (!($result > 0)) $error++;
229 $subject = $societeName.
' - [NOTIFICATION] Stripe payout done';
230 if (!empty($user->email)) {
233 $sendto = $conf->global->MAIN_INFO_SOCIETE_MAIL.
'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.
'>';
237 if (!empty($conf->global->ONLINE_PAYMENT_SENDEMAIL)) {
238 $sendtocc = $conf->global->ONLINE_PAYMENT_SENDEMAIL.
'" <'.$conf->global->ONLINE_PAYMENT_SENDEMAIL.
'>';
241 $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');
257 $ret = $mailfile->sendfile();
259 http_response_code(200);
263 http_response_code(500);
266 } elseif ($event->type ==
'customer.source.created') {
268 } elseif ($event->type ==
'customer.source.updated') {
270 } elseif ($event->type ==
'customer.source.delete') {
272 } elseif ($event->type ==
'customer.deleted') {
274 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"societe_account WHERE key_account = '".$db->escape($event->data->object->id).
"' and site='stripe'";
277 } elseif ($event->type ==
'payment_intent.succeeded') {
280 } elseif ($event->type ==
'payment_intent.payment_failed') {
282 } elseif ($event->type ==
'checkout.session.completed')
286 } elseif ($event->type ==
'payment_method.attached') {
287 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companypaymentmode.class.php';
288 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
293 $idthirdparty = $societeaccount->getThirdPartyID($db->escape($event->data->object->customer),
'stripe', $servicestatus);
294 if ($idthirdparty > 0)
296 $companypaymentmode->stripe_card_ref = $db->escape($event->data->object->id);
297 $companypaymentmode->fk_soc = $idthirdparty;
298 $companypaymentmode->bank = null;
299 $companypaymentmode->label = null;
300 $companypaymentmode->number = $db->escape($event->data->object->id);
301 $companypaymentmode->last_four = $db->escape($event->data->object->card->last4);
302 $companypaymentmode->card_type = $db->escape($event->data->object->card->branding);
303 $companypaymentmode->proprio = $db->escape($event->data->object->billing_details->name);
304 $companypaymentmode->exp_date_month = $db->escape($event->data->object->card->exp_month);
305 $companypaymentmode->exp_date_year = $db->escape($event->data->object->card->exp_year);
306 $companypaymentmode->cvn = null;
307 $companypaymentmode->datec = $db->escape($event->data->object->created);
308 $companypaymentmode->default_rib = 0;
309 $companypaymentmode->type = $db->escape($event->data->object->type);
310 $companypaymentmode->country_code = $db->escape($event->data->object->card->country);
311 $companypaymentmode->status = $servicestatus;
316 $result = $companypaymentmode->create($user);
329 } elseif ($event->type ==
'payment_method.updated') {
330 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companypaymentmode.class.php';
332 $companypaymentmode->fetch(0,
'', 0,
'',
" AND stripe_card_ref = '".$db->escape($event->data->object->id).
"'");
333 $companypaymentmode->bank = null;
334 $companypaymentmode->label = null;
335 $companypaymentmode->number = $db->escape($event->data->object->id);
336 $companypaymentmode->last_four = $db->escape($event->data->object->card->last4);
337 $companypaymentmode->proprio = $db->escape($event->data->object->billing_details->name);
338 $companypaymentmode->exp_date_month = $db->escape($event->data->object->card->exp_month);
339 $companypaymentmode->exp_date_year = $db->escape($event->data->object->card->exp_year);
340 $companypaymentmode->cvn = null;
341 $companypaymentmode->datec = $db->escape($event->data->object->created);
342 $companypaymentmode->default_rib = 0;
343 $companypaymentmode->type = $db->escape($event->data->object->type);
344 $companypaymentmode->country_code = $db->escape($event->data->object->card->country);
345 $companypaymentmode->status = $servicestatus;
350 $result = $companypaymentmode->update($user);
362 } elseif ($event->type ==
'payment_method.detached') {
364 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"societe_rib WHERE number = '".$db->escape($event->data->object->id).
"' and status = ".$servicestatus;
367 } elseif ($event->type ==
'charge.succeeded') {
370 } elseif ($event->type ==
'charge.failed') {
372 } elseif (($event->type ==
'source.chargeable') && ($event->data->object->type ==
'three_d_secure') && ($event->data->object->three_d_secure->authenticated ==
true)) {
376 http_response_code(200);
dol_now($mode= 'auto')
Return date for now.
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 Dolibarr users.
Class for SocieteAccount.
Class to manage bank accounts.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto); $mailfile->sendfile();.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
accessforbidden($message= '', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
print $_SERVER["PHP_SELF"]
Edit parameters.
Class for CompanyPaymentMode.
print
Draft customers invoices.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.