diff --git a/bank_fonctions.php b/bank_fonctions.php index 0c6b9eb9..9ed06e3c 100644 --- a/bank_fonctions.php +++ b/bank_fonctions.php @@ -61,6 +61,24 @@ function balise_PAYER_ABONNEMENT_dist($p){ return $p; } +/** + * #CHANGER_MOYEN_PAIEMENT{#ID_ABONNEMENT,#ID_TRANSACTION} + * @param $p + * @return mixed + */ +function balise_CHANGER_MOYEN_PAIEMENT_dist($p){ + + $_idabo = interprete_argument_balise(1, $p); + $_idtrans = interprete_argument_balise(2, $p); + $p->code = ""; + + if ($_idabo AND $_idtrans) { + $p->code = "bank_changer_paiement($_idabo,$_idtrans)"; + } + + $p->interdire_scripts = false; + return $p; +} /** * #GERER_ABONNEMENT{#MODE_PAIEMENT,#ABO_UID} diff --git a/bank_options.php b/bank_options.php index 2f91b5de..c5757e14 100644 --- a/bank_options.php +++ b/bank_options.php @@ -183,3 +183,23 @@ function bank_affiche_gerer_abonnement($config, $abo_uid){ return ""; } + +/** + * Afficher le bouton pour changer de moyen de paiement d'un abonnement + * @param array|string $config + * @param string $abo_uid + * @return array|string + */ +function bank_changer_paiement($id_abo, $id_trans){ + // $config de type string ? + include_spip('inc/bank'); + + if ($trans = sql_fetsel("*", "spip_transactions", $w = "id_transaction=" . sql_quote($id_trans))){ + $config = bank_config($trans['mode']); + if ($configurer = charger_fonction('configurer', 'presta/'.$config['presta'] . '/payer', true)){ + return $configurer($id_trans, $trans['transaction_hash']); + } + } + + return ""; +} diff --git a/presta/stripe/call/modifier_paiement.php b/presta/stripe/call/modifier_paiement.php new file mode 100644 index 00000000..ad5185e0 --- /dev/null +++ b/presta/stripe/call/modifier_paiement.php @@ -0,0 +1,130 @@ + $id_transaction, + 'transaction_hash' => $transaction_hash, + ); + $contexte['sign'] = bank_sign_response_simple($mode, $contexte); + $url_success = bank_url_api_retour($config,"response"); + $url_cancel = bank_url_api_retour($config,"cancel"); + foreach($contexte as $k=>$v){ + $url_success = parametre_url($url_success, $k, $v, '&'); + $url_cancel = parametre_url($url_cancel, $k, $v, '&'); + } + + $contexte['action'] = str_replace('&', '&', $url_success); + $contexte['email'] = $email; + $contexte['key'] = ($config['mode_test'] ? $config['PUBLISHABLE_KEY_test'] : $config['PUBLISHABLE_KEY']); + $contexte['name'] = bank_nom_site(); + $contexte['description'] = _T('bank:titre_transaction') . '#' . $id_transaction; + $contexte['image'] = find_in_path('img/logo-paiement-stripe.png'); + + stripe_init_api($config); + stripe_set_webhook($config); + + // ================== CUSTOMER =================== + + // Coté Stripe : on vérifie que le customer existe + if ($row['id_auteur']) { + $config_id = bank_config_id($config); + $customer_id = sql_getfetsel('pay_id', 'spip_transactions', + 'pay_id!=' . sql_quote('') . ' AND id_auteur=' . intval($row['id_auteur']) . ' AND statut=' . sql_quote('ok') . ' AND mode=' . sql_quote("$mode/$config_id"), + '', 'date_paiement DESC', '0,1'); + if ($customer_id + and $customer = stripe_customer($mode, ['id' => $customer_id]) + and $customer->email === $contexte['email'] + ){ + spip_log("call_modifier_paiement : Customer retrouvé :". $id_customer,$mode._LOG_DEBUG); + $checkout_customer_id = $customer_id; + } else { + spip_log("call_modifier_paiement : ce Customer n'existe pas chez Stripe". $id_customer,$mode._LOG_ERREUR); + return false; + } + if (!$checkout_customer_id + and $customer = stripe_customer($mode, ['email' => $contexte['email'], 'nom' => trim($billing['prenom'] . ' ' . $billing['nom'])])){ + $checkout_customer_id = $customer->id; + } + } + + // On prépare la sessions avec les données génériques + $session_desc = [ + 'payment_method_types' => ['card'], + 'success_url' => $url_success . '&session_id={CHECKOUT_SESSION_ID}', + 'cancel_url' => $url_success, // on revient sur success aussi car response gerera l'echec du fait de l'absence de session_id + 'locale' => stripe_locale($GLOBALS['spip_lang']), + 'customer_email' => $email, + ]; + + // Et on déclare les paramètres particuliers du mode "setup" de la session checkout. + $session_desc['mode'] = 'setup'; + $session_desc[ 'setup_intent_data']['description'] = 'Enregistrement de nouveau moyen de paiement'; + $session_desc[ 'setup_intent_data']['metadata']['customer_id'] = $id_customer; + $session_desc[ 'setup_intent_data']['metadata']['subscription_id'] = str_replace('/','',$row['abo_uid']); + + $session = \Stripe\Checkout\Session::create($session_desc); + $contexte['checkout_session_id'] = $session->id; + + return $contexte; + +} \ No newline at end of file diff --git a/presta/stripe/call/request.php b/presta/stripe/call/request.php index 060da881..c29a1054 100644 --- a/presta/stripe/call/request.php +++ b/presta/stripe/call/request.php @@ -291,6 +291,8 @@ function presta_stripe_call_request_dist($id_transaction, $transaction_hash, $co } } + $interval_count = 1; + $session_desc = [ 'payment_method_types' => $payment_types, 'mode' => 'subscription', @@ -321,7 +323,7 @@ function presta_stripe_call_request_dist($id_transaction, $transaction_hash, $co 'unit_amount' => $montant_echeance, 'recurring' => [ 'interval' => $interval, - 'interval_count' => 1, + 'interval_count' => $echeance['interval_count'], //'trial_period_days' => 0, // default ], //'billing_scheme' => 'per_unit', // implicite, non modifiable via price_data diff --git a/presta/stripe/payer/configurer.html b/presta/stripe/payer/configurer.html new file mode 100644 index 00000000..58584468 --- /dev/null +++ b/presta/stripe/payer/configurer.html @@ -0,0 +1,28 @@ +[(#REM) +/* +* Paiement Bancaire +* module de paiement bancaire multi prestataires +* stockage des transactions +* +* Auteurs : + * Laurent Lefebvre largement inspiré du code de Cedric Morin / Nursit.com + * (c) 2012-2024 - Distribue sous licence GNU/GPL +* +*/ +]#CACHE{0} + +
+ [(#SET{id,[setup(#ID_TRANSACTION)]})] + [(#BOUTON_ACTION{ + Modifier le moyen de paiement pour cet abonnement, + #ENV*{action}, + [stripe_button_(#GET{id}) btn btn-defaut btn-petit btn-fleche], + '', + '', + [stripe_button_(#GET{id})_callback\(\)] + })] + [ + ] + +
+ diff --git a/presta/stripe/payer/configurer.php b/presta/stripe/payer/configurer.php new file mode 100644 index 00000000..83acad8c --- /dev/null +++ b/presta/stripe/payer/configurer.php @@ -0,0 +1,35 @@ +