diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php
index 8ce3e2a45d7..0f5546e5e46 100644
--- a/htdocs/compta/prelevement/class/bonprelevement.class.php
+++ b/htdocs/compta/prelevement/class/bonprelevement.class.php
@@ -747,9 +747,10 @@ class BonPrelevement extends CommonObject
* @param string $executiondate Date to execute the transfer
* @param int $notrigger Disable triggers
* @param string $type 'direct-debit' or 'bank-transfer'
+ * @param int $did ID of payment request
* @return int <0 if KO, No of invoice included into file if OK
*/
- public function create($banque = 0, $agence = 0, $mode = 'real', $format = 'ALL', $executiondate = '', $notrigger = 0, $type = 'direct-debit')
+ public function create($banque = 0, $agence = 0, $mode = 'real', $format = 'ALL', $executiondate = '', $notrigger = 0, $type = 'direct-debit', $did = 0)
{
// phpcs:enable
global $conf, $langs, $user;
@@ -811,7 +812,9 @@ class BonPrelevement extends CommonObject
$sql .= " AND pfd.traite = 0";
$sql .= " AND f.total_ttc > 0";
$sql .= " AND pfd.ext_payment_id IS NULL";
-
+ if ($did > 0) {
+ $sql .= " AND pfd.rowid = ".((int) $did);
+ }
dol_syslog(__METHOD__."::Read invoices,", LOG_DEBUG);
$resql = $this->db->query($sql);
diff --git a/htdocs/core/class/commoninvoice.class.php b/htdocs/core/class/commoninvoice.class.php
index a7197d7c9ae..9786ad65168 100644
--- a/htdocs/core/class/commoninvoice.class.php
+++ b/htdocs/core/class/commoninvoice.class.php
@@ -911,9 +911,9 @@ abstract class CommonInvoice extends CommonObject
$sql = "SELECT rowid, date_demande, amount, fk_facture, fk_facture_fourn";
$sql .= " FROM ".$this->db->prefix()."prelevement_demande";
- $sql .= " AND fk_facture = ".((int) $this->fk_facture); // Add a protection to not pay another invoice than current one
- $sql .= " AND traite = 0"; // Add a protection to not process twice
$sql .= " WHERE rowid = ".((int) $did);
+ $sql .= " AND fk_facture = ".((int) $this->id); // Add a protection to not pay another invoice than current one
+ $sql .= " AND traite = 0"; // Add a protection to not process twice
dol_syslog(get_class($this)."::makeStripeSepaRequest 1", LOG_DEBUG);
$resql = $this->db->query($sql);
@@ -1196,128 +1196,6 @@ abstract class CommonInvoice extends CommonObject
// Default description used for label of event. Will be overwrite by another value later.
$description = 'Stripe payment request OK (' . $charge->id . ') from makeStripeSepaRequest: ' . $FULLTAG;
-
-
- // @TODO LMR Save request to status pending instead of done. Done should be set with a webhook.
-
-
- $db = $this->db;
-
- $ipaddress = getUserRemoteIP();
-
- $TRANSACTIONID = $charge->id;
- $currency = $conf->currency;
- $paymentmethod = 'stripe';
- $emetteur_name = $charge->customer;
-
- // Same code than into paymentok.php...
-
- $paymentTypeId = 0;
- if ($paymentmethod == 'paybox') {
- $paymentTypeId = $conf->global->PAYBOX_PAYMENT_MODE_FOR_PAYMENTS;
- }
- if ($paymentmethod == 'paypal') {
- $paymentTypeId = $conf->global->PAYPAL_PAYMENT_MODE_FOR_PAYMENTS;
- }
- if ($paymentmethod == 'stripe') {
- $paymentTypeId = $conf->global->STRIPE_PAYMENT_MODE_FOR_PAYMENTS;
- }
- if (empty($paymentTypeId)) {
- //erics
- if ($sepaMode) {
- $paymentType = 'PRE';
- } else {
- $paymentType = $_SESSION["paymentType"];
- if (empty($paymentType)) {
- $paymentType = 'CB';
- }
- }
- $paymentTypeId = dol_getIdFromCode($this->db, $paymentType, 'c_paiement', 'code', 'id', 1);
- }
-
- $currencyCodeType = $currency;
-
- $ispostactionok = 1;
-
- // Creation of payment line
- // TODO LMR This must be move into the stripe server listening hooks public/stripe/ipn.php
- include_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
- $paiement = new Paiement($this->db);
- $paiement->datepaye = $now;
- $paiement->date = $now;
- if ($currencyCodeType == $conf->currency) {
- $paiement->amounts = [$this->id => $amounttopay]; // Array with all payments dispatching with invoice id
- } else {
- $paiement->multicurrency_amounts = [$this->id => $amounttopay]; // Array with all payments dispatching
-
- $postactionmessages[] = 'Payment was done in a different currency than currency expected of company';
- $ispostactionok = -1;
- // Not yet supported, so error
- $error++;
- $errorforinvoice++;
- }
- $paiement->paiementid = $paymentTypeId;
- $paiement->num_paiement = '';
- $paiement->num_payment = '';
- // Add a comment with keyword 'SellYourSaas' in text. Used by trigger.
- $paiement->note_public = 'StripeSepa payment ' . dol_print_date($now, 'standard') . ' using ' . $paymentmethod . ($ipaddress ? ' from ip ' . $ipaddress : '') . ' - Transaction ID = ' . $TRANSACTIONID;
- $paiement->note_private = 'StripeSepa payment ' . dol_print_date($now, 'standard') . ' using ' . $paymentmethod . ($ipaddress ? ' from ip ' . $ipaddress : '') . ' - Transaction ID = ' . $TRANSACTIONID;
- $paiement->ext_payment_id = $charge->id . ':' . $customer->id . '@' . $stripearrayofkeys['publishable_key'];
- $paiement->ext_payment_site = 'stripe';
-
- if (!$errorforinvoice) {
- dol_syslog('* Record payment for invoice id ' . $this->id . '. It includes closing of invoice and regenerating document');
-
- // This include closing invoices to 'paid' (and trigger including unsuspending) and regenerating document
- $paiement_id = $paiement->create($user, 1);
- if ($paiement_id < 0) {
- $postactionmessages[] = $paiement->error . ($paiement->error ? ' ' : '') . join("
\n", $paiement->errors);
- $ispostactionok = -1;
- $error++;
- $errorforinvoice++;
- } else {
- $postactionmessages[] = 'Payment created';
- }
-
- dol_syslog("The payment has been created for invoice id " . $this->id);
- }
-
- if (!$errorforinvoice && isModEnabled('banque')) {
- dol_syslog('* Add payment to bank');
-
- // The bank used is the one defined into Stripe setup
- $bankaccountid = 0;
- if ($paymentmethod == 'stripe') {
- $bankaccountid = $conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS;
- }
-
- if ($bankaccountid > 0) {
- $label = '(CustomerInvoicePayment)';
- if ($this->type == Facture::TYPE_CREDIT_NOTE) {
- $label = '(CustomerInvoicePaymentBack)';
- } // Refund of a credit note
- $result = $paiement->addPaymentToBank($user, 'payment', $label, $bankaccountid, $emetteur_name, '');
- if ($result < 0) {
- $postactionmessages[] = $paiement->error . ($paiement->error ? ' ' : '') . join("
\n", $paiement->errors);
- $ispostactionok = -1;
- $error++;
- $errorforinvoice++;
- } else {
- $postactionmessages[] = 'Bank transaction of payment created (by makeStripeSepaRequest)';
- }
- } else {
- $postactionmessages[] = 'Setup of bank account to use in module ' . $paymentmethod . ' was not set. No way to record the payment.';
- $ispostactionok = -1;
- $error++;
- $errorforinvoice++;
- }
- }
-
- if ($ispostactionok < 1) {
- $description = 'Stripe payment OK (' . $charge->id . ' - ' . $amounttopay . ' ' . $conf->currency . ') but post action KO from makeStripeSepaRequest: ' . $FULLTAG;
- } else {
- $description = 'Stripe payment+post action OK (' . $charge->id . ' - ' . $amounttopay . ' ' . $conf->currency . ') from makeStripeSepaRequest: ' . $FULLTAG;
- }
}
$object = $this;
@@ -1571,24 +1449,44 @@ abstract class CommonInvoice extends CommonObject
}
// TODO Create a prelevement_bon and set its status to sent instead of this
- $idtransferfile = 0;
-
- // Update the direct debit payment request of the processed invoice to save the id of the prelevement_bon
- $sql = "UPDATE".MAIN_DB_PREFIX."prelevement_demande SET";
- $sql .= " traite = 1,"; // TODO Remove this
- $sql .= " date_traite = '".$this->db->idate(dol_now())."'"; // TODO Remove this
- if ($idtransferfile > 0) {
- $sql .= " fk_prelevement_bons = ".((int) $idtransferfile);
- }
- $sql .= "WHERE rowid = ".((int) $did);
-
-
- dol_syslog(get_class($this)."::makeStripeSepaRequest", LOG_DEBUG);
- $resql = $this->db->query($sql);
- if (!$resql) {
- $this->error = $this->db->lasterror();
- dol_syslog(get_class($this).'::makeStripeSepaRequest Erreur');
+ $bon = new BonPrelevement($this->db);
+ $nbinvoices = $bon->create(0, 0, 'real', 'ALL', '', 0, 'direct-debit', $did);
+ if ($nbinvoices <= 0) {
$error++;
+ $errorforinvoice++;
+ dol_syslog("Error on BonPrelevement creation", LOG_ERR);
+ $this->errors[] = "Error on BonPrelevement creation";
+ }
+
+ if (!$errorforinvoice) {
+ $result = $bon->set_infotrans($user, $now, 'internet');
+ if ($result < 0) {
+ $error++;
+ $errorforinvoice++;
+ dol_syslog("Error on BonPrelevement creation", LOG_ERR);
+ $this->errors[] = "Error on BonPrelevement creation";
+ }
+ }
+
+ if (!$errorforinvoice) {
+ $idtransferfile = $bon->id;
+ // Update the direct debit payment request of the processed invoice to save the id of the prelevement_bon
+ $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_demande SET";
+ $sql .= " traite = 1,"; // TODO Remove this
+ $sql .= " date_traite = '".$this->db->idate(dol_now())."',"; // TODO Remove this
+ if ($idtransferfile > 0) {
+ $sql .= " fk_prelevement_bons = ".((int) $idtransferfile);
+ }
+ $sql .= " WHERE rowid = ".((int) $did);
+
+
+ dol_syslog(get_class($this)."::makeStripeSepaRequest", LOG_DEBUG);
+ $resql = $this->db->query($sql);
+ if (!$resql) {
+ $this->error = $this->db->lasterror();
+ dol_syslog(get_class($this).'::makeStripeSepaRequest Erreur');
+ $error++;
+ }
}
} else {
$this->error = 'WithdrawRequestErrorNilAmount';
diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php
index 0b4cbe9dd5b..077e008e768 100644
--- a/htdocs/public/stripe/ipn.php
+++ b/htdocs/public/stripe/ipn.php
@@ -43,9 +43,9 @@ require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
+require_once DOL_DOCUMENT_ROOT.'/compta/prelevement/class/bonprelevement.class.php';
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
-
require_once DOL_DOCUMENT_ROOT.'/includes/stripe/stripe-php/init.php';
require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
@@ -300,6 +300,148 @@ if ($event->type == 'payout.created') {
} elseif ($event->type == 'payment_intent.succeeded') { // Called when making payment with PaymentIntent method ($conf->global->STRIPE_USE_NEW_CHECKOUT is on).
// TODO: create fees
// TODO: Redirect to paymentok.php
+ include_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
+ $object = $event->data->object;
+ $invoice_id = $object->metadata->dol_id;
+ $ipaddress = $object->metadata->ipaddress;
+ $invoice_amount = $object->amount / 100;
+ $TRANSACTIONID = $object->id;
+ $customer_id = $object->customer;
+ $errorforinvoice = 0;
+ $paymentmethod = 'stripe';
+ $now = dol_now();
+ $currencyCodeType = strtoupper($object->currency);
+ $paymentmethodstripeid = $object->payment_method;
+
+ global $stripearrayofkeysbyenv;
+ $stripeacc = $stripearrayofkeysbyenv[$servicestatus]['secret_key'];
+
+ dol_syslog("Try to create sepa_debit with data = ".json_encode($dataforcard));
+
+ $s = new \Stripe\StripeClient($stripeacc);
+
+ $paymentmethodstripe = $s->paymentMethods->retrieve($paymentmethodstripeid);
+
+ //$paymentTypeId = $conf->global->STRIPE_PAYMENT_MODE_FOR_PAYMENTS;
+ $paymentTypeId = $paymentmethodstripe->type;
+ if ($paymentTypeId == "sepa_debit") {
+ $paymentTypeId = "BANCON";
+ } elseif ($paymentTypeId == "card") {
+ $paymentTypeId = "CB";
+ }
+
+ $paiement = new Paiement($db);
+ $paiement->datepaye = $now;
+ $paiement->date = $now;
+ if ($currencyCodeType == $conf->currency) {
+ $paiement->amounts = [$invoice_id => $invoice_amount]; // Array with all payments dispatching with invoice id
+ } else {
+ $paiement->multicurrency_amounts = [$invoice_id => $invoice_amount]; // Array with all payments dispatching
+
+ $postactionmessages[] = 'Payment was done in a different currency than currency expected of company';
+ $ispostactionok = -1;
+ // Not yet supported, so error
+ $error++;
+ $errorforinvoice++;
+ }
+ $paiement->paiementid = $paymentTypeId;
+ $paiement->num_paiement = '';
+ $paiement->num_payment = '';
+ // Add a comment with keyword 'SellYourSaas' in text. Used by trigger.
+ $paiement->note_public = 'StripeSepa payment ' . dol_print_date($now, 'standard') . ' using ' . $paymentmethod . ($ipaddress ? ' from ip ' . $ipaddress : '') . ' - Transaction ID = ' . $TRANSACTIONID;
+ $paiement->note_private = 'StripeSepa payment ' . dol_print_date($now, 'standard') . ' using ' . $paymentmethod . ($ipaddress ? ' from ip ' . $ipaddress : '') . ' - Transaction ID = ' . $TRANSACTIONID;
+ $paiement->ext_payment_id = $TRANSACTIONID . ':' . $customer_id . '@' . $stripearrayofkeysbyenv[$servicestatus]['publishable_key'];
+ $paiement->ext_payment_site = 'stripe';
+
+ if (!$errorforinvoice) {
+ dol_syslog('* Record payment for invoice id ' . $invoice_id . '. It includes closing of invoice and regenerating document');
+
+ // This include closing invoices to 'paid' (and trigger including unsuspending) and regenerating document
+ $paiement_id = $paiement->create($user, 1);
+ if ($paiement_id < 0) {
+ $postactionmessages[] = $paiement->error . ($paiement->error ? ' ' : '') . join("
\n", $paiement->errors);
+ $ispostactionok = -1;
+ $error++;
+ $errorforinvoice++;
+ } else {
+ $postactionmessages[] = 'Payment created';
+ }
+
+ dol_syslog("The payment has been created for invoice id " . $invoice_id);
+ }
+
+ if (!$errorforinvoice && isModEnabled('banque')) {
+ dol_syslog('* Add payment to bank');
+
+ // The bank used is the one defined into Stripe setup
+ $bankaccountid = 0;
+ if ($paymentmethod == 'stripe') {
+ $bankaccountid = $conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS;
+ }
+
+ if ($bankaccountid > 0) {
+ $label = '(CustomerInvoicePayment)';
+ $result = $paiement->addPaymentToBank($user, 'payment', $label, $bankaccountid, $customer_id, '');
+ if ($result < 0) {
+ $postactionmessages[] = $paiement->error . ($paiement->error ? ' ' : '') . join("
\n", $paiement->errors);
+ $ispostactionok = -1;
+ $error++;
+ $errorforinvoice++;
+ } else {
+ $postactionmessages[] = 'Bank transaction of payment created (by makeStripeSepaRequest)';
+ }
+ } else {
+ $postactionmessages[] = 'Setup of bank account to use in module ' . $paymentmethod . ' was not set. No way to record the payment.';
+ $ispostactionok = -1;
+ $error++;
+ $errorforinvoice++;
+ }
+ }
+
+ if (!$errorforinvoice && isModEnabled('prelevement')) {
+ $bon = new BonPrelevement($db);
+ $idbon = 0;
+ $sql = "SELECT dp.fk_prelevement_bons as idbon";
+ $sql .= " FROM ".MAIN_DB_PREFIX."prelevement_demande as dp";
+ $sql .= " WHERE dp.fk_facture = '".$db->escape($invoice_id)."'";
+ $sql .= " AND dp.sourcetype = 'facture'";
+ $sql .= " AND dp.traite = 1";
+ $result = $db->query($sql);
+ if ($result) {
+ if ($db->num_rows($result)) {
+ $obj = $db->fetch_object($result);
+ $idbon = $obj->idbon;
+ }
+ } else {
+ $postactionmessages[] = $db->lasterror();
+ $ispostactionok = -1;
+ $error++;
+ $errorforinvoice++;
+ }
+
+ if (!empty($idbon)) {
+ $bon->fetch($idbon);
+ $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons";
+ $sql .= " SET fk_user_credit = ".$user->id;
+ $sql .= ", statut = '".$db->escape($bon::STATUS_CREDITED)."'";
+ $sql .= ", date_credit = '".$db->idate($now)."'";
+ $sql .= ", credite = 1";
+ $sql .= " WHERE rowid=".((int) $bon->id);
+ $sql .= " AND statut = '".$db->escape($bon::STATUS_TRANSFERED)."'";
+
+ $db->begin();
+ $result = $db->query($sql);
+ if (!$result) {
+ $db->rollback();
+ $postactionmessages[] = $db->lasterror();
+ $ispostactionok = -1;
+ $error++;
+ $errorforinvoice++;
+ } else {
+ $db->commit();
+ }
+ }
+ }
} elseif ($event->type == 'payment_intent.payment_failed') {
// TODO: Redirect to paymentko.php
} elseif ($event->type == 'checkout.session.completed') { // Called when making payment with new Checkout method ($conf->global->STRIPE_USE_NEW_CHECKOUT is on).
diff --git a/htdocs/stripe/class/stripe.class.php b/htdocs/stripe/class/stripe.class.php
index 7f66ae05383..5c49868b8c8 100644
--- a/htdocs/stripe/class/stripe.class.php
+++ b/htdocs/stripe/class/stripe.class.php
@@ -1015,7 +1015,9 @@ class Stripe extends CommonObject
dol_syslog("Try to create sepa_debit with data = ".json_encode($dataforcard));
$s = new \Stripe\StripeClient($stripeacc);
+
//var_dump($dataforcard);exit;
+
$sepa = $s->paymentMethods->create($dataforcard);
if (!$sepa) {
$this->error = 'Creation of payment method sepa_debit on Stripe has failed';