From 0afb2c0046b55c123eafb4ab9ee77efa68d12463 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sat, 06 Jun 2026 17:15:31 +0000
Subject: [PATCH] =Starting the Favourites.js setup, converting previous Northeh stuff to new Registrar, fixing up Square.php integration to match
---
inc/integrations/Square.php | 1339 ++++++++++++++++++++++++++++++++--------------------------
1 files changed, 739 insertions(+), 600 deletions(-)
diff --git a/inc/integrations/Square.php b/inc/integrations/Square.php
index d82ea73..ba37b92 100644
--- a/inc/integrations/Square.php
+++ b/inc/integrations/Square.php
@@ -1,9 +1,16 @@
<?php
namespace JVBase\integrations;
-use JVBase\meta\MetaManager;
+use JVBase\meta\Form;
+use JVBase\meta\Meta;
use Exception;
+use JVBase\registrar\Fields;
+use JVBase\registrar\Posts;
+use JVBase\registrar\Registrar;
use WP_Error;
+use JVBase\ui\Checkout;
+use JVBase\managers\queue\TypeConfig;
+use JVBase\managers\queue\executors\IntegrationExecutor;
if (!defined('ABSPATH')) {
exit;
@@ -71,6 +78,8 @@
$this->title = 'Square';
$this->icon = 'square-logo';
+ $this->refresh_interval = 7 * DAY_IN_SECONDS;
+
// Define credential fields
$this->fields = [
'environment' => [
@@ -171,6 +180,8 @@
'sync_to_square' => 'Sync Site to Square',
]
);
+
+ add_action('init', [$this, 'registerSquarePostTypes']);
}
/**
@@ -207,6 +218,127 @@
}
+ public function getOrderFields():array
+ {
+ 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' => [
+ 'type' => 'select',
+ 'label' => 'Order Status',
+ 'options' => [
+ 'PROPOSED' => 'Proposed',
+ 'RESERVED' => 'Reserved',
+ 'PREPARED' => 'Prepared (Ready for Pickup)',
+ 'COMPLETED' => 'Completed',
+ 'CANCELED' => 'Canceled'
+ ],
+ 'readonly' => true
+ ],
+ 'fulfillment_status' => [
+ 'type' => 'select',
+ 'label' => 'Fulfillment Status',
+ 'options' => [
+ 'PROPOSED' => 'Proposed',
+ 'RESERVED' => 'Reserved',
+ 'PREPARED' => 'Prepared',
+ 'COMPLETED' => 'Completed',
+ 'CANCELED' => 'Canceled',
+ 'FAILED' => 'Failed'
+ ],
+ 'readonly' => true
+ ],
+ 'pickup_time' => [
+ 'type' => 'datetime',
+ 'label' => 'Pickup Time'
+ ],
+ 'customer_email' => [
+ 'type' => 'email',
+ 'label' => 'Customer Email',
+ 'readonly' => true
+ ],
+ 'customer_name' => [
+ 'type' => 'text',
+ 'label' => 'Customer Name',
+ 'readonly' => true
+ ],
+ 'customer_phone' => [
+ 'type' => 'tel',
+ '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'],
+ 'price' => ['type' => 'number', 'label' => 'Price'],
+ 'note' => ['type' => 'text', 'label' => 'Note']
+ ]
+ ],
+ '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
+ ]
+ ];
+ }
+
+ public function registerSquarePostTypes():void
+ {
+ $orders = Registrar::forPost('_sq_orders', 'Square Order', 'Square Orders');
+ $orders->make([
+ 'public' => false
+ ]
+ );
+ $orders->setAll(['system']);
+
+ $fields = $orders->fields();
+ foreach ($this->getOrderFields() as $fieldName => $config) {
+ $fields->addField($fieldName, $config);
+ }
+
+ }
+
/**
* Get request headers for API calls
*/
@@ -245,8 +377,6 @@
*/
protected function exchangeOAuthCode(string $code): ?array
{
- error_log('Exchanging tokens with credentials: '.print_r($this->credentials, true));
-
$this->ensureInitialized();
// Prepare the request body as an array
@@ -272,7 +402,6 @@
}
$data = json_decode(wp_remote_retrieve_body($response), true);
- error_log('OAuth Response: '.print_r($data, true));
if (isset($data['access_token'])) {
return [
'access_token' => $data['access_token'],
@@ -334,7 +463,6 @@
$data = json_decode(wp_remote_retrieve_body($response), true);
- error_log('RefreshAccessToken Response: '.print_r($data, true));
if (isset($data['access_token'])) {
$this->credentials['access_token'] = $data['access_token'];
$this->credentials['expires_at'] = time() + ($data['expires_in'] ?? 2592000); // 30 days
@@ -357,7 +485,6 @@
{
// Skip if we don't have credentials yet (during OAuth flow)
if (empty($this->credentials['access_token'])) {
- error_log('[Square] Skipping loadLocations - no access token yet');
return;
}
try {
@@ -714,203 +841,95 @@
if (!$this->isSetUp()) {
return;
}
- // User login tracking for security
+
add_action('wp_login', [$this, 'trackUserLogin'], 10, 2);
-
- add_action('wp_footer', [$this, 'outputCheckout']);
-
- // Enqueue checkout scripts
add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']);
- }
+ // Shared checkout UI (replaces outputCheckout)
+ add_filter('jvbAdditionalActions', [Checkout::class, 'render']);
- public function outputCheckout():void {
- if (is_singular(BASE.'dash') || is_post_type_archive(BASE.'dash')) {
- return;
- }
- ?>
- <button type="button" class="toggle-cart row" title="Your Cart" data-action="toggle-cart" aria-label="Open Cart" aria-controls="checkout" aria-expanded="false" hidden>
- <?= jvbIcon('cart')?><span class="abs"></span><span class="abs count"></span>
- </button>
- <aside id="cart">
- <form id="checkout" data-form-id="checkout" data-save="checkout">
- <?php
- $tabs = [
- 'cartItems' => [
- 'title' => 'Your Order',
- 'icon' => 'cart',
- 'description' => 'Here\'s your order. You can change quantities, remove items, or clear your cart.',
- 'content' => $this->cartContent()
- ],
- 'checkout' => [
- 'title' => 'Checkout',
- 'icon' => 'checkout',
- 'description' => 'Securely checkout with your name, email, and payments processed by Square.',
- 'content' => '<div class="checkout-section">
- <h3>Customer Information</h3>
-
- <input type="text" name="name" placeholder="Full Name" required autocomplete="name">
- <input type="email" name="email" placeholder="Email" required autocomplete="email">
- <input type="tel" name="phone" placeholder="Phone" required autocomplete="tel"">
- <h3>Pickup Details</h3>
- <input type="time" name="pickup_time" min="11:00" max="20:00" required>
- <textarea name="special_instructions" placeholder="Special instructions or dietary notes"></textarea>
- </div>
-
- <div class="checkout-section">
- <h3>Payment Information</h3>
- <div id="saved-cards"></div>
- <div id="square-card-container"></div>
- </div>'
- ],
- 'order' => [
- 'title' => 'Your Order',
- 'icon' => 'truck',
- 'hidden' => true,
- 'description' => '',
- 'content' => $this->renderOrderStatus()
- ]
- ];
- jvbRenderTabs($tabs);
- ?>
-
- <div class="cart-total row end"><p class="tax">Tax: <span></span></p><p class="total">GRAND TOTAL: <span></span></p></div>
- </form>
- </aside>
- <template class="restoredCart">
- <div class="restored">
- <h3>Looks like we left things hanging</h3>
- <p>We've restored your cart from your last session below.</p>
- <p>If you'd rather start over, click the button below.</p>
- <div class="row btw">
- <button type="button" onclick="window.squareCheckout.clearCart();this.closest('.restored').remove()"><?=jvbIcon('trash')?>Clear Cart</button>
- <button type="button" onclick="this.closest('.restored').remove()"><?= jvbIcon('x')?>Dismiss</button>
- </div>
- </div>
- </template>
- <template class="cartItem">
- <tr class="item">
- <td class="item">
- <label for="quantity"></label>
- <div class="quantity field" data-min="0" data-max="50" data-step="1" data-price="17" data-id="">
-
- <button type="button" class="decrease"aria-label="Decrease Add to Order">
- <i class="icon minus"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M208,36H48A12,12,0,0,0,36,48V208a12,12,0,0,0,12,12H208a12,12,0,0,0,12-12V48A12,12,0,0,0,208,36Zm4,172a4,4,0,0,1-4,4H48a4,4,0,0,1-4-4V48a4,4,0,0,1,4-4H208a4,4,0,0,1,4,4Zm-40-80a4,4,0,0,1-4,4H88a4,4,0,0,1,0-8h80A4,4,0,0,1,172,128Z"></path></svg></i> </button>
-
- <input type="number" id="quantity" name="quantity" value="0" min="0" max="50" step="1" class="quantity-input">
-
- <button type="button" class="increase" aria-label="Increase Add to Order">
- <i class="icon add"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M208,36H48A12,12,0,0,0,36,48V208a12,12,0,0,0,12,12H208a12,12,0,0,0,12-12V48A12,12,0,0,0,208,36Zm4,172a4,4,0,0,1-4,4H48a4,4,0,0,1-4-4V48a4,4,0,0,1,4-4H208a4,4,0,0,1,4,4Zm-40-80a4,4,0,0,1-4,4H132v36a4,4,0,0,1-8,0V132H88a4,4,0,0,1,0-8h36V88a4,4,0,0,1,8,0v36h36A4,4,0,0,1,172,128Z"></path></svg></i> </button>
- </div>
- </td>
- <td class="price">
- <span class="price"></span>
- </td>
- <td class="total">
- <span class="total"></span>
- </td>
- <td>
- <button type="button" data-remove-from-cart><?= jvbIcon('trash')?></button>
- </td>
- </tr>
- </template>
- <template class="emptyCart">
- <div class="empty">
- <p><i><b>No items in cart.</b></i></p>
- <p>You can <a href="<?= get_post_type_archive_link(BASE.'menu_item')?>" title="Browse our menu">browse our menu</a> to order.</p>
- </div>
- </template>
- <?php
- }
-
- private function cartContent():string
- {
- ob_start();
- ?>
- <div class="cart-items">
- <table>
- <thead>
- <tr>
- <th scope="col">Item</th>
- <th scope="col">Price</th>
- <th scope="col">Total</th>
- </tr>
- </thead>
- <tbody>
-
- </tbody>
- </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' => $this->renderOrderHistory()
- ],
- 'favourites' => [
- 'title' => 'Favourites',
- 'icon' => 'heart',
- 'description' => 'View your favourites from our menu',
- 'content' => $this->renderFavourites()
- ]
- ];
- jvbRenderTabs($tabs);
+ // 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.';
}
+ return $desc;
+ }, 10, 2);
- ?>
- </details>
+ // Square-specific pickup fields (extracted from old outputCheckout)
+ add_filter('jvb_checkout_fields', [$this, 'addPickupFields'], 10, 2);
- <?php
- return ob_get_clean();
+ // Browse URL for this client (restaurant menu)
+ add_filter('jvb_checkout_browse_url', function () {
+ return get_post_type_archive_link(BASE . 'menu_item');
+ });
+ add_filter('jvb_checkout_browse_text', function () {
+ return 'browse our menu';
+ });
+
+ // Register queue executor types
+ $this->registerQueueTypes();
}
- private function renderOrderHistory():string
+ /**
+ * Pickup/ordering fields for the shared checkout form.
+ * Specific to this Square client's food ordering use case.
+ */
+ public function addPickupFields(string $html, string $provider): string
{
- ob_start();
- //TODO: getRequest, cache for 1 day
- return ob_get_clean();
- }
- private function renderFavourites():string
- {
- ob_start();
- //TODO: get user's favourites and list them
- return ob_get_clean();
+ if ($provider !== 'square') {
+ return $html;
+ }
+
+ return $html
+ . '<h3>Pickup Details</h3>'
+ . Form::render('pickup_time', null, [
+ 'type' => 'datetime',
+ 'label' => 'Pickup Time',
+ 'min' => '11:00',
+ 'max' => '20:00',
+ 'required' => true,
+ ])
+ . Form::render('special_instructions', null, [
+ 'type' => 'textarea',
+ 'label' => 'Special Instructions',
+ 'quill' => true,
+ ]);
}
- protected function renderOrderStatus():string
+ protected function registerQueueTypes(): void
{
- ob_start();
- ?>
- <div class="order-confirmation">
- <h2>Order Confirmed!</h2>
- <div id="order-status" data-order="">
- <p>Order #<span class="order-num"></span></p>
- <div class="status-timeline">
- <div class="status-item active" data-status="received">Order Received</div>
- <div class="status-item" data-status="preparing">Preparing</div>
- <div class="status-item" data-status="ready">Ready for Pickup</div>
- </div>
- <div class="pickup-time">
- Estimated pickup: <span id="eta">Calculating...</span>
- </div>
- </div>
- </div>
- <?php
- return ob_get_clean();
+ $queue = JVB()->queue();
+ $executor = new IntegrationExecutor();
+
+ $queue->registry()->register('square_sync_to', new TypeConfig(
+ executor: $executor,
+ chunkKey: 'items',
+ chunkSize: 50,
+ maxRetries: 3
+ ));
+
+ $queue->registry()->register('square_delete_from', new TypeConfig(
+ executor: $executor,
+ chunkKey: 'external_ids',
+ chunkSize: 200,
+ maxRetries: 2
+ ));
+
+ $queue->registry()->register('square_sync_from', new TypeConfig(
+ executor: $executor,
+ maxRetries: 3
+ ));
+
+ $queue->registry()->register('square_sync_customer', new TypeConfig(
+ executor: $executor,
+ maxRetries: 2
+ ));
+
+ $queue->registry()->register('square_import', new TypeConfig(
+ executor: $executor,
+ maxRetries: 3
+ ));
}
/******************************************************************
@@ -922,14 +941,12 @@
*/
protected function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings): void
{
- error_log('Queuing Sync to Square');
- // Queue the sync operation
- $this->queueOperation('sync_to_square', [
- 'items' => [$postID],
- 'user_id' => $this->userID
+ $this->queueOperation('sync_to', [
+ 'items' => [$postID],
+ 'user_id' => $this->userID,
], [
'priority' => 'high',
- 'delay' => 30, // Small delay to batch multiple saves
+ 'delay' => 30,
]);
update_post_meta($postID, BASE . '_square_sync_status', 'queued');
@@ -943,40 +960,35 @@
$square_id = get_post_meta($postID, BASE . '_square_catalog_id', true);
if ($square_id) {
- $this->queueOperation('delete_from_square', [
- 'square_ids' => [$square_id],
- 'post_id' => $postID
+ $this->queueOperation('delete_from', [
+ 'external_ids' => [$square_id],
+ 'post_id' => $postID,
], [
- 'priority' => 'high'
+ 'priority' => 'high',
]);
}
}
/**
- * Process queued operations
+ * @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;
- switch ($operation->type) {
- case $base.'sync_to_square':
- return $square->processSyncToSquare($data);
+ $base = strtolower($this->service_name) . '_';
+ $square = array_key_exists('user', $data) ? new self((int) $data['user']) : $this;
- case $base.'delete_from_square':
- return $square->processDeleteFromSquare($data);
-
- case $base.'sync_from_square':
- return $square->processSyncFromSquare($data);
-
- case $base.'sync_customer':
- return $square->processSyncCustomer($data);
-
- default:
- return $result;
- }
+ 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
*/
@@ -1164,7 +1176,7 @@
return new WP_Error('post_not_found', "Post $postID not found");
}
- $meta = new MetaManager($postID, 'post');
+ $meta = Meta::forPost($postID);
$post_type = get_post_type($postID);
// Get existing Square catalog ID if it exists
@@ -1189,13 +1201,13 @@
}
// Add variations
- $variations = $meta->getValue('product_variations');
+ $variations = $meta->get('product_variations');
if (empty($variations)) {
// Create default variation if none exist
- $price = floatval($meta->getValue('price') ?: 0);
+ $price = floatval($meta->get('price') ?: 0);
$catalog_object['item_data']['variations'][] = [
'type' => 'ITEM_VARIATION',
- 'id' => $existing_square_id ? null : '#neb_menu_item_' . $postID . '_var_default',
+ 'id' => $existing_square_id ? null : '#'.BASE.'menu_item_' . $postID . '_var_default',
'item_variation_data' => [
'name' => 'Regular',
'ordinal' => 0,
@@ -1213,7 +1225,7 @@
$existing_var_id = get_post_meta($postID, BASE . '_square_variation_' . $index . '_id', true);
$catalog_object['item_data']['variations'][] = [
'type' => 'ITEM_VARIATION',
- 'id' => $existing_var_id ?: '#neb_menu_item_' . $postID . '_var_' . $index,
+ 'id' => $existing_var_id ?: '#'.BASE.'menu_item_' . $postID . '_var_' . $index,
'item_variation_data' => [
'name' => $variation['name'] ?? 'Variation ' . ($index + 1),
'ordinal' => $index,
@@ -1245,7 +1257,7 @@
}
// Add modifiers if they exist
- $modifiers = $meta->getValue('modifiers');
+ $modifiers = $meta->get('modifiers');
if (!empty($modifiers)) {
$modifier_ids = [];
foreach ($modifiers as $modifier) {
@@ -1261,7 +1273,7 @@
}
// Add tax settings
- $tax_ids = $meta->getValue('tax_ids');
+ $tax_ids = $meta->get('tax_ids');
if (!empty($tax_ids)) {
$catalog_object['item_data']['tax_ids'] = $tax_ids;
}
@@ -1367,7 +1379,12 @@
*/
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 = [
@@ -1436,7 +1453,12 @@
*/
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 = [
@@ -1632,7 +1654,7 @@
// 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);
@@ -1684,24 +1706,26 @@
private function sendWelcomeEmail(\WP_User $user, string $reset_key): void
{
$site_name = get_bloginfo('name');
- $reset_url = get_home_url(2, "wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode($user->user_login), 'login');
+ $reset_url = get_home_url(null, "wp-login.php?action=rp&key=$reset_key&login=" . rawurlencode($user->user_login), 'login');
$message = sprintf(
"Welcome to %s!\n\n" .
- "Your account has been created. Please click the link below to set your password:\n\n" .
+ "Your account has been created. Please click the button below to set your password:\n\n" .
"%s\n\n" .
- "Once you've set your password, you can log in to:\n" .
+ "Or, copy and paste the link below:\n\n".
+ "%s\n\n" .
+ "Once you've set your password, you can:\n" .
"- View your order history\n" .
"- Save your favorite items\n" .
"- Speed up checkout with saved payment methods\n\n" .
"If you didn't create this account, please ignore this email.\n\n" .
- "Thanks,\n%s",
+ "Thanks,\n",
$site_name,
- $reset_url,
- $site_name
+ JVB()->email()->button('Reset Password', $reset_url),
+ JVB()->email()->link($reset_url),
);
- jvbMail(
+ JVB()->email()->sendEmail(
$user->user_email,
sprintf('[%s] Welcome! Set Your Password', $site_name),
$message
@@ -1714,11 +1738,11 @@
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++;
@@ -1729,8 +1753,6 @@
if ($login_count % self::PASSWORD_RESET_INTERVAL === 0) {
$this->schedulePasswordReset($user->ID);
}
-
- break;
}
}
}
@@ -1745,182 +1767,15 @@
// Send notification
$user = get_user_by('ID', $user_id);
if ($user) {
- wp_mail(
+ JVB()->email()->sendEmail(
$user->user_email,
'['.get_bloginfo('name').'] Security Code',
'For your security, enter this code to continue accessing your account and saved payment methods.',
- ['Content-Type: text/html; charset=UTF-8']
);
}
}
/******************************************************************
- * ORDER PROCESSING
- ******************************************************************/
-
- /**
- * Process checkout order
- */
- public function processOrder($data):WP_Error|array
- {
- check_ajax_referer('square_checkout_nonce', 'nonce');
-
- $cart_items = json_decode(stripslashes($data['cart'] ?? '[]'), true);
- $customer_info = [
- 'name' => sanitize_text_field($data['name'] ?? ''),
- 'email' => sanitize_email($data['email'] ?? ''),
- 'phone' => sanitize_text_field($data['phone'] ?? ''),
- ];
- $payment_token = sanitize_text_field($data['payment_token'] ?? '');
-
- if (empty($cart_items) || empty($payment_token)) {
- return new WP_Error('error', 'Invalid order data');
- }
-
- // Calculate order total
- $order_total = $this->calculateOrderTotal($cart_items);
-
- // Create Square order
- $order_response = $this->createSquareOrder($cart_items, $customer_info, $order_total);
-
- if (is_wp_error($order_response)) {
- return new WP_Error('error', $order_response->get_error_message());
- }
-
- // Process payment
- $payment_response = $this->processSquarePayment($payment_token, $order_response['order']['id'], $order_total);
-
- if (is_wp_error($payment_response)) {
- return new WP_Error('error', $order_response->get_error_message());
- }
-
- // Save order to user if logged in
- if (is_user_logged_in()) {
- $this->saveOrderToUser(get_current_user_id(), $order_response['order']['id']);
- }
-
- return [
- 'success' => true,
- 'order_id' => $order_response['order']['id'],
- 'receipt_url' => $payment_response['payment']['receipt_url'] ?? '',
- 'message' => 'Order placed successfully!'
- ];
- }
-
- /**
- * Calculate order total from cart items
- */
- private function calculateOrderTotal(array $cart_items): int
- {
- $total = 0;
-
- foreach ($cart_items as $item) {
- $post_id = intval($item['id'] ?? 0);
- if (!$post_id) continue;
-
- $meta = new MetaManager($post_id, 'post');
- $price = floatval($meta->getValue('price'));
- $quantity = intval($item['quantity'] ?? 1);
-
- $total += ($price * $quantity * 100); // Convert to cents
- }
-
- // Add tax (simplified - you'd want more complex tax calculation)
- $tax_rate = floatval(get_option(BASE . 'square_tax_rate', 0.05));
- $tax = intval($total * $tax_rate);
-
- return $total + $tax;
- }
-
- /**
- * Create Square order
- */
- private function createSquareOrder(array $cart_items, array $customer_info, int $total): array|WP_Error
- {
- $line_items = [];
-
- foreach ($cart_items as $item) {
- $post_id = intval($item['id'] ?? 0);
- $variation_index = $item['variation'] ?? null;
-
- if (!$post_id) continue;
-
- $post = get_post($post_id);
- $square_catalog_id = get_post_meta($post_id, BASE . '_square_catalog_id', true);
-
- $line_item = [
- 'quantity' => strval($item['quantity'] ?? 1),
- 'name' => $post->post_title
- ];
-
- // If variation specified, get variation ID
- if ($variation_index !== null && $square_catalog_id) {
- $variation_id = get_post_meta($post_id, BASE . '_square_variation_' . $variation_index . '_id', true);
- if ($variation_id) {
- $line_item['catalog_object_id'] = $variation_id;
-
- // Add variation name to line item
- $meta = new MetaManager($post_id, 'post');
- $variations = $meta->getValue('product_variations');
- if (!empty($variations[$variation_index]['name'])) {
- $line_item['name'] .= ' - ' . $variations[$variation_index]['name'];
- }
- }
- } elseif ($square_catalog_id) {
- // Use default variation if no specific variation
- $default_variation_id = get_post_meta($post_id, BASE . '_square_variation_0_id', true);
- if ($default_variation_id) {
- $line_item['catalog_object_id'] = $default_variation_id;
- }
- }
-
- // If no catalog ID, create ad-hoc line item
- if (empty($line_item['catalog_object_id'])) {
- $meta = new MetaManager($post_id, 'post');
- $variations = $meta->getValue('product_variations');
-
- if ($variation_index !== null && !empty($variations[$variation_index]['price'])) {
- $price = floatval($variations[$variation_index]['price']);
- } else {
- $price = floatval($meta->getValue('price'));
- }
-
- $line_item['base_price_money'] = [
- 'amount' => intval($price * 100),
- 'currency' => 'CAD'
- ];
- }
-
- $line_items[] = $line_item;
- }
-
- return $this->postRequest('orders', [
- 'order' => [
- 'location_id' => $this->locationId,
- 'line_items' => $line_items,
- 'customer_id' => $this->getOrCreateSquareCustomer($customer_info)
- ]
- ]);
- }
-
- /**
- * Process Square payment
- */
- private function processSquarePayment(string $payment_token, string $order_id, int $amount): array|WP_Error
- {
- return $this->postRequest('payments', [
- 'source_id' => $payment_token,
- 'idempotency_key' => wp_generate_uuid4(),
- 'amount_money' => [
- 'amount' => $amount,
- 'currency' => 'CAD'
- ],
- 'order_id' => $order_id,
- 'location_id' => $this->locationId
- ]);
- }
-
- /******************************************************************
* WEBHOOK HANDLING
******************************************************************/
@@ -2000,16 +1855,49 @@
/**
* Handle order status webhook
*/
+ /**
+ * Handle order status webhook - NOW UPDATES POST TYPE
+ */
private function handleOrderWebhook(array $data): bool
{
$order_id = $data['object']['order']['id'] ?? '';
$state = $data['object']['order']['state'] ?? '';
+ $fulfillments = $data['object']['order']['fulfillments'] ?? [];
if (!$order_id) {
return false;
}
- // Update cached order status
+ // Find the WP post for this order
+ $wp_order_id = get_option(BASE . 'square_order_map_' . $order_id);
+
+ if ($wp_order_id) {
+ // Update the post meta
+ $meta = Meta::forPost($wp_order_id);
+ $updates = [
+ 'status' => $state,
+ 'updated_at' => current_time('mysql')
+ ];
+
+ // Extract fulfillment status and pickup time
+ if (!empty($fulfillments[0])) {
+ $fulfillment = $fulfillments[0];
+ $updates['fulfillment_status'] = $fulfillment['state'] ?? $state;
+
+ if (!empty($fulfillment['pickup_details']['pickup_at'])) {
+ $updates['pickup_time'] = $fulfillment['pickup_details']['pickup_at'];
+ }
+ }
+
+ $meta->setAll($updates);
+
+ // Trigger notification to customer if order is ready
+ if ($state === 'PREPARED') {
+ do_action(BASE . 'square_order_ready', $wp_order_id, $order_id);
+ }
+ }
+
+ // Also update transient cache for quick status checks
set_transient(BASE . 'square_order_' . $order_id, $state, HOUR_IN_SECONDS);
// Trigger action for other integrations
@@ -2075,7 +1963,7 @@
/**
* Enqueue checkout scripts with Square configuration
*/
- public function enqueueScripts():void
+ public function enqueueScripts(): void
{
$this->loadCredentials();
$sdk_url = $this->environment === 'production'
@@ -2087,43 +1975,40 @@
$sdk_url,
[],
null,
- [
- 'strategy' => 'defer',
- 'in_footer' => true
- ]
+ ['strategy' => 'defer', 'in_footer' => true]
);
- // Register your custom checkout script
+ // Shared cart checkout base class
+ wp_register_script(
+ 'jvb-checkout',
+ JVB_URL . 'assets/js/min/checkout.min.js',
+ ['jvb-utility', 'jvb-queue', 'jvb-a11y', 'jvb-cache', 'jvb-tabs', 'jvb-popup'],
+ '1.1.31',
+ ['strategy' => 'defer', 'in_footer' => true]
+ );
+
+ // Square checkout extends CartCheckout
wp_register_script(
'jvb-square-checkout',
JVB_URL . 'assets/js/min/square.min.js',
- [
-// 'square-payments-sdk',
- 'jvb-utility',
- 'jvb-queue',
- 'jvb-a11y',
- 'jvb-cache',
- 'jvb-tabs',
- 'jvb-modal',
- ],
- '1.0.0',
- [
- 'strategy' => 'defer',
- 'in_footer' => true
- ]
+ ['jvb-checkout', 'square-payments-sdk'],
+ '1.1.31',
+ ['strategy' => 'defer', 'in_footer' => true]
);
wp_enqueue_script('jvb-square-checkout');
- // Localize the checkout script with Square config
- wp_localize_script(
- 'jvb-square-checkout',
- 'squareConfig',
- [
- 'isOpen' => jvbIsOpen(),
-// 'currency' => get_option('jvb_currency', 'CAD')
- ]
- );
+ wp_localize_script('jvb-square-checkout', 'squareConfig', [
+//TODO 'isOpen' => jvbIsOpen(),
+ 'application_id' => $this->credentials['client_id'] ?? '',
+ 'location_id' => $this->locationId,
+ 'environment' => $this->environment,
+ 'api_url' => rest_url('jvb/v1/square/'),
+ 'nonce' => wp_create_nonce('wp_rest'),
+ 'currency' => get_option(BASE . 'currency', 'CAD'),
+ 'is_logged_in' => is_user_logged_in(),
+ 'user_email' => is_user_logged_in() ? wp_get_current_user()->user_email : '',
+ ]);
}
/******************************************************************
@@ -2167,6 +2052,43 @@
}
/**
+ * Save order reference for status tracking
+ */
+ public function saveOrderReference($data): array
+ {
+ $order_id = sanitize_text_field($data['order_id'] ?? '');
+ $payment_id = sanitize_text_field($data['payment_id'] ?? '');
+
+ if (!$order_id) {
+ return ['success' => false, 'message' => 'Invalid order data'];
+ }
+
+ // Save to user if logged in
+ if (is_user_logged_in()) {
+ $user_id = get_current_user_id();
+ $orders = get_user_meta($user_id, BASE . '_square_orders', true) ?: [];
+ $orders[] = [
+ 'order_id' => $order_id,
+ 'payment_id' => $payment_id,
+ 'date' => current_time('mysql'),
+ 'customer' => $data['customer'] ?? []
+ ];
+
+ // Keep last 50 orders
+ if (count($orders) > 50) {
+ $orders = array_slice($orders, -50);
+ }
+
+ update_user_meta($user_id, BASE . '_square_orders', $orders);
+ }
+
+ return [
+ 'success' => true,
+ 'order_id' => $order_id,
+ 'message' => 'Order saved'
+ ];
+ }
+ /**
* Save order to user meta
*/
private function saveOrderToUser(int $user_id, string $order_id): void
@@ -2186,9 +2108,9 @@
}
/**
- * Get order status
+ * Get order status (for customer feedback)
*/
- public function getOrderStatus($data):WP_Error|array
+ public function getOrderStatus($data): WP_Error|array
{
$order_id = sanitize_text_field($data['order_id'] ?? '');
@@ -2196,26 +2118,24 @@
return new WP_Error('error', 'Order ID required');
}
- // Check cache first
- $cached_status = get_transient(BASE . 'square_order_' . $order_id);
-
- if ($cached_status !== false) {
- return $cached_status;
- }
-
// Fetch from Square
- $response = $this->getRequest('orders/' . $order_id);
+ $response = $this->getRequest('v2/orders/' . $order_id);
if (is_wp_error($response)) {
return new WP_Error('error', 'Could not fetch order status');
}
- $status = $response['order']['state'] ?? 'UNKNOWN';
- set_transient(BASE . 'square_order_' . $order_id, $status, 5 * MINUTE_IN_SECONDS);
+ $order = $response['order'] ?? [];
+ $status_data = [
+ 'state' => $order['state'] ?? 'UNKNOWN',
+ 'fulfillment_eta' => $order['fulfillments'][0]['pickup_details']['pickup_at'] ?? null
+ ];
- return array_merge([
- 'success' => true,
- ], $status);
+ return [
+ 'success' => true,
+ 'status' => $status_data['state'],
+ 'eta' => $status_data['fulfillment_eta']
+ ];
}
/**
@@ -2318,16 +2238,21 @@
*/
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) {
@@ -2371,7 +2296,7 @@
*/
private function mapSquareFieldsToWordPress(int $post_id, array $item): void
{
- $meta = new MetaManager($post_id, 'post');
+ $meta = Meta::forPost($post_id);
$field_map = $this->getFieldMapping(get_post_type($post_id));
$values_to_save = [];
@@ -2507,7 +2432,7 @@
update_user_meta($user->ID, BASE . '_square_customer_updated', current_time('mysql'));
// Clear cached customer data
- $this->cache->delete('square_customer_' . $user->ID);
+ $this->cache->forget('square_customer_' . $user->ID);
}
return true;
@@ -2738,7 +2663,6 @@
// Validate environment setting
if (isset($credentials['environment'])) {
- error_log('Environment: '.print_r($credentials['environment'], true));
$validEnvironments = ['sandbox', 'production'];
if (!in_array($credentials['environment'], $validEnvironments)) {
$this->logError('Invalid environment setting', [
@@ -2828,6 +2752,23 @@
'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
+ );
+ }
protected function setBaseFields():array
{
return [
@@ -3026,137 +2967,61 @@
IMAGE PROCESSING
*********************************************************/
/**
- * Upload featured image to Square catalog
- *
- * @param int $postID WordPress post ID
- * @return array|WP_Error Result of the upload operation
- */
- public function uploadImageToSquare(int $imgID): array|WP_Error
- {
-
- if ($imgID === 0) {
- return new WP_Error('no_image', 'No image found for post');
- }
-
- try {
- // Get the supported image (converts WebP if needed)
- $supported_image_id = $this->getSupportedImage($imgID);
-
- // Check if we've already uploaded this image
- $existing_square_image_id = $this->getSquareImageId($supported_image_id);
- if ($existing_square_image_id) {
- return [
- 'success' => true,
- 'image_id' => $existing_square_image_id,
- 'message' => 'Image already exists in Square catalog'
- ];
- }
-
- // Step 1: Create image object in catalog
- $image_object = $this->createSquareImageObject($supported_image_id);
- if (is_wp_error($image_object)) {
- return $image_object;
- }
-
- // Step 2: Upload the actual image file
- $upload_result = $this->uploadImageFileToSquare(
- $supported_image_id,
- $image_object['id']
- );
-
- if (is_wp_error($upload_result)) {
- return $upload_result;
- }
-
- // Store the Square image ID for future reference
- $this->setSquareImageId($supported_image_id, $image_object['id']);
-
- return [
- 'success' => true,
- 'image_id' => $image_object['id'],
- 'message' => 'Image successfully uploaded to Square'
- ];
-
- } catch (\Exception $e) {
- $this->logError('Failed to upload image to Square', [
- 'method' => 'uploadImageToSquare',
- 'error' => $e->getMessage()
- ]);
-
- return new WP_Error('upload_failed', $e->getMessage());
- }
- }
-
- /**
- * Create image object in Square catalog
- *
- * @param int $attachment_id WordPress attachment ID
- * @return array|WP_Error Square image object or error
- */
- protected function createSquareImageObject(int $attachment_id): array|WP_Error
- {
- $image_title = get_the_title($attachment_id);
- $alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
-
- $image_object = [
- 'type' => 'IMAGE',
- 'id' => '#IMAGE_' . $attachment_id . '_' . time(),
- 'image_data' => [
- 'name' => $image_title ?: 'Image',
- 'caption' => $alt_text ?: ''
- ]
- ];
-
- $response = $this->postRequest('catalog/batch-upsert', [
- 'idempotency_key' => wp_generate_uuid4(),
- 'batches' => [[
- 'objects' => [$image_object]
- ]]
- ]);
-
- if (is_wp_error($response)) {
- return $response;
- }
-
- if (!empty($response['objects'][0])) {
- return $response['objects'][0];
- }
-
- return new WP_Error('creation_failed', 'Failed to create image object in Square');
- }
-
- /**
* Upload image file to Square
*
- * @param int $attachment_id WordPress attachment ID
- * @param string $square_image_id Square catalog image ID
+ * @param int $imgID WordPress attachment ID
* @return array|WP_Error Upload result or error
*/
- protected function uploadImageFileToSquare(int $attachment_id, string $square_image_id): array|WP_Error
+ protected function uploadImageToSquare(int $imgID): array|WP_Error
{
- $file_path = get_attached_file($attachment_id);
+ $supported_image_id = $this->getSupportedImage($imgID);
+ // Check if already uploaded
+ $existing_square_image_id = $this->getSquareImageId($supported_image_id);
+ if ($existing_square_image_id) {
+ return [
+ 'success' => true,
+ 'image_id' => $existing_square_image_id
+ ];
+ }
+
+ $file_path = get_attached_file($supported_image_id);
if (!file_exists($file_path)) {
return new WP_Error('file_not_found', 'Image file not found');
}
// Verify file type
- $mime_type = get_post_mime_type($attachment_id);
- $allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
-
- if (!in_array($mime_type, $allowed_types)) {
+ $mime_type = get_post_mime_type($supported_image_id);
+ if (!in_array($mime_type, ['image/jpeg', 'image/png', 'image/gif'])) {
return new WP_Error('invalid_type', 'Square only supports JPEG, PNG, and GIF images');
}
- // Prepare the multipart form data
+ $image_title = get_the_title($supported_image_id);
+ $alt_text = get_post_meta($supported_image_id, '_wp_attachment_image_alt', true);
+
+ // Build multipart request - SINGLE STEP
$boundary = wp_generate_password(24);
$headers = $this->getRequestHeaders();
$headers['Content-Type'] = 'multipart/form-data; boundary=' . $boundary;
- $body = $this->buildMultipartBody($file_path, $square_image_id, $boundary);
+ // Request JSON part
+ $request_json = [
+ 'idempotency_key' => wp_generate_uuid4(),
+ 'image' => [
+ 'type' => 'IMAGE',
+ 'id' => '#IMAGE_' . $supported_image_id . '_' . time(),
+ 'image_data' => [
+ 'name' => $image_title ?: 'Image',
+ 'caption' => $alt_text ?: ''
+ ]
+ ],
+ 'object_id' => $supported_image_id
+ ];
+
+ $body = $this->buildMultipartBody($file_path, $request_json, $boundary);
$response = wp_remote_post(
- $this->getApiUrl('catalog/images'),
+ $this->getApiUrl('v2/catalog/images'),
[
'headers' => $headers,
'body' => $body,
@@ -3168,56 +3033,43 @@
return $response;
}
- $body = wp_remote_retrieve_body($response);
- $data = json_decode($body, true);
+ $data = json_decode(wp_remote_retrieve_body($response), true);
if (!empty($data['errors'])) {
- $error_message = $data['errors'][0]['detail'] ?? 'Unknown error';
- return new WP_Error('upload_error', $error_message);
+ return new WP_Error('upload_error', $data['errors'][0]['detail'] ?? 'Unknown error');
}
- if (!empty($data['image'])) {
- return $data;
+ if (!empty($data['image']['id'])) {
+ $this->setSquareImageId($supported_image_id, $data['image']['id']);
+ return [
+ 'success' => true,
+ 'image_id' => $data['image']['id']
+ ];
}
- return new WP_Error('upload_failed', 'Failed to upload image to Square');
+ return new WP_Error('upload_failed', 'Failed to upload image');
}
- /**
- * Build multipart form data for image upload
- *
- * @param string $file_path Path to image file
- * @param string $square_image_id Square catalog image ID
- * @param string $boundary Multipart boundary
- * @return string Multipart body
- */
- protected function buildMultipartBody(string $file_path, string $square_image_id, string $boundary): string
+ protected function buildMultipartBody(string $file_path, array $request_json, string $boundary): string
{
$eol = "\r\n";
$body = '';
- // Add request JSON
- $request_data = [
- 'idempotency_key' => wp_generate_uuid4(),
- 'object_id' => $square_image_id
- ];
-
+ // Add request JSON part
$body .= '--' . $boundary . $eol;
$body .= 'Content-Disposition: form-data; name="request"' . $eol;
$body .= 'Content-Type: application/json' . $eol . $eol;
- $body .= json_encode($request_data) . $eol;
+ $body .= json_encode($request_json) . $eol;
- // Add image file
+ // Add image file part
$filename = basename($file_path);
$file_contents = file_get_contents($file_path);
$mime_type = mime_content_type($file_path);
$body .= '--' . $boundary . $eol;
- $body .= 'Content-Disposition: form-data; name="image"; filename="' . $filename . '"' . $eol;
+ $body .= 'Content-Disposition: form-data; name="file"; filename="' . $filename . '"' . $eol;
$body .= 'Content-Type: ' . $mime_type . $eol . $eol;
$body .= $file_contents . $eol;
-
- // End boundary
$body .= '--' . $boundary . '--' . $eol;
return $body;
@@ -3492,4 +3344,291 @@
$this->deleteCredentials();
}
}
+
+ private function createSquareOrder(array $items, ?string $customer_id, array $data): array|WP_Error
+ {
+ // Build line items for Square
+ $line_items = [];
+ foreach ($items as $item) {
+ $line_item = [
+ 'quantity' => (string)$item['quantity'], // MUST be string!
+ ];
+
+ // Use catalog_object_id if available (recommended)
+ if (!empty($item['catalog_object_id'])) {
+ $line_item['catalog_object_id'] = $item['catalog_object_id'];
+ $line_item['catalog_version'] = $item['catalog_version'] ?? null;
+ } else {
+ // Ad-hoc line item (not recommended - no tax/inventory automation)
+ $line_item['name'] = $item['name'];
+ $line_item['base_price_money'] = [
+ 'amount' => (int)$item['price'],
+ 'currency' => $this->getCurrency()
+ ];
+ }
+
+ if (!empty($item['note'])) {
+ $line_item['note'] = $item['note'];
+ }
+
+ $line_items[] = $line_item;
+ }
+
+ $order_data = [
+ 'idempotency_key' => wp_generate_uuid4(), // Different from payment idempotency key
+ 'order' => [
+ 'location_id' => $this->locationId,
+ 'line_items' => $line_items,
+ 'state' => 'OPEN'
+ ]
+ ];
+
+ // Add customer if available
+ if ($customer_id) {
+ $order_data['order']['customer_id'] = $customer_id;
+ }
+
+ // Add metadata
+ if (!empty($data['note'])) {
+ $order_data['order']['metadata'] = [
+ 'special_instructions' => $data['note']
+ ];
+ }
+
+ if (!empty($data['pickup_time'])) {
+ $order_data['order']['metadata']['pickup_time'] = $data['pickup_time'];
+ }
+
+ return $this->postRequest('orders', $order_data);
+ }
+
+ private function createSquarePayment(
+ string $source_id,
+ string $idempotency_key,
+ int $amount_cents,
+ string $order_id,
+ ?string $customer_id
+ ): array|WP_Error
+ {
+ $payment_data = [
+ 'idempotency_key' => $idempotency_key,
+ 'source_id' => $source_id,
+ 'amount_money' => [
+ 'amount' => $amount_cents, // Already in cents!
+ 'currency' => $this->getCurrency()
+ ],
+ 'order_id' => $order_id,
+ 'location_id' => $this->locationId,
+ 'autocomplete' => true, // Capture immediately
+ ];
+
+ // Add customer if available
+ if ($customer_id) {
+ $payment_data['customer_id'] = $customer_id;
+ }
+
+ // Add reference ID for tracking
+ $payment_data['reference_id'] = 'WP_' . time();
+
+ return $this->postRequest('payments', $payment_data);
+ }
+
+ private function saveOrderToWordPress(array $order_data): int
+ {
+ // Extract customer info
+ $customer_email = $order_data['customer']['email'] ?? '';
+ $customer_name = $order_data['customer']['name'] ?? '';
+
+ // Find or create WP user for logged-in association
+ $user_id = 0;
+ if ($customer_email) {
+ $user = get_user_by('email', $customer_email);
+ if ($user) {
+ $user_id = $user->ID;
+ // Store Square customer ID on user
+ if (!empty($order_data['square_customer_id'])) {
+ update_user_meta($user_id, BASE . '_square_customer_id', $order_data['square_customer_id']);
+ }
+ }
+ }
+
+ // Create order post
+ $order_post_id = wp_insert_post([
+ 'post_type' => BASE . '_sq_orders',
+ 'post_title' => 'Order #' . $order_data['square_order_id'],
+ 'post_status' => 'publish',
+ 'post_author' => $user_id // Associate with user if logged in
+ ]);
+
+ if (!$order_post_id || is_wp_error($order_post_id)) {
+ $this->logError('Failed to create order post', ['order_data' => $order_data]);
+ return 0;
+ }
+
+ // Save all order meta
+ $meta = Meta::forPost($order_post_id);
+ $fields = $this->getOrderFields();
+ unset($fields['post_title']);
+
+ $meta->setAll([
+ 'square_order_id' => $order_data['square_order_id'],
+ 'square_payment_id' => $order_data['square_payment_id'] ?? '',
+ 'square_customer_id' => $order_data['square_customer_id'] ?? '',
+ 'amount' => $order_data['amount'],
+ 'status' => $order_data['status'],
+ 'fulfillment_status' => $order_data['fulfillment_status'] ?? 'PROPOSED',
+ 'pickup_time' => $order_data['pickup_time'] ?? '',
+ 'customer_email' => $customer_email,
+ 'customer_name' => $customer_name,
+ 'customer_phone' => $order_data['customer']['phone'] ?? '',
+ 'special_instructions' => $order_data['note'] ?? '',
+ 'items' => $order_data['items'],
+ 'receipt_url' => $order_data['receipt_url'] ?? '',
+ 'created_at' => current_time('mysql'),
+ 'updated_at' => current_time('mysql')
+ ]);
+
+ // Index by Square order ID for quick webhook lookups
+ update_option(BASE . 'square_order_map_' . $order_data['square_order_id'], $order_post_id);
+
+ return $order_post_id;
+ }
+
+ /**
+ * Get currency code
+ */
+ private function getCurrency(): string
+ {
+ return get_option(BASE . 'currency', 'CAD');
+ }
+
+ /**
+ * Get customer with saved cards (2025-compliant)
+ */
+ public function getUserCards(string $customer_id): array
+ {
+ $response = $this->getRequest('cards?customer_id=' . $customer_id);
+ return $response['cards'] ?? [];
+ }
+
+
+ public function getUserOrders(string $customer_email): array
+ {
+ // First get Square customer ID
+ $customer_response = $this->postRequest('customers/search', [
+ 'filter' => [
+ 'email_address' => ['exact' => $customer_email]
+ ]
+ ]);
+
+ $customer_id = $customer_response['customers'][0]['id'] ?? null;
+ if (!$customer_id) {
+ return [];
+ }
+
+ // Get their orders
+ $orders_response = $this->postRequest('orders/search', [
+ 'filter' => [
+ 'customer_filter' => [
+ 'customer_ids' => [$customer_id]
+ ]
+ ],
+ 'sort' => [
+ 'sort_field' => 'CREATED_AT',
+ 'sort_order' => 'DESC'
+ ],
+ 'limit' => 50
+ ]);
+
+ return $orders_response['orders'] ?? [];
+ }
+
+ public function checkOrderStatus(string $order_id): ?string
+ {
+ // Check transient cache first
+ $cached = get_transient(BASE . 'square_order_' . $order_id);
+ if ($cached) {
+ return $cached;
+ }
+
+ // Fetch from Square
+ $response = $this->getRequest('orders/' . $order_id);
+ if (!is_wp_error($response)) {
+ $state = $response['order']['state'] ?? null;
+ set_transient(BASE . 'square_order_' . $order_id, $state, HOUR_IN_SECONDS);
+ return $state;
+ }
+
+ return null;
+ }
+
+ /**
+ * Single-item sync. Called by IntegrationExecutor::processSyncTo().
+ * Delegates to syncBatchToService since Square uses batch-upsert.
+ */
+ public function syncPostToService(int $postID): array|WP_Error
+ {
+ return $this->syncBatchToService(['items' => [$postID]]);
+ }
+
+ /**
+ * Batch sync — preferred by IntegrationExecutor when available.
+ * Wraps existing processSyncToSquare which already handles batches.
+ */
+ public function syncBatchToService(array $data): array|WP_Error
+ {
+ $result = $this->processSyncToSquare($data);
+
+ if (empty($result['success'])) {
+ $errors = implode(', ', $result['result']['errors'] ?? ['Sync failed']);
+ return new WP_Error('square_sync_failed', $errors);
+ }
+
+ return $result;
+ }
+
+ /**
+ * Delete catalog object from Square.
+ * Called by IntegrationExecutor::processDeleteFrom().
+ */
+ public function deleteFromService(string $externalId): array|WP_Error
+ {
+ $result = $this->processDeleteFromSquare(['square_ids' => [$externalId]]);
+
+ if (empty($result['success'])) {
+ return new WP_Error('square_delete_failed', $result['result']['error'] ?? 'Delete failed');
+ }
+
+ return $result;
+ }
+
+ /**
+ * Import from Square catalog → WordPress.
+ * Called by IntegrationExecutor::processImport().
+ */
+ public function importFromService(array $data): array|WP_Error
+ {
+ $result = $this->processSyncFromSquare($data);
+
+ if (empty($result['success'])) {
+ return new WP_Error('square_import_failed', $result['result']['error'] ?? 'Import failed');
+ }
+
+ return $result;
+ }
+
+ /**
+ * Sync customer to Square.
+ * Called by IntegrationExecutor::processSyncCustomer().
+ */
+ public function syncCustomer(array $data): array|WP_Error
+ {
+ $result = $this->processSyncCustomer($data);
+
+ if (empty($result['success'])) {
+ return new WP_Error('square_customer_sync_failed', $result['result']['error'] ?? 'Customer sync failed');
+ }
+
+ return $result;
+ }
}
--
Gitblit v1.10.0