Plugin Directory

Changeset 2355540


Ignore:
Timestamp:
08/09/2020 11:59:58 AM (6 years ago)
Author:
alonezcount
Message:

move session to cookies

Location:
ezcount/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ezcount/trunk/EZcount.php

    r2352461 r2355540  
    44 * Plugin URI:
    55 * Description: invoicing, clearing and paypal integration plugin.
    6  * Version: 1.9.4
     6 * Version: 1.10.1
    77 * Author: EZcount
    88 * Author URI:
     
    8181         */
    8282        public function init_form_fields() {
    83 
    8483            $env = array(
    8584                'demo.ezcount.co.il' => 'DEMO',
     
    8988                $env['devapp.ezcount.co.il'] = 'DEV';
    9089            }
    91             $current_user      = wp_get_current_user();
    92             $defaultEmail      = $current_user->user_email;
     90            $current_user = wp_get_current_user();
     91            $defaultEmail = $current_user->user_email;
    9392
    9493            $this->form_fields = array(
    95                 'environment'          => array(
     94                'environment'       => array(
    9695                    'title'       => __( 'Environment', $this->environment ),
    9796                    'type'        => 'select',
     
    101100                    'desc_tip'    => true,
    102101                ),
    103                 'api_key'              => array(
     102                'api_key'           => array(
    104103                    'title'       => __( 'API Key', $this->api_key ),
    105104                    'type'        => 'text',
     
    109108                    'required'    => true
    110109                ),
    111                 'user_email'           => array(
     110                'user_email'        => array(
    112111                    'title'       => __( 'Email Address', $this->user_email ),
    113112                    'type'        => 'text',
     
    117116                    'required'    => true
    118117                ),
    119                 'checkout_window'      => array(
     118                'checkout_window'   => array(
    120119                    'title'       => __( 'Checkout Window Option', $this->environment ),
    121120                    'type'        => 'select',
     
    128127                    'desc_tip'    => true,
    129128                ),
    130                 'doc_type'             => array(
     129                'doc_type'          => array(
    131130                    'title'       => __( 'Doc Type', $this->doc_type ),
    132131                    'type'        => 'select',
     
    160159                    'desc_tip'    => true,
    161160                ),
    162                 'section_title2'    => array(
     161                'section_title2'   => array(
    163162                    'title' => __( 'Other Settings', '' ),
    164163                    'type'  => 'title',
     
    220219                );
    221220                $this->form_fields = array_merge($arr, $this->form_fields);
    222             }
     221        }
    223222
    224223        }
     
    263262
    264263        public function getFormUrl( $order_id ) {
    265             $order = new WC_Order( $order_id );
    266 
    267             $sum = number_format( $order->get_total(), 2, '.', '' );
     264            $order      = new WC_Order( $order_id );
     265            $zc_payment = new WC_Gateway_EZcount();
     266            $api_key    = $zc_payment->get_option( 'api_key' );
     267            $sum        = number_format( $order->get_total(), 2, '.', '' );
    268268
    269269            $tab_id = base_convert( microtime(), 10, 32 );
     
    295295            }
    296296
    297             if ( ! WC()->session->has_session() ) {
    298                 $response = "WC session is not working, the plugin must have an open session to work, please consult your developer";
    299 
    300                 return "data:,; $response";
    301             }
    302 
    303             WC()->session->set( 'transaction_id:' . $tab_id, $result->secretTransactionId );
     297            $data = json_encode( array(
     298                                     'order_id'            => $order_id,
     299                                     'tab_id'              => $tab_id,
     300                                     "secretTransactionId" => $result->secretTransactionId,
     301                                     "creation"            => time()
     302                                 ) );
     303            //encrypt the data
     304            $dataEncrypted = EZcount_helpers::encrypt( $data, $api_key );
     305
     306            $name= "wc-ez-order-$order_id-$tab_id";
     307            $value=$dataEncrypted;
     308            //store for 5 days
     309            $expire= time() + 60 * 60 * 24 * 5;
     310            //https://stackoverflow.com/a/59654832/2992810
     311            if (PHP_VERSION_ID < 70300) {
     312                setcookie($name, $value, $expire, "/; samesite=Lax", null, is_ssl(), true);
     313            }
     314            else {
     315                setcookie($name, $value, [
     316                    'expires' => $expire,
     317                    'path' => '/',
     318                    'domain' => null,
     319                    'samesite' => 'Lax',
     320                    'secure' => is_ssl(),
     321                    'httponly' => true,
     322                ]);
     323            }
     324
    304325
    305326            return $result->url;
     
    310331            global $ezcountFormLoaded;
    311332            //if the iframe been called already
    312             // some users have this bug in their template and they are calling the iframe multiple times,
     333            // some users have this bug in their template and they are calling the iframe multiple times, 
    313334            // this creates multiple cookies, and the validation fails
    314335            if ( $ezcountFormLoaded ) {
     
    336357            $zc_payment->init_form_fields();
    337358            $zc_payment->init_settings();
     359            $api_key = $zc_payment->get_option( 'api_key' );
    338360
    339361            $order_id = (int) @$_GET['order_id'];
     362            $tab_id   = $_GET['tab_id'];
    340363
    341364            //get order by id
    342365            $order = new WC_Order( $order_id );
    343366
    344             $secretTransactionId = WC()->session->get( 'transaction_id:' . $_GET['tab_id'] );
    345             if ( ! $secretTransactionId) {
    346                 $msg = "בעיה באישור הזמנה $order_id, לא הצלחנו לבצע אישור מול חברת האשראי לסליקה, ההזמנה נכנסה לhold אנא בדקו מול חברת האשראי האם הכסף נכנס. \n<br/>\n
    347                 נראה שהמשתמשים שלכם מאבדים את הsession אנא בדקו את אורך הsession ועקבו אחרי המאמר הבא כדי לטפל בבעיות נפוצות של איבוד session.
    348                 it's seems the user lost the session, please check your session settings using the next article\n<br/>\n
    349                 https://en.blogpascher.com/wordpress-tutorial/How-correct-the-loss-of-session-on-wordpress\n<br/>\n
     367
     368            $cookieName    = "wc-ez-order-$order_id-$tab_id";
     369            $dataEncrypted = $_COOKIE[ $cookieName ];
     370
     371            // we have an error unless we found the transaction id
     372            $errType = 2;
     373
     374            if ( ! $dataEncrypted ) {
     375                $errType = 2.1;
     376            } else {
     377                $dataDecrypted = EZcount_helpers::decrypt( $dataEncrypted, $api_key );
     378                if ( ! $dataDecrypted ) {
     379                    $errType = 2.2;
     380                } else {
     381                    $data = json_decode( $dataDecrypted ,true);
     382                    if ( ! is_array( $data ) ) {
     383                        $errType = 2.3;
     384                    } elseif ( $data['order_id'] != $order_id || $data['tab_id'] != $tab_id ) {
     385                        $errType = 2.4;
     386                    } elseif ( ! $data['secretTransactionId'] ) {
     387                        $errType = 2.5;
     388                    } else {
     389                        $secretTransactionId = $data['secretTransactionId'];
     390                        //everything is OK, no error
     391                        $errType = 0;
     392                    }
     393                }
     394            }
     395
     396
     397            if ( $errType || ! $secretTransactionId ) {
     398                $msg = "בעיה באישור הזמנה $order_id, לא הצלחנו לבצע אישור מול חברת האשראי לסליקה, ההזמנה נכנסה לhold אנא בדקו מול חברת האשראי האם הכסף נכנס. \n
     399                נראה שהמשתמש שלכם איבד את העוגיה של הסליקה, זה יכול לקרות אם הוא ניקה עוגיות, המתין יותר מיומיים עד סיום הסליקה או משתמש בחסימת עוגיות בדפדפן. \n
     400                ללא העוגיה של הסליקה לא נוכל לבצע לה אימות. \n
     401                \n
     402                אנא הכנסו אל ממשק הדוחות->סליקה באיזיקאונט, וודאו שהסליקה עברה לפני שאתם נותנים ללקוח את הטובין\n
     403                \n\n\n
     404                מצורף מידע טכני
    350405                \n
    351                 Order ($order_id) transaction Id is empty, can't validate payment \n<br/>\n" .
     406                Order ($order_id) transaction Id is empty, can't validate payment \n\n" .
    352407                       json_encode( [
    353408                                        'order_id'            => $_GET['order_id'],
    354409                                        'tab_id'              => $_GET['tab_id'],
     410                                        "cookieName"          => $cookieName,
     411                                        "dataEncrypted"       => $dataEncrypted,
     412                                        "api_key"             => "..." . substr( $api_key, - 10 ),
     413                                        "dataDecrypted"       => $dataDecrypted,
     414                                        "data"                => json_encode( $data, 128 ),
     415                                        'secretTransactionId' => $secretTransactionId,
    355416                                        'timezone'            => date_default_timezone_get(),
    356                                         'server_date'         => date(DATE_ATOM),
    357                                         'secretTransactionId' => $secretTransactionId,
     417                                        'server_date'         => date( DATE_ATOM ),
    358418                                        '$_SERVER'            => $_SERVER
    359                                     ] );
    360                 wp_mail( $this->user_email, 'Order transaction Id is empty, can\'t validate payment', $msg );
    361                 echo "VALIDATION ERROR #1.5 לא הצלחנו לבצע אישור לסליקה, אבל יתכן והיא עברה, אנא צרו קשר עם בעל החנות";
    362                 $order->update_status( 'on-hold' );
     419                                    ],
     420                                    128 );
     421                wp_mail( $this->user_email, "VALIDATION ERROR #$errType,  לא הצלחנו לבצע אישור לסליקה עבור הזמנה מספר $order_id", $msg );
     422                echo "VALIDATION ERROR #$errType לא הצלחנו לבצע אישור לסליקה, אבל יתכן והיא עברה, אנא צרו קשר עם בעל החנות";
     423                 $order->update_status( 'on-hold' );
    363424                die();
    364425            }
     
    559620    }
    560621
    561     /**
    562      * We need longer sessions so we add this filters
    563      */
    564     //https://stackoverflow.com/a/39707517/2992810
    565     // https://github.com/ericmann/wp-session-manager/issues/25#issuecomment-299819341
    566     add_action( 'plugins_loaded',
    567         function () {
    568             add_filter( 'wp_session_expiration',
    569                 function () {
    570                     return 48 * 60 * 60;
    571                 } );
    572             add_filter( 'wp_session_expiration_variant',
    573                 function () {
    574                     return 46 * 60 * 60;
    575                 } );
    576         },
    577                 1 );
    578     // not sure which of them will do the job..
    579     // https://stackoverflow.com/a/62241135/2992810
    580     add_filter( 'wc_session_expiring', 'wc_custom_session_expiring' );
    581     add_filter( 'wc_session_expiration', 'wc_custom_session_expiring' );
    582     function wc_custom_session_expiring( $expiry ) {
    583         return 60 * 60 * 48;
    584     }
    585 
    586622    // load the ezcount gateway
    587623    add_filter( 'woocommerce_payment_gateways', 'add_ezcount_gateway' );
  • ezcount/trunk/readme.txt

    r2352461 r2355540  
    44Tags        : Invoicing and clearing for Woocommerce, by EasyCount.
    55Tested up to: 4.9.8
    6 Version     : 1.9.4
     6Version     : 1.10.1
    77Stable tag: trunk
    88Requires PHP: 5.4
     
    51511.9.4 - improve the fix
    5252
     531.10.1 - move session to cookies
    5354== Upgrade notice ==
    5455nothing speacial about upgrading
Note: See TracChangeset for help on using the changeset viewer.