| | |
| | | use JVBase\meta\Form; |
| | | use JVBase\meta\Meta; |
| | | use Exception; |
| | | use JVBase\registry\PostTypeRegistrar; |
| | | use JVBase\registrar\Fields; |
| | | use JVBase\registrar\Posts; |
| | | use JVBase\registrar\Registrar; |
| | | use WP_Error; |
| | | use JVBase\ui\Checkout; |
| | | use JVBase\managers\queue\TypeConfig; |
| | |
| | | */ |
| | | class Square extends Integrations |
| | | { |
| | | protected array $allowedContent = [ |
| | | 'REGULAR', |
| | | 'FOOD_AND_BEV', |
| | | 'APPOINTMENTS_SERVICE', |
| | | 'DIGITAL', |
| | | 'EVENT', |
| | | 'DONATION' |
| | | ]; |
| | | /** |
| | | * Square API Configuration |
| | | */ |
| | |
| | | * OAuth Configuration |
| | | */ |
| | | protected bool $isOAuthService = true; |
| | | |
| | | protected string $orderPostType = '_sq_order'; |
| | | protected array $newOrder = []; |
| | | protected array $oauth = [ |
| | | 'authorize' => '', |
| | | 'token' => '', |
| | |
| | | |
| | | $this->refresh_interval = 7 * DAY_IN_SECONDS; |
| | | |
| | | $this->newOrder = [ |
| | | 'post_type' => $this->orderPostType, |
| | | 'post_status' => 'PROPOSED', |
| | | ]; |
| | | |
| | | // Define credential fields |
| | | $this->fields = [ |
| | | 'environment' => [ |
| | |
| | | 'sync_to_square' => 'Sync Site to Square', |
| | | ] |
| | | ); |
| | | |
| | | add_action('init', [$this, 'registerSquarePostTypes']); |
| | | add_action('init', [$this, 'registerSquarePostTypes'], 5); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | } |
| | | |
| | | public function getSquarePostConfig(string $post = 'all'):array |
| | | public function getOrderFields():array |
| | | { |
| | | $posts = [ |
| | | '_sq_orders' => [ |
| | | 'singular' => 'Square Order', |
| | | 'plural' => 'Square Orders', |
| | | 'public' => false, |
| | | 'fields' => [ |
| | | return [ |
| | | 'post_title' => [ |
| | | 'type' => 'text', |
| | | 'label' => 'Order Number' |
| | |
| | | 'square_order_id' => [ |
| | | 'type' => 'text', |
| | | 'label' => 'Square Order ID', |
| | | 'readonly' => true |
| | | ], |
| | | 'square_payment_id' => [ |
| | | 'type' => 'text', |
| | | 'label' => 'Square Payment ID', |
| | | 'readonly' => true |
| | | ], |
| | | 'square_customer_id' => [ |
| | | 'type' => 'text', |
| | | 'label' => 'Square Customer ID', |
| | | 'readonly' => true |
| | | ], |
| | | 'amount' => [ |
| | | 'type' => 'number', |
| | | 'label' => 'Total Amount (cents)', |
| | | 'readonly' => true |
| | | ], |
| | | 'status' => [ |
| | | 'post_status' => [ |
| | | 'type' => 'select', |
| | | 'label' => 'Order Status', |
| | | 'options' => [ |
| | |
| | | 'COMPLETED' => 'Completed', |
| | | 'CANCELED' => 'Canceled' |
| | | ], |
| | | 'readonly' => true |
| | | ], |
| | | 'fulfillment_status' => [ |
| | | 'type' => 'select', |
| | |
| | | 'CANCELED' => 'Canceled', |
| | | 'FAILED' => 'Failed' |
| | | ], |
| | | 'readonly' => true |
| | | ], |
| | | 'pickup_time' => [ |
| | | 'type' => 'datetime', |
| | |
| | | 'customer_email' => [ |
| | | 'type' => 'email', |
| | | 'label' => 'Customer Email', |
| | | 'readonly' => true |
| | | ], |
| | | 'customer_name' => [ |
| | | 'type' => 'text', |
| | | 'label' => 'Customer Name', |
| | | 'readonly' => true |
| | | ], |
| | | 'customer_phone' => [ |
| | | 'type' => 'tel', |
| | | 'type' => 'phone', |
| | | 'label' => 'Customer Phone', |
| | | 'readonly' => true |
| | | ], |
| | | 'special_instructions' => [ |
| | | 'type' => 'textarea', |
| | | 'label' => 'Special Instructions', |
| | | 'readonly' => true |
| | | ], |
| | | 'items' => [ |
| | | 'type' => 'repeater', |
| | | 'label' => 'Order Items', |
| | | 'readonly' => true, |
| | | 'fields' => [ |
| | | 'name' => ['type' => 'text', 'label' => 'Item Name'], |
| | | 'quantity' => ['type' => 'number', 'label' => 'Quantity'], |
| | |
| | | 'receipt_url' => [ |
| | | 'type' => 'url', |
| | | 'label' => 'Receipt URL', |
| | | 'readonly' => true |
| | | ], |
| | | 'created_at' => [ |
| | | 'type' => 'datetime', |
| | | 'label' => 'Created At', |
| | | 'readonly' => true |
| | | ], |
| | | 'updated_at' => [ |
| | | 'type' => 'datetime', |
| | | 'label' => 'Last Updated', |
| | | 'readonly' => true |
| | | ] |
| | | ] |
| | | ] |
| | | ]; |
| | | |
| | | if ($post === 'all'){ |
| | | return $posts; |
| | | }elseif(array_key_exists($post, $posts)) { |
| | | return $posts[$post]; |
| | | } |
| | | return []; |
| | | ]; |
| | | } |
| | | |
| | | public function registerSquarePostTypes():void |
| | | { |
| | | $squarePostTypes = $this->getSquarePostConfig(); |
| | | foreach ($squarePostTypes as $slug => $config) { |
| | | $registrar = new PostTypeRegistrar($slug, $config); |
| | | $registrar->register(); |
| | | $orders = Registrar::forPost('_sq_orders', 'Square Order', 'Square Orders'); |
| | | $orders->make([ |
| | | 'public' => true |
| | | ]); |
| | | $orders->setAll(['system']); |
| | | |
| | | $fields = $orders->fields(); |
| | | foreach ($this->getOrderFields() as $fieldName => $config) { |
| | | $fields->addField($fieldName, $config); |
| | | } |
| | | } |
| | | |
| | |
| | | add_action('wp_login', [$this, 'trackUserLogin'], 10, 2); |
| | | add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']); |
| | | |
| | | // Shared checkout UI (replaces outputCheckout) |
| | | add_filter('jvbAdditionalActions', [Checkout::class, 'render']); |
| | | |
| | | // Square-specific checkout description |
| | | |
| | | add_filter('jvb_checkout_description', function (string $desc, string $provider) { |
| | | if ($provider === 'square') { |
| | | return 'Securely checkout with your name, email, and payments processed by Square.'; |
| | |
| | | $queue = JVB()->queue(); |
| | | $executor = new IntegrationExecutor(); |
| | | |
| | | $queue->registry()->register('square_sync_to', new TypeConfig( |
| | | $queue->registry()->register(self::$syncTo, new TypeConfig( |
| | | executor: $executor, |
| | | chunkKey: 'items', |
| | | chunkSize: 50, |
| | | maxRetries: 3 |
| | | )); |
| | | |
| | | $queue->registry()->register('square_delete_from', new TypeConfig( |
| | | $queue->registry()->register(self::$deleteFrom, new TypeConfig( |
| | | executor: $executor, |
| | | chunkKey: 'external_ids', |
| | | chunkSize: 200, |
| | | maxRetries: 2 |
| | | )); |
| | | |
| | | $queue->registry()->register('square_sync_from', new TypeConfig( |
| | | $queue->registry()->register(self::$syncFrom, new TypeConfig( |
| | | executor: $executor, |
| | | maxRetries: 3 |
| | | )); |
| | | |
| | | $queue->registry()->register('square_sync_customer', new TypeConfig( |
| | | $queue->registry()->register(self::$syncCustomer, new TypeConfig( |
| | | executor: $executor, |
| | | maxRetries: 2 |
| | | )); |
| | | |
| | | $queue->registry()->register('square_import', new TypeConfig( |
| | | $queue->registry()->register(self::$import, new TypeConfig( |
| | | executor: $executor, |
| | | maxRetries: 3 |
| | | )); |
| | |
| | | */ |
| | | protected function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings): void |
| | | { |
| | | $this->queueOperation('sync_to', [ |
| | | $this->queueOperation(self::$syncTo, [ |
| | | 'items' => [$postID], |
| | | 'user_id' => $this->userID, |
| | | ], [ |
| | |
| | | $square_id = get_post_meta($postID, BASE . '_square_catalog_id', true); |
| | | |
| | | if ($square_id) { |
| | | $this->queueOperation('delete_from', [ |
| | | $this->queueOperation(self::$deleteFrom, [ |
| | | 'external_ids' => [$square_id], |
| | | 'post_id' => $postID, |
| | | ], [ |
| | |
| | | } |
| | | |
| | | /** |
| | | * @deprecated IntegrationExecutor handles new operations via registerQueueTypes(). |
| | | * Kept for legacy-typed operations ('square_sync_to_square') already queued. |
| | | * Safe to remove once all legacy operations have been processed. |
| | | */ |
| | | public function processOperation(WP_Error|array $result, object $operation, array $data): WP_Error|array |
| | | { |
| | | $base = strtolower($this->service_name) . '_'; |
| | | $square = array_key_exists('user', $data) ? new self((int) $data['user']) : $this; |
| | | |
| | | return match ($operation->type) { |
| | | $base . 'sync_to_square' => $square->processSyncToSquare($data), |
| | | $base . 'delete_from_square' => $square->processDeleteFromSquare($data), |
| | | $base . 'sync_from_square' => $square->processSyncFromSquare($data), |
| | | $base . 'sync_customer' => $square->processSyncCustomer($data), |
| | | default => $result, |
| | | }; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Process sync to Square |
| | | */ |
| | | private function processSyncToSquare(array $data): array |
| | |
| | | */ |
| | | protected function getVariationMapping(string $post_type): array |
| | | { |
| | | $product_type = JVB_CONTENT[jvbNoBase($post_type)]['integrations']['square']['content_type'] ?? 'REGULAR'; |
| | | $registrar = Registrar::getInstance($post_type); |
| | | if (!$registrar) { |
| | | return []; |
| | | } |
| | | $config = $registrar->getIntegrationConfig($this->service_name); |
| | | $product_type = $config['content_type']??'REGULAR'; |
| | | $valid_fields = $this->getValidFieldsForProductType($product_type); |
| | | |
| | | $defaults = [ |
| | |
| | | */ |
| | | protected function getFieldMapping(string $post_type): array |
| | | { |
| | | $product_type = JVB_CONTENT[jvbNoBase($post_type)]['integrations']['square']['content_type'] ?? 'REGULAR'; |
| | | $registrar = Registrar::getInstance($post_type); |
| | | if (!$registrar) { |
| | | return []; |
| | | } |
| | | $config = $registrar->getIntegrationConfig($this->service_name); |
| | | $product_type = $config['content_type']??'REGULAR'; |
| | | $valid_fields = $this->getValidFieldsForProductType($product_type); |
| | | |
| | | $defaults = [ |
| | |
| | | |
| | | // Set user role (assuming you have a customer role defined) |
| | | $user = new \WP_User($user_id); |
| | | $user->set_role(BASE.'foodie'); // Or whatever role from JVB_USER |
| | | $user->set_role(BASE.'foodie'); // Or whatever role |
| | | |
| | | // Generate password reset key |
| | | $reset_key = get_password_reset_key($user); |
| | |
| | | public function trackUserLogin(string $user_login, \WP_User $user): void |
| | | { |
| | | // Check if user has Square integration |
| | | $roles = array_keys(JVB_USER); |
| | | $user_roles = $user->roles; |
| | | |
| | | foreach ($user_roles as $role) { |
| | | if (isset(JVB_USER[$role]['integrations']['square']['is_customer'])) { |
| | | $role = jvbUserRole($user->ID); |
| | | $registrar = Registrar::getInstance($role); |
| | | if ($registrar) { |
| | | $config = $registrar->getIntegration($this->service_name); |
| | | if ($config->isCustomer()) { |
| | | $login_count = (int)get_user_meta($user->ID, BASE . '_square_login_count', true); |
| | | $login_count++; |
| | | |
| | |
| | | if ($login_count % self::PASSWORD_RESET_INTERVAL === 0) { |
| | | $this->schedulePasswordReset($user->ID); |
| | | } |
| | | |
| | | break; |
| | | } |
| | | } |
| | | } |
| | |
| | | wp_enqueue_script('jvb-square-checkout'); |
| | | |
| | | wp_localize_script('jvb-square-checkout', 'squareConfig', [ |
| | | 'isOpen' => jvbIsOpen(), |
| | | //TODO 'isOpen' => jvbIsOpen(), |
| | | 'application_id' => $this->credentials['client_id'] ?? '', |
| | | 'location_id' => $this->locationId, |
| | | 'environment' => $this->environment, |
| | |
| | | */ |
| | | private function importSquareItem(array $item): bool|int |
| | | { |
| | | //TODO: We need to add the post type to custom meta for Square, this is not good if we have multiple post types with the same product type |
| | | // Find matching content type |
| | | $product_type = $item['item_data']['product_type'] ?? 'REGULAR'; |
| | | $post_type = null; |
| | | |
| | | foreach (JVB_CONTENT as $key => $config) { |
| | | if (isset($config['integrations']['square']['content_type']) && |
| | | $config['integrations']['square']['content_type'] === $product_type) { |
| | | $post_type = jvbCheckBase($key); |
| | | foreach (Registrar::getRegistered() as $registrar) { |
| | | if (!$registrar->hasIntegration($this->service_name)) { |
| | | continue; |
| | | } |
| | | $config = $registrar->getIntegration($this->service_name); |
| | | if ($config->getContent_type() && $config->getContent_type() === $product_type) { |
| | | $post_type = jvbCheckBase($registrar->getSlug()); |
| | | break; |
| | | } |
| | | |
| | | } |
| | | |
| | | if (!$post_type) { |
| | |
| | | 'GIFT_CARD' => array_merge($this->setGiftCardFields()) |
| | | ]; |
| | | } |
| | | public function getAdditionalFields(?string $content_type = null):array { |
| | | if ($content_type && array_key_exists($content_type, $this->contentTypes)){ |
| | | $array = $this->contentTypes[$content_type]; |
| | | return array_combine( |
| | | array_map(fn($k) => 'sq_' . $k, array_keys($array)), |
| | | $array |
| | | ); |
| | | } else if ($content_type && !array_key_exists($content_type, $this->contentTypes)) { |
| | | error_log('Could not get default fields for '.$this->service_name.' content type: '.$content_type); |
| | | return []; |
| | | } |
| | | $array = $this->setBaseFields(); |
| | | $return = array_combine( |
| | | array_map(fn($k) => 'sq_' . $k, array_keys($array)), |
| | | $array |
| | | ); |
| | | |
| | | return $return; |
| | | } |
| | | protected function setBaseFields():array |
| | | { |
| | | return [ |
| | | 'price' => [ |
| | | 'type' => 'number', |
| | | 'bulkEdit' => true, |
| | | 'label' => 'Price', |
| | | 'step' => 0.01, |
| | | 'max' => 99999, |
| | |
| | | |
| | | // Save all order meta |
| | | $meta = Meta::forPost($order_post_id); |
| | | $fields = $this->getSquarePostConfig('_sq_orders')['fields']; |
| | | $fields = $this->getOrderFields(); |
| | | unset($fields['post_title']); |
| | | |
| | | $meta->setAll([ |