From b5ffe6204fe6df0d14215e32b6cb04f110c00873 Mon Sep 17 00:00:00 2001 From: hystepik Date: Fri, 10 Feb 2023 15:30:36 +0100 Subject: [PATCH] push with ipn not working --- .../class/bonprelevement.class.php | 2 +- htdocs/core/class/commoninvoice.class.php | 67 ++++++++++--------- htdocs/langs/en_US/bills.lang | 2 - htdocs/public/stripe/ipn.php | 48 ++++++++++++- 4 files changed, 83 insertions(+), 36 deletions(-) diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php index b9d1fd7c54a..bbfc694d414 100644 --- a/htdocs/compta/prelevement/class/bonprelevement.class.php +++ b/htdocs/compta/prelevement/class/bonprelevement.class.php @@ -106,7 +106,7 @@ class BonPrelevement extends CommonObject */ public function __construct($db) { - global $conf, $langs; + global $conf, $langs, $db; $this->db = $db; diff --git a/htdocs/core/class/commoninvoice.class.php b/htdocs/core/class/commoninvoice.class.php index 2063bb59bf9..41d76310779 100644 --- a/htdocs/core/class/commoninvoice.class.php +++ b/htdocs/core/class/commoninvoice.class.php @@ -92,10 +92,6 @@ abstract class CommonInvoice extends CommonObject */ const STATUS_ABANDONED = 3; - /** - * Classified processing because payment requested - */ - const STATUS_PROCESSING = 4; public $totalpaid; // duplicate with sumpayed public $totaldeposits; // duplicate with sumdeposit @@ -618,10 +614,6 @@ abstract class CommonInvoice extends CommonObject $labelStatus = $langs->transnoentitiesnoconv('BillStatusClosedPaidPartially'); $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusClosedPaidPartially'); $statusType = 'status9'; - } elseif ($status == 4) { - $labelStatus = $langs->transnoentitiesnoconv('BillStatusRequestProcessing'); - $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusRequestProcessing'); - $statusType = 'status3'; } elseif ($alreadypaid == 0) { $labelStatus = $langs->transnoentitiesnoconv('BillStatusNotPaid'); $labelStatusShort = $langs->transnoentitiesnoconv('Bill'.$prefix.'StatusNotPaid'); @@ -1197,13 +1189,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; - - $result =$this->setStatut($this::STATUS_PROCESSING); - if ($result < 0) { - $error++; - $errorforinvoice++; - $this->errors[] = 'Error on invoice status change to processing'; - } } $object = $this; @@ -1457,24 +1442,42 @@ 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($db); + $nbinvoices = $bon->create(); + if ($nbinvoices <= 0) { $error++; + $errorforinvoice++; + dol_syslog("Error on BonPrelevement creation", LOG_ERR); + $this->errors[] = "Error on BonPrelevement creation"; + } + + $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/langs/en_US/bills.lang b/htdocs/langs/en_US/bills.lang index 40adce33c66..183620c72da 100644 --- a/htdocs/langs/en_US/bills.lang +++ b/htdocs/langs/en_US/bills.lang @@ -148,8 +148,6 @@ BillShortStatusNotPaid=Not paid BillShortStatusNotRefunded=Not refunded BillShortStatusClosedUnpaid=Closed BillShortStatusClosedPaidPartially=Paid (partially) -BillStatusRequestProcessing=Payment request processing -BillShortStatusRequestProcessing=Processing PaymentStatusToValidShort=To validate ErrorVATIntraNotConfigured=Intra-Community VAT number not yet defined ErrorNoPaiementModeConfigured=No default payment type defined. Go to Invoice module setup to fix this. diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php index b41149a88f8..de4ed61f802 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'; @@ -399,6 +399,52 @@ if ($event->type == 'payout.created') { $errorforinvoice++; } } + + if (!$errorforinvoice && isModEnabled('prelevement')) { //TEST IPN + $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 = ".$bon::STATUS_CREDITED; + $sql .= ", date_credit = '".$db->idate($now)."'"; + $sql .= ", credite = 1"; + $sql .= " WHERE rowid=".((int) $bon->id); + $sql .= " AND entity = ".((int) $bon->entity); + $sql .= " AND statut = ".$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).