From c3e59771740abdf6d20f15e1b7d7882e15eca28c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 6 Feb 2023 19:22:08 +0100 Subject: [PATCH] NEW sepaStripe now creates the payment mode with type pm_ using new API --- htdocs/societe/paymentmodes.php | 2 +- htdocs/stripe/class/stripe.class.php | 51 +++++++++++++++------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/htdocs/societe/paymentmodes.php b/htdocs/societe/paymentmodes.php index c26aaac80bd..75aa8730d44 100644 --- a/htdocs/societe/paymentmodes.php +++ b/htdocs/societe/paymentmodes.php @@ -1907,7 +1907,7 @@ if ($socid && $action == 'edit' && $permissiontoaddupdatepaymentinformation) { print $form->selectarray("frstrecur", $tblArraychoice, dol_escape_htmltag(GETPOST('frstrecur', 'alpha') ?GETPOST('frstrecur', 'alpha') : $companybankaccount->frstrecur), 0); print ''; - print ''.$langs->trans("StripeID")." ('src_....')"; + print ''.$langs->trans("StripeID")." ('pm_...' or 'src_...')"; print ''; print ''; diff --git a/htdocs/stripe/class/stripe.class.php b/htdocs/stripe/class/stripe.class.php index fd82c6e600b..7f66ae05383 100644 --- a/htdocs/stripe/class/stripe.class.php +++ b/htdocs/stripe/class/stripe.class.php @@ -922,7 +922,7 @@ class Stripe extends CommonObject global $conf, $user, $langs; $sepa = null; - $sql = "SELECT sa.stripe_card_ref, sa.proprio, sa.iban_prefix, sa.rum"; // stripe_card_ref is 'src_...' for Stripe SEPA + $sql = "SELECT sa.stripe_card_ref, sa.proprio, sa.iban_prefix as iban, sa.rum"; // stripe_card_ref is 'src_...' for Stripe SEPA $sql .= " FROM ".MAIN_DB_PREFIX."societe_rib as sa"; $sql .= " WHERE sa.rowid = ".((int) $object->id); // We get record from ID, no need for filter on entity $sql .= " AND sa.type = 'ban'"; //type ban to get normal bank account of customer (prelevement) @@ -960,41 +960,41 @@ class Stripe extends CommonObject dol_syslog($this->error, LOG_WARNING); } } elseif ($createifnotlinkedtostripe) { - // We will create the BAN on Stripe side - $iban = $obj->iban_prefix; //prefix ? + $iban = $obj->iban; $ipaddress = getUserRemoteIP(); + $metadata = array('dol_version'=>DOL_VERSION, 'dol_entity'=>$conf->entity, 'ipaddress'=>$ipaddress); + if (is_object($object)) { + $metadata['dol_type'] = $object->element; + $metadata['dol_id'] = $object->id; + $metadata['dol_thirdparty_id'] = $soc->id; + } + + $description = 'SEPA for IBAN '.$iban; $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, - 'dol_id'=>$object->id, - 'dol_version'=>DOL_VERSION, - 'dol_entity'=>$conf->entity, - 'ipaddress'=>$ipaddress - ) + "metadata" => $metadata ); // 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(); @@ -1015,15 +1015,17 @@ class Stripe extends CommonObject dol_syslog("Try to create sepa_debit with data = ".json_encode($dataforcard)); $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); + //var_dump($dataforcard);exit; + $sepa = $s->paymentMethods->create($dataforcard); if (!$sepa) { - $this->error = 'Creation of sepa_debit on Stripe has failed'; + $this->error = 'Creation of payment method sepa_debit on Stripe has failed'; } else { // link customer and src - $cs = $cu->createSource($cu->id, array('source' => $sepa->id)); + //$cs = $this->getSetupIntent($description, $soc, $cu, '', $status); + $dataforintent = array(['description'=> $description, 'payment_method_types' => ['sepa_debit'], 'customer' => $cu->id, 'payment_method' => $sepa->id], 'metadata'=>$metadata); + $cs = $s->setupIntents->create($dataforintent); + //$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 { @@ -1043,6 +1045,7 @@ class Stripe extends CommonObject } } } catch (Exception $e) { + $sepa = null; $this->error = $e->getMessage(); dol_syslog($this->error, LOG_WARNING); }