120 lines
4.2 KiB
PHP
Executable file
120 lines
4.2 KiB
PHP
Executable file
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Modèle de la Page affichée à l'utilisateur lors du succès d'un paiement « Succesful Order ».
|
|
*/
|
|
|
|
namespace HaikuAtelier;
|
|
|
|
use HaikuAtelier\WP\Resource;
|
|
use Roots\WPConfig\Config;
|
|
use Stripe\Checkout\Session;
|
|
use Stripe\StripeClient;
|
|
use Timber\Timber;
|
|
use WC_Cart;
|
|
use WC_Order;
|
|
use WC_Order_Refund;
|
|
|
|
use function Crell\fp\pipe;
|
|
|
|
/** @var string $url_accueil L'URL de la page d'Accueil. */
|
|
$url_accueil = get_page_link(get_page_by_path('home')?->ID);
|
|
|
|
/** @var string $session_id L'ID de la Session Stripe liée à la Commande. */
|
|
$session_id = $_GET['session_id'];
|
|
// Redirige à l'Accueil si le paramètre d'ID de Session Stripe n'est pas présent avec un code HTTP 301
|
|
if (!$session_id) {
|
|
header("Location: {$url_accueil}");
|
|
|
|
return;
|
|
}
|
|
|
|
// Instancie un Client Stripe
|
|
/** @var StripeClient $client_stripe Un Client Stripe pour récupérer les informations des Commande et Transaction . */
|
|
$client_stripe = new StripeClient(Config::get('STRIPE_API_SECRET'));
|
|
|
|
try {
|
|
/** @var Session $session_stripe La Session Stripe pour la Commande. */
|
|
$session_stripe = $client_stripe->checkout->sessions->retrieve($session_id);
|
|
|
|
/** @var string $order_id L'ID de la Commande WooCommerce passée en métadonnée à la Session. */
|
|
$order_id = $session_stripe->metadata['order_id'];
|
|
|
|
/** @var bool|WC_Order|WC_Order_Refund $commande La Commande WooCommerce liée à la Session Stripe, `false` si inexistante. */
|
|
$commande = wc_get_order("{$order_id}");
|
|
|
|
/** @var WC_Cart $panier */
|
|
$panier = WC()->cart;
|
|
|
|
if (false === $commande) {
|
|
throw new Error("La commande {$order_id} n'existe pas.");
|
|
}
|
|
|
|
// Passe la Commande en état "Payé" et réinitialise le Panier
|
|
if ('pending' === $commande->get_status()) {
|
|
$commande->payment_complete($session_id);
|
|
// $commande->set_payment_method_title("Stripe - Carte bancaire");
|
|
$commande->set_transaction_id($session_id);
|
|
|
|
$panier->empty_cart();
|
|
}
|
|
|
|
$context = Timber::context();
|
|
$templates = ['succes-commande.twig'];
|
|
|
|
// Récupère les données des Produits
|
|
/** @var mixed $produits Les Produits de la Commande sous forme de tableau contenant uniquement les données affichées nécessaires pour le Page. */
|
|
$produits = collect($commande->get_items())->map(static function (WC_Order_Item $produit_commande) {
|
|
/** @var string $id_produit L'ID du Produit. */
|
|
$id_produit = $produit_commande['product_id'];
|
|
|
|
/** @var false|WC_Product|WC_Product_Variable $produit Les informations du Produit. */
|
|
$produit = wc_get_product($produit_commande['product_id']);
|
|
|
|
// Récupère le nom et la valeur de l'attribut du Produit
|
|
$attribut = $produit->is_type('variable')
|
|
? collect($produit->get_attributes())->mapWithKeys(static function ($_atr, $cle) use ($produit_commande) {
|
|
$nom_attribut = wc_attribute_label($cle, $produit_commande->get_product());
|
|
$valeur_attribut = $produit_commande->get_product()->get_attribute($cle);
|
|
|
|
return [['nom' => $nom_attribut, 'valeur' => $valeur_attribut]];
|
|
})->toArray()
|
|
: [];
|
|
|
|
return [
|
|
'attribut' => $attribut,
|
|
'id_produit' => $id_produit,
|
|
'image' => pipe($produit->get_image_id(), static fn($id): string => Resource::output_multi_formats_img_tag(
|
|
id: $id,
|
|
lazy: true,
|
|
)),
|
|
'permalien' => $produit->get_permalink(),
|
|
'prix' => $produit_commande->get_data()['total'],
|
|
'quantite' => $produit_commande->get_quantity(),
|
|
'titre' => $produit->get_title(),
|
|
];
|
|
});
|
|
|
|
$context['produits'] = $produits;
|
|
|
|
// Charge les scripts et styles de la page
|
|
function charge_scripts_styles_page_succes_commande(): void {
|
|
wp_enqueue_style(
|
|
handle: 'haiku-atelier-2024-styles-page-succes-commande',
|
|
src: get_template_directory_uri() . '/assets/css/pages/page-succes-commande.css',
|
|
deps: [],
|
|
ver: filemtime(get_template_directory() . '/assets/css/pages/page-succes-commande.css'),
|
|
media: 'all',
|
|
);
|
|
}
|
|
|
|
add_action('wp_enqueue_scripts', 'charge_scripts_styles_page_succes_commande');
|
|
|
|
// Rendu
|
|
Timber::render(filenames: $templates, data: $context);
|
|
} catch (Error $error) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => esc_html($error->getMessage())]);
|
|
}
|