From 0721c8b5d8e558dd7519a56e47cf6fbc3571df69 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 20 Jan 2023 15:14:44 +0100 Subject: [PATCH] Clean code --- htdocs/core/class/commoninvoice.class.php | 27 +++++++++++++++----- htdocs/stripe/class/stripe.class.php | 31 ++++++++++++++++++----- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/htdocs/core/class/commoninvoice.class.php b/htdocs/core/class/commoninvoice.class.php index 93d125291ef..17c9c7deae3 100644 --- a/htdocs/core/class/commoninvoice.class.php +++ b/htdocs/core/class/commoninvoice.class.php @@ -865,7 +865,7 @@ abstract class CommonInvoice extends CommonObject /** - * Create a withdrawal request at Stripe for a direct debit order or a credit transfer order. + * Create a withdrawal request, from a prelevement_demande, to Stripe for a direct debit order or a credit transfer order. * Use the remain to pay excluding all existing open direct debit requests. * * @param User $fuser User asking the direct debit transfer @@ -901,6 +901,7 @@ 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); dol_syslog(get_class($this)."::makeStripeSepaRequest 1", LOG_DEBUG); @@ -1077,10 +1078,11 @@ abstract class CommonInvoice extends CommonObject $stripecard = null; if ($companypaymentmode->type == 'ban') { $sepaMode = true; + // Check into societe_rib if a payment mode for Stripe and ban payment exists $stripecard = $stripe->sepaStripe($customer, $companypaymentmode, $stripeacc, $servicestatus, 0); } - if ($stripecard) { // Can be src_... (for sepa) - Other card_... (old mode) or pm_... (new mode) should not happen here. + if ($stripecard) { // Can be src_... (for sepa). Note that card_... (old card mode) or pm_... (new card mode) should not happen here. $FULLTAG = 'INV=' . $this->id . '-CUS=' . $thirdparty->id; $description = 'Stripe payment from makeStripeSepaRequest: ' . $FULLTAG . ' ref=' . $this->ref; @@ -1128,6 +1130,7 @@ abstract class CommonInvoice extends CommonObject //var_dump("stripefailurecode=".$stripefailurecode." stripefailuremessage=".$stripefailuremessage." stripefailuredeclinecode=".$stripefailuredeclinecode); //exit; + // Return $charge = array('id'=>'ch_XXXX', 'status'=>'succeeded|pending|failed', 'failure_code'=>, 'failure_message'=>...) if (empty($charge) || $charge->status == 'failed') { dol_syslog('Failed to charge payment mode ' . $stripecard->id . ' stripefailurecode=' . $stripefailurecode . ' stripefailuremessage=' . $stripefailuremessage . ' stripefailuredeclinecode=' . $stripefailuredeclinecode, LOG_WARNING); @@ -1179,7 +1182,11 @@ abstract class CommonInvoice extends CommonObject $this->stripechargedone++; // Default description used for label of event. Will be overwrite by another value later. - $description = 'Stripe payment OK (' . $charge->id . ') from makeStripeSepaRequest: ' . $FULLTAG; + $description = 'Stripe payment request OK (' . $charge->id . ') from makeStripeSepaRequest: ' . $FULLTAG; + + + // TODO Save request to status pending. Done should be set with a webhook. + $db = $this->db; @@ -1549,13 +1556,19 @@ abstract class CommonInvoice extends CommonObject $this->errors[] = "Remain to pay is null for the invoice " . $this->id . " " . $this->ref . ". Why is the invoice not classified 'Paid' ?"; } - // TODO Create a prelevement_bon ? - // For the moment no + // TODO Create a prelevement_bon and set its status to sent instead of this + $idtransferfile = 0; - // We must update the direct debit payment request as "done" - $sql = "UPDATE".MAIN_DB_PREFIX."prelevement_demande SET traite = 1, date_traite = '".$this->db->idate(dol_now())."'"; + // 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) { diff --git a/htdocs/stripe/class/stripe.class.php b/htdocs/stripe/class/stripe.class.php index 972d2e25c23..942d5d8d275 100644 --- a/htdocs/stripe/class/stripe.class.php +++ b/htdocs/stripe/class/stripe.class.php @@ -602,7 +602,7 @@ class Stripe extends CommonObject * but not when using the STRIPE_USE_NEW_CHECKOUT. * * @param string $description Description - * @param Societe $object Object to pay with Stripe + * @param Societe $object Object of company to link the Stripe payment mode with * @param string $customer Stripe customer ref 'cus_xxxxxxxxxxxxx' via customerStripe() * @param string $key ''=Use common API. If not '', it is the Stripe connect account 'acc_....' to use Stripe connect * @param int $status Status (0=test, 1=live) @@ -813,8 +813,21 @@ class Stripe extends CommonObject $ipaddress = getUserRemoteIP(); $dataforcard = array( - "source" => array('object'=>'card', 'exp_month'=>$exp_date_month, 'exp_year'=>$exp_date_year, 'number'=>$number, 'cvc'=>$cvc, 'name'=>$cardholdername), - "metadata" => array('dol_id'=>$object->id, 'dol_version'=>DOL_VERSION, 'dol_entity'=>$conf->entity, 'ipaddress'=>$ipaddress) + "source" => array( + 'object'=>'card', + 'exp_month'=>$exp_date_month, + 'exp_year'=>$exp_date_year, + 'number'=>$number, + 'cvc'=>$cvc, + 'name'=>$cardholdername + ), + "metadata" => array( + 'dol_type'=>$object->element, + 'dol_id'=>$object->id, + 'dol_version'=>DOL_VERSION, + 'dol_entity'=>$conf->entity, + 'ipaddress'=>$ipaddress + ) ); //$a = \Stripe\Stripe::getApiKey(); @@ -898,8 +911,8 @@ class Stripe extends CommonObject * @param CompanyPaymentMode $object Object companypaymentmode to check, or create on stripe (create on stripe also update the societe_rib table for current entity) * @param string $stripeacc ''=Use common API. If not '', it is the Stripe connect account 'acc_....' to use Stripe connect * @param int $status Status (0=test, 1=live) - * @param int $createifnotlinkedtostripe 1=Create the stripe sepa and the link if the sepa is not yet linked to a stripe sepa. Deprecated with new Stripe API and SCA. - * @return \Stripe\PaymentMethod|null Stripe SEPA or null if not found + * @param int $createifnotlinkedtostripe 1=Create the stripe sepa and the link if the sepa is not yet linked to a stripe sepa. Used by the "Create bank to Stripe" feature. + * @return \Stripe\PaymentMethod|null Stripe SEPA or null if not found */ public function sepaStripe($cu, CompanyPaymentMode $object, $stripeacc = '', $status = 0, $createifnotlinkedtostripe = 0) { @@ -996,8 +1009,10 @@ class Stripe extends CommonObject $stripeacc = $stripearrayofkeysbyenv[$servicestatus]['secret_key']; dol_syslog("Try to create sepa_debit with data = ".json_encode($dataforcard)); - // TODO Replace with PaymentIntent. Can use $stripe->getPaymentIntent ? + $s = new \Stripe\StripeClient($stripeacc); + + // TODO Deprecated with the new Stripe API and SCA. Replace ->create() and ->createSource() Replace with getSetupIntent() ? $sepa = $s->sources->create($dataforcard); if (!$sepa) { $this->error = 'Creation of sepa_debit on Stripe has failed'; @@ -1007,9 +1022,10 @@ class Stripe extends CommonObject if (!$cs) { $this->error = 'Link SEPA <-> Customer failed'; } else { - dol_syslog("Update the payment request"); + dol_syslog("Update the payment mode of the customer"); // print json_encode($sepa); + // Save the Stripe payment mode ID into the Dolibarr database $sql = "UPDATE ".MAIN_DB_PREFIX."societe_rib"; $sql .= " SET stripe_card_ref = '".$this->db->escape($sepa->id)."', card_type = 'sepa_debit',"; $sql .= " stripe_account= '" . $this->db->escape($cu->id . "@" . $stripeacc) . "'"; @@ -1034,6 +1050,7 @@ class Stripe extends CommonObject return $sepa; } + /** * Create charge. * This is called by page htdocs/stripe/payment.php and may be deprecated.