21if (!defined(
'NOLOGIN')) {
24if (!defined(
'NOCSRFCHECK')) {
25 define(
"NOCSRFCHECK", 1);
27if (!defined(
'NOIPCHECK')) {
28 define(
'NOIPCHECK',
'1');
30if (!defined(
'NOBROWSERNOTIF')) {
31 define(
'NOBROWSERNOTIF',
'1');
35$entity = (!empty($_GET[
'entity']) ? (int) $_GET[
'entity'] : (!empty($_POST[
'entity']) ? (int) $_POST[
'entity'] : 1));
36if (is_numeric($entity)) {
37 define(
"DOLENTITY", $entity);
41if (!defined(
'USESUFFIXINLOG')) {
42 define(
'USESUFFIXINLOG',
'_stripeipn');
46require
'../../main.inc.php';
47require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
48require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
49require_once DOL_DOCUMENT_ROOT.
'/core/class/ccountry.class.php';
50require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
51require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
52require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
53require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
54require_once DOL_DOCUMENT_ROOT.
'/compta/prelevement/class/bonprelevement.class.php';
55require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
56require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
57require_once DOL_DOCUMENT_ROOT.
'/includes/stripe/stripe-php/init.php';
58require_once DOL_DOCUMENT_ROOT.
'/stripe/class/stripe.class.php';
66if (GETPOSTISSET(
'connect')) {
67 if (GETPOSTISSET(
'test')) {
69 $service =
'StripeTest';
73 $service =
'StripeLive';
77 if (GETPOSTISSET(
'test')) {
79 $service =
'StripeTest';
83 $service =
'StripeLive';
88if (!isModEnabled(
'stripe')) {
92if (empty($endpoint_secret)) {
98 $user =
new User($db);
115$payload = @file_get_contents(
"php://input");
116$sig_header = empty($_SERVER[
"HTTP_STRIPE_SIGNATURE"]) ?
'' : $_SERVER[
"HTTP_STRIPE_SIGNATURE"];
120 $fh = fopen(DOL_DATA_ROOT.
'/dolibarr_stripeipn_payload.log',
'w+');
122 fwrite($fh,
dol_print_date(
dol_now(
'gmt'),
'standard').
' IPN Called. service='.$service.
' HTTP_STRIPE_SIGNATURE='.$sig_header.
"\n");
123 fwrite($fh, $payload);
125 dolChmod(DOL_DATA_ROOT.
'/dolibarr_stripeipn_payload.log');
132 $event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
133}
catch (UnexpectedValueException $e) {
137 httponly_accessforbidden(
'Invalid signature. May be a hook for an event created by another Stripe env ? Check setup of your keys whsec_...', 400);
147if (isModEnabled(
'multicompany') && !empty(
$conf->stripeconnect->enabled) && is_object($mc)) {
148 $sql =
"SELECT entity";
149 $sql .=
" FROM ".MAIN_DB_PREFIX.
"oauth_token";
150 $sql .=
" WHERE service = '".$db->escape($service).
"' and tokenstring LIKE '%".$db->escape($db->escapeforlike($event->account)).
"%'";
152 dol_syslog(get_class($db).
"::fetch", LOG_DEBUG);
153 dol_syslog(get_class($db).
"::fetch", LOG_DEBUG, 0,
'_payment');
154 $result = $db->query($sql);
156 if ($db->num_rows($result)) {
157 $obj = $db->fetch_object($result);
165 $ret = $mc->switchEntity($key);
179dol_syslog(
"***** Stripe IPN was called with event->type=".$event->type.
" service=".$service);
180dol_syslog(
"***** Stripe IPN was called with event->type=".$event->type.
" service=".$service, LOG_DEBUG, 0,
'_payment');
183if ($event->type ==
'payout.created' &&
getDolGlobalString(
'STRIPE_AUTO_RECORD_PAYOUT')) {
185 dol_syslog(
"object = ".var_export($event->data,
true));
186 dol_syslog(
"object = ".var_export($event->data,
true), LOG_DEBUG, 0,
'_payment');
190 $result =
dolibarr_set_const($db, $service.
"_NEXTPAYOUT", date(
'Y-m-d H:i:s', $event->data->object->arrival_date),
'chaine', 0,
'',
$conf->entity);
193 $subject =
'['.$societeName.
'] Notification - Stripe payout scheduled';
194 if (!empty($user->email)) {
205 $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');
221 $ret = $mailfile->sendfile();
226 http_response_code(500);
229} elseif ($event->type ==
'payout.paid' &&
getDolGlobalString(
'STRIPE_AUTO_RECORD_PAYOUT')) {
231 dol_syslog(
"object = ".var_export($event->data,
true));
232 dol_syslog(
"object = ".var_export($event->data,
true), LOG_DEBUG, 0,
'_payment');
237 $langs->load(
"errors");
240 $label = $event->data->object->description;
241 $amount = $event->data->object->amount / 100;
242 $amount_to = $event->data->object->amount / 100;
243 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
245 $accountfrom =
new Account($db);
246 $accountfrom->fetch(
getDolGlobalInt(
'STRIPE_BANK_ACCOUNT_FOR_PAYMENTS'));
249 $accountto->fetch(
getDolGlobalInt(
'STRIPE_BANK_ACCOUNT_FOR_BANKTRANSFERS'));
251 if (($accountto->id != $accountfrom->id) && empty($error)) {
252 $bank_line_id_from = 0;
253 $bank_line_id_to = 0;
263 $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1 * (
float)
price2num($amount),
'',
'', $user);
265 if (!($bank_line_id_from > 0)) {
269 $bank_line_id_to = $accountto->addline($dateo, $typeto, $label,
price2num($amount),
'',
'', $user);
271 if (!($bank_line_id_to > 0)) {
276 $result = $accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.
'/compta/bank/line.php?rowid=',
'(banktransfert)',
'banktransfert');
278 if (!($result > 0)) {
282 $result = $accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.
'/compta/bank/line.php?rowid=',
'(banktransfert)',
'banktransfert');
284 if (!($result > 0)) {
296 $subject =
'['.$societeName.
'] - NotificationOTIFICATION] Stripe payout done';
297 if (!empty($user->email)) {
308 $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');
324 $ret = $mailfile->sendfile();
331 http_response_code(500);
334} elseif ($event->type ==
'customer.source.created') {
336} elseif ($event->type ==
'customer.source.updated') {
338} elseif ($event->type ==
'customer.source.delete') {
340} elseif ($event->type ==
'customer.deleted') {
343 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"societe_account WHERE key_account = '".$db->escape($event->data->object->id).
"' AND site = 'stripe'";
346} elseif ($event->type ==
'payment_intent.succeeded') {
348 dol_syslog(
"object = ".var_export($event->data,
true));
349 dol_syslog(
"object = ".var_export($event->data,
true), LOG_DEBUG, 0,
'_payment');
351 include_once DOL_DOCUMENT_ROOT .
'/compta/paiement/class/paiement.class.php';
352 global $stripearrayofkeysbyenv;
354 $object = $event->data->object;
356 $ipaddress =
$object->metadata->ipaddress;
358 $currencyCodeType = strtoupper(
$object->currency);
359 $paymentmethodstripeid =
$object->payment_method;
360 $customer_id =
$object->customer;
362 $paymentTypeCode =
"";
363 $paymentTypeCodeInDolibarr =
"";
365 $payment_amountInDolibarr = 0;
367 dol_syslog(
"Try to find a payment in database for the payment_intent id = ".$TRANSACTIONID);
368 dol_syslog(
"Try to find a payment in database for the payment_intent id = ".$TRANSACTIONID, LOG_DEBUG, 0,
'_payment');
370 $sql =
"SELECT pi.rowid, pi.fk_facture, pi.fk_prelevement_bons, pi.amount, pi.type, pi.traite";
371 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
372 $sql .=
" WHERE pi.ext_payment_id = '".$db->escape($TRANSACTIONID).
"'";
373 $sql .=
" AND pi.ext_payment_site = '".$db->escape($service).
"'";
375 $result = $db->query($sql);
377 $obj = $db->fetch_object($result);
379 if ($obj->type ==
'ban') {
381 if ($obj->traite == 1) {
384 $invoice_id = $obj->fk_facture;
385 $directdebitorcreditransfer_id = $obj->fk_prelevement_bons;
386 $payment_amountInDolibarr = $obj->amount;
387 $paymentTypeCodeInDolibarr = $obj->type;
389 dol_syslog(
"Found a request in database to pay with direct debit generated (pdid = ".$pdid.
" directdebitorcreditransfer_id=".$directdebitorcreditransfer_id.
")");
390 dol_syslog(
"Found a request in database to pay with direct debit generated (pdid = ".$pdid.
" directdebitorcreditransfer_id=".$directdebitorcreditransfer_id.
")", LOG_DEBUG, 0,
'_payment');
392 dol_syslog(
"Found a request in database not yet generated (pdid = ".$pdid.
" directdebitorcreditransfer_id=".$directdebitorcreditransfer_id.
"). Was the order deleted after being sent ?", LOG_WARNING);
393 dol_syslog(
"Found a request in database not yet generated (pdid = ".$pdid.
" directdebitorcreditransfer_id=".$directdebitorcreditransfer_id.
"). Was the order deleted after being sent ?", LOG_WARNING, 0,
'_payment');
396 if ($obj->type ==
'card' || empty($obj->type)) {
398 if ($obj->traite == 0) {
400 $invoice_id = $obj->fk_facture;
401 $payment_amountInDolibarr = $obj->amount;
402 $paymentTypeCodeInDolibarr = empty($obj->type) ?
'card' : $obj->type;
404 dol_syslog(
"Found a request in database to pay with card (pdid = ".$pdid.
"). We should fix status traite to 1");
405 dol_syslog(
"Found a request in database to pay with card (pdid = ".$pdid.
"). We should fix status traite to 1", LOG_DEBUG, 0,
'_payment');
407 dol_syslog(
"Found a request in database to pay with card (pdid = ".$pdid.
") already set to traite=1. Nothing to fix.");
408 dol_syslog(
"Found a request in database to pay with card (pdid = ".$pdid.
") already set to traite=1. Nothing to fix.", LOG_DEBUG, 0,
'_payment');
412 dol_syslog(
"Payment intent ".$TRANSACTIONID.
" not found into database, so ignored.");
413 dol_syslog(
"Payment intent ".$TRANSACTIONID.
" not found into database, so ignored.", LOG_DEBUG, 0,
'_payment');
414 http_response_code(200);
415 print
"Payment intent ".$TRANSACTIONID.
" not found into database, so ignored.";
419 http_response_code(500);
420 print $db->lasterror();
424 if ($paymentTypeCodeInDolibarr) {
427 $stripeacc = $stripearrayofkeysbyenv[$servicestatus][
'secret_key'];
429 dol_syslog(
"Get the Stripe payment object for the payment method id = ".json_encode($paymentmethodstripeid));
430 dol_syslog(
"Get the Stripe payment object for the payment method id = ".json_encode($paymentmethodstripeid), LOG_DEBUG, 0,
'_payment');
432 $s = new \Stripe\StripeClient($stripeacc);
434 $paymentmethodstripe = $s->paymentMethods->retrieve($paymentmethodstripeid);
435 $paymentTypeCode = $paymentmethodstripe->type;
436 if ($paymentTypeCode ==
"ban" || $paymentTypeCode ==
"sepa_debit") {
437 $paymentTypeCode =
"PRE";
438 } elseif ($paymentTypeCode ==
"card") {
439 $paymentTypeCode =
"CB";
442 $payment_amount = $payment_amountInDolibarr;
445 $postactionmessages = array();
447 if ($paymentTypeCode ==
"CB" && ($paymentTypeCodeInDolibarr ==
'card' || empty($paymentTypeCodeInDolibarr))) {
453 dol_syslog(
"TODO update flag traite to 1", LOG_DEBUG, 0,
'_payment');
454 } elseif ($paymentTypeCode ==
"PRE" && $paymentTypeCodeInDolibarr ==
'ban') {
459 $paiement->datepaye = $now;
460 $paiement->date = $now;
461 if ($currencyCodeType ==
$conf->currency) {
462 $paiement->amounts = [$invoice_id => $payment_amount];
464 $paiement->multicurrency_amounts = [$invoice_id => $payment_amount];
466 $postactionmessages[] =
'Payment was done in a currency ('.$currencyCodeType.
') other than the expected currency of company ('.
$conf->currency.
')';
467 $ispostactionok = -1;
473 $paiement->paiementcode = $paymentTypeCode;
474 $sql =
"SELECT id FROM ".MAIN_DB_PREFIX.
"c_paiement";
475 $sql .=
" WHERE code = '".$db->escape($paymentTypeCode).
"'";
476 $sql .=
" AND entity IN (".getEntity(
'c_paiement').
")";
477 $resql = $db->query($sql);
479 $obj = $db->fetch_object($resql);
480 $paiement->paiementid = $obj->id;
485 $paiement->num_payment =
'';
486 $paiement->note_public =
'';
487 $paiement->note_private =
'StripeSepa payment received by IPN webhook - ' .
dol_print_date($now,
'standard') .
' (TZ server) using servicestatus=' . $servicestatus . ($ipaddress ?
' from ip ' . $ipaddress :
'') .
' - Transaction ID = ' . $TRANSACTIONID;
488 $paiement->ext_payment_id = $TRANSACTIONID.
':'.$customer_id.
'@'.$stripearrayofkeysbyenv[$servicestatus][
'publishable_key'];
489 $paiement->ext_payment_site = $service;
492 $sql =
"SELECT p.rowid FROM ".MAIN_DB_PREFIX.
"paiement as p";
493 $sql .=
" WHERE p.ext_payment_id = '".$db->escape($paiement->ext_payment_id).
"'";
494 $sql .=
" AND p.ext_payment_site = '".$db->escape($paiement->ext_payment_site).
"'";
495 $result = $db->query($sql);
497 if ($db->num_rows($result)) {
499 dol_syslog(
'* Payment for ext_payment_id '.$paiement->ext_payment_id.
' already done. We do not recreate the payment');
500 dol_syslog(
'* Payment for ext_payment_id '.$paiement->ext_payment_id.
' already done. We do not recreate the payment', LOG_DEBUG, 0,
'_payment');
506 if (!$error && !$ispaymentdone) {
507 dol_syslog(
'* Record payment type PRE for invoice id ' . $invoice_id .
'. It includes closing of invoice and regenerating document.');
508 dol_syslog(
'* Record payment type PRE for invoice id ' . $invoice_id .
'. It includes closing of invoice and regenerating document.', LOG_DEBUG, 0,
'_payment');
511 $paiement_id = $paiement->create($user, 1);
512 if ($paiement_id < 0) {
513 $postactionmessages[] = $paiement->error . ($paiement->error ?
' ' :
'') . implode(
"<br>\n", $paiement->errors);
514 $ispostactionok = -1;
517 dol_syslog(
"Failed to create the payment for invoice id " . $invoice_id);
518 dol_syslog(
"Failed to create the payment for invoice id " . $invoice_id, LOG_DEBUG, 0,
'_payment');
520 $postactionmessages[] =
'Payment created';
522 dol_syslog(
"The payment has been created for invoice id " . $invoice_id);
523 dol_syslog(
"The payment has been created for invoice id " . $invoice_id, LOG_DEBUG, 0,
'_payment');
527 if (!$error && isModEnabled(
'bank')) {
530 $sql =
"SELECT p.rowid, p.fk_bank FROM ".MAIN_DB_PREFIX.
"paiement as p";
531 $sql .=
" WHERE p.ext_payment_id = '".$db->escape($paiement->ext_payment_id).
"'";
532 $sql .=
" AND p.ext_payment_site = '".$db->escape($paiement->ext_payment_site).
"'";
533 $sql .=
" AND p.fk_bank <> 0";
534 $result = $db->query($sql);
536 if ($db->num_rows($result)) {
538 $obj = $db->fetch_object($result);
539 dol_syslog(
'* Payment already linked to bank record '.$obj->fk_bank.
' . We do not recreate the link');
540 dol_syslog(
'* Payment already linked to bank record '.$obj->fk_bank.
' . We do not recreate the link', LOG_DEBUG, 0,
'_payment');
543 if (!$ispaymentdone) {
545 dol_syslog(
'* Add payment to bank', LOG_DEBUG, 0,
'_payment');
548 $paymentmethod =
'stripe';
551 if ($bankaccountid > 0) {
552 $label =
'(CustomerInvoicePayment)';
553 $result = $paiement->addPaymentToBank($user,
'payment', $label, $bankaccountid, $customer_id,
'');
555 $postactionmessages[] = $paiement->error . ($paiement->error ?
' ' :
'') . implode(
"<br>\n", $paiement->errors);
556 $ispostactionok = -1;
559 $postactionmessages[] =
'Bank transaction of payment created (by ipn.php file)';
562 $postactionmessages[] =
'Setup of bank account to use in module ' . $paymentmethod .
' was not set. No way to record the payment.';
563 $ispostactionok = -1;
569 if (!$error && isModEnabled(
'prelevement')) {
572 $sql =
"SELECT dp.fk_prelevement_bons as idbon";
573 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as dp";
574 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"prelevement_bons as pb";
575 $sql .=
" ON pb.rowid = dp.fk_prelevement_bons";
576 $sql .=
" WHERE dp.fk_facture = ".((int) $invoice_id);
577 $sql .=
" AND dp.sourcetype = 'facture'";
578 $sql .=
" AND dp.ext_payment_id = '".$db->escape($TRANSACTIONID).
"'";
579 $sql .=
" AND dp.traite = 1";
580 $sql .=
" AND statut = ".((int) $bon::STATUS_TRANSFERED);
581 $result = $db->query($sql);
583 if ($db->num_rows($result)) {
584 $obj = $db->fetch_object($result);
585 $idbon = $obj->idbon;
586 dol_syslog(
'* Prelevement must be set to credited');
587 dol_syslog(
'* Prelevement must be set to credited', LOG_DEBUG, 0,
'_payment');
589 dol_syslog(
'* Prelevement not found or already credited');
590 dol_syslog(
'* Prelevement not found or already credited', LOG_DEBUG, 0,
'_payment');
593 $postactionmessages[] = $db->lasterror();
594 $ispostactionok = -1;
598 if (!$error && !empty($idbon)) {
599 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_bons";
600 $sql .=
" SET fk_user_credit = ".((int) $user->id);
601 $sql .=
", statut = ".((int) $bon::STATUS_CREDITED);
602 $sql .=
", date_credit = '".$db->idate($now).
"'";
603 $sql .=
", credite = 1";
604 $sql .=
" WHERE rowid = ".((int) $idbon);
605 $sql .=
" AND statut = ".((int) $bon::STATUS_TRANSFERED);
607 $result = $db->query($sql);
609 $postactionmessages[] = $db->lasterror();
610 $ispostactionok = -1;
615 if (!$error && !empty($idbon)) {
616 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_lignes";
617 $sql .=
" SET statut = 2";
618 $sql .=
" WHERE fk_prelevement_bons = ".((int) $idbon);
619 $result = $db->query($sql);
621 $postactionmessages[] = $db->lasterror();
622 $ispostactionok = -1;
630 http_response_code(200);
634 http_response_code(500);
638 dol_syslog(
"The payment mode of this payment is ".$paymentTypeCode.
" in Stripe and ".$paymentTypeCodeInDolibarr.
" in Dolibarr. This case is not managed by the IPN");
639 dol_syslog(
"The payment mode of this payment is ".$paymentTypeCode.
" in Stripe and ".$paymentTypeCodeInDolibarr.
" in Dolibarr. This case is not managed by the IPN", LOG_DEBUG, 0,
'_payment');
642 dol_syslog(
"Nothing to do in database because we don't know paymentTypeIdInDolibarr");
643 dol_syslog(
"Nothing to do in database because we don't know paymentTypeIdInDolibarr", LOG_DEBUG, 0,
'_payment');
645} elseif ($event->type ==
'payment_intent.payment_failed') {
647 dol_syslog(
"A try to make a payment has failed");
648 dol_syslog(
"A try to make a payment has failed", LOG_DEBUG, 0,
'_payment');
650 $object = $event->data->object;
651 $ipaddress =
$object->metadata->ipaddress;
652 $currencyCodeType = strtoupper(
$object->currency);
653 $paymentmethodstripeid =
$object->payment_method;
654 $customer_id =
$object->customer;
656 $chargesdataarray = array();
662 $objpaymentmodetype =
'';
663 if (!empty(
$object->charges)) {
664 $chargesdataarray =
$object->charges->data;
665 foreach ($chargesdataarray as $chargesdata) {
666 $objpayid = $chargesdata->id;
667 $objpaydesc = $chargesdata->description;
669 if ($chargesdata->metadata->dol_type ==
'facture') {
670 $objinvoiceid = $chargesdata->metadata->dol_id;
672 $objerrcode = $chargesdata->outcome->reason;
673 $objerrmessage = $chargesdata->outcome->seller_message;
675 $objpaymentmodetype = $chargesdata->payment_method_details->type;
679 if (!empty(
$object->last_payment_error)) {
681 $objpayid =
$object->latest_charge;
682 $objpaydesc =
$object->description;
684 if (
$object->metadata->dol_type ==
'facture') {
685 $objinvoiceid =
$object->metadata->dol_id;
687 $objerrcode = empty(
$object->last_payment_error->code) ?
$object->last_payment_error->decline_code :
$object->last_payment_error->code;
688 $objerrmessage =
$object->last_payment_error->message;
690 $objpaymentmodetype =
$object->last_payment_error->payment_method->type;
693 dol_syslog(
"objpayid=".$objpayid.
" objpaymentmodetype=".$objpaymentmodetype.
" objerrcode=".$objerrcode);
694 dol_syslog(
"objpayid=".$objpayid.
" objpaymentmodetype=".$objpaymentmodetype.
" objerrcode=".$objerrcode, LOG_DEBUG, 0,
'_payment');
697 if ($objpaymentmodetype ==
'sepa_debit') {
700 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
703 if ($objinvoiceid > 0) {
704 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
706 $invoice->fetch($objinvoiceid);
708 $actioncomm->userownerid = 0;
709 $actioncomm->percentage = -1;
711 $actioncomm->type_code =
'AC_OTH_AUTO';
712 $actioncomm->code =
'AC_IPN';
714 $actioncomm->datep = $now;
715 $actioncomm->datef = $now;
717 $actioncomm->socid = $invoice->socid;
718 $actioncomm->fk_project = $invoice->fk_project;
719 $actioncomm->fk_element = $invoice->id;
720 $actioncomm->elementtype =
'invoice';
724 $actioncomm->note_private =
'Error returned on payment id '.$objpayid.
' after SEPA payment request '.$objpaydesc.
'<br>Error code is: '.$objerrcode.
'<br>Error message is: '.$objerrmessage;
725 $actioncomm->label =
'Payment error (SEPA Stripe)';
727 $result = $actioncomm->create($user);
730 dol_syslog($actioncomm->error, LOG_ERR, 0,
'_payment');
738 http_response_code(500);
742} elseif ($event->type ==
'checkout.session.completed') {
744} elseif ($event->type ==
'payment_method.attached') {
745 dol_syslog(
"object = ".var_export($event->data,
true));
746 dol_syslog(
"object = ".var_export($event->data,
true), LOG_DEBUG, 0,
'_payment');
749 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companypaymentmode.class.php';
750 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
755 $idthirdparty = $societeaccount->getThirdPartyID($db->escape($event->data->object->customer),
'stripe', $servicestatus);
756 if ($idthirdparty > 0) {
759 $companypaymentmode->stripe_card_ref = $event->data->object->id;
760 $companypaymentmode->fk_soc = $idthirdparty;
761 $companypaymentmode->bank =
null;
762 $companypaymentmode->label =
'';
763 $companypaymentmode->number = $event->data->object->id;
764 $companypaymentmode->last_four = $event->data->object->card->last4;
765 $companypaymentmode->card_type = $event->data->object->card->branding;
767 $companypaymentmode->owner_name = $event->data->object->billing_details->name;
768 $companypaymentmode->proprio = $companypaymentmode->owner_name;
770 $companypaymentmode->exp_date_month = (int) $event->data->object->card->exp_month;
771 $companypaymentmode->exp_date_year = (int) $event->data->object->card->exp_year;
772 $companypaymentmode->cvn =
null;
773 $companypaymentmode->datec = $event->data->object->created;
774 $companypaymentmode->default_rib = 0;
775 $companypaymentmode->type = $event->data->object->type;
776 $companypaymentmode->country_code = $event->data->object->card->country;
777 $companypaymentmode->status = $servicestatus;
783 $result = $companypaymentmode->create($user);
791 http_response_code(500);
796} elseif ($event->type ==
'payment_method.updated') {
797 dol_syslog(
"object = ".var_export($event->data,
true));
798 dol_syslog(
"object = ".var_export($event->data,
true), LOG_DEBUG, 0,
'_payment');
801 require_once DOL_DOCUMENT_ROOT.
'/societe/class/companypaymentmode.class.php';
803 $companypaymentmode->fetch(0,
'', 0,
'',
" AND stripe_card_ref = '".$db->escape($event->data->object->id).
"'");
804 if ($companypaymentmode->id > 0) {
806 $companypaymentmode->bank =
null;
807 $companypaymentmode->label =
'';
808 $companypaymentmode->number = $db->escape($event->data->object->id);
809 $companypaymentmode->last_four = $db->escape($event->data->object->card->last4);
810 $companypaymentmode->proprio = $db->escape($event->data->object->billing_details->name);
811 $companypaymentmode->owner_name = $db->escape($event->data->object->billing_details->name);
812 $companypaymentmode->exp_date_month = (int) $event->data->object->card->exp_month;
813 $companypaymentmode->exp_date_year = (int) $event->data->object->card->exp_year;
814 $companypaymentmode->cvn =
null;
815 $companypaymentmode->datec = (int) $event->data->object->created;
816 $companypaymentmode->default_rib = 0;
817 $companypaymentmode->type = $db->escape($event->data->object->type);
818 $companypaymentmode->country_code = $db->escape($event->data->object->card->country);
819 $companypaymentmode->status = $servicestatus;
823 $result = $companypaymentmode->update($user);
834} elseif ($event->type ==
'payment_method.detached') {
837 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"societe_rib WHERE number = '".$db->escape($event->data->object->id).
"' and status = ".((int) $servicestatus);
840} elseif ($event->type ==
'charge.succeeded') {
842} elseif ($event->type ==
'charge.failed') {
844} elseif (($event->type ==
'source.chargeable') && ($event->data->object->type ==
'three_d_secure') && ($event->data->object->three_d_secure->authenticated ==
true)) {
846} elseif ($event->type ==
'charge.dispute.closed') {
848 dol_syslog(
"object = ".var_export($event->data,
true));
849 dol_syslog(
"object = ".var_export($event->data,
true), LOG_DEBUG, 0,
'_payment');
850} elseif ($event->type ==
'charge.dispute.funds_withdrawn') {
852 dol_syslog(
"object = ".var_export($event->data,
true));
853 dol_syslog(
"object = ".var_export($event->data,
true), LOG_DEBUG, 0,
'_payment');
855 global $stripearrayofkeysbyenv;
858 $object = $event->data->object;
859 $TRANSACTIONID =
$object->payment_intent;
860 $ipaddress =
$object->metadata->ipaddress;
862 $currencyCodeType = strtoupper(
$object->currency);
863 $paymentmethodstripeid =
$object->payment_method;
864 $customer_id =
$object->customer;
866 $amountdisputestripe =
$object->amoutndispute;
867 $amountdispute = $amountdisputestripe;
870 $paymentTypeCode =
"";
871 $paymentTypeCodeInDolibarr =
"";
873 $payment_amountInDolibarr = 0;
875 dol_syslog(
"Try to find the payment in database for the payment_intent id = ".$TRANSACTIONID);
876 dol_syslog(
"Try to find the payment in database for the payment_intent id = ".$TRANSACTIONID, LOG_DEBUG, 0,
'_payment');
878 $sql =
"SELECT pi.rowid, pi.fk_facture, pi.fk_prelevement_bons, pi.amount, pi.type, pi.traite";
879 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
880 $sql .=
" WHERE pi.ext_payment_id = '".$db->escape($TRANSACTIONID).
"'";
881 $sql .=
" AND pi.ext_payment_site = '".$db->escape($service).
"'";
883 $result = $db->query($sql);
885 $obj = $db->fetch_object($result);
887 if ($obj->type ==
'ban') {
890 $invoice_id = $obj->fk_facture;
891 $directdebitorcreditransfer_id = $obj->fk_prelevement_bons;
892 $payment_amountInDolibarr = $obj->amount;
893 $paymentTypeCodeInDolibarr = $obj->type;
895 dol_syslog(
"Found the payment intent for ban in database (pdid = ".$pdid.
" directdebitorcreditransfer_id=".$directdebitorcreditransfer_id.
")");
896 dol_syslog(
"Found the payment intent for ban in database (pdid = ".$pdid.
" directdebitorcreditransfer_id=".$directdebitorcreditransfer_id.
")", LOG_DEBUG, 0,
'_payment');
898 if ($obj->type ==
'card' || empty($obj->type)) {
901 $invoice_id = $obj->fk_facture;
902 $directdebitorcreditransfer_id = 0;
903 $payment_amountInDolibarr = $obj->amount;
904 $paymentTypeCodeInDolibarr = empty($obj->type) ?
'card' : $obj->type;
906 dol_syslog(
"Found the payment intent for card in database (pdid = ".$pdid.
" directdebitorcreditransfer_id=".$directdebitorcreditransfer_id.
")");
907 dol_syslog(
"Found the payment intent for card in database (pdid = ".$pdid.
" directdebitorcreditransfer_id=".$directdebitorcreditransfer_id.
")", LOG_DEBUG, 0,
'_payment');
910 dol_syslog(
"Payment intent ".$TRANSACTIONID.
" not found into database, so ignored.");
911 dol_syslog(
"Payment intent ".$TRANSACTIONID.
" not found into database, so ignored.", LOG_DEBUG, 0,
'_payment');
912 http_response_code(200);
913 print
"Payment intent ".$TRANSACTIONID.
" not found into database, so ignored.";
917 http_response_code(500);
918 print $db->lasterror();
923 dol_syslog(
"objinvoiceid=".$invoice_id, LOG_DEBUG, 0,
'_payment');
924 $tmpinvoice =
new Facture($db);
925 $tmpinvoice->fetch($invoice_id);
926 $tmpinvoice->fetch_thirdparty();
928 dol_syslog(
"The payment disputed is ".$amountdispute.
" and the invoice is ".$payment_amountInDolibarr);
929 dol_syslog(
"The payment disputed is ".$amountdispute.
" and the invoice is ".$payment_amountInDolibarr, LOG_DEBUG, 0,
'_payment');
931 if ($amountdispute != $payment_amountInDolibarr) {
932 http_response_code(500);
933 print
"The payment disputed is ".$amountdispute.
" and the invoice is ".$payment_amountInDolibarr.
". Amount differs, we don't know what to do.";
937 $accountfrom =
new Account($db);
938 $accountfrom->fetch(
getDolGlobalInt(
'STRIPE_BANK_ACCOUNT_FOR_PAYMENTS'));
944 $amounts[$tmpinvoice->id] = -1 * $payment_amountInDolibarr;
946 $paiement->datepaye =
dol_now();
947 $paiement->amounts = $amounts;
952 $paiement->paiementid =
dol_getIdFromCode($db,
'PRE',
'c_paiement',
'code',
'id', 1);
953 $paiement->num_payment =
$object->id;
954 $paiement->note_public =
'Fund withdrawn by bank. Reason: '.$reason;
955 $paiement->note_private =
'';
956 $paiement->fk_account = $accountfrom->id;
960 $alreadytransferedinaccounting = $tmpinvoice->getVentilExportCompta();
962 if ($alreadytransferedinaccounting) {
965 $errormsg =
'Error: the invoice '.$tmpinvoice->id.
' is already transferred into accounting. Don\'t know what to do.';
973 $errormsg = $tmpinvoice->error.implode(
', ', $tmpinvoice->errors);
979 $paiement_id = $paiement->create($user, 0, $tmpinvoice->thirdparty);
980 if ($paiement_id < 0) {
981 $errormsg = $paiement->error.implode(
', ', $paiement->errors);
989 http_response_code(500);
993 http_response_code(500);
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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 manage agenda events (actions)
Class to manage withdrawal receipts.
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class for CompanyPaymentMode.
Class to manage invoices.
const STATUS_VALIDATED
Validated (need to be paid)
const STATUS_CLOSED
Classified paid.
Class to manage payments of customer invoices.
Class for SocieteAccount.
Stripe class @TODO No reason to extends CommonObject.
Class to manage Dolibarr users.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='', $useCache=true)
Return an id or code from a code or id.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $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.
getUserRemoteIP($trusted=0)
Return the real IP of remote user.
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.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.