// ---------- Stripe + AJAX for PaymentIntent ---------- add_action( 'wp_enqueue_scripts', 'golite_enqueue_stripe_scripts' ); function golite_enqueue_stripe_scripts() { if ( ! is_checkout() ) return; // Stripe.js wp_enqueue_script( 'stripe-js', 'https://js.stripe.com/v3/', array(), null, true ); // Your custom checkout JS (adjust path if you save JS elsewhere) wp_register_script( 'golite-stripe-checkout', get_stylesheet_directory_uri() . '/js/stripe-checkout.js', array( 'jquery', 'stripe-js' ), '1.0', true ); // Localize needed data (ajax URL, publishable key) $publishable_key = get_option( 'stripe_publishable_key' ); // set this in WP options wp_localize_script( 'golite-stripe-checkout', 'woof_husky_txt', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'publishableKey' => $publishable_key ?: '', ) ); wp_enqueue_script( 'golite-stripe-checkout' ); } // AJAX handler that creates a Stripe PaymentIntent and returns client_secret add_action( 'wp_ajax_fetch_strip_client_secret', 'golite_fetch_strip_client_secret' ); add_action( 'wp_ajax_nopriv_fetch_strip_client_secret', 'golite_fetch_strip_client_secret' ); function golite_fetch_strip_client_secret() { if ( ! class_exists( 'WC_Cart' ) ) { wp_send_json( array( 'status' => false, 'error' => 'WooCommerce not available.' ) ); wp_die(); } // Use numeric cart total from WC cart (server-side) $cart = WC()->cart; if ( ! $cart ) { wp_send_json( array( 'status' => false, 'error' => 'Cart not available' ) ); wp_die(); } // Use the cart total property (string numeric), safer than formatted strings $total = (float) $cart->total; // e.g. 49.99 $currency = get_woocommerce_currency(); if ( $total <= 0 ) { wp_send_json( array( 'status' => false, 'error' => 'Cart total invalid' ) ); wp_die(); } $amount_cents = round( $total * 100 ); // cents / smallest currency unit // Your Stripe secret key from WP options $secret_key = get_option( 'stripe_secret_key' ); // set this in WP options if ( empty( $secret_key ) ) { wp_send_json( array( 'status' => false, 'error' => 'Stripe secret key not configured' ) ); wp_die(); } // Build body for PaymentIntent creation $body = array( 'amount' => $amount_cents, 'currency' => strtolower( $currency ), // enable automatic payment methods so Stripe decides card / wallet etc 'automatic_payment_methods[enabled]' => 'true', // optionally attach metadata like WC order draft reference (not created yet) 'metadata[wc_cart_total]' => (string) $total, ); $args = array( 'body' => $body, 'timeout' => 30, 'headers' => array( 'Authorization' => 'Bearer ' . $secret_key, 'Accept' => 'application/json', ), ); $response = wp_remote_post( 'https://api.stripe.com/v1/payment_intents', $args ); if ( is_wp_error( $response ) ) { wp_send_json( array( 'status' => false, 'error' => 'Stripe request error: ' . $response->get_error_message() ) ); wp_die(); } $code = wp_remote_retrieve_response_code( $response ); $body_json = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code !== 200 && $code !== 201 ) { $msg = isset( $body_json['error']['message'] ) ? $body_json['error']['message'] : 'Stripe API error'; wp_send_json( array( 'status' => false, 'error' => 'Stripe error: ' . $msg ) ); wp_die(); } if ( empty( $body_json['client_secret'] ) ) { wp_send_json( array( 'status' => false, 'error' => 'Stripe did not return client_secret' ) ); wp_die(); } // Optional: you can also send back billing/shipping placeholders if you want wp_send_json( array( 'status' => true, 'clientSecret' => $body_json['client_secret'], ) ); wp_die(); } Bolivia - Equal Access Archives | GoLite Mobile

All Product

Need Help?
×

Need Help?

Live Chat (Fastest Response)

Hours: Mon–Sat 8:30am–8:30pm, Sun 10am–6pm.

After hours, Mariko supports 24/7.

Chat with Mariko

Call Us

800-801-9385


More Help

Scroll to Top