if (!defined('ABSPATH')) exit; // βœ… Stripe Payment Processing with Booking Confirmation function neaarme_process_stripe_payment() { require_once 'vendor/autoload.php'; if (!defined('STRIPE_SECRET_KEY')) { wp_send_json_error(['message' => 'Stripe API Key not found.']); exit; } \Stripe\Stripe::setApiKey(STRIPE_SECRET_KEY); $amount = isset($_GET['amount']) ? intval($_GET['amount']) * 100 : 1000; $pickup = isset($_GET['pickup']) ? sanitize_text_field($_GET['pickup']) : ''; $destination = isset($_GET['destination']) ? sanitize_text_field($_GET['destination']) : ''; $customerEmail = isset($_GET['email']) ? sanitize_email($_GET['email']) : ''; $selectedCar = isset($_GET['car']) ? sanitize_text_field($_GET['car']) : ''; $selectedDriver = isset($_GET['driver']) ? sanitize_text_field($_GET['driver']) : ''; $session = \Stripe\Checkout\Session::create([ 'payment_method_types' => ['card'], 'line_items' => [[ 'price_data' => [ 'currency' => 'gbp', 'product_data' => ['name' => 'Ride Payment'], 'unit_amount' => $amount, ], 'quantity' => 1, ]], 'mode' => 'payment', 'success_url' => site_url("/wp-json/neaarme/complete-booking?pickup=$pickup&destination=$destination&email=$customerEmail&car=$selectedCar&driver=$selectedDriver&amount=$amount"), 'cancel_url' => site_url('/payment-failed'), ]); wp_redirect($session->url); exit; } // βœ… Secure Paystack Payment Processing function neaarme_process_paystack_payment() { if (!defined('PAYSTACK_SECRET_KEY')) { wp_send_json_error(['message' => 'Paystack API Key not found.']); exit; } $amount = isset($_GET['amount']) ? intval($_GET['amount']) : 1000; $customerEmail = isset($_GET['email']) ? sanitize_email($_GET['email']) : ''; $callback_url = site_url("/wp-json/neaarme/complete-booking?amount=$amount&email=$customerEmail"); $headers = [ "Authorization: Bearer " . PAYSTACK_SECRET_KEY, "Content-Type: application/json" ]; $data = [ "amount" => $amount * 100, "email" => $customerEmail, "callback_url" => $callback_url ]; $ch = curl_init("https://api.paystack.co/transaction/initialize"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); curl_close($ch); $response_data = json_decode($response, true); if ($response_data && isset($response_data['data']['authorization_url'])) { wp_redirect($response_data['data']['authorization_url']); exit; } else { wp_send_json_error(['message' => 'Failed to initialize Paystack payment.']); } } // βœ… Ensure Booking is Only Created After Successful Payment function neaarme_complete_booking() { $pickup = sanitize_text_field($_GET['pickup']); $destination = sanitize_text_field($_GET['destination']); $customerEmail = sanitize_email($_GET['email']); $amount = sanitize_text_field($_GET['amount']); $post_data = [ 'post_title' => "Ride from $pickup to $destination", 'post_type' => 'ride_booking', 'post_status' => 'publish', 'meta_input' => [ '_pickup_location' => $pickup, '_destination' => $destination, '_customer_email' => $customerEmail, '_fare' => $amount / 100, '_payment_status' => 'paid', ], ]; wp_insert_post($post_data); wp_redirect(site_url('/payment-success')); exit; } add_action('rest_api_init', function() { register_rest_route('neaarme', '/complete-booking', [ 'methods' => 'GET', 'callback' => 'neaarme_complete_booking', 'permission_callback' => '__return_true' ]); }); Ndoro - neaarme
Smart Search

πŸŽ‰ Your First Ride is FREE – Get Β£5 OFF Now!

Book Your Ride πŸš– Neaarme SatNav πŸš—
F X L W T

Ndoro