This commit is contained in:
gcch 2025-06-19 16:07:29 +02:00
commit de73fc619a
3560 changed files with 747274 additions and 0 deletions

View file

@ -0,0 +1,144 @@
<?php
/**
* Route pour la préparation du paiement via Stripe (« Checkout »)
*/
declare(strict_types=1);
use Ramsey\Uuid\Uuid;
use Roots\WPConfig\Config;
use Stripe\BillingPortal\Session;
header("Content-Type: application/json; charset=utf-8");
// Récupère les informations nécessaires
/** @var WC_Session_Handler $session_wc La Session WooCommerce contenant entre autre le Panier. */
$session_wc = WC()->session;
/** @var array<string,string> $urls URLs utilisables pour rediriger l'Utilisateur. */
$urls = [
"accueil" => get_page_link(get_page_by_path("home")),
"succes_commande" => get_page_link(get_page_by_path("successful-order")),
"echec_commande" => get_page_link(get_page_by_path("failed-order")),
];
// Redirige à la page d'accueil si le Panier est vide
if (WC()->cart->is_empty()) {
header("Location: " . $urls["accueil"]);
return;
}
// Vérifie que les paramètres d'URLs nécessaires soient présents
/** @var string $order_id */
$order_id = $_GET["order_id"];
if (!$order_id) {
$reponse = ["succes" => false, "status" => "order_key is missing"];
echo json_encode($reponse);
http_response_code(400);
return;
}
/** @var string $order_key */
$order_key = $_GET["order_key"];
if (!$order_key) {
$reponse = ["succes" => false, "status" => "order_key is missing"];
echo json_encode($reponse);
http_response_code(400);
return;
}
// Récupère le Panier et l'Email du Client
/** @var WC_Cart $panier */
$panier = WC()->cart;
/** @var string $email_client */
$email_client = WC()->session->get("customer")["email"];
/** @var array<\Stripe\Product> $articles */
$articles = collect($panier->get_cart())
->map(function ($article_panier) {
$titre_produit = match ($article_panier["data"]?->get_type() == "variable") {
true => $article_panier["data"]?->get_title() .
" (" .
explode(": ", $article_panier["data"]?->get_attribute_summary())[1] .
")",
false => $article_panier["data"]?->get_title(),
};
return [
"price_data" => [
"currency" => "EUR",
"product_data" => [
"name" => $titre_produit,
"images" => [wp_get_attachment_image_url($article_panier["data"]?->get_image_id())],
],
"unit_amount" => $article_panier["data"]?->get_price() * 100,
],
"quantity" => $article_panier["quantity"],
];
})
->values()
->toArray();
// Récupère la Commande et la Méthode de Livraison
/** @var WC_Order $commande */
$commande = wc_get_order($order_id);
/** @var mixed $methode_livraison */
$methode_livraison = [
"nom" => $commande->get_shipping_method(),
"cout" => $commande->get_shipping_total(),
];
// Sélectionne la clé API Stripe
\Stripe\Stripe::setApiKey(Config::get("STRIPE_API_SECRET"));
// Met à jour les Codes promos
$coupons_stripe = collect(\Stripe\Coupon::all()->data);
$coupons_wc = collect(WC()->cart->get_coupons())
->map(
fn(WC_Coupon $coupon) => [
"duration" => "forever",
"id" => $coupon->get_code(),
"name" => $coupon->get_code(),
$coupon->get_discount_type() == "fixed_cart" ? "amount_off" : "percent_off" => $coupon->get_amount(),
],
)
->each(function (array $item) use ($coupons_stripe) {
// Si le code promo n'existe, le créer
if (!$coupons_stripe->contains("name", $item["name"])) {
\Stripe\Coupon::create($item);
}
});
$reductions_stripe = $coupons_wc->map(fn($coupon) => ["coupon" => $coupon["name"]])->values()->toArray();
/** @var Session $session_checkout_stripe */
$session_checkout_stripe = \Stripe\Checkout\Session::create(
[
"cancel_url" => $urls["echec_commande"],
"customer_email" => $email_client,
"discounts" => $reductions_stripe,
"line_items" => $articles,
"mode" => "payment",
"success_url" => $urls["succes_commande"] . "?session_id={CHECKOUT_SESSION_ID}",
"metadata" => [
"order_id" => $order_id,
"order_key" => $order_key,
],
"shipping_options" => [
[
"shipping_rate_data" => [
"display_name" => $methode_livraison["nom"],
"fixed_amount" => [
"amount" => $methode_livraison["cout"],
"currency" => "EUR",
],
"tax_behavior" => "inclusive",
"type" => "fixed_amount",
],
],
],
],
[
"idempotency_key" => Uuid::uuid4(),
],
);
// echo json_encode($session_checkout_stripe);
header("HTTP/1.1 303 See Other");
header("Location: " . $session_checkout_stripe->url);
exit();