From 5b52dc9f0470d3ae64de8cc13ebb8ebdfb60350e Mon Sep 17 00:00:00 2001 From: hystepik Date: Tue, 31 Jan 2023 10:43:58 +0100 Subject: [PATCH 1/7] Fix : stripe sepa payment process --- htdocs/core/class/commoninvoice.class.php | 137 +++------------------- htdocs/langs/en_US/bills.lang | 2 + htdocs/public/stripe/ipn.php | 101 +++++++++++++++- htdocs/stripe/class/stripe.class.php | 24 ++-- 4 files changed, 129 insertions(+), 135 deletions(-) diff --git a/htdocs/core/class/commoninvoice.class.php b/htdocs/core/class/commoninvoice.class.php index d1eb324cc7f..0f38b39e0af 100644 --- a/htdocs/core/class/commoninvoice.class.php +++ b/htdocs/core/class/commoninvoice.class.php @@ -92,6 +92,10 @@ 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 @@ -610,6 +614,10 @@ 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'); @@ -900,9 +908,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); @@ -1186,126 +1194,11 @@ 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 + $result =$this->setStatut($this::STATUS_PROCESSING); + if ($result < 0) { $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; + $this->errors[] = 'Error on invoice status change to processing'; } } @@ -1563,13 +1456,13 @@ abstract class CommonInvoice extends CommonObject $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 = "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); + $sql .= " WHERE rowid = ".((int) $did); dol_syslog(get_class($this)."::makeStripeSepaRequest", LOG_DEBUG); diff --git a/htdocs/langs/en_US/bills.lang b/htdocs/langs/en_US/bills.lang index 183620c72da..40adce33c66 100644 --- a/htdocs/langs/en_US/bills.lang +++ b/htdocs/langs/en_US/bills.lang @@ -148,6 +148,8 @@ 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 dd5b1db148f..b41149a88f8 100644 --- a/htdocs/public/stripe/ipn.php +++ b/htdocs/public/stripe/ipn.php @@ -72,7 +72,9 @@ if (isset($_GET['connect'])) { $servicestatus = 1; } } - +$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_KEY; +$service = 'StripeTest'; +$servicestatus = 0; if (empty($conf->stripe->enabled)) { httponly_accessforbidden('Module Stripe not enabled'); } @@ -300,6 +302,103 @@ 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++; + } + } } 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 bc98a085d08..0684fb370b7 100644 --- a/htdocs/stripe/class/stripe.class.php +++ b/htdocs/stripe/class/stripe.class.php @@ -964,10 +964,9 @@ class Stripe extends CommonObject $dataforcard = array( 'type'=>'sepa_debit', "sepa_debit" => array('iban' => $iban), - 'currency' => strtolower($conf->currency), - 'usage' => 'reusable', - 'owner' => array( + 'billing_details' => array( 'name' => $soc->name, + 'email' => !empty($soc->email) ? $soc->email : "", ), "metadata" => array( 'dol_type'=>$object->element, @@ -979,19 +978,19 @@ class Stripe extends CommonObject ); // Complete owner name if (!empty($soc->town)) { - $dataforcard['owner']['address']['city']=$soc->town; + $dataforcard['billing_details']['address']['city']=$soc->town; } if (!empty($soc->country_code)) { - $dataforcard['owner']['address']['country']=$soc->country_code; + $dataforcard['billing_details']['address']['country']=$soc->country_code; } if (!empty($soc->address)) { - $dataforcard['owner']['address']['line1']=$soc->address; + $dataforcard['billing_details']['address']['line1']=$soc->address; } if (!empty($soc->zip)) { - $dataforcard['owner']['address']['postal_code']=$soc->zip; + $dataforcard['billing_details']['address']['postal_code']=$soc->zip; } if (!empty($soc->state)) { - $dataforcard['owner']['address']['state']=$soc->state; + $dataforcard['billing_details']['address']['state']=$soc->state; } //$a = \Stripe\Stripe::getApiKey(); @@ -1013,14 +1012,14 @@ class Stripe extends CommonObject $s = new \Stripe\StripeClient($stripeacc); - // TODO LMR Deprecated with the new Stripe API and SCA. - // TODO LMR Replace ->create() and ->createSource() and replace with ->getSetupIntent() to then, get the Payment mode with $payment_method = \Stripe\PaymentMethod::retrieve($setupintent->payment_method); ? - $sepa = $s->sources->create($dataforcard); + $sepa = $s->paymentMethods->create($dataforcard); if (!$sepa) { $this->error = 'Creation of sepa_debit on Stripe has failed'; } else { // link customer and src - $cs = $cu->createSource($cu->id, array('source' => $sepa->id)); + $cs = $s->setupIntents->create(['payment_method_types' => ['sepa_debit'], 'customer' => $cu->id]); + $cs = $s->setupIntents->update($cs->id, ['payment_method' => $sepa->id]); + $cs = $s->setupIntents->confirm($cs->id, ['mandate_data' => ['customer_acceptance' => ['type' => 'offline']]]); if (!$cs) { $this->error = 'Link SEPA <-> Customer failed'; } else { @@ -1040,6 +1039,7 @@ class Stripe extends CommonObject } } } catch (Exception $e) { + $sepa = null; $this->error = $e->getMessage(); dol_syslog($this->error, LOG_WARNING); } From b5ffe6204fe6df0d14215e32b6cb04f110c00873 Mon Sep 17 00:00:00 2001 From: hystepik Date: Fri, 10 Feb 2023 15:30:36 +0100 Subject: [PATCH 2/7] 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). From 89f3ba1a322c4949ab741a5bbf5d8736482460ae Mon Sep 17 00:00:00 2001 From: hystepik Date: Fri, 10 Feb 2023 16:12:24 +0100 Subject: [PATCH 3/7] fix ipn --- htdocs/public/stripe/ipn.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php index de4ed61f802..ad51e37c310 100644 --- a/htdocs/public/stripe/ipn.php +++ b/htdocs/public/stripe/ipn.php @@ -400,7 +400,7 @@ if ($event->type == 'payout.created') { } } - if (!$errorforinvoice && isModEnabled('prelevement')) { //TEST IPN + if (!$errorforinvoice && isModEnabled('prelevement')) { $bon = new BonPrelevement($db); $idbon = 0; $sql = "SELECT dp.fk_prelevement_bons as idbon"; @@ -423,13 +423,12 @@ if ($event->type == 'payout.created') { if (!empty($idbon)) { $bon->fetch($idbon); - $sql = " UPDATE ".MAIN_DB_PREFIX."prelevement_bons "; + $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(); From bc0b0514bb4547885d1b27eb682f51d54b2a0a7d Mon Sep 17 00:00:00 2001 From: hystepik Date: Fri, 10 Feb 2023 16:29:39 +0100 Subject: [PATCH 4/7] fix Ci --- htdocs/public/stripe/ipn.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php index ad51e37c310..2c93654829b 100644 --- a/htdocs/public/stripe/ipn.php +++ b/htdocs/public/stripe/ipn.php @@ -425,11 +425,11 @@ if ($event->type == 'payout.created') { $bon->fetch($idbon); $sql = "UPDATE ".MAIN_DB_PREFIX."prelevement_bons"; $sql .= " SET fk_user_credit = ".$user->id; - $sql .= ", statut = ".$bon::STATUS_CREDITED; + $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 = ".$bon::STATUS_TRANSFERED; + $sql .= " AND statut = '".$db->escape($bon::STATUS_TRANSFERED)."'"; $db->begin(); $result = $db->query($sql); From 646c1b15022afe398d3b2fcf6f7f61f57b1b1cf0 Mon Sep 17 00:00:00 2001 From: hystepik Date: Wed, 22 Feb 2023 10:39:19 +0100 Subject: [PATCH 5/7] remove $db --- htdocs/compta/prelevement/class/bonprelevement.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php index bbfc694d414..b9d1fd7c54a 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, $db; + global $conf, $langs; $this->db = $db; From be2ee21462f9e0cc545f5af4431e115afdcc4792 Mon Sep 17 00:00:00 2001 From: hystepik Date: Wed, 22 Feb 2023 15:09:37 +0100 Subject: [PATCH 6/7] creation of prelevement_bon only for 1 demande --- .../prelevement/class/bonprelevement.class.php | 7 +++++-- htdocs/core/class/commoninvoice.class.php | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php index b9d1fd7c54a..7e183f7795d 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 = '".$this->db->escape($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 41d76310779..55824f118aa 100644 --- a/htdocs/core/class/commoninvoice.class.php +++ b/htdocs/core/class/commoninvoice.class.php @@ -1442,8 +1442,8 @@ abstract class CommonInvoice extends CommonObject } // TODO Create a prelevement_bon and set its status to sent instead of this - $bon = new BonPrelevement($db); - $nbinvoices = $bon->create(); + $bon = new BonPrelevement($this->db); + $nbinvoices = $bon->create(0, 0, 'real', 'ALL', '', 0, 'direct-debit', $did); if ($nbinvoices <= 0) { $error++; $errorforinvoice++; @@ -1451,12 +1451,14 @@ abstract class CommonInvoice extends CommonObject $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) { + $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) { From 829e818b6d13db88f30661a557ece1bdb4d71c7d Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 24 Feb 2023 18:06:08 +0100 Subject: [PATCH 7/7] Update bonprelevement.class.php --- htdocs/compta/prelevement/class/bonprelevement.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php index 0db7f85a4ec..0f5546e5e46 100644 --- a/htdocs/compta/prelevement/class/bonprelevement.class.php +++ b/htdocs/compta/prelevement/class/bonprelevement.class.php @@ -813,7 +813,7 @@ class BonPrelevement extends CommonObject $sql .= " AND f.total_ttc > 0"; $sql .= " AND pfd.ext_payment_id IS NULL"; if ($did > 0) { - $sql .= " AND pfd.rowid = '".$this->db->escape($did)."'"; + $sql .= " AND pfd.rowid = ".((int) $did); } dol_syslog(__METHOD__."::Read invoices,", LOG_DEBUG);