141 lines
4.7 KiB
PHP
Executable file
141 lines
4.7 KiB
PHP
Executable file
<?php
|
|
|
|
/**
|
|
* Route pour la préparation du paiement via Stripe (« Checkout »).
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Roots\WPConfig\Config;
|
|
use Stripe\BillingPortal\Session;
|
|
use Stripe\Coupon;
|
|
use Stripe\Product;
|
|
use Stripe\Stripe;
|
|
use Symfony\Component\Uid\Uuid;
|
|
|
|
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 list<Product> $articles */
|
|
$articles = collect($panier->get_cart())
|
|
->map(static function ($article_panier) {
|
|
$titre_produit = match ('variable' === $article_panier['data']?->get_type()) {
|
|
true => $article_panier['data']?->get_title()
|
|
. ' ('
|
|
. explode(': ', (string) $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);
|
|
|
|
$methode_livraison = ['nom' => $commande->get_shipping_method(), 'cout' => $commande->get_shipping_total() * 100];
|
|
|
|
// Le nom de la méthode de livraison ne peut être une chaîne vide.
|
|
if (empty($methode_livraison['nom'])) {
|
|
$methode_livraison['nom'] = 'Free';
|
|
}
|
|
|
|
// Sélectionne la clé API Stripe
|
|
Stripe::setApiKey(Config::get('STRIPE_API_SECRET'));
|
|
|
|
// Met à jour les Codes promos
|
|
$coupons_stripe = collect(Coupon::all()->data);
|
|
$coupons_wc = collect(WC()->cart->get_coupons())
|
|
->map(static fn(WC_Coupon $coupon): array => [
|
|
'duration' => 'forever',
|
|
'id' => $coupon->get_code(),
|
|
'name' => $coupon->get_code(),
|
|
'fixed_cart' === $coupon->get_discount_type() ? 'amount_off' : 'percent_off' => $coupon->get_amount(),
|
|
])
|
|
->each(static function (array $item) use ($coupons_stripe): void {
|
|
// Si le code promo n'existe, le créer
|
|
if (!$coupons_stripe->contains('name', $item['name'])) {
|
|
Coupon::create($item);
|
|
}
|
|
});
|
|
$reductions_stripe = $coupons_wc->map(static fn($coupon): array => ['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::v4()]);
|
|
// echo json_encode($session_checkout_stripe);
|
|
header('HTTP/1.1 303 See Other');
|
|
header('Location: ' . $session_checkout_stripe->url);
|
|
|
|
exit;
|