add_action('woocommerce_loaded', 'custom_stripe_checkout_redirect'); function custom_stripe_checkout_redirect() { if (!is_checkout() || !isset($_GET['stripe_embedded']) || $_GET['stripe_embedded'] !== '1') { return; } if (!function_exists('wc_get_checkout_order_received_url')) { include_once WP_PLUGIN_DIR . '/woocommerce/includes/wc-template-functions.php'; } $cart = WC()->cart; if (!$cart || $cart->is_empty()) { wc_add_notice('Ваша корзина пуста.', 'error'); return; } $order = wc_create_order(); foreach ($cart->get_cart() as $cart_item) { $order->add_product($cart_item['data'], $cart_item['quantity']); } $order->calculate_totals(); $total_amount = $order->get_total(); $currency = $order->get_currency(); $first_item = reset($cart->get_cart()); $product_id = $first_item['product_id']; $vendor_id = get_post_field('post_author', $product_id); $stripe_account_id = get_user_meta($vendor_id, '_stripe_connect_user_id', true); if (!$stripe_account_id) { $stripe_account_id = get_user_meta($vendor_id, 'stripe_user_id', true); } $application_fee = round($total_amount * 0.10, 2); require_once '/opt/bitnami/wordpress/vendor/autoload.php'; \Stripe\Stripe::setApiKey('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'); // Замените на ваш ключ try { $session = \Stripe\Checkout\Session::create([ 'line_items' => [[ 'price_data' => [ 'currency' => strtolower($currency), 'product_data' => [ 'name' => 'Заказ #' . $order->get_id(), ], 'unit_amount' => intval($total_amount * 100), ], 'quantity' => 1, ]], 'mode' => 'payment', 'success_url' => add_query_arg('session_id', '{CHECKOUT_SESSION_ID}', wc_get_checkout_order_received_url($order->get_id())), 'cancel_url' => wc_get_cart_url(), 'payment_intent_data' => [ 'application_fee_amount' => intval($application_fee * 100), 'transfer_data' => [ 'destination' => $stripe_account_id, ], ], ]); update_post_meta($order->get_id(), '_stripe_checkout_session_id', $session->id); WC()->cart->empty_cart(); wp_redirect($session->url); exit; } catch (Exception $e) { wc_add_notice('Ошибка Stripe: ' . $e->getMessage(), 'error'); error_log('❌ Исключение Stripe: ' . $e->getMessage()); } }