Plugin Directory

Changeset 2946797


Ignore:
Timestamp:
08/02/2023 08:08:57 PM (2 years ago)
Author:
Helcim
Message:
  • Optimized transaction processing
  • Tested upto WordPress 6.2
  • Tested upto woocommerce v7.9
Location:
helcim-commerce-for-woocommerce
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • helcim-commerce-for-woocommerce/trunk/WCHelcimGateway.php

    r2893666 r2946797  
    1111require_once 'classes/HelcimCurl.php';
    1212require_once 'classes/HelcimDirectService.php';
     13require_once 'classes/HelcimApiFactory.php';
    1314
    1415/**
     
    2021 * @class        WCHelcimGateway
    2122 * @extends    WC_Payment_Gateway
    22  * @version    3.0.7
     23 * @version    4.0.0
    2324 * @author        Helcim Inc.
    2425 */
     
    2930    private const GOOGLE_RECAPTCHA_TEST_SITE_KEY = '6LcgxK0UAAAAAH9gzsILRr82KZSLrBcry9RynMn4';
    3031    private const GOOGLE_RECAPTCHA_LIVE_SITE_KEY = '6LdixK0UAAAAABmBXVo_jyFJSkQ5Doj9kloLyxGG';
     32    private const TRANSACTION_TYPE_PURCHASE = 'purchase';
     33    private const TRANSACTION_TYPE_PREAUTH = 'preauth';
    3134
    3235    public const PLUGIN_NAME = 'woocommerce';
    33     public const VERSION = '3.0.7';
     36    public const VERSION = '4.0.0';
    3437    public const HELCIM_CARD_TOKEN = 'helcim-card-token';
     38    /**
     39     * @deprecated please use self::API_V2_ENDPOINT
     40     */
    3541    public const API_ENDPOINT = 'https://secure.myhelcim.com/api/';
     42    public const API_V2_ENDPOINT = 'https://api.helcim.com/v2/';
    3643    public const HELCIM_SERVER_TIMEZONE = 'America/Edmonton';
    3744    public const FORCE_HELCIM_JS_TO_RUN_VERIFY = 1;
     
    4350    private $accountId;
    4451    private $apiToken;
     52    private $apiTokenV2;
    4553    private $show_logo;
    4654    private $transactionType;
     
    6674        $this->accountId = $this->get_option('accountId');
    6775        $this->apiToken = $this->get_option('apiToken');
     76        $this->apiTokenV2 = $this->get_option('apiTokenV2');
    6877        $this->title = '';
    6978        $this->description = $this->get_option('description');
     
    121130    {
    122131        return $this->apiToken;
     132    }
     133
     134    public function getAPITokenV2(): string
     135    {
     136        return $this->apiTokenV2;
    123137    }
    124138
     
    174188    {
    175189        $methods = ['js' => __('Helcim JS', 'woocommerce'), 'direct' => __('Direct Integration')];
    176         $transactionTypes = ['purchase' => __('Purchase', 'woocommerce'), 'preauth' => __('Pre-Authorization')];
     190        $transactionTypes = [
     191            self::TRANSACTION_TYPE_PURCHASE => __('Purchase', 'woocommerce'),
     192            self::TRANSACTION_TYPE_PREAUTH => __('Pre-Authorization')
     193        ];
    177194        $this->form_fields['enabled'] = [
    178195            'title' => __('Enable/Disable', 'woocommerce'),
     
    184201            'title' => __('Test', 'woocommerce'),
    185202            'type' => 'checkbox',
     203            'description' => __('Only used for verify transactions. Purchase & preauth will not be processed as test. please assign a demo terminal to your currency in commerce if you want to run a test purchase or preauth', 'woocommerce'),
    186204            'label' => __('Test Mode', 'woocommerce'),
    187205            'default' => 'no'
     
    192210            'options' => $methods,
    193211            'description' => __(
    194                 'Choose between using Helcim.js and Direct Integration - <a href="https://support.helcim.com/article/helcim-commerce-new-ui-integrations-woocommerce-integration-woocommerce/" target="_blank">Support Page</a>',
     212                'Choose between using Helcim.js and Direct Integration - <a href="https://learn.helcim.com/docs/accepting-payments-using-woocommerce" target="_blank">Support Page</a>',
    195213                'woocommerce'
    196214            ),
     
    218236            'default' => ''
    219237        ];
    220         $this->form_fields['apiToken'] = [
     238        $this->form_fields['apiTokenV2'] = [
    221239            'title' => __('API Token', 'woocommerce'),
    222240            'type' => 'text',
    223241            'description' => __('Your Helcim Commerce API Token', 'woocommerce'),
    224             'default' => ''
     242            'default' => '',
     243        ];
     244        $this->form_fields['apiToken'] = [
     245            'title' => __('API Token - Deprecated', 'woocommerce'),
     246            'type' => 'text',
     247            'description' => __('Deprecated. Please fill API Token instead', 'woocommerce'),
     248            'default' => '',
     249            'custom_attributes' => [
     250                'disabled' => 'disabled',
     251            ],
    225252        ];
    226253        $this->form_fields['transactionType'] = [
     
    232259        ];
    233260        $this->form_fields['terminalId'] = [
    234             'title' => __('Terminal Id', 'woocommerce'),
     261            'title' => __('Terminal Id - Deprecated', 'woocommerce'),
    235262            'type' => 'number',
    236             'description' => __('Commerce Terminal Id', 'woocommerce'),
    237             'default' => '0'
     263            'description' => __('Deprecated. Default terminal for the currency will be used', 'woocommerce'),
     264            'default' => '0',
     265            'custom_attributes' => [
     266                'disabled' => 'disabled',
     267            ],
    238268        ];
    239269        $this->form_fields['description'] = [
     
    280310        }
    281311        if (!$this->getHelcimDirectService() instanceof HelcimDirectService) {
    282             $this->setHelcimDirectService(new HelcimDirectService());
     312            $this->setHelcimDirectService(new HelcimDirectService(new HelcimCurl()));
    283313        }
    284314        $order = wc_get_order($orderId);
     
    308338            ];
    309339        }
    310         $xmlObject = $this->getHelcimDirectService()->processPurchasePreauth($order, $this);
    311         if (!$xmlObject instanceof SimpleXMLElement) {
    312             wc_add_notice('<b>Payment error:</b> Something went wrong please contact the Merchant', 'error');
    313             self::log("ORDER {$order->get_id()} ERROR - {$this->getHelcimDirectService()->getError()}");
    314             $order->add_order_note(
    315                 "Helcim Payment Failed - {$this->getHelcimDirectService()->getError()}"
     340        if(empty($this->apiTokenV2)){
     341            // todo remove whole if condition
     342            $xmlObject = $this->getHelcimDirectService()->processPurchasePreauth($order, $this);
     343            if (!$xmlObject instanceof SimpleXMLElement) {
     344                wc_add_notice('<b>Payment error:</b> Something went wrong please contact the Merchant', 'error');
     345                self::log("ORDER {$order->get_id()} ERROR - {$this->getHelcimDirectService()->getError()}");
     346                $order->add_order_note(
     347                    "Helcim Payment Failed - {$this->getHelcimDirectService()->getError()}"
     348                );
     349                return [];
     350            }
     351            $this->completeOrder(
     352                $order,
     353                (string)$xmlObject->transaction->approvalCode ?: '',
     354                isset($xmlObject->transaction->transactionId) ? (string)$xmlObject->transaction->transactionId : '0',
     355                (string)$xmlObject->transaction->cardToken ?: ''
    316356            );
    317             return [];
    318         }
    319         $this->completeOrder(
    320             $order,
    321             (string)$xmlObject->transaction->approvalCode ?: '',
    322             isset($xmlObject->transaction->transactionId) ? (string)$xmlObject->transaction->transactionId : '0',
    323             (string)$xmlObject->transaction->cardToken ?: ''
    324         );
     357        }else{
     358            $responsePayload = [];
     359
     360            // create customer
     361            $customerCode = '';
     362            if(!empty($_POST['customerCode']) && $this->isJS()){
     363                $customerCode = (string)$_POST['customerCode'];
     364                if(get_current_user_id() && (string)$_POST['customerCode'] !== (string)get_current_user_id()){
     365                    $customerCode = (string)get_current_user_id();
     366                    $customerIdOld = $this->getHelcimDirectService()->getCustomerId((string)$_POST['customerCode'], $this);
     367                    $customerIdCurrent = $this->getHelcimDirectService()->getCustomerId($customerCode, $this);
     368                    if(!$customerIdCurrent){
     369                        if($customerIdOld){
     370                            $this->getHelcimDirectService()->updateCustomer($customerIdOld, $customerCode, $order, $this);
     371                        }else{
     372                            $this->getHelcimDirectService()->createCustomer($customerCode, $order, $this);
     373                        }
     374                    }else{
     375                        self::log("cannot update customer code from(".(string)$_POST['customerCode'].") to(".(string)get_current_user_id()."). new customer code is already used");
     376                    }
     377                }
     378            }elseif(get_current_user_id()){
     379                $customerCode = (string)get_current_user_id();
     380                $customerIdCurrent = $this->getHelcimDirectService()->getCustomerId($customerCode, $this);
     381                if(!$customerIdCurrent){
     382                    $this->getHelcimDirectService()->createCustomer($customerCode, $order, $this);
     383                }
     384            }
     385
     386            // process
     387            if($this->transactionType === self::TRANSACTION_TYPE_PURCHASE){
     388                $responsePayload = $this->getHelcimDirectService()->processPurchase($order, $this, $customerCode);
     389                if (!is_array($responsePayload)) {
     390                    wc_add_notice('<b>Payment error:</b> Something went wrong please contact the Merchant', 'error');
     391                    self::log("ORDER {$order->get_id()} ERROR - {$this->getHelcimDirectService()->getError()}");
     392                    $order->add_order_note(
     393                        "Helcim Payment Failed - {$this->getHelcimDirectService()->getError()}"
     394                    );
     395                    return [];
     396                }
     397            }elseif($this->transactionType === self::TRANSACTION_TYPE_PREAUTH){
     398                $responsePayload = $this->getHelcimDirectService()->processPreauth($order, $this, $customerCode);
     399                if (!is_array($responsePayload)) {
     400                    wc_add_notice('<b>Payment error:</b> Something went wrong please contact the Merchant', 'error');
     401                    self::log("ORDER {$order->get_id()} ERROR - {$this->getHelcimDirectService()->getError()}");
     402                    $order->add_order_note(
     403                        "Helcim Payment Failed - {$this->getHelcimDirectService()->getError()}"
     404                    );
     405                    return [];
     406                }
     407            }
     408            $this->completeOrder(
     409                $order,
     410                (string)$responsePayload['approvalCode'],
     411                (string)$responsePayload['transactionId'],
     412                (string)$responsePayload['cardToken']
     413            );
     414        }
    325415        return [
    326416            'result' => 'success',
     
    339429    {
    340430        if (!$this->getHelcimDirectService() instanceof HelcimDirectService) {
    341             $this->setHelcimDirectService(new HelcimDirectService());
    342         }
     431            $this->setHelcimDirectService(new HelcimDirectService(new HelcimCurl()));
     432        }
     433
    343434        $amount = (float)$amount;
    344435        $order = wc_get_order($orderId);
    345         $approvalCode = $this->getHelcimDirectService()->processRefund($order, $amount, $this);
     436        if(empty($this->apiTokenV2)) {
     437            // todo remove whole if condition
     438            $approvalCode = $this->getHelcimDirectService()->processRefund($order, $amount, $this);
     439        }else{
     440            $approvalCode = $this->getHelcimDirectService()->processRefundV2($order, $amount, $this);
     441        }
    346442        if (!is_string($approvalCode)) {
    347443            wc_add_notice("Refund Failed - {$this->getHelcimDirectService()->getError()}", 'error');
     
    718814    {
    719815        if (!$this->getHelcimDirectService() instanceof HelcimDirectService) {
    720             $this->setHelcimDirectService(new HelcimDirectService());
     816            $this->setHelcimDirectService(new HelcimDirectService(new HelcimCurl()));
    721817        }
    722818        $objectXML = $this->getHelcimDirectService()->processVoid($transactionId, $this);
  • helcim-commerce-for-woocommerce/trunk/classes/HelcimCurl.php

    r2605840 r2946797  
    33class HelcimCurl
    44{
    5     private $error;
    6     private $curlErrorNumber;
    7     private $curlError;
    8     private $request;
    9     private $response;
     5    private string $error;
     6    private int $curlErrorNumber;
     7    private string $curlError;
     8    private string $request;
     9    private string $response;
    1010
    1111    public function __construct()
     
    2929    }
    3030
    31     public function curl(array $request, string $url): ?string
     31    public function curl(array $request, string $url, array $headers = [], string $method = 'POST'): ?string
    3232    {
    33         $this->setRequest(http_build_query($request));
    3433        $curlOptions = [
    3534            CURLOPT_RETURNTRANSFER => 1,
     
    3736            CURLOPT_FRESH_CONNECT => true,
    3837            CURLOPT_HEADER => false,
    39             CURLOPT_POST => true,
    4038            CURLOPT_POSTFIELDS => $this->getRequest(),
    4139            CURLOPT_TIMEOUT => 60
    4240        ];
     41        if ($method === 'GET') {
     42            $this->setRequest(http_build_query($request));
     43            $url .= "?{$this->getRequest()}";
     44            $curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
     45        } else {
     46            $this->setRequest(json_encode($request));
     47            $curlOptions[CURLOPT_POSTFIELDS] = $this->getRequest();
     48            $curlOptions[CURLOPT_HTTPHEADER][] = 'Content-Type: application/json';
     49            if ($method === 'POST') {
     50                $curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
     51            } elseif ($method === 'PUT') {
     52                $curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
     53            } else {
     54                WCHelcimGateway::log("Invalid Method: $method");
     55                return null;
     56            }
     57        }
     58
     59        if (count($headers) > 0) {
     60            foreach ($headers as $h){
     61                $curlOptions[CURLOPT_HTTPHEADER][] = $h;
     62            }
     63        }
    4364        $curl = curl_init($url);
    4465        curl_setopt_array($curl, $curlOptions);
    4566        $response = curl_exec($curl);
     67
     68        $info = curl_getinfo($curl);
    4669        $this->setResponse('');
    4770        $this->setCurlErrorNumber(curl_errno($curl));
     
    5073        if ($response === false) {
    5174            $this->setError("Communication to Helcim Failed - ({$this->getCurlErrorNumber()}) {$this->getCurlError()}");
     75            WCHelcimGateway::log("$method $url {$info['http_code']} curlError:({$this->getCurlErrorNumber()}) {$this->getCurlError()}");
    5276            return null;
    5377        }
     78        if (!isset($info['http_code']) || (int)$info['http_code'] !== 200){
     79            $responseArray = json_decode($response, true);
     80            if(is_array($responseArray) && isset($responseArray['errors'])){
     81                $errors = is_array($responseArray['errors'])
     82                    ? implode(', ',$responseArray['errors']) : (string)$responseArray['errors'];
     83            }else{
     84                $errors = (string)$response;
     85            }
     86            WCHelcimGateway::log("$method $url {$info['http_code']} error:$errors");
     87            $this->setError("Communication to Helcim Failed - httpCode:{$info['http_code']} response:$response");
     88            return null;
     89        }
     90        WCHelcimGateway::log("$method $url {$info['http_code']}");
    5491        $this->setResponse($response);
    5592        return $this->getResponse();
     
    135172    }
    136173
     174    /**
     175     * @deprecated please use HelcimApiFactory::genericPaymentPayload
     176     * @param WCHelcimGateway $wcHelcimGateway
     177     * @return array
     178     */
    137179    public function buildGenericPostData(WCHelcimGateway $wcHelcimGateway): array
    138180    {
  • helcim-commerce-for-woocommerce/trunk/classes/HelcimDirectService.php

    r2893666 r2946797  
    33class HelcimDirectService
    44{
    5     private $helcimCURL;
    6     private $error;
    7 
    8     public function __construct()
    9     {
     5    private HelcimCurl $helcimCURL;
     6    private string $error;
     7
     8    public function __construct(HelcimCurl $helcimCurl)
     9    {
     10        $this->helcimCURL = $helcimCurl;
    1011        $this->error = '';
    1112    }
    1213
    1314    /**
     15     * @deprecated please use self::processRefundV2
    1416     * @param WC_Order $order
    1517     * @param float $amount
    1618     * @param WCHelcimGateway $helcimGateway
    17      * @return false|string
     19     * @return string|null
    1820     */
    1921    public function processRefund(WC_Order $order, float $amount, WCHelcimGateway $helcimGateway): ?string
    2022    {
    21         if (!$this->getHelcimCURL() instanceof HelcimCurl) {
    22             $this->setHelcimCURL(new HelcimCurl());
    23         }
     23
    2424        if (!$this->checkOrderForRefund($order, $amount)) {
    2525            $this->setError("Error - {$this->getError()}");
    2626            return null;
    2727        }
    28         $response = $this->getHelcimCURL()->curl(
     28        $response = $this->helcimCURL->curl(
    2929            $this->buildRefundPostData($helcimGateway, $order, $amount),
    3030            WCHelcimGateway::API_ENDPOINT
    3131        );
    3232        if (!is_string($response)) {
    33             $this->setError($this->getHelcimCURL()->getError());
    34             return null;
    35         }
    36         $objectXML = $this->getHelcimCURL()->validXML($response);
     33            $this->setError($this->helcimCURL->getError());
     34            return null;
     35        }
     36        $objectXML = $this->helcimCURL->validXML($response);
    3737        if (!$objectXML instanceof SimpleXMLElement) {
    38             $this->setError($this->getHelcimCURL()->getError());
     38            $this->setError($this->helcimCURL->getError());
    3939            return null;
    4040        }
     
    4242    }
    4343
     44    public function processRefundV2(WC_Order $order, float $amount, WCHelcimGateway $helcimGateway): ?string
     45    {
     46
     47        if (!$this->checkOrderForRefund($order, $amount)) {
     48            $this->setError("Error - {$this->getError()}");
     49            return null;
     50        }
     51        $response = $this->helcimCURL->curl(
     52            HelcimApiFactory::buildRefund($order, $amount, $helcimGateway),
     53            WCHelcimGateway::API_V2_ENDPOINT.'payment/refund',
     54            array_merge(
     55                HelcimApiFactory::apiV2Headers($helcimGateway),
     56                ['idempotency-key: '.HelcimApiFactory::buildIdempotencyKey(25)]
     57            )
     58        );
     59        if (!is_string($response)) {
     60            $this->setError($this->helcimCURL->getError());
     61            return null;
     62        }
     63        $responsePayload = json_decode($response, true);
     64        if (!is_array($responsePayload)) {
     65            $this->setError($this->helcimCURL->getError());
     66            return null;
     67        }
     68        return isset($responsePayload['approvalCode']) ? (string)$responsePayload['approvalCode'] : '';
     69    }
     70
    4471    private function buildRefundPostData(WCHelcimGateway $helcimGateway, $order, float $amount): array
    4572    {
    46         if (!$this->getHelcimCURL() instanceof HelcimCurl) {
    47             $this->setHelcimCURL(new HelcimCurl());
    48         }
    49         $postData = $this->getHelcimCURL()->buildGenericPostData($helcimGateway);
     73
     74        $postData = $this->helcimCURL->buildGenericPostData($helcimGateway);
    5075        $postData['transactionType'] = 'refund';
    5176        $postData['transactionId'] = $order->get_transaction_id();
     
    5984    public function processVoid(int $transactionId, WCHelcimGateway $wcHelcimGateway): ?SimpleXMLElement
    6085    {
    61         if (!$this->getHelcimCURL() instanceof HelcimCurl) {
    62             $this->setHelcimCURL(new HelcimCurl());
    63         }
    64         $response = $this->getHelcimCURL()->curl(
     86
     87        $response = $this->helcimCURL->curl(
    6588            $this->buildVoidPostData($wcHelcimGateway, $transactionId),
    6689            WCHelcimGateway::API_ENDPOINT
    6790        );
    6891        if (!is_string($response)) {
    69             $this->setError($this->getHelcimCURL()->getError());
    70             return null;
    71         }
    72         $objectXML = $this->getHelcimCURL()->validXML($response);
     92            $this->setError($this->helcimCURL->getError());
     93            return null;
     94        }
     95        $objectXML = $this->helcimCURL->validXML($response);
    7396        if (!$objectXML instanceof SimpleXMLElement) {
    74             $this->setError($this->getHelcimCURL()->getError());
     97            $this->setError($this->helcimCURL->getError());
    7598            return null;
    7699        }
     
    80103    private function buildVoidPostData(WCHelcimGateway $wcHelcimGateway, int $transactionId): array
    81104    {
    82         if (!$this->getHelcimCURL() instanceof HelcimCurl) {
    83             $this->setHelcimCURL(new HelcimCurl());
    84         }
    85         $postData = $this->getHelcimCURL()->buildGenericPostData($wcHelcimGateway);
     105
     106        $postData = $this->helcimCURL->buildGenericPostData($wcHelcimGateway);
    86107        $postData['transactionType'] = 'void';
    87108        $postData['transactionId'] = $transactionId;
     
    90111    }
    91112
     113    /**
     114     * @deprecated please use self::processPurchase or self::processPreauth
     115     */
    92116    public function processPurchasePreauth($order, WCHelcimGateway $wcHelcimGateway): ?SimpleXMLElement
    93117    {
    94         if (!$this->getHelcimCURL() instanceof HelcimCurl) {
    95             $this->setHelcimCURL(new HelcimCurl());
    96         }
    97         $response = $this->getHelcimCURL()->curl(
     118
     119        $response = $this->helcimCURL->curl(
    98120            $this->buildPurchasePreauthPostData($order, $wcHelcimGateway),
    99121            WCHelcimGateway::API_ENDPOINT
    100122        );
    101123        if (!is_string($response)) {
    102             $this->setError($this->getHelcimCURL()->getError());
    103             return null;
    104         }
    105         $objectXML = $this->getHelcimCURL()->validXML($response);
     124            $this->setError($this->helcimCURL->getError());
     125            return null;
     126        }
     127        $objectXML = $this->helcimCURL->validXML($response);
    106128        if (!$objectXML instanceof SimpleXMLElement) {
    107             $this->setError($this->getHelcimCURL()->getError());
     129            $this->setError($this->helcimCURL->getError());
    108130            return null;
    109131        }
     
    111133    }
    112134
     135    public function processPurchase(WC_Abstract_Order $order, WCHelcimGateway $wcHelcimGateway, string $customerCode): ?array
     136    {
     137        $response = $this->helcimCURL->curl(
     138            HelcimApiFactory::buildPurchasePreauth($wcHelcimGateway, $order, $customerCode),
     139            WCHelcimGateway::API_V2_ENDPOINT.'payment/purchase',
     140            array_merge(
     141                HelcimApiFactory::apiV2Headers($wcHelcimGateway),
     142                ['idempotency-key: '.HelcimApiFactory::buildIdempotencyKey(25)]
     143            )
     144        );
     145        //(string)openssl_random_pseudo_bytes(25)
     146        if (!is_string($response)) {
     147            $this->setError($this->helcimCURL->getError());
     148            return null;
     149        }
     150        $responsePayload = json_decode($response, true);
     151        if (!is_array($responsePayload)) {
     152            $this->setError($this->helcimCURL->getError());
     153            return null;
     154        }
     155        return $responsePayload;
     156    }
     157
     158    public function processPreauth(WC_Abstract_Order $order, WCHelcimGateway $wcHelcimGateway, string $customerCode): ?array
     159    {
     160
     161        $response = $this->helcimCURL->curl(
     162            HelcimApiFactory::buildPurchasePreauth($wcHelcimGateway, $order, $customerCode),
     163            WCHelcimGateway::API_V2_ENDPOINT.'payment/preauth',
     164            array_merge(
     165                HelcimApiFactory::apiV2Headers($wcHelcimGateway),
     166                ['idempotency-key: '.HelcimApiFactory::buildIdempotencyKey(25)]
     167            )
     168        );
     169        if (!is_string($response)) {
     170            $this->setError($this->helcimCURL->getError());
     171            return null;
     172        }
     173        $responsePayload = json_decode($response, true);
     174        if (!is_array($responsePayload)) {
     175            $this->setError($this->helcimCURL->getError());
     176            return null;
     177        }
     178        return $responsePayload;
     179    }
     180
     181    /**
     182     * @deprecated please use self::buildPurchasePreauthPayload
     183     */
    113184    private function buildPurchasePreauthPostData($order, WCHelcimGateway $wcHelcimGateway): array
    114185    {
     
    123194            }
    124195        }
    125         if (!$this->getHelcimCURL() instanceof HelcimCurl) {
    126             $this->setHelcimCURL(new HelcimCurl());
    127         }
    128         $helcimArgs = $this->getHelcimCURL()->buildGenericPostData($wcHelcimGateway);
     196
     197        $helcimArgs = $this->helcimCURL->buildGenericPostData($wcHelcimGateway);
    129198        $helcimArgs['transactionType'] = $wcHelcimGateway->getTransactionType();
    130199        $helcimArgs['terminalId'] = $wcHelcimGateway->getTerminalId();
     
    167236            foreach ($order->get_items() as $item) {
    168237                $orderItemCounter++;
    169                 $product = $order->get_product_from_item($item);
     238                $product = $item->get_product();
    170239                $sku = $product->get_sku() ?: 'NoSKU';
    171240                $helcimArgs['itemSKU' . $orderItemCounter] = $sku;
     
    209278    }
    210279
    211     public function getHelcimCURL(): ?HelcimCurl
    212     {
    213         return $this->helcimCURL;
    214     }
    215 
    216     public function setHelcimCURL(HelcimCurl $helcimCURL): HelcimDirectService
    217     {
    218         $this->helcimCURL = $helcimCURL;
    219         return $this;
    220     }
    221 
    222280    private function checkOrderForRefund(WC_Order $order, float $amount): bool
    223281    {
     
    247305        return $this;
    248306    }
     307
     308    public function getCustomerId(string $customerCode, WCHelcimGateway $helcimGateway): ?int
     309    {
     310
     311        $response = $this->helcimCURL->curl(
     312            ['customerCode' => $customerCode],
     313            WCHelcimGateway::API_V2_ENDPOINT.'customers/',
     314            HelcimApiFactory::apiV2Headers($helcimGateway),
     315            'GET'
     316        );
     317        if (!is_string($response)) {
     318            $this->setError($this->helcimCURL->getError());
     319            return false;
     320        }
     321        $responsePayload = json_decode($response, true);
     322        if(!is_array($responsePayload) || count($responsePayload) === 0){
     323            return 0;
     324        }
     325        $customer = reset($responsePayload);
     326        return isset($customer['id']) ? (int)$customer['id'] : 0;
     327    }
     328
     329    public function updateCustomer(int $customerId, string $customerCode, WC_Abstract_Order $order, WCHelcimGateway $helcimGateway): ?array
     330    {
     331
     332        $response = $this->helcimCURL->curl(
     333            [
     334                'customerCode' => $customerCode,
     335                'contactName' => $customerCode,
     336                'businessName' => $customerCode
     337            ],
     338            WCHelcimGateway::API_V2_ENDPOINT."customers/$customerId",
     339            HelcimApiFactory::apiV2Headers($helcimGateway),
     340            'PUT'
     341        );
     342        if (!is_string($response)) {
     343            $this->setError($this->helcimCURL->getError());
     344            return null;
     345        }
     346        $responsePayload = json_decode($response, true);
     347        if (!is_array($responsePayload)) {
     348            $this->setError($this->helcimCURL->getError());
     349            return null;
     350        }
     351        return $responsePayload;
     352    }
     353
     354    public function createCustomer(string $customerCode, WC_Abstract_Order $order, WCHelcimGateway $wcHelcimGateway): ?array
     355    {
     356
     357        $response = $this->helcimCURL->curl(
     358            HelcimApiFactory::buildCustomerCreate($customerCode, $order),
     359            WCHelcimGateway::API_V2_ENDPOINT."customers/",
     360            HelcimApiFactory::apiV2Headers($wcHelcimGateway)
     361        );
     362        if (!is_string($response)) {
     363            $this->setError($this->helcimCURL->getError());
     364            return null;
     365        }
     366        $responsePayload = json_decode($response, true);
     367        if (!is_array($responsePayload)) {
     368            $this->setError($this->helcimCURL->getError());
     369            return null;
     370        }
     371        return $responsePayload;
     372    }
    249373}
  • helcim-commerce-for-woocommerce/trunk/index.php

    r2893666 r2946797  
    44Plugin URI: https://www.helcim.com/
    55Description: Helcim Commerce for WooCommerce
    6 Version: 3.0.7
     6Version: 4.0.0
    77Author: Helcim Inc.
    88Author URI: https://www.helcim.com/
  • helcim-commerce-for-woocommerce/trunk/readme.txt

    r2893666 r2946797  
    44Requires at least: 4.7.3
    55Tested up to: 6.1.0
     6PHP Version: 7.4 or higher
    67
    78== Description ==
     
    1920
    2021**REQUIREMENTS:**
    21        - WooCommerce 2.6.14 to 6.4.1
     22       - WooCommerce 2.6.14 to 7.8.0
    2223       - [Helcim account](https://hub.helcim.com/signup/register/)
    2324
     
    7374== Changelog ==
    7475
     76= 4.0.0 =
     77* Optimized transaction processing
     78* Tested upto WordPress 6.2
     79* Tested upto woocommerce v7.9
     80
     81= 3.0.8 =
     82* Tested upto WordPress 6.2
     83* Tested upto woocommerce v7.8
     84
    7585= 3.0.7 =
    7686* Tested upto WordPress 6.1
  • helcim-commerce-for-woocommerce/trunk/uninstall.php

    r2893666 r2946797  
    55 * Uninstalls Helcim Gateway.
    66 *
    7  * @version    3.0.7
     7 * @version    4.0.0
    88 * @author        Helcim Inc.
    99 */
Note: See TracChangeset for help on using the changeset viewer.