id = 'custom_stripe'; $this->icon = ''; $this->has_fields = true; $this->method_title = 'Custom Stripe'; $this->method_description = 'Accept payments via Stripe'; $this->supports = array( 'products' ); $this->init_form_fields(); $this->init_settings(); $this->title = $this->get_option('title'); $this->description = $this->get_option('description'); $this->enabled = $this->get_option('enabled'); $this->testmode = 'yes' === $this->get_option('testmode'); $this->private_key = $this->testmode ? $this->get_option('test_private_key') : $this->get_option('private_key'); $this->publishable_key = $this->testmode ? $this->get_option('test_publishable_key') : $this->get_option('publishable_key'); add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); } public function init_form_fields() { $this->form_fields = array( 'enabled' => array( 'title' => 'Enable/Disable', 'label' => 'Enable Custom Stripe Gateway', 'type' => 'checkbox', 'description' => '', 'default' => 'no' ), 'title' => array( 'title' => 'Title', 'type' => 'text', 'description' => 'This controls the title displayed during checkout.', 'default' => 'Credit Card', 'desc_tip' => true, ), 'description' => array( 'title' => 'Description', 'type' => 'textarea', 'description' => 'Payment method description that the customer will see on your checkout.', 'default' => 'Pay with your credit card via Stripe.', ), 'testmode' => array( 'title' => 'Test mode', 'label' => 'Enable Test Mode', 'type' => 'checkbox', 'description' => 'Place the payment gateway in test mode using test API keys.', 'default' => 'yes', 'desc_tip' => true, ), 'test_publishable_key' => array( 'title' => 'Test Publishable Key', 'type' => 'text', 'default' => STRIPE_PUBLISH_KEY ), 'test_private_key' => array( 'title' => 'Test Secret Key', 'type' => 'password', 'default' => STRIPE_TEST_KEY ), 'publishable_key' => array( 'title' => 'Live Publishable Key', 'type' => 'text' ), 'private_key' => array( 'title' => 'Live Secret Key', 'type' => 'password' ) ); } public function payment_scripts() { if (!is_admin() && !is_cart() && !is_checkout() && !isset($_GET['pay_for_order'])) { return; } if ('no' === $this->enabled) { return; } if (empty($this->private_key) || empty($this->publishable_key)) { return; } if (!$this->testmode && !is_ssl()) { return; } wp_enqueue_script('stripe_js', 'https://js.stripe.com/v3/', array(), '3.0', true); wp_enqueue_script('woocommerce_stripe_js', plugin_dir_url(__FILE__) . 'stripe.js', array('jquery', 'stripe_js'), '1.0', true); wp_localize_script('woocommerce_stripe_js', 'stripe_params', array( 'publishableKey' => $this->publishable_key, 'ajaxurl' => admin_url('admin-ajax.php') )); } public function payment_fields() { if ($this->description) { echo wpautop(wp_kses_post($this->description)); } echo '
'; } public function validate_fields() { if (empty($_POST['stripe_token'])) { wc_add_notice('Please complete the payment form.', 'error'); return false; } return true; } public function process_payment($order_id) { $order = wc_get_order($order_id); $stripe_token = sanitize_text_field($_POST['stripe_token']); // Include Stripe PHP library if (!class_exists('\Stripe\Stripe')) { require_once plugin_dir_path(__FILE__) . 'stripe-php/init.php'; } \Stripe\Stripe::setApiKey($this->private_key); try { $charge = \Stripe\Charge::create(array( 'amount' => $order->get_total() * 100, // Amount in cents 'currency' => get_woocommerce_currency(), 'description' => 'Order #' . $order->get_order_number(), 'source' => $stripe_token, 'metadata' => array( 'order_id' => $order_id ) )); if ($charge->paid) { $order->payment_complete($charge->id); $order->add_order_note('Stripe payment completed. Transaction ID: ' . $charge->id); // Empty cart WC()->cart->empty_cart(); return array( 'result' => 'success', 'redirect' => $this->get_return_url($order) ); } else { wc_add_notice('Payment failed. Please try again.', 'error'); return; } } catch (Exception $e) { wc_add_notice('Payment error: ' . $e->getMessage(), 'error'); return; } } } // Add the gateway to WooCommerce function add_custom_stripe_gateway($gateways) { $gateways[] = 'WC_Custom_Stripe_Gateway'; return $gateways; } add_filter('woocommerce_payment_gateways', 'add_custom_stripe_gateway'); } // Task 3: Add 2% surcharge for non-US orders add_action('woocommerce_cart_calculate_fees', 'add_international_surcharge'); function add_international_surcharge() { if (is_admin() && !defined('DOING_AJAX')) return; $country = WC()->customer->get_shipping_country(); if ($country && 'US' !== $country) { $surcharge = WC()->cart->get_subtotal() * 0.02; WC()->cart->add_fee( __('International Surcharge (2%)', 'txtdomain'), $surcharge, true, 'standard' ); } } //Task 4: Redirect to custom thank-you page for orders > $100 add_filter('woocommerce_get_checkout_order_received_url', 'custom_order_received_redirect', 10, 2); function custom_order_received_redirect($return_url, $order) { if ($order->get_total() > 100) { $custom_page_id = 79; // premium-order-thank-you page ID return get_permalink($custom_page_id); } return $return_url; }