Plugin Directory

Changeset 2349223


Ignore:
Timestamp:
07/30/2020 04:26:02 PM (6 years ago)
Author:
alonezcount
Message:

add integration testing to the plugin

Location:
ezcount/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ezcount/trunk/EZcount.php

    r2346759 r2349223  
    44 * Plugin URI:
    55 * Description: invoicing, clearing and paypal integration plugin.
    6  * Version: 1.8.3
     6 * Version: 1.8.4
    77 * Author: EZcount
    88 * Author URI:
     
    2424add_action( 'plugins_loaded', 'init_ezcount_gateway_class', 0 );
    2525
     26
    2627function init_ezcount_gateway_class() {
    2728    if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
    2829        return;
    2930    }
     31
    3032
    3133    /**
     
    8082         */
    8183        public function init_form_fields() {
     84
    8285            $env = array(
    8386                'demo.ezcount.co.il' => 'DEMO',
     
    8992            $current_user      = wp_get_current_user();
    9093            $defaultEmail      = $current_user->user_email;
     94
     95
     96            $integrationProblem=EZcount_helpers::testIntegration($this->environment,$this->api_key,$defaultEmail);
     97
     98
     99
    91100            $this->form_fields = array(
    92101                'clearing_integration' => array(
     
    165174                    'desc_tip'    => true,
    166175                ),
    167                 'section_title'    => array(
     176                'section_title2'    => array(
    168177                    'title' => __( 'Other Settings', '' ),
    169178                    'type'  => 'title',
     
    213222                    'desc_tip' => true
    214223                ),
    215 
    216224            );
     225
     226            /**
     227             * show the integration error
     228             */
     229            if($integrationProblem){
     230                $this->form_fields=array_merge(array('integrationProblem' => array(
     231                    'title' => "INTEGRATION TESTING ERROR! please fix the next issue",
     232                    'description' => "$integrationProblem",
     233                    'type' => 'title',
     234                )), $this->form_fields);
     235            }
    217236        }
    218237
  • ezcount/trunk/EZcount_helpers.php

    r2308563 r2349223  
    33class EZcount_helpers {
    44    static public $lastResponseDebugStr;
     5
     6    public static function testIntegration( $environment, $api_key, $developer_email ) {
     7        if ( ! is_email( $developer_email ) ) {
     8            return "a valid developer email is mandatory";
     9        }
     10        if ( preg_match( "/^([a-f0-9]{64})$/", $api_key ) != 1 ) {
     11            return "the api key is missing or in wrong format! $api_key";
     12        }
     13        // call ezcount
     14        $data["api_key"]         = $api_key;
     15        $data["developer_email"] = $developer_email;
     16        $res                     = self::_sendJsonRequest( 'https://' . $environment . '/api/createDoc', $data );
     17        if ( ! $res ) {
     18            return self::$lastResponseDebugStr;
     19        }
     20        /**
     21         * invalid api key
     22         */
     23        if($res->errNum==1){
     24            return $res->errMsg;
     25        }
     26
     27
     28        $success = wp_mail( $developer_email, 'EZcount plugin, test developer email', 'This email been sent as part of EZcount plugin integration testing' );
     29        if ( ! $success ) {
     30            return "Email integration testing failed, please check your mail server, the function wp_mail is not working properly while trying to send email to $developer_email";
     31        }
     32
     33        // empty string means we are goot!
     34        return "";
     35    }
    536
    637    public static function getApiBase() {
     
    940        $zc_payment->init_form_fields();
    1041        $zc_payment->init_settings();
    11         $environment = $zc_payment->get_option('environment');
     42        $environment = $zc_payment->get_option( 'environment' );
     43
    1244        return 'https://' . $environment . '/api/';
    1345    }
    1446
    15     public static function sendJsonRequest($url, $data = array()) {
     47    private static function _sendJsonRequest( $url, $data = array() ) {
     48        $content     = json_encode( $data );
     49        $responseObj = $data = wp_remote_post( $url,
     50                                               array(
     51                                                   'headers'     => array( 'Content-Type' => 'application/json; charset=utf-8' ),
     52                                                   'body'        => $content,
     53                                                   'method'      => 'POST',
     54                                                   'data_format' => 'body',
     55                                                   'sslverify'   => false,
     56                                               ) );
     57
     58        $jsonStr = wp_remote_retrieve_body( $responseObj );
     59        //connection problem
     60        if ( is_wp_error( $responseObj ) || wp_remote_retrieve_response_code( $responseObj ) != 200 ) {
     61            self::$lastResponseDebugStr = "Error in opening request, please check your Firewall, and check that CURL have permission to call the url " . $url . "\n response code" . wp_remote_retrieve_response_code( $responseObj );
     62        } else {
     63            self::$lastResponseDebugStr = $jsonStr;
     64        }
     65
     66        return json_decode( $jsonStr );
     67    }
     68
     69    public static function sendJsonRequest( $url, $data = array() ) {
    1670        //do validation
    1771        $zc_payment = new WC_Gateway_EZcount();
    1872        $zc_payment->init_form_fields();
    1973        $zc_payment->init_settings();
    20         $data["api_key"] = $zc_payment->get_option('api_key');
    21         $data["developer_email"] = $zc_payment->get_option('user_email');
     74        $data["api_key"]         = $zc_payment->get_option( 'api_key' );
     75        $data["developer_email"] = $zc_payment->get_option( 'user_email' );
    2276
    23         $content = json_encode($data);
    24         $responseObj = $data = wp_remote_post($url, array(
    25             'headers' => array('Content-Type' => 'application/json; charset=utf-8'),
    26             'body' => $content,
    27             'method' => 'POST',
    28             'data_format' => 'body',
    29             'sslverify' => false,
    30         ));
    31 
    32         $jsonStr = wp_remote_retrieve_body($responseObj);
    33         //connection problem
    34         if (is_wp_error($responseObj) || wp_remote_retrieve_response_code($responseObj) != 200) {
    35             self::$lastResponseDebugStr = "Error in opening request, please check your Firewall, and check that CURL have permission to call the url " . $url . "\n response code" . wp_remote_retrieve_response_code($responseObj);
    36         } else {
    37             self::$lastResponseDebugStr = $jsonStr;
    38         }
    39         return json_decode($jsonStr);
     77        return self::_sendJsonRequest( $url, $data );
    4078    }
    4179
    42     static public function encrypt($plaintext, $key) {
    43         $ivlen = openssl_cipher_iv_length($cipher = "AES-128-CBC");
    44         $iv = openssl_random_pseudo_bytes($ivlen);
    45         $ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, $options = OPENSSL_RAW_DATA, $iv);
    46         $hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary = true);
    47         $ciphertext = base64_encode($iv . $hmac . $ciphertext_raw);
     80    static public function encrypt( $plaintext, $key ) {
     81        $ivlen          = openssl_cipher_iv_length( $cipher = "AES-128-CBC" );
     82        $iv             = openssl_random_pseudo_bytes( $ivlen );
     83        $ciphertext_raw = openssl_encrypt( $plaintext, $cipher, $key, $options = OPENSSL_RAW_DATA, $iv );
     84        $hmac           = hash_hmac( 'sha256', $ciphertext_raw, $key, $as_binary = true );
     85        $ciphertext     = base64_encode( $iv . $hmac . $ciphertext_raw );
     86
    4887        return $ciphertext;
    4988    }
    5089
    51     static public function decrypt($ciphertext, $key) {
    52         $c = base64_decode($ciphertext);
    53         $ivlen = openssl_cipher_iv_length($cipher = "AES-128-CBC");
    54         $iv = substr($c, 0, $ivlen);
    55         $hmac = substr($c, $ivlen, $sha2len = 32);
    56         $ciphertext_raw = substr($c, $ivlen + $sha2len);
    57         $original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options = OPENSSL_RAW_DATA, $iv);
    58         $calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary = true);
    59         if (hash_equals($hmac, $calcmac))//PHP 5.6+ timing attack safe comparison
     90    static public function decrypt( $ciphertext, $key ) {
     91        $c                  = base64_decode( $ciphertext );
     92        $ivlen              = openssl_cipher_iv_length( $cipher = "AES-128-CBC" );
     93        $iv                 = substr( $c, 0, $ivlen );
     94        $hmac               = substr( $c, $ivlen, $sha2len = 32 );
     95        $ciphertext_raw     = substr( $c, $ivlen + $sha2len );
     96        $original_plaintext = openssl_decrypt( $ciphertext_raw, $cipher, $key, $options = OPENSSL_RAW_DATA, $iv );
     97        $calcmac            = hash_hmac( 'sha256', $ciphertext_raw, $key, $as_binary = true );
     98        if ( hash_equals( $hmac, $calcmac ) )//PHP 5.6+ timing attack safe comparison
    6099        {
    61100            return $original_plaintext;
    62101        }
     102
    63103        //failed to decrypt
    64104        return '';
  • ezcount/trunk/readme.txt

    r2346759 r2349223  
    44Tags        : Invoicing and clearing for Woocommerce, by EasyCount.
    55Tested up to: 4.9.8
    6 Version     : 1.8.3
     6Version     : 1.8.4
    77Stable tag: trunk
    88Requires PHP: 5.4
Note: See TracChangeset for help on using the changeset viewer.