| | |
| | | <?php |
| | | |
| | | namespace JVBase\integrations\services; |
| | | use Exception; |
| | | use JVBase\integrations\Integrations; |
| | | use JVBase\managers\queue\executors\IntegrationExecutor; |
| | | use JVBase\managers\queue\Queue; |
| | | use JVBase\managers\queue\TypeConfig; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\ui\Checkout; |
| | | use WP_Error; |
| | | use WP_Post; |
| | | use WP_REST_Request; |
| | | use WP_REST_Response; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | |
| | | * PRODUCT SYNC |
| | | *****************************************************************/ |
| | | |
| | | protected function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings): void |
| | | protected function handleTheSavePost(int $postID, WP_Post $post, bool $update, array $settings): void |
| | | { |
| | | |
| | | error_log('==== [Helcim]::handleTheSavePost ===='); |
| | |
| | | /** |
| | | * Override the webhook request handler to capture raw body for signature verification |
| | | */ |
| | | public function handleWebhookRequest(\WP_REST_Request $request): \WP_REST_Response |
| | | public function handleWebhookRequest(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $payload = $request->get_params(); |
| | | $payload['_headers'] = $request->get_headers(); |
| | |
| | | |
| | | $success = $this->handleWebhook($payload); |
| | | |
| | | return new \WP_REST_Response([ |
| | | return new WP_REST_Response([ |
| | | 'success' => $success, |
| | | ], $success ? 200 : 400); |
| | | } |
| | |
| | | * Sync a WordPress post to Helcim as a product. |
| | | * Called by IntegrationExecutor::processSyncTo() |
| | | */ |
| | | public function syncPostToService(int $postID): array|\WP_Error |
| | | public function syncPostToService(int $postID): array|WP_Error |
| | | { |
| | | $post = get_post($postID); |
| | | if (!$post) { |
| | | return new \WP_Error('not_found', "Post {$postID} not found"); |
| | | return new WP_Error('not_found', "Post {$postID} not found"); |
| | | } |
| | | |
| | | $helcimProductId = get_post_meta($postID, BASE . '_helcim_item_id', true); |
| | |
| | | * Delete a product from Helcim. |
| | | * Called by IntegrationExecutor::processDeleteFrom() |
| | | */ |
| | | public function deleteFromService(string $externalId): array|\WP_Error |
| | | public function deleteFromService(string $externalId): array|WP_Error |
| | | { |
| | | $response = $this->deleteRequest("products/{$externalId}"); |
| | | |
| | |
| | | /** |
| | | * Build Helcim product payload from a WordPress post. |
| | | */ |
| | | protected function buildProductPayload(int $postID): array|\WP_Error |
| | | protected function buildProductPayload(int $postID): array|WP_Error |
| | | { |
| | | $meta = \JVBase\meta\Meta::forPost($postID); |
| | | $meta = Meta::forPost($postID); |
| | | $post = get_post($postID); |
| | | |
| | | $price = $meta->get('price'); |
| | | if (empty($price) || !is_numeric($price)) { |
| | | return new \WP_Error('invalid_price', "Post {$postID} has no valid price"); |
| | | return new WP_Error('invalid_price', "Post {$postID} has no valid price"); |
| | | } |
| | | |
| | | return [ |