NEW sepaStripe now creates the payment mode with type pm_ using new API

This commit is contained in:
Laurent Destailleur 2023-02-06 19:22:08 +01:00
parent bf13a8ead9
commit c3e5977174
2 changed files with 28 additions and 25 deletions

View File

@ -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 $form->selectarray("frstrecur", $tblArraychoice, dol_escape_htmltag(GETPOST('frstrecur', 'alpha') ?GETPOST('frstrecur', 'alpha') : $companybankaccount->frstrecur), 0);
print '</td></tr>'; print '</td></tr>';
print '<tr><td>'.$langs->trans("StripeID")." ('src_....')</td>"; print '<tr><td>'.$langs->trans("StripeID")." ('pm_...' or 'src_...')</td>";
print '<td><input class="minwidth300" type="text" name="stripe_card_ref" value="'.$companypaymentmode->stripe_card_ref.'"></td></tr>'; print '<td><input class="minwidth300" type="text" name="stripe_card_ref" value="'.$companypaymentmode->stripe_card_ref.'"></td></tr>';
print '</table>'; print '</table>';

View File

@ -922,7 +922,7 @@ class Stripe extends CommonObject
global $conf, $user, $langs; global $conf, $user, $langs;
$sepa = null; $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 .= " 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 .= " 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) $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); dol_syslog($this->error, LOG_WARNING);
} }
} elseif ($createifnotlinkedtostripe) { } elseif ($createifnotlinkedtostripe) {
// We will create the BAN on Stripe side $iban = $obj->iban;
$iban = $obj->iban_prefix; //prefix ?
$ipaddress = getUserRemoteIP(); $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( $dataforcard = array(
'type'=>'sepa_debit', 'type'=>'sepa_debit',
"sepa_debit" => array('iban' => $iban), "sepa_debit" => array('iban' => $iban),
'currency' => strtolower($conf->currency), 'billing_details' => array(
'usage' => 'reusable',
'owner' => array(
'name' => $soc->name, 'name' => $soc->name,
'email' => !empty($soc->email) ? $soc->email : "",
), ),
"metadata" => array( "metadata" => $metadata
'dol_type'=>$object->element,
'dol_id'=>$object->id,
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress
)
); );
// Complete owner name // Complete owner name
if (!empty($soc->town)) { if (!empty($soc->town)) {
$dataforcard['owner']['address']['city']=$soc->town; $dataforcard['billing_details']['address']['city']=$soc->town;
} }
if (!empty($soc->country_code)) { if (!empty($soc->country_code)) {
$dataforcard['owner']['address']['country']=$soc->country_code; $dataforcard['billing_details']['address']['country']=$soc->country_code;
} }
if (!empty($soc->address)) { if (!empty($soc->address)) {
$dataforcard['owner']['address']['line1']=$soc->address; $dataforcard['billing_details']['address']['line1']=$soc->address;
} }
if (!empty($soc->zip)) { if (!empty($soc->zip)) {
$dataforcard['owner']['address']['postal_code']=$soc->zip; $dataforcard['billing_details']['address']['postal_code']=$soc->zip;
} }
if (!empty($soc->state)) { if (!empty($soc->state)) {
$dataforcard['owner']['address']['state']=$soc->state; $dataforcard['billing_details']['address']['state']=$soc->state;
} }
//$a = \Stripe\Stripe::getApiKey(); //$a = \Stripe\Stripe::getApiKey();
@ -1015,15 +1015,17 @@ class Stripe extends CommonObject
dol_syslog("Try to create sepa_debit with data = ".json_encode($dataforcard)); dol_syslog("Try to create sepa_debit with data = ".json_encode($dataforcard));
$s = new \Stripe\StripeClient($stripeacc); $s = new \Stripe\StripeClient($stripeacc);
//var_dump($dataforcard);exit;
// TODO LMR Deprecated with the new Stripe API and SCA. $sepa = $s->paymentMethods->create($dataforcard);
// 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);
if (!$sepa) { 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 { } else {
// link customer and src // 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) { if (!$cs) {
$this->error = 'Link SEPA <-> Customer failed'; $this->error = 'Link SEPA <-> Customer failed';
} else { } else {
@ -1043,6 +1045,7 @@ class Stripe extends CommonObject
} }
} }
} catch (Exception $e) { } catch (Exception $e) {
$sepa = null;
$this->error = $e->getMessage(); $this->error = $e->getMessage();
dol_syslog($this->error, LOG_WARNING); dol_syslog($this->error, LOG_WARNING);
} }