Plugin Directory

Changeset 2519268


Ignore:
Timestamp:
04/21/2021 04:52:19 PM (5 years ago)
Author:
rspective
Message:

Committing changes for 2.1.4

Location:
voucherify/trunk
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • voucherify/trunk/readme.txt

    r2517904 r2519268  
    77WC tested up to: 4.3.1
    88WC requires at least: 3.0.0
    9 Stable tag: 2.1.3
     9Stable tag: 2.1.4
    1010
    1111Integrates Voucherify API with woocommerce
     
    5050== Changelog ==
    5151
     52= 2.1.4 - 2021-04-21 =
     53* Fix: Releasing voucherify session when order is cancelled
     54* Improvement: added session release button to the order edit screen
     55
    5256= 2.1.3 - 2021-04-19 =
    5357* Fix: Supporting PayPal IPN
  • voucherify/trunk/src/class-voucherify-client-extension.php

    r2491101 r2519268  
    2323
    2424        public function release_session_lock( $code, $session_key ) {
    25             $this->client->delete( '/vouchers/' . $code . '/sessions/' . $session_key );
     25            return $this->client->delete( '/vouchers/' . $code . '/sessions/' . $session_key );
    2626        }
    2727    }
  • voucherify/trunk/src/class-voucherify-order-placing-session.php

    r2504530 r2519268  
    99
    1010        private $fake_session_for_rest_api = [
    11             'voucherify_valid_vouchers' => [],
     11            'voucherify_valid_vouchers'  => [],
    1212            'voucherify_removed_coupons' => []
    1313        ];
     
    3030                $validations = [];
    3131            }
    32             $validations[ $code ] = $response;
     32            $response_array                 = (array) $response;
     33            $response_array['created_time'] = time();
     34            $validations[ $code ]           = (object) $response_array;
    3335            WC()->session->set( 'voucherify_valid_vouchers', $validations );
    3436        }
     
    5658         */
    5759        public function get_all_validation_responses() {
    58             if ( vcrf_is_rest_request() ) {
     60            if ( vcrf_is_rest_request() || is_admin() ) {
    5961                return $this->fake_session_for_rest_api['voucherify_valid_vouchers'];
    6062            }
     
    7274         */
    7375        public function add_removed_coupon( $code ) {
    74             if ( vcrf_is_rest_request() ) {
     76            if ( vcrf_is_rest_request() || is_admin() ) {
    7577                $this->fake_session_for_rest_api['voucherify_removed_coupons'][] = $code;
    7678
     
    9092         * @param string $code
    9193         *
    92          * @return string session_key
     94         * @return array session_data
    9395         */
    94         public function get_validation_session_key( $code ) {
     96        public function get_validation_session_data( $code ) {
    9597            $response = $this->get_validation_response( $code );
    9698
    9799            return isset( $response, $response->session, $response->session->key )
    98                 ? $response->session->key : '';
     100                ? [
     101                    'key'          => $response->session->key,
     102                    'ttl'          => $response->session->ttl,
     103                    'created_time' => $response->created_time
     104                ] : [ 'key' => '', 'ttl' => null, 'created_time' => null ];
    99105        }
    100106
     
    105111         */
    106112        public function get_removed_coupons() {
    107             if ( vcrf_is_rest_request() ) {
     113            if ( vcrf_is_rest_request() || is_admin() ) {
    108114                return $this->fake_session_for_rest_api['voucherify_removed_coupons'];
    109115            }
     
    140146         */
    141147        public function clear() {
    142             if ( vcrf_is_rest_request() ) {
     148            if ( vcrf_is_rest_request() || is_admin() ) {
    143149                $this->fake_session_for_rest_api['voucherify_removed_coupons'] = [];
    144150                $this->fake_session_for_rest_api['voucherify_valid_vouchers']  = [];
  • voucherify/trunk/src/class-voucherify-redemption-service.php

    r2491101 r2519268  
    158158                $order = vcrf_get_admin_order();
    159159            }
    160             if ( ! empty( $order ) && $order->meta_exists( '_voucherify_session_key' ) ) {
     160            $session_key = $order->get_meta( '_voucherify_session_key', true );
     161            if ( ! empty( $order ) && ! empty($session_key) ) {
    161162                $session_key = $order->get_meta( '_voucherify_session_key', true );
    162163            } else {
    163                 $session_key = $this->order_placing_session->get_validation_session_key( $code );
     164                $session_data = $this->order_placing_session->get_validation_session_data( $code );
     165                $session_key = $session_data['key'];
    164166                $order->add_meta_data( '_voucherify_session_key', $session_key, true );
     167                $order->add_meta_data( '_voucherify_session_ttl', $session_data['ttl'], true );
     168                $order->add_meta_data( '_voucherify_session_created_time', $session_data['created_time'], true );
    165169            }
    166170            if ( empty( $session_key ) ) {
  • voucherify/trunk/src/class-voucherify-refund-order-listener.php

    r2491101 r2519268  
    99        /** @var Voucherify_Redemption_Service $redemption_service */
    1010        private $redemption_service;
     11        /** @var Voucherify_Remove_Coupon_Service $remove_coupon_service */
     12        private $remove_coupon_service;
    1113
    1214        /**
     
    1416         */
    1517        public function __construct(
    16             Voucherify_Redemption_Service $redemption_service
     18            Voucherify_Redemption_Service $redemption_service,
     19            Voucherify_Remove_Coupon_Service $remove_coupon_service
    1720        ) {
    1821            $this->redemption_service = $redemption_service;
     22            $this->remove_coupon_service = $remove_coupon_service;
    1923        }
    2024
     
    7882                    $voucher_value = $redeemed_voucher->value;
    7983                    $this->redemption_service->rollback_redemption( $voucher_value['redemption_id'] );
     84                    $this->remove_coupon_service->remove_coupon_from_voucherify_session( $voucher_value['code'], is_admin() );
    8085                    $order->add_meta_data( '_voucherify_rolled_back_voucher', $voucher_value, false );
    8186                    $order->delete_meta_data_by_mid( $redeemed_voucher->id );
  • voucherify/trunk/src/class-voucherify-remove-coupon-service.php

    r2491101 r2519268  
    3636         */
    3737        public function remove_coupon_from_voucherify_session( $coupon_code, $is_in_admin_panel = false ) {
     38            $order = null;
    3839            if ( $is_in_admin_panel ) {
    3940                $order       = vcrf_get_admin_order();
    4041                $session_key = empty( $order ) ? '' : $order->get_meta( '_voucherify_session_key', true );
    4142            } else {
    42                 $session_key = $this->order_placing_session->get_validation_session_key( $coupon_code );
     43                $session_key = $this->order_placing_session->get_validation_session_data( $coupon_code )['key'];
    4344            }
    4445            if ( ! empty( $session_key ) ) {
    45                 $this->voucherify_client_extension->release_session_lock( $coupon_code, $session_key );
     46                $result = $this->voucherify_client_extension->release_session_lock( $coupon_code, $session_key );
     47                if ( $is_in_admin_panel ) {
     48                    $order->delete_meta_data( '_voucherify_session_key' );
     49                    $order->save_meta_data();
     50                }
    4651            }
    47             $this->order_placing_session->clear();
     52            $this->order_placing_session->clear($coupon_code);
    4853        }
    4954    }
  • voucherify/trunk/src/class-voucherify-save-order-listener.php

    r2504530 r2519268  
    5959                $vcrf_current_order = $order;
    6060            }
    61             if ( ! in_array( $order->get_status(), [ 'processing', 'completed' ] ) ) {
     61
     62            $old_data = $order->get_data();
     63            $changes  = $order->get_changes();
     64
     65            if ( empty( $changes['status'] ) ) {
     66                return;
     67            }
     68
     69            $new_status = $changes['status'];
     70
     71            $old_status = null;
     72            if ( ! empty( $old_data['status'] ) ) {
     73                $old_status = $old_data['status'];
     74            }
     75
     76            if ( in_array( $new_status, [ 'cancelled' ] ) ) {
     77                $coupons = $this->get_applied_coupons();
     78                if ( ! empty( $coupons ) ) {
     79                    $this->remove_coupon_service->remove_coupon_from_voucherify_session( current( $coupons ), is_admin() );
     80                }
     81
     82                return;
     83            }
     84
     85            if ( ! in_array( $new_status, [ 'processing', 'completed' ] )
     86                 || in_array( $old_status, [ 'processing', 'completed' ] ) ) {
    6287                return;
    6388            }
     
    6994                return;
    7095            }
     96
    7197            $this->handle_new_order_save_by_customer( $order );
     98        }
     99
     100        /**
     101         * Callback function for hook just before the order is saved, after redemption. This callback may be called even for existing
     102         * orders.
     103         *
     104         * @param WC_Order $order
     105         *
     106         * @throws ClientException
     107         * @throws Exception
     108         */
     109        public function on_before_order_save_after_redemption( WC_Order $order ) {
     110            $session_key = $order->get_meta( '_voucherify_session_key', true );
     111            if ( ! empty( $session_key ) ) {
     112                return;
     113            }
     114            $codes = $this->get_applied_coupons();
     115            if ( empty( $codes ) ) {
     116                return;
     117            }
     118            $session_data = $this->order_placing_session->get_validation_session_data( current( $codes ) );
     119            if ( empty( $session_data['key'] ) ) {
     120                return;
     121            }
     122            $order->add_meta_data( '_voucherify_session_key', $session_data['key'], true );
     123            $order->add_meta_data( '_voucherify_session_ttl', $session_data['ttl'], true );
     124            $order->add_meta_data( '_voucherify_session_created_time', $session_data['created_time'], true );
    72125        }
    73126
  • voucherify/trunk/src/class-voucherify-validation-service.php

    r2504530 r2519268  
    229229        private function validate_voucher( $code ) {
    230230            $response = $this->order_placing_session->get_validation_response( $code );
    231             if ( empty( $response ) ) {
    232                 $context  = apply_filters( 'voucherify_validation_service_validation_context',
    233                     vcrf_get_customer_data() + vcrf_get_order_data() + $this->create_session_lock_data( $code ) );
    234                 $context  = $this->add_shipping_to_context( $context );
    235                 $context  = apply_filters( 'voucherify_validation_service_voucher_validation_context', $context );
    236                 $response = $this->validations->validate( $code, $context );
     231            if ( ! empty( $response ) ) {
     232                return $response;
     233            }
     234
     235            $context  = apply_filters( 'voucherify_validation_service_validation_context',
     236                vcrf_get_customer_data() + vcrf_get_order_data() + $this->create_session_lock_data( $code ) );
     237            $context  = $this->add_shipping_to_context( $context );
     238            $context  = apply_filters( 'voucherify_validation_service_voucher_validation_context', $context );
     239            $response = $this->validations->validate( $code, $context );
     240
     241            if ( isset( $response->valid ) && true === $response->valid ) {
    237242                $this->save_session_key( $code, $response );
    238243            }
     
    296301            }
    297302
    298             if ( $order->meta_exists( '_voucherify_session_key' ) ) {
    299                 $session_data['session']['key'] = $order->get_meta( '_voucherify_session_key', true );
     303            $session_key = $order->get_meta( '_voucherify_session_key', true );
     304            if ( ! empty( $session_key ) ) {
     305                $session_data['session']['key'] = $session_key;
    300306            }
    301307
     
    417423            //add coupon by admin
    418424            $order = vcrf_get_admin_order();
    419             if ( ! empty( $order )
    420                  && ! $order->meta_exists( '_voucherify_session_key' )
     425            if ( empty( $order ) ) {
     426                return;
     427            }
     428            $session_key = $order->get_meta( '_voucherify_session_key', true );
     429            if ( empty( $session_key )
    421430                 && isset( $response, $response->session, $response->session->key ) ) {
    422431                $order->add_meta_data( '_voucherify_session_key', $response->session->key, true );
     432                $order->add_meta_data( '_voucherify_session_ttl', $response->session->ttl, true );
     433                $order->add_meta_data( '_voucherify_session_created_time', time(), true );
     434                $order->save_meta_data();
    423435            }
    424436        }
  • voucherify/trunk/src/class-voucherify.php

    r2504530 r2519268  
    3030require_once "class-voucherify-remove-coupon-service.php";
    3131require_once "class-voucherify-voucher-amount-calculator.php";
     32require_once "class-voucherify-unlock-order-listener.php";
    3233
    3334if ( ! class_exists( 'Voucherify' ) ) {
     
    6263        /** @var Voucherify_Remove_Coupon_Service $remove_coupon_service */
    6364        private $remove_coupon_service;
     65        /** @var Voucherify_Unlock_Order_Listener $unlock_order_listener */
     66        private $unlock_order_listener;
    6467
    6568        /**
     
    7982            $this->save_order_listener      = new Voucherify_Save_Order_Listener( $this->validataion_service,
    8083                $this->redemption_service, $this->order_placing_session, $this->remove_coupon_service );
    81             $this->refund_order_listener    = new Voucherify_Refund_Order_Listener( $this->redemption_service );
     84            $this->refund_order_listener    = new Voucherify_Refund_Order_Listener( $this->redemption_service, $this->remove_coupon_service );
    8285            $this->promotion_service        = new Voucherify_Promotion_Service();
    8386            $this->all_promotions_fetcher   =
     
    8992            $this->messaging_modificator    = new Voucherify_Messaging_Modificator();
    9093            $this->form_handler             = new Voucherify_Form_Handler( $this->promotion_service );
     94            $this->unlock_order_listener    = new Voucherify_Unlock_Order_Listener( $this->remove_coupon_service );
    9195        }
    9296
     
    131135                    'on_before_order_save'
    132136                ], 10 );
     137                add_action( 'woocommerce_before_order_object_save', [
     138                    $this->save_order_listener,
     139                    'on_before_order_save_after_redemption'
     140                ], 20 );
    133141                add_filter( 'woocommerce_thankyou_order_received_text', [
    134142                    $this->save_order_listener,
     
    195203                    'add_partial_refund_rollback_checkbox'
    196204                ], 10, 1 );
     205
     206                add_action( 'woocommerce_add_to_cart', [ $this, 'on_add_to_cart' ] );
    197207
    198208                add_action( 'woocommerce_removed_coupon',
     
    210220                    $this,
    211221                    'on_update_cart_action_cart_updated'
     222                ], 10 );
     223
     224                add_action( 'wp_ajax_vcrf_unlock_order', [
     225                    $this->unlock_order_listener,
     226                    'unlock_order_ajax_handler'
     227                ], 10 );
     228
     229                add_action( 'add_meta_boxes', [
     230                    $this->unlock_order_listener,
     231                    'add_unlock_order_meta_box'
    212232                ], 10 );
    213233            }
     
    245265                    'on_before_order_save'
    246266                ], 10 );
     267
     268                remove_action( 'woocommerce_before_order_object_save', [
     269                    $this->save_order_listener,
     270                    'on_before_order_save_after_redemption'
     271                ], 20 );
     272
    247273                remove_filter( 'woocommerce_thankyou_order_received_text', [
    248274                    $this->save_order_listener,
     
    300326                ] );
    301327
     328                remove_action( 'woocommerce_add_to_cart', [ $this, 'on_add_to_cart' ] );
     329
    302330                remove_action( 'woocommerce_before_calculate_totals', [
    303331                    $this->order_placing_session,
     
    308336                    $this,
    309337                    'on_update_cart_action_cart_updated'
     338                ] );
     339
     340                remove_action( 'wp_ajax_vcrf_unlock_order', [
     341                    $this->unlock_order_listener,
     342                    'unlock_order_ajax_handler'
     343                ] );
     344
     345                remove_action( 'add_meta_boxes', [
     346                    $this->unlock_order_listener,
     347                    'add_unlock_order_meta_box'
    310348                ] );
    311349
     
    351389
    352390            return apply_filters( 'voucherify_remove_coupons_menu_items', $settings );
     391        }
     392
     393        public function on_add_to_cart() {
     394            if ( ! isset( WC()->cart ) || WC()->cart->is_empty() ) {
     395                return;
     396            }
     397
     398            $this->on_update_cart_action_cart_updated( true );
    353399        }
    354400
     
    358404                $this->validataion_service->remove_coupons();
    359405                $cart->remove_coupons();
     406            } else {
     407                $this->on_update_cart_action_cart_updated( true );
    360408            }
    361409        }
    362410
    363411        public function on_update_cart_action_cart_updated( $cart_updated ) {
     412            if ( ! isset( WC()->cart ) ) {
     413                return $cart_updated;
     414            }
     415
    364416            if ( $cart_updated ) {
    365                 $this->order_placing_session->clear();
     417                voucherify()->disable_coupons_validation();
     418                $current_coupon = current( WC()->cart->get_coupons() );
     419                voucherify()->enable_coupons_validation();
     420                $this->remove_coupon_service->remove_coupon_from_voucherify_session(
     421                    $current_coupon ? $current_coupon->get_code() : ''
     422                );
    366423            }
    367424
  • voucherify/trunk/voucherify.php

    r2517904 r2519268  
    88 * Plugin URI: https://wordpress.org/plugins/voucherify/
    99 * Description: Integrates Voucherify API with woocommerce replacing core coupons functionality
    10  * Version: 2.1.3
     10 * Version: 2.1.4
    1111 * Author: rspective
    1212 * Author URI: https://www.rspective.com/
Note: See TracChangeset for help on using the changeset viewer.