canTest = true; $this->service_name = 'helcim'; $this->title = 'Helcim'; $this->icon = 'currency-dollar'; $this->canSync = [ 'initial' => true, 'update' => true, 'delete' => true, ]; $this->apiVersion = 'v2'; $this->baseUrl = sprintf('https://api.helcim.com/%s/', $this->apiVersion); $this->defineFields(); $this->defineInstructions(); parent::__construct(); } protected function defineFields():void { $api_token = $this->addField( 'api_token', 'API Token', 'text', [$this, 'testUserCanConnect'] ); $api_token->setSubType('password'); $api_token->setRequired(); $api_token->setHint('Found in Helcim Dashboard → Settings → API Access'); $api_token->makeSetupField(); $api_token->setHideStored(); $this->updateField('api_token', $api_token); $currency = $this->addField( 'currency', 'Currency', 'select', [$this, 'testUserSetup'] ); $currency->setOptions([ 'CAD' => 'CAD', 'USD' => 'USD' ]); $currency->setDefault('CAD'); $currency->makeAdditionalField(); $feeSaver = $this->addField( 'fee_saver', 'Fee Saver', 'true_false', [$this, 'testUserSetup'] ); $feeSaver->setHint('Pass processing fees to customers? (Not compatible with Google Pay)'); $feeSaver->makeAdditionalField(); $allowACH = $this->addField( 'allow_ach', 'Allow ACH/Bank Payments', 'true_false', [$this, 'testUserSetup'] ); $allowACH->setHint('Enable bank account payments alongside credit card'); $allowACH->makeAdditionalField(); } public function testUserCanConnect():bool { $user = get_current_user_id(); return $this->userCanConnect($user); } public function testUserSetup():bool { return $this->isSetUp() && $this->testUserCanConnect() && !empty($this->getApiToken()); } protected function defineInstructions():void { $this->instructions = [ 'Go to Helcim Dashboard', 'Navigate to Settings → API Access', 'Create a new API Access Configuration', 'Enable permissions: General (Customers, Invoices, Products), Transaction Processing', 'Copy the API Token and paste it below', ]; } protected function defineActions():void { if ($this->isSetUp()) { $this->addAction( 'Sync to Helcim', [$this, 'syncToHelcim'], 'sync_to_helcim', 'upload-simple' ); $this->addAction( 'Sync to Site', [$this, 'syncToSite'], 'sync_to_site', 'download-simple' ); } } protected function initialize(): void { // TODO: Implement initialize() method. } protected function getRequestHeaders(): array { return empty($this->getApiToken()) ? [] : [ 'api-token' => $this->getApiToken(), 'Accept' => 'application/json', ]; } protected function setContentTypes(): void { $this->contentTypes = [ 'recurring' => $this->buildRecurringPaymentFields(), 'product' => $this->buildPaymentFields(), 'customer' => $this->buildCustomerFields(), 'invoice' => $this->buildInvoiceFields(), ]; } protected function buildRecurringPaymentFields(): array { return []; } protected function buildPaymentFields(): array { return []; } protected function buildCustomerFields():array { return []; } protected function buildInvoiceFields():array { return []; } protected function formatForService(int $itemID, string $type = 'post'): array { return []; // TODO: Implement formatForService() method. } public function initializeCheckout():void { } public function syncToHelcim():void { } public function syncToSite():void { } /************************************************************************* * REQUESTS *************************************************************************/ protected function handleResponse(WP_Error|array $response, string $method, string $endpoint): array { if (is_wp_error($response) && $response->get_error_message() === 'rate_limit_exceeded') { return $this->response(false, 'Rate limit exceeded'); } return match ($endpoint) { 'connection-test' => $this->response(true,'Connection successful', $response), default => $this->response(false, 'Unhandled response for '.$endpoint), }; } protected function performConnectionTest(): bool { $response = $this->getRequest( 'connection-test' ); error_log('Helcim connection test: '.print_r($response, true)); return $response['success']??false; } /************************************************************************* * UTILITY *************************************************************************/ protected function getApiToken():string|false { return $this->loadCredentials()['api_token']??false; } }