). */ const BLOCK_GSTIN = 'ebgi/gstin'; const BLOCK_NAME = 'ebgi/business-name'; /** * Wire hooks. */ public static function init() { // Classic checkout. add_action( 'woocommerce_after_checkout_billing_form', array( __CLASS__, 'render_classic_fields' ) ); add_action( 'woocommerce_checkout_process', array( __CLASS__, 'validate_classic' ) ); add_action( 'woocommerce_checkout_create_order', array( __CLASS__, 'save_classic' ), 10, 2 ); add_action( 'woocommerce_checkout_update_customer', array( __CLASS__, 'save_customer' ), 10, 2 ); add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue' ) ); // Block checkout / Store API. add_action( 'woocommerce_init', array( __CLASS__, 'register_block_fields' ) ); add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( __CLASS__, 'save_block' ), 10, 2 ); // Display. add_filter( 'woocommerce_admin_billing_fields', array( __CLASS__, 'admin_billing_fields' ) ); add_action( 'woocommerce_order_details_after_customer_details', array( __CLASS__, 'render_customer_details' ) ); add_filter( 'woocommerce_email_order_meta_fields', array( __CLASS__, 'email_fields' ), 10, 3 ); } /** * Human message for a validation result. * * @param string $code Validator code, or 'state_mismatch'. * @return string */ public static function error_message( $code ) { switch ( $code ) { case 'length': return __( 'GSTIN must be exactly 15 characters.', '8-bit-gst-invoice-for-woocommerce' ); case 'format': return __( 'GSTIN format is invalid. Example: 27AAAAA0000A1Z2.', '8-bit-gst-invoice-for-woocommerce' ); case 'state_code': return __( 'GSTIN starts with an unknown state code.', '8-bit-gst-invoice-for-woocommerce' ); case 'checksum': return __( 'GSTIN check digit is wrong. Please re-check the number on your GST certificate.', '8-bit-gst-invoice-for-woocommerce' ); case 'state_mismatch': return __( 'The first two digits of your GSTIN do not match the selected billing state. Please choose the state where this GSTIN is registered.', '8-bit-gst-invoice-for-woocommerce' ); } return __( 'Please enter a valid GSTIN.', '8-bit-gst-invoice-for-woocommerce' ); } /** * Validate a GSTIN against an optional billing state. * * @param string $gstin Raw GSTIN ('' is allowed: the field is optional). * @param string $billing_state WooCommerce state code. * @param string $country Billing country. * @return true|WP_Error */ public static function check( $gstin, $billing_state, $country = 'IN' ) { $result = EBGI_GSTIN_Validator::validate( $gstin ); if ( 'empty' === $result['code'] ) { return true; } if ( ! $result['valid'] ) { return new WP_Error( 'ebgi_invalid_gstin', self::error_message( $result['code'] ) ); } if ( 'yes' === EBGI_Settings::get( 'enforce_state_match' ) && 'IN' === strtoupper( (string) $country ) && '' !== (string) $billing_state && '97' !== $result['state_code'] && ! EBGI_GSTIN_Validator::state_matches( $result['gstin'], $billing_state ) ) { return new WP_Error( 'ebgi_gstin_state_mismatch', self::error_message( 'state_mismatch' ) ); } return true; } /* --------------------------------------------------------------------- Classic checkout */ /** * Render GSTIN fields after the billing form. * * @param WC_Checkout $checkout Checkout. */ public static function render_classic_fields( $checkout ) { echo '
'; woocommerce_form_field( 'billing_ebgi_gstin', array( 'type' => 'text', // WooCommerce appends "(optional)" to non-required fields itself. 'label' => __( 'Purchase for Business? Enter GSTIN', '8-bit-gst-invoice-for-woocommerce' ), 'placeholder' => '27AAAAA0000A1Z2', 'required' => false, 'class' => array( 'form-row-wide', 'ebgi-gstin-row' ), 'input_class' => array( 'ebgi-gstin-input' ), 'maxlength' => 15, 'custom_attributes' => array( 'autocomplete' => 'off', 'autocapitalize' => 'characters', 'spellcheck' => 'false', ), ), $checkout->get_value( 'billing_ebgi_gstin' ) ); // Live GSTIN feedback sits right under the GSTIN field it describes. echo '

'; woocommerce_form_field( 'billing_ebgi_legal_name', array( 'type' => 'text', 'label' => __( 'Business Registered Name', '8-bit-gst-invoice-for-woocommerce' ), 'required' => false, 'class' => array( 'form-row-wide', 'ebgi-gst-name-row' ), ), $checkout->get_value( 'billing_ebgi_legal_name' ) ); echo '
'; } /** * Posted values of the current classic checkout request, read once after the nonce check. * * @var array|null array( gstin, name, state, country ) or null when not read yet. */ private static $posted = null; /** * Verify the classic checkout nonce, then read and sanitise the posted fields. * * Called on woocommerce_checkout_process, before WooCommerce may create and log in a new * customer account. The result is kept for the later save hooks of the same request, * because the nonce no longer verifies once the logged-in user has changed. * * @return array array( gstin, name, state, country ); empty values when the nonce is invalid. */ private static function read_posted() { $empty = array( '', '', '', 'IN' ); $nonce = ''; // Same lookup as WC_Checkout::process_checkout(): $_REQUEST, checkout nonce first, then _wpnonce. if ( isset( $_REQUEST['woocommerce-process-checkout-nonce'] ) ) { $nonce = sanitize_text_field( wp_unslash( $_REQUEST['woocommerce-process-checkout-nonce'] ) ); } elseif ( isset( $_REQUEST['_wpnonce'] ) ) { $nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ); } if ( ! wp_verify_nonce( $nonce, 'woocommerce-process_checkout' ) ) { return $empty; } $gstin = isset( $_POST['billing_ebgi_gstin'] ) ? EBGI_GSTIN_Validator::normalize( sanitize_text_field( wp_unslash( $_POST['billing_ebgi_gstin'] ) ) ) : ''; $name = isset( $_POST['billing_ebgi_legal_name'] ) ? sanitize_text_field( wp_unslash( $_POST['billing_ebgi_legal_name'] ) ) : ''; $state = isset( $_POST['billing_state'] ) ? sanitize_text_field( wp_unslash( $_POST['billing_state'] ) ) : ''; $country = isset( $_POST['billing_country'] ) ? sanitize_text_field( wp_unslash( $_POST['billing_country'] ) ) : 'IN'; return array( $gstin, $name, $state, $country ); } /** * Posted values for the save hooks: the values read during validation, or a fresh nonce-checked read. * * @return array array( gstin, name, state, country ). */ private static function posted() { if ( null === self::$posted ) { self::$posted = self::read_posted(); } return self::$posted; } /** * Validate on woocommerce_checkout_process. */ public static function validate_classic() { self::$posted = self::read_posted(); list( $gstin, , $state, $country ) = self::$posted; $check = self::check( $gstin, $state, $country ); if ( is_wp_error( $check ) ) { wc_add_notice( $check->get_error_message(), 'error', array( 'id' => 'billing_ebgi_gstin' ) ); } } /** * Save on the order object (WooCommerce saves the order right after this hook). * * @param WC_Order $order Order. * @param array $data Posted checkout data. */ public static function save_classic( $order, $data ) { list( $gstin, $name ) = self::posted(); self::store_on_order( $order, $gstin, $name ); // The order is the last consumer (WC runs the customer update before creating the order): // forget the values so a later order in the same PHP request never inherits them. self::$posted = null; } /** * Remember GSTIN on the customer for next checkout (pre-fills via WC_Checkout::get_value()). * * @param WC_Customer $customer Customer. * @param array $data Posted data. */ public static function save_customer( $customer, $data ) { list( $gstin, $name ) = self::posted(); if ( '' !== $gstin && EBGI_GSTIN_Validator::is_valid( $gstin ) ) { $customer->update_meta_data( 'billing_ebgi_gstin', $gstin ); $customer->update_meta_data( 'billing_ebgi_legal_name', $name ); } } /** * Put validated values on the order. * * @param WC_Order $order Order. * @param string $gstin GSTIN. * @param string $name Legal name. */ private static function store_on_order( $order, $gstin, $name ) { $gstin = EBGI_GSTIN_Validator::normalize( $gstin ); if ( '' === $gstin || ! EBGI_GSTIN_Validator::is_valid( $gstin ) ) { $order->delete_meta_data( self::META_GSTIN ); $order->delete_meta_data( self::META_NAME ); return; } $order->update_meta_data( self::META_GSTIN, $gstin ); $order->update_meta_data( self::META_NAME, '' !== $name ? $name : $order->get_billing_company() ); } /** * Live validation script on the classic checkout page. */ public static function enqueue() { if ( ! function_exists( 'is_checkout' ) || ! is_checkout() || is_order_received_page() ) { return; } wp_register_script( 'ebgi-gstin-validator', EBGI_URL . 'assets/js/ebgi-gstin-validator.js', array(), EBGI_VERSION, true ); wp_enqueue_script( 'ebgi-checkout', EBGI_URL . 'assets/js/ebgi-checkout.js', array( 'jquery', 'ebgi-gstin-validator' ), EBGI_VERSION, true ); wp_localize_script( 'ebgi-checkout', 'ebgiCheckout', array( 'enforceStateMatch' => 'yes' === EBGI_Settings::get( 'enforce_state_match' ), 'i18n' => array( 'valid' => __( 'Valid GSTIN ✓', '8-bit-gst-invoice-for-woocommerce' ), 'length' => self::error_message( 'length' ), 'format' => self::error_message( 'format' ), 'state_code' => self::error_message( 'state_code' ), 'checksum' => self::error_message( 'checksum' ), 'state_mismatch' => self::error_message( 'state_mismatch' ), ), ) ); wp_enqueue_style( 'ebgi-checkout', EBGI_URL . 'assets/css/ebgi-checkout.css', array(), EBGI_VERSION ); } /* --------------------------------------------------------------------- Block checkout */ /** * Register additional checkout fields (WooCommerce 8.6+ Additional Checkout Fields API). */ public static function register_block_fields() { if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) { return; } woocommerce_register_additional_checkout_field( array( 'id' => self::BLOCK_GSTIN, 'label' => __( 'Purchase for Business? Enter GSTIN (Optional)', '8-bit-gst-invoice-for-woocommerce' ), 'optionalLabel' => __( 'Purchase for Business? Enter GSTIN (Optional)', '8-bit-gst-invoice-for-woocommerce' ), 'location' => 'contact', 'type' => 'text', 'required' => false, 'show_in_order_confirmation' => false, 'sanitize_callback' => array( 'EBGI_GSTIN_Validator', 'normalize' ), 'validate_callback' => array( __CLASS__, 'validate_block_gstin' ), ) ); woocommerce_register_additional_checkout_field( array( 'id' => self::BLOCK_NAME, 'label' => __( 'Business Registered Name', '8-bit-gst-invoice-for-woocommerce' ), 'optionalLabel' => __( 'Business Registered Name (Optional)', '8-bit-gst-invoice-for-woocommerce' ), 'location' => 'contact', 'type' => 'text', 'required' => false, 'show_in_order_confirmation' => false, ) ); } /** * Field-level validation (format + Mod-36 checksum) for the block field. * * @param mixed $value Value. * @return true|WP_Error */ public static function validate_block_gstin( $value ) { $result = EBGI_GSTIN_Validator::validate( (string) $value ); if ( 'empty' === $result['code'] || $result['valid'] ) { return true; } return new WP_Error( 'ebgi_invalid_gstin', self::error_message( $result['code'] ) ); } /** * Store API: cross-check GSTIN with billing state, then copy to canonical meta. * * @param WC_Order $order Order. * @param WP_REST_Request $request Request. * @throws \Automattic\WooCommerce\StoreApi\Exceptions\RouteException When the GSTIN is invalid. */ public static function save_block( $order, $request ) { $gstin = EBGI_GSTIN_Validator::normalize( (string) $order->get_meta( '_wc_other/' . self::BLOCK_GSTIN ) ); $name = sanitize_text_field( (string) $order->get_meta( '_wc_other/' . self::BLOCK_NAME ) ); $check = self::check( $gstin, $order->get_billing_state(), $order->get_billing_country() ); if ( is_wp_error( $check ) ) { if ( class_exists( '\Automattic\WooCommerce\StoreApi\Exceptions\RouteException' ) ) { throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException( esc_html( $check->get_error_code() ), esc_html( $check->get_error_message() ), 400 ); } $gstin = ''; } self::store_on_order( $order, $gstin, $name ); $order->save(); } /* --------------------------------------------------------------------- Display */ /** * GSTIN of an order (canonical meta, falling back to the raw block field). * * @param WC_Order $order Order. * @return string */ public static function get_gstin( $order ) { $gstin = (string) $order->get_meta( self::META_GSTIN ); if ( '' === $gstin ) { $gstin = EBGI_GSTIN_Validator::normalize( (string) $order->get_meta( '_wc_other/' . self::BLOCK_GSTIN ) ); } return EBGI_GSTIN_Validator::is_valid( $gstin ) ? $gstin : ''; } /** * Business legal name of an order. * * @param WC_Order $order Order. * @return string */ public static function get_legal_name( $order ) { $name = (string) $order->get_meta( self::META_NAME ); if ( '' === $name ) { $name = (string) $order->get_meta( '_wc_other/' . self::BLOCK_NAME ); } if ( '' === $name ) { $name = $order->get_billing_company(); } return $name; } /** * Show + edit GSTIN in the admin order billing column (saved as _billing_ebgi_gstin / _billing_ebgi_legal_name). * * @param array $fields Fields. * @return array */ public static function admin_billing_fields( $fields ) { $fields['ebgi_gstin'] = array( 'label' => __( 'GSTIN', '8-bit-gst-invoice-for-woocommerce' ), 'show' => true, 'wrapper_class' => 'form-field-wide', 'update_callback' => array( __CLASS__, 'admin_save_gstin' ), ); $fields['ebgi_legal_name'] = array( 'label' => __( 'Business Registered Name', '8-bit-gst-invoice-for-woocommerce' ), 'show' => true, 'wrapper_class' => 'form-field-wide', ); return $fields; } /** * Admin save: normalise; refuse invalid GSTINs instead of storing garbage. * * @param string $key Meta key. * @param string $value Value. * @param WC_Order $order Order. */ public static function admin_save_gstin( $key, $value, $order ) { $gstin = EBGI_GSTIN_Validator::normalize( $value ); if ( '' === $gstin ) { $order->delete_meta_data( self::META_GSTIN ); return; } if ( EBGI_GSTIN_Validator::is_valid( $gstin ) ) { $order->update_meta_data( self::META_GSTIN, $gstin ); } elseif ( class_exists( 'WC_Admin_Meta_Boxes' ) ) { /* translators: %s: GSTIN */ WC_Admin_Meta_Boxes::add_error( sprintf( __( 'GSTIN "%s" was not saved because it is invalid.', '8-bit-gst-invoice-for-woocommerce' ), esc_html( $gstin ) ) ); } } /** * Customer-facing order view (My Account + thank-you page). * * @param WC_Order $order Order. */ public static function render_customer_details( $order ) { $gstin = self::get_gstin( $order ); if ( '' === $gstin ) { return; } echo '

'; echo '' . esc_html__( 'GSTIN:', '8-bit-gst-invoice-for-woocommerce' ) . ' ' . esc_html( $gstin ); $name = self::get_legal_name( $order ); if ( '' !== $name ) { echo '
' . esc_html__( 'Business Registered Name:', '8-bit-gst-invoice-for-woocommerce' ) . ' ' . esc_html( $name ); } echo '

'; } /** * Order emails (HTML + plain text). * * @param array $fields Fields. * @param bool $sent_to_admin Admin email. * @param WC_Order $order Order. * @return array */ public static function email_fields( $fields, $sent_to_admin, $order ) { if ( ! $order instanceof WC_Order ) { return $fields; } $gstin = self::get_gstin( $order ); if ( '' !== $gstin ) { $fields['ebgi_gstin'] = array( 'label' => __( 'GSTIN', '8-bit-gst-invoice-for-woocommerce' ), 'value' => $gstin, ); $name = self::get_legal_name( $order ); if ( '' !== $name ) { $fields['ebgi_gst_legal_name'] = array( 'label' => __( 'Business Registered Name', '8-bit-gst-invoice-for-woocommerce' ), 'value' => $name, ); } } return $fields; } }