Jake Vanderwerf
2026-09-18 a6413da67a4579866f0fd0323ddb23a7b6b9654a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
 
namespace JVBase\integrations\services;
use JVBase\integrations\Admin;
use JVBase\Instance;
use JVBase\integrations\Integrations;
use JVBase\integrations\OAuth;
use JVBase\integrations\OAuthCredentials;
use JVBase\integrations\Requests;
use JVBase\integrations\SyncToPost;
use JVBase\integrations\UserConnection;
use WP_Error;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Helcim Integration Class
 *
 * Handles HelcimPay.js checkout, invoice retrieval, customer/card management,
 * and bidirectional product sync via API token authentication.
 *
 * Helcim is the source of truth for invoices and orders.
 * Products sync bidirectionally through the standard integration field flow.
 *
 * @since 1.0.0
 */
class Helcim extends Integrations
{
    use Admin, Instance, Requests, UserConnection, SyncToPost;
 
    protected function __construct()
    {
        $this->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 <a href="https://my.helcim.com" target="_blank">Helcim Dashboard</a>',
                '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;
    }
}