Plugin Directory

Changeset 3409924


Ignore:
Timestamp:
12/03/2025 05:30:31 PM (5 weeks ago)
Author:
aarsiv
Message:

Onboarding setps added

Location:
shipi
Files:
15 added
2 edited

Legend:

Unmodified
Added
Removed
  • shipi/trunk/readme.txt

    r3323824 r3409924  
    44Requires at least: 4.0.1 
    55Tested up to: 6.8
    6 Stable tag: 1.2.2 
     6Stable tag: 1.3.0 
    77Requires PHP: 5.6 
    88License: GPLv3 or later 
     
    9797== Changelog ==
    9898
     99= 1.3.0 = 
     100* Added Onboarding page.
     101
    99102= 1.2.2 = 
    100103* JS Updated for better compatibility
  • shipi/trunk/shipi.php

    r3319856 r3409924  
    33 * Plugin Name: Shipi
    44 * Description: 15+ Shipping carriers in one package.
    5  * Version: 1.2.2
     5 * Version: 1.3.0
    66 * Author: Shipi
    77 * Author URI: https://myshipi.com/
     
    6767                add_action('woocommerce_admin_order_data_after_shipping_address', array($this, 'order_details_screen_data'));
    6868                add_action( 'add_meta_boxes', array($this, 'create_meta_box'), 10, 1);
     69                add_action( 'wp_ajax_shipi_connect_account', array( $this, 'ajax_shipi_connect_account' ) );
    6970
    7071            }
     
    118119                    array($this, 'shipi_settings_callback')
    119120                );
     121                add_submenu_page(
     122                    'shipi-configuration',
     123                    __('Onboarding', 'shipi'),
     124                    'Onboarding',
     125                    'manage_options',
     126                    'shipi-onboarding',
     127                    array($this, 'shipi_onboarding_callback')
     128                );
     129
    120130            }
    121131           
    122132            function shipi_settings(){
     133                $this->shipi_validate_connection_and_redirect();
    123134                $key = get_option("shipi_integration_key", false);
    124135                if(!$key){
     
    132143            }
    133144            function shipi_settings_callback(){
     145                if( isset($_GET['page']) && $_GET['page'] !== 'shipi-onboarding' ){
     146                    $this->shipi_validate_connection_and_redirect();
     147                }
    134148                $error = '';
    135149                if(isset($_POST["shipi_connection_submit"])){
     
    213227               
    214228            }
     229
     230            public function ajax_shipi_connect_account() {
     231                $error   = '';
     232                $success = '';
     233                if ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) {
     234                    wp_send_json_error( array(
     235                        'message' => __( 'Invalid request method.', 'shipi' ),
     236                    ) );
     237                }
     238                if (
     239                    ! isset( $_POST['shipi_connection_nonce'] ) ||
     240                    ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['shipi_connection_nonce'] ) ), 'shipi_connection_action' )
     241                ) {
     242                    wp_send_json_error( array(
     243                        'message' => __( 'Nonce verification failed.', 'shipi' ),
     244                    ) );
     245                }
     246                $shipi_connection_username = isset( $_POST['shipi_connection_username'] )
     247                    ? sanitize_email( wp_unslash( $_POST['shipi_connection_username'] ) )
     248                    : '';
     249
     250                $shipi_connection_password = isset( $_POST['shipi_connection_password'] )
     251                    ? sanitize_text_field( wp_unslash( $_POST['shipi_connection_password'] ) )
     252                    : '';
     253
     254                if ( ! $shipi_connection_username || ! $shipi_connection_password ) {
     255                    wp_send_json_error( array(
     256                        'message' => __( 'Please enter both email and password.', 'shipi' ),
     257                    ) );
     258                }
     259                $random_nonce = wp_generate_password( 16, false );
     260                set_transient( 'shipi_nonce_temp', $random_nonce, HOUR_IN_SECONDS );
     261
     262                $link_request = wp_json_encode(
     263                    array(
     264                        'site_url'    => esc_url( site_url() ),
     265                        'site_name'   => get_bloginfo( 'name' ),
     266                        'email_address' => $shipi_connection_username,
     267                        'password'    => $shipi_connection_password,
     268                        'nonce'       => $random_nonce,
     269                        'pulgin'      => 'Shipi All in One',
     270                        'platfrom'    => 'woocommerce',
     271                        'update_path' => '/wp-json/shipi/connect',
     272                    )
     273                );
     274
     275                $link_site_url      = 'https://app.myshipi.com/api/link-site.php';
     276                $link_site_response = wp_remote_post(
     277                    $link_site_url,
     278                    array(
     279                        'method'      => 'POST',
     280                        'timeout'     => 45,
     281                        'redirection' => 5,
     282                        'httpversion' => '1.0',
     283                        'blocking'    => true,
     284                        'headers'     => array( 'Content-Type' => 'application/json; charset=utf-8' ),
     285                        'body'        => $link_request,
     286                        'sslverify'   => 0,
     287                    )
     288                );
     289                $raw_body = is_array($link_site_response) && isset($link_site_response['body'])
     290                    ? trim($link_site_response['body'])
     291                    : '';
     292
     293                if ($raw_body) {
     294                    if (preg_match('/(\{.+\})\s*$/s', $raw_body, $match)) {
     295                        $link_site_response = json_decode($match[1], true);
     296                    } else {
     297                        $link_site_response = array();
     298                    }
     299                } else {
     300                    $link_site_response = array();
     301                }
     302                if ( $link_site_response ) {
     303                    if ( isset( $link_site_response['status'] ) && 'error' !== $link_site_response['status'] ) {
     304                        if ( ! empty( $link_site_response['integration_key'] ) ) {
     305                            update_option( 'shipi_integration_key', sanitize_text_field( $link_site_response['integration_key'] ) );
     306                        }
     307
     308                        wp_send_json_success( array(
     309                            'message' => __( 'Connected successfully.', 'shipi' ),
     310                            'data'    => $link_site_response,
     311                        ) );
     312                    } else {
     313                        $msg = ! empty( $link_site_response['message'] ) ? $link_site_response['message'] : __( 'Connection failed.', 'shipi' );
     314                        wp_send_json_error( array(
     315                            'message' => $msg,
     316                        ) );
     317                    }
     318                } else {
     319                    wp_send_json_error( array(
     320                        'message' => __( 'Failed to connect with Shipi.', 'shipi' ),
     321                    ) );
     322                }
     323            }
     324
     325
     326            public function shipi_onboarding_callback(){
     327                $formHTML = '
     328                <div class="shipi-connect-wrapper">
     329                    <div class="shipi-container">
     330                        <h2>
     331                            <img src="'.esc_url(plugin_dir_url(__FILE__) . 'assets/img/shipi_100px.png').'" alt="Shipi Logo">
     332                        </h2>
     333                        <p>Please Enter the below details.</p>
     334                        <div class="shipi-inline-message"></div>
     335                        <form method="post" class="shipi-connect-form">
     336                            <input class="shipi-input" type="email" name="shipi_connection_username" placeholder="Email Address" required />
     337                            <input class="shipi-input" type="password" name="shipi_connection_password" placeholder="Password" required />
     338                            <p class="shipi-small-note">
     339                                If you already have an account with Shipi, it will connect.
     340                                Otherwise, it will create a new user and connect the site.
     341                            </p>
     342                            '.wp_nonce_field('shipi_connection_action', 'shipi_connection_nonce', true, false).'
     343                            <input class="shipi-button" type="submit" name="shipi_connection_submit" value="Connect or Create Account" />
     344                        </form>
     345                        <div class="steps">
     346                            <h3>Next Steps:</h3>
     347                            <ol>
     348                                <li>Setup the shipping account.</li>
     349                                <li>Verify the shipping rates in your checkout.</li>
     350                                <li>Generate the shipping label.</li>
     351                            </ol>
     352                        </div>
     353                        <div class="support-note">
     354                            If you need any help or you are stuck on this place,
     355                            <a href="https://calendar.app.google/aVfnftudzdtZwDVT9" target="_blank"><b>Contact us</b></a>
     356                            and we will set it up for you.
     357                        </div>
     358                        <div class="shipi-links">
     359                            <a href="https://calendar.app.google/aVfnftudzdtZwDVT9" target="_blank">Book Meeting with Us</a>
     360                            <a href="https://app.myshipi.com/support/" target="_blank">Create Support Ticket</a>
     361                        </div>
     362                    </div>
     363                </div>';
     364                $formJS = json_encode($formHTML);
     365                $integration_key = get_option("shipi_integration_key", false);
     366                ?>
     367                <?php
     368                $checkSVG = '';
     369                ?>
     370                <div class="shipi-onboard-wrapper">
     371                    <h2 class="shipi-onboard-title">Get ready for Fast Shipping with Shipi</h2>
     372                    <p class="shipi-onboard-subtext">
     373                        Here’s your personalized guide to get started and streamline your shipping process.
     374                    </p>
     375                    <div class="shipi-progress-box">
     376                        <div class="shipi-progress-header">
     377                            <span>Setup guide</span>
     378                            <span class="shipi-progress-badge" id="shipiProgress">0/2 Completed</span>
     379                        </div>
     380                        <p style="margin:5px 0 0;color:#777;font-size:13px;">Use this personalised guide to get started</p>
     381                    </div>
     382                    <div class="shipi-onboard-box" id="step1Box">
     383                        <div class="shipi-onboard-text">
     384                            <div class="shipi-onboard-label">
     385                                1. Connect with Shipi
     386                                <span id="step1Check"></span>
     387                            </div>
     388                            <div class="shipi-onboard-small">
     389                                Connect your store with Shipi to start managing shipments.
     390                            </div>
     391                        </div>
     392
     393                        <div class="shipi-right" id="step1Action">
     394                            <?php if(!$integration_key): ?>
     395                                <button class="shipi-btn" id="shipiConnectButton">Connect</button>
     396                            <?php else: ?>
     397                                <div class="shipi-connected"><?php echo $checkSVG; ?> Connected</div>
     398                            <?php endif; ?>
     399                        </div>
     400                    </div>
     401                    <div id="step2Box" class="shipi-step-disabled" style="justify-content:space-between;align-items:center;">
     402                        <div>
     403                            <div class="shipi-step-toggle">
     404                                2. Shipping Account Configuration <span id="step2Check"></span>
     405                            </div>
     406                            <div class="shipi-onboard-small">
     407                                Configure your carrier accounts and finish your onboarding setup.
     408                            </div>
     409                        </div>
     410                        <div class="shipi-right" id="step2Action"></div>
     411                    </div><br>
     412                    <div id="shipiCompletedBlock" class="shipi-hidden" style="text-align:center;margin-top:30px;">
     413                        <div style="font-size:50px;margin-bottom:10px;">🎉</div><br>
     414                        <h2 style="font-size:26px;font-weight:700;margin:0 0 5px;">Onboarding Completed!</h2>
     415                        <p style="font-size:14px;color:#666;margin-bottom:20px;">You're all set! Access your Shipi dashboard below.</p>
     416                        <div style="display:flex;gap:12px;justify-content:center;flex-wrap:wrap;">
     417                            <button class="shipi-nav-btn" data-link="shipments">Shipments</button>
     418                            <button class="shipi-nav-btn" data-link="shipping_accounts">Shipping Accounts</button>
     419                            <button class="shipi-nav-btn" data-link="support">Support</button>
     420                        </div>
     421                    </div>
     422                </div>
     423                <div id="shipiOnboardingModal" class="shipi-onboard-modal shipi-hidden">
     424                    <div class="shipi-onboard-modal-content">
     425                        <button class="shipi-onboard-close">×</button>
     426                        <div id="shipiOnboardingFormContainer"></div>
     427                    </div>
     428                </div>
     429                <div id="shipiOfferSetupModal" class="shipi-onboard-modal shipi-hidden">
     430                    <div class="shipi-onboard-modal-content offer-box">
     431                        <button class="shipi-onboard-close">×</button>
     432                        <div id="shipiOfferMainBlock">
     433                            <h2 class="offer-title">Want our team to fully configure Shipi for you?</h2>
     434                            <p class="offer-subtitle">
     435                                We offer 100% free setup — carriers, rates, API keys, workflows…
     436                                everything handled by experts.
     437                            </p>
     438
     439                            <div class="offer-actions">
     440                                <button id="shipiSetupYesButton" class="offer-primary">Yes, Setup for Me (Free)</button>
     441                                <button id="shipiSetupNoButton" class="offer-secondary">No, I’ll Configure Myself</button>
     442                            </div>
     443                        </div>
     444                        <div id="shipiOfferSuccessBlock" class="shipi-hidden" style="text-align:center;">
     445                            <div style="font-size:20px;color:#0ba156;margin-bottom:10px;">✔</div>
     446                            <h3 style="margin-bottom:5px;font-size:20px;font-weight:600;">Request Received!</h3>
     447                            <p style="color:#555;margin-bottom:20px;">
     448                                Our team will contact you in 24 hours to set up your account.
     449                            </p>
     450
     451                            <button id="shipiOfferDoneBtn" class="offer-secondary">Done</button>
     452                        </div>
     453                    </div>
     454                </div>
     455                <style>
     456                .shipi-onboard-page{background:#f7f8fa;padding:30px 0;
     457                }
     458                .shipi-onboard-wrapper{max-width:880px;margin:0 auto;font-family:'Poppins',system-ui,sans-serif;
     459                }
     460                .shipi-onboard-title{text-align:center;font-size:30px;font-weight:700;margin-bottom:8px;color:#111;
     461                }
     462                .shipi-onboard-subtext{text-align:center;color:#666;font-size:15px;margin-bottom:32px;
     463                }
     464                .shipi-progress-box,
     465                .shipi-onboard-box,
     466                .shipi-step-disabled,
     467                .shipi-step-enabled,
     468                .shipi-step-completed{background:#fff;border:1px solid #e3e3e3;border-radius:12px;padding:20px 24px;
     469                }
     470                .shipi-progress-box{margin-bottom:28px;
     471                }
     472                .shipi-progress-header{display:flex;justify-content:space-between;font-weight:600;
     473                }
     474                .shipi-progress-badge{background:#eef0f3;border-radius:30px;padding:5px 12px;font-size:12px;color:#555;
     475                }
     476                .shipi-onboard-box{margin-bottom:22px;
     477                }
     478                .shipi-onboard-text{max-width:75%;}
     479                .shipi-onboard-label, .shipi-step-toggle{font-size:18px;font-weight:600;margin-bottom:4px;
     480                }
     481                .shipi-onboard-small{font-size:14px;color:#666;
     482                }
     483                .shipi-step-disabled{opacity:0.55;
     484                }
     485                .shipi-step-enabled{}
     486                .shipi-step-completed{border:1px solid #e3e3e3;
     487                }
     488                .shipi-btn{background:#0ba156;color:#fff;border:none;padding:9px 22px;font-size:14px;font-weight:500;border-radius:7px;cursor:pointer;
     489                }
     490                .shipi-right, .shipi-connected{text-align: end;
     491                }
     492                .shipi-btn:hover{background:#00994a;
     493                }
     494                .shipi-connected{background:#e8ffef;color:#0ba156;display:inline-block;align-items:center;gap:6px;padding:6px 14px;border-radius:22px;border:1px solid #d8f6e1;font-weight:600;
     495                }
     496                #shipiCompletedBlock{margin-top:40px;
     497                }
     498                .shipi-nav-btn{background:#fff;border:1px solid #ddd;padding:9px 20px;font-size:14px;border-radius:7px;cursor:pointer;
     499                }
     500                .shipi-nav-btn:hover{background:#f3f3f3;
     501                }
     502                .shipi-onboard-modal{position:fixed;inset:0;background:rgba(0,0,0,0.55);display:flex;align-items:center;justify-content:center;z-index:999999;
     503                }
     504                .shipi-onboard-modal-content{background:#fff;width:620px;max-width:95%;padding:42px 46px;border-radius:18px;box-shadow:0 10px 38px rgba(0,0,0,0.18);position:relative;
     505                }
     506                .shipi-onboard-close{position:absolute;top:18px;right:22px;font-size:22px;cursor:pointer;border:none;background:none;
     507                }
     508                .shipi-connect-wrapper{display:flex;justify-content:center;
     509                }
     510                .shipi-container{width:100%;text-align:center;
     511                }
     512                .shipi-connect-form h2{display:flex;justify-content:center;
     513                }
     514                .shipi-connect-form h2 img{width:130px;margin-bottom:10px;
     515                }
     516                .shipi-input{width:100%;height:46px;border-radius:8px;border:1.6px solid #dbdbdb;padding:12px 14px;font-size:15px;margin-bottom:14px;
     517                }
     518                .shipi-input:focus{outline:none;border-color:#0ba156;box-shadow:0 0 0 2px rgba(11,161,86,0.25);
     519                }
     520                .shipi-button{width:100%;background:#0ba156;padding:13px 0;border:none;color:#fff;font-size:15px;font-weight:600;border-radius:8px;margin-top:8px;
     521                }
     522                .shipi-button:hover{background:#00994a;
     523                }
     524                .shipi-small-note,
     525                .shipi-container p{color:#555;font-size:14px;
     526                }
     527                .steps{background:#f7f8fa;border:1px solid #e7e7e7;border-radius:12px;padding:22px 24px;margin-top:25px;text-align:left;
     528                }
     529                .steps h3{font-size:17px;font-weight:600;margin-bottom:12px;
     530                }
     531                .steps ol{margin:0;padding-left:20px;
     532                }
     533                .steps ol li{margin-bottom:8px;color:#444;
     534                }
     535                .support-note{margin-top:18px;padding:16px;border-radius:8px;background:#fff4df;border:1px solid #ffe1b3;font-size:14px;color:#a66a00;line-height:1.5;
     536                }
     537                .support-note a{text-decoration:underline;color:#0067d1;
     538                }
     539                .shipi-links{margin-top:20px;
     540                }
     541                .shipi-links a{text-decoration:none;color:#0067d1;font-weight:500;
     542                }
     543                .shipi-links a:hover{text-decoration:underline;
     544                }
     545                .shipi-hidden{display:none!important;}
     546                .shipi-inline-message {margin-bottom: 12px;font-size: 14px;text-align:center;
     547                }
     548                .shipi-msg-success {background:#e8ffed;border:1px solid #b9efc7;padding:10px;border-radius:6px;color:#0a8b45;font-weight:500;
     549                }
     550                .shipi-msg-error {background:#ffe8e8;border:1px solid #ffbaba;padding:10px;border-radius:6px;color:#b30000;font-weight:500;
     551                }
     552                .offer-box {max-width: 550px;text-align: center;padding: 45px 40px !important;}
     553                .offer-title {font-size: 22px;font-weight: 700;margin-bottom: 12px;color:#222;}
     554                .offer-subtitle {font-size:15px;color:#555;margin-bottom:22px;
     555                }
     556                .offer-actions button {margin:0 8px;
     557                }
     558                .offer-primary {background:#0ba156;padding:10px 20px;border:none;color:#fff;border-radius:8px;font-weight:600;cursor:pointer;}
     559                .offer-primary:hover {
     560                    background:#009b47;
     561                }
     562                .offer-secondary {background:#ececec;padding:10px 20px;border:none;color:#333;border-radius:8px;font-weight:600;cursor:pointer;}
     563                .offer-secondary:hover {
     564                    background:#dcdcdc;
     565                }
     566                .shipi-loading-spinner {width: 16px;height: 16px;border: 2px solid #ffffff70;border-top-color: #fff;border-radius: 50%;animation: shipiSpin 0.7s linear infinite;display: inline-block;vertical-align: middle;margin-right: 6px;}
     567                @keyframes shipiSpin {
     568                    100% { transform: rotate(360deg); }
     569                }
     570                </style>
     571                <script>
     572                    document.addEventListener("DOMContentLoaded", () => {
     573                        const integration_key = "<?php echo esc_js($integration_key); ?>";
     574                        const checkSVG = `<?php echo $checkSVG; ?>`;
     575                        if(integration_key){
     576                            jQuery.ajax({
     577                                url: "https://app.myshipi.com/embed/check_key.php",
     578                                type: "POST",
     579                                data: {
     580                                    site_url: "<?php echo esc_url(site_url()); ?>",
     581                                    key: integration_key
     582                                },
     583                                dataType: "json",
     584                                success: function(response) {
     585                                    let steps = parseInt(response.steps);
     586                                    if(steps === 1){
     587                                        jQuery("#step1Action").html(`<button class="shipi-btn" id="shipiConnectButton">Connect</button>`);
     588                                        jQuery("#step1Check").html('');
     589                                    }
     590                                    if(steps >= 2){
     591                                        jQuery("#step1Action").html(`<div class="shipi-connected">${checkSVG} Connected</div>`);
     592                                        jQuery("#step1Check").html(checkSVG);
     593                                    }
     594                                    if(steps === 1){
     595                                        jQuery("#step2Box").removeClass().addClass("shipi-step-disabled");
     596                                        jQuery("#step2Action").html('');
     597                                    }
     598                                    if(steps === 2){
     599                                        jQuery("#step2Box").removeClass().addClass("shipi-step-enabled");
     600                                        jQuery("#step2Action").html(
     601                                            `<button class="shipi-btn" onclick="window.location.href='<?php echo admin_url('admin.php?page=shipi-settings'); ?>'">Configure Shipping Accounts</button>`
     602                                        );
     603                                        jQuery("#step2Check").html('');
     604                                    }
     605                                    if(steps === 3){
     606                                        jQuery("#step2Box").removeClass().addClass("shipi-step-completed");
     607                                        jQuery("#step2Action").html(
     608                                            `<div class="shipi-connected">${checkSVG} Accounts Configured</div>`
     609                                        );
     610                                        jQuery("#step2Check").html(checkSVG);
     611                                    }
     612                                    if(steps === 3){
     613                                        jQuery("#step2Box").removeClass().addClass("shipi-step-completed");
     614                                        jQuery("#step2Action").html(
     615                                            `<div class="shipi-connected">${checkSVG} Accounts Configured</div>`
     616                                        );
     617                                        jQuery("#step2Check").html(checkSVG);
     618                                        jQuery("#shipiCompletedBlock").removeClass("shipi-hidden");
     619                                    }
     620                                    let completed = 0;
     621                                    if(steps === 2) completed = 1;
     622                                    if(steps === 3) completed = 2;
     623                                    jQuery("#shipiProgress").text(completed + "/2 Completed");
     624                                }
     625                            });
     626                        }
     627                        jQuery(document).on("click", "#shipiConnectButton", function(){
     628                            jQuery("#shipiOnboardingModal").removeClass("shipi-hidden");
     629                            jQuery("#shipiOnboardingFormContainer").html(<?php echo $formJS; ?>);
     630                        });
     631                        jQuery(document).on("click", ".shipi-onboard-close", function(){
     632                            jQuery("#shipiOnboardingModal").addClass("shipi-hidden");
     633                        });
     634                        jQuery(document).on("click", "#shipiOnboardingModal", function(e){
     635                            if(e.target.id === "shipiOnboardingModal"){
     636                                jQuery("#shipiOnboardingModal").addClass("shipi-hidden");
     637                            }
     638                        });
     639                        jQuery(document).on("click", ".shipi-nav-btn", function(){
     640                            let link = jQuery(this).attr("data-link");
     641                            switch(link){
     642                                case "shipments":
     643                                    window.location.href = "<?php echo admin_url('admin.php?page=shipi-configuration'); ?>";
     644                                    break;
     645                                case "shipping_accounts":
     646                                    window.location.href = "<?php echo admin_url('admin.php?page=shipi-settings'); ?>";
     647                                    break;
     648                                case "support":
     649                                    window.open("https://app.myshipi.com/support", "_blank");
     650                                    break;
     651                            }
     652                        });
     653                        jQuery(document).on("submit", ".shipi-connect-form", function(e){
     654                            e.preventDefault();
     655                            const $form   = jQuery(this);
     656                            const $btn    = $form.find(".shipi-button");
     657                            const $msgBox = $form.closest(".shipi-container").find(".shipi-inline-message");
     658                            $msgBox.removeClass("shipi-msg-success shipi-msg-error").text("");
     659                            const formData = $form.serialize() + "&action=shipi_connect_account";
     660                            $btn.prop("disabled", true).val("Connecting...");
     661                            jQuery.post(ajaxurl, formData)
     662                                .done(function(response){
     663                                    if (response && response.success) {
     664                                        $msgBox
     665                                            .addClass("shipi-msg-success")
     666                                            .text(
     667                                                (response.data && response.data.message)
     668                                                ? response.data.message
     669                                                : "Connected successfully."
     670                                            );
     671                                        setTimeout(function(){
     672                                            jQuery("#shipiOnboardingModal").addClass("shipi-hidden");
     673                                            jQuery("#shipiOfferSetupModal").removeClass("shipi-hidden");
     674                                            window.shipi_user_email = $form.find("[name='shipi_connection_username']").val();
     675                                        }, 1000);
     676                                    } else {
     677                                        let msg =
     678                                            response?.data?.message
     679                                            ?? "Connection failed. Please try again.";
     680                                        $msgBox
     681                                            .addClass("shipi-msg-error")
     682                                            .text(msg);
     683                                    }
     684                                })
     685                                .fail(function(){
     686                                    $msgBox
     687                                        .addClass("shipi-msg-error")
     688                                        .text("Something went wrong. Please try again.");
     689                                })
     690                                .always(function(){
     691                                    $btn.prop("disabled", false).val("Connect or Create Account");
     692                                });
     693                        });
     694                        jQuery(document).on("click", "#shipiSetupYesButton", function(){
     695                            let email = window.shipi_user_email || "";
     696                            if(!email) return;
     697                            let $btn = jQuery(this);
     698                            let originalText = $btn.text();
     699                            $btn.prop("disabled", true)
     700                                .html(`<span class="shipi-loading-spinner"></span> Sending...`);
     701                            let payload = {
     702                                email: email,
     703                                platform: "wordpress-shipi"
     704                            };
     705                            jQuery.ajax({
     706                                url: "https://app.myshipi.com/setup_request.php",
     707                                method: "POST",
     708                                data: JSON.stringify(payload),
     709                                contentType: "application/json",
     710                                dataType: "text",
     711                                success: function(res) {
     712                                    let raw = (typeof res === "string") ? res : JSON.stringify(res);
     713                                    let start = raw.indexOf("{");
     714                                    let end   = raw.lastIndexOf("}");
     715                                    let data = {};
     716                                    if (start !== -1 && end !== -1) {
     717                                        let jsonString = raw.substring(start, end + 1);
     718                                        try {
     719                                            data = JSON.parse(jsonString);
     720                                        } catch (e) {
     721                                            console.log("JSON Parse Failed:", jsonString);
     722                                        }
     723                                    } else {
     724                                        console.log("No JSON found in response:", raw);
     725                                    }
     726
     727                                    if (data.status === "success") {
     728                                        jQuery("#shipiOfferMainBlock").addClass("shipi-hidden");
     729                                        jQuery("#shipiOfferSuccessBlock").removeClass("shipi-hidden");
     730                                    } else {
     731                                        alert(data.message || "Something went wrong. Try again.");
     732                                        $btn.prop("disabled", false).text(originalText);
     733                                    }
     734                                },
     735                                error: function(xhr, status, error) {
     736                                    console.log("AJAX ERROR:", status, error);
     737                                    console.log("RAW RESPONSE:", xhr.responseText);
     738                                    alert("Something went wrong. Try again.");
     739                                    $btn.prop("disabled", false).text(originalText);
     740                                }
     741                            });
     742                        });
     743                        jQuery(document).on("click", "#shipiOfferDoneBtn", function(){
     744                            window.location.reload();
     745                        });
     746                        jQuery(document).on("click", "#shipiSetupNoButton", function(){
     747                            window.location.reload();
     748                        });
     749                    });
     750                </script>
     751                <?php
     752            }
     753            private function shipi_validate_connection_and_redirect(){
     754                $integration_key = get_option("shipi_integration_key", false);
     755                if(!$integration_key){
     756                    wp_redirect(admin_url('admin.php?page=shipi-onboarding'));
     757                    exit;
     758                }
     759                $response = wp_remote_post(
     760                    "https://app.myshipi.com/embed/check_key.php",
     761                    array(
     762                        'timeout' => 30,
     763                        'blocking' => true,
     764                        'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
     765                        'body' => array(
     766                            'site_url' => site_url(),
     767                            'key'      => $integration_key
     768                        ),
     769                        'sslverify' => false,
     770                    )
     771                );
     772                $body = isset($response['body']) ? trim($response['body']) : '';
     773                $data = [];
     774                if($body && preg_match('/(\{.+\})/s', $body, $match)){
     775                    $data = json_decode($match[1], true);
     776                }
     777                if( empty($data['steps']) || intval($data['steps']) === 1 ){
     778                    wp_redirect(admin_url('admin.php?page=shipi-onboarding&st=not_connected'));
     779                    exit;
     780                }
     781            }
     782
    215783            function wc_new_order_column( $columns ) {
    216784                $columns['shipi_shipping'] = 'Shipping';
Note: See TracChangeset for help on using the changeset viewer.