Jake Vanderwerf
2026-07-12 c204185ae86a98994f80010abf35a190c9406739
inc/ui/Checkout.php
@@ -4,6 +4,7 @@
use JVBase\base\Site;
use JVBase\integrations\Integrations;
use JVBase\meta\Form;
use JVBase\meta\Meta;
if (!defined('ABSPATH')) {
   exit;
@@ -30,11 +31,13 @@
{
    public static function getProvider():Integrations|false
    {
        foreach (Site::getIntegrations() as $integration => $active) {
            if (in_array($integration, ['square', 'helcim']) && $active) {
                return JVB()->connect($integration);
            }
        }
      $int = ['square','helcim'];
      foreach ($int as $i) {
         if (Site::hasIntegration($i)) {
            error_log('Site has '.$i);
            return JVB()->connect($i);
         }
      }
        return false;
    }
   /**
@@ -55,52 +58,51 @@
      $providerName = strtolower($provider->getServiceName());
      $form = sprintf(
            '<aside id="cart" class="right main"><div class="wrap">
             <form id="checkout" data-form-id="checkout" data-save="checkout" data-provider="%s">',
            esc_attr($providerName)
        );
      $aside = '<aside id="cart" class="right main"><div class="wrap">';
      $tabs = new Tabs();
      $tabs->addTab('cartItems')
         ->title('Your Cart')
         ->icon('shopping-cart-simple')
         ->description('Here\'s your order. You can change quantities, remove items, or clear your cart. To checkout, click the checkout tab.')
         ->content(self::cartContent($providerName));
      $tabs = [
         'cartItems' => [
            'title'       => 'Your Order',
            'icon'        => 'cart',
            'description' => 'Here\'s your order. You can change quantities, remove items, or clear your cart.',
            'content'     => self::cartContent(),
         ],
         'checkout' => [
            'title'       => 'Checkout',
            'icon'        => 'checkout',
            'description' => apply_filters('jvb_checkout_description',
               'Securely checkout with your name, email, and payment.',
               $providerName
            ),
            'content' => self::checkoutContent($providerName),
         ],
         'order' => [
            'title'       => 'Your Order',
            'icon'        => 'truck',
            'hidden'      => true,
            'description' => '',
            'content'     => self::orderStatus(),
         ],
      ];
      if (!is_user_logged_in()) {
         $tabs->addTab('thelogin')
            ->title('Login')
            ->description('Login to see past orders and access your saved cards')
            ->content(JVB()->magicLink()->renderForm());
      } else {
         $tabs->addTab('account')
            ->title('Account')
            ->description('Manage your contact information, saved cards, and see past orders')
            ->content(self::renderAccount());
      }
      $form .= jvbRenderTabs($tabs, true);
      $tabs->addTab('checkout')
         ->title('Checkout')
         ->icon('hand-coins')
         ->description(apply_filters('jvb_checkout_description',
            'Securely checkout with your name, email, and payment.', $providerName))
         ->content(self::checkoutContent($providerName));
      $form .= '<div class="cart-total row right">
            <p class="tax">Tax: <span></span></p>
            <p class="total">GRAND TOTAL: <span></span></p>
         </div>
      </form>
      </div>
      $tabs->addTab('order')
         ->title('Your Order')
         ->icon('truck')
         ->hidden()
         ->content(self::orderStatus());
      $aside .= $tabs->render();
      $aside .= '</div>
      </aside>';
      $form .= self::templates($providerName);
      $aside .= self::templates($providerName);
      $actions[] = [
         'button'  => self::toggleButton(),
         'content' => $form,
         'content' => $aside,
      ];
      return $actions;
@@ -115,50 +117,66 @@
    */
   private static function checkoutContent(string $provider): string
   {
      $fields = '<div class="checkout-section">
         <h3>Customer Information</h3>'
         . Form::render('cart_name', null, [
      $name = $email = $phone = '';
      if (is_user_logged_in()) {
         $meta = Meta::forUser(get_current_user_id());
         if ($meta) {
            $name = $meta->get('display_name');
            $email = $meta->get('user_email');
            $phone = $meta->get('phone');
         }
      }
      $form = sprintf(
         '<form id="checkout" data-handled>
            <h3>Your Information</h3>
            %s%s%s',
         Form::render('cart_name', $name, [
            'type'         => 'text',
            'label'        => 'Your Name',
            'required'     => true,
            'autocomplete' => 'name',
         ])
         . Form::render('cart_email', null, [
         ]),
         Form::render('cart_email', $email, [
            'type'         => 'email',
            'label'        => 'Your Email',
            'required'     => true,
            'autocomplete' => 'email',
         ])
         . Form::render('cart_phone', null, [
         ]),
         Form::render('cart_phone', $phone, [
            'type'         => 'tel',
            'label'        => 'Your Phone',
            'required'     => true,
            'autocomplete' => 'phone',
         ]);
         ])
      );
      // Optional sections — integrations can add pickup, scheduling, etc.
      $fields .= apply_filters('jvb_checkout_fields', '', $provider);
      $form .= apply_filters('jvb_checkout_fields', '', $provider);
      $fields .= '</div>';
      // Payment section — provider JS mounts its own UI inside #payment-container
      $fields .= '<div class="checkout-section">
      $form .= '<div class="checkout-section">
         <h3>Payment Information</h3>
         <div id="saved-cards"></div>
         <div id="payment-container" data-provider="' . esc_attr($provider) . '"></div>
      </div>';
      </div>
      </form>';
      return $fields;
      return $form;
   }
   /**
    * Cart items tab: table + account details
    */
   private static function cartContent(): string
   private static function cartContent(string $provider): string
   {
      ob_start();
      ?>
      <div class="cart-items">
      $content = sprintf('<form id="checkout" data-form-id="checkout" data-save="checkout" data-provider="%s">',
            esc_attr($provider)
      );
      $content.= '<div class="cart-items">
         <table>
            <thead>
            <tr>
@@ -184,39 +202,8 @@
                </tfoot>
         </table>
      </div>
      <details class="account">
         <summary>
            <?php
            if (is_user_logged_in()) {
               echo 'Your Favourites and Order History';
            } else {
               echo '<a href="' . wp_login_url(get_the_permalink()) . '">Log in</a> to save your favourites and view order history.';
            }
            ?>
         </summary>
         <?php
         if (is_user_logged_in()) {
            $tabs = [
               'history' => [
                  'title'       => 'Order History',
                  'icon'        => 'checkout',
                  'description' => 'View your past orders and quickly reorder',
                  'content'     => apply_filters('jvb_checkout_order_history', ''),
               ],
               'favourites' => [
                  'title'       => 'Favourites',
                  'icon'        => 'heart',
                  'description' => 'View your favourites',
                  'content'     => apply_filters('jvb_checkout_favourites', ''),
               ],
            ];
            jvbRenderTabs($tabs);
         }
         ?>
      </details>
      <?php
      return ob_get_clean();
      </form>';
      return $content;
   }
   /**
@@ -253,6 +240,11 @@
      return ob_get_clean();
   }
   private static function renderAccount():string
   {
      return '';
   }
   /*****************************************************************
    * TEMPLATES — cloned by JS at runtime
    *****************************************************************/