Changeset 2519268
- Timestamp:
- 04/21/2021 04:52:19 PM (5 years ago)
- Location:
- voucherify/trunk
- Files:
-
- 1 added
- 10 edited
-
partials/admin-order-ajax-handler.php (added)
-
readme.txt (modified) (2 diffs)
-
src/class-voucherify-client-extension.php (modified) (1 diff)
-
src/class-voucherify-order-placing-session.php (modified) (7 diffs)
-
src/class-voucherify-redemption-service.php (modified) (1 diff)
-
src/class-voucherify-refund-order-listener.php (modified) (3 diffs)
-
src/class-voucherify-remove-coupon-service.php (modified) (1 diff)
-
src/class-voucherify-save-order-listener.php (modified) (2 diffs)
-
src/class-voucherify-validation-service.php (modified) (3 diffs)
-
src/class-voucherify.php (modified) (12 diffs)
-
voucherify.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
voucherify/trunk/readme.txt
r2517904 r2519268 7 7 WC tested up to: 4.3.1 8 8 WC requires at least: 3.0.0 9 Stable tag: 2.1. 39 Stable tag: 2.1.4 10 10 11 11 Integrates Voucherify API with woocommerce … … 50 50 == Changelog == 51 51 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 52 56 = 2.1.3 - 2021-04-19 = 53 57 * Fix: Supporting PayPal IPN -
voucherify/trunk/src/class-voucherify-client-extension.php
r2491101 r2519268 23 23 24 24 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 ); 26 26 } 27 27 } -
voucherify/trunk/src/class-voucherify-order-placing-session.php
r2504530 r2519268 9 9 10 10 private $fake_session_for_rest_api = [ 11 'voucherify_valid_vouchers' => [],11 'voucherify_valid_vouchers' => [], 12 12 'voucherify_removed_coupons' => [] 13 13 ]; … … 30 30 $validations = []; 31 31 } 32 $validations[ $code ] = $response; 32 $response_array = (array) $response; 33 $response_array['created_time'] = time(); 34 $validations[ $code ] = (object) $response_array; 33 35 WC()->session->set( 'voucherify_valid_vouchers', $validations ); 34 36 } … … 56 58 */ 57 59 public function get_all_validation_responses() { 58 if ( vcrf_is_rest_request() ) {60 if ( vcrf_is_rest_request() || is_admin() ) { 59 61 return $this->fake_session_for_rest_api['voucherify_valid_vouchers']; 60 62 } … … 72 74 */ 73 75 public function add_removed_coupon( $code ) { 74 if ( vcrf_is_rest_request() ) {76 if ( vcrf_is_rest_request() || is_admin() ) { 75 77 $this->fake_session_for_rest_api['voucherify_removed_coupons'][] = $code; 76 78 … … 90 92 * @param string $code 91 93 * 92 * @return string session_key94 * @return array session_data 93 95 */ 94 public function get_validation_session_ key( $code ) {96 public function get_validation_session_data( $code ) { 95 97 $response = $this->get_validation_response( $code ); 96 98 97 99 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 ]; 99 105 } 100 106 … … 105 111 */ 106 112 public function get_removed_coupons() { 107 if ( vcrf_is_rest_request() ) {113 if ( vcrf_is_rest_request() || is_admin() ) { 108 114 return $this->fake_session_for_rest_api['voucherify_removed_coupons']; 109 115 } … … 140 146 */ 141 147 public function clear() { 142 if ( vcrf_is_rest_request() ) {148 if ( vcrf_is_rest_request() || is_admin() ) { 143 149 $this->fake_session_for_rest_api['voucherify_removed_coupons'] = []; 144 150 $this->fake_session_for_rest_api['voucherify_valid_vouchers'] = []; -
voucherify/trunk/src/class-voucherify-redemption-service.php
r2491101 r2519268 158 158 $order = vcrf_get_admin_order(); 159 159 } 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) ) { 161 162 $session_key = $order->get_meta( '_voucherify_session_key', true ); 162 163 } 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']; 164 166 $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 ); 165 169 } 166 170 if ( empty( $session_key ) ) { -
voucherify/trunk/src/class-voucherify-refund-order-listener.php
r2491101 r2519268 9 9 /** @var Voucherify_Redemption_Service $redemption_service */ 10 10 private $redemption_service; 11 /** @var Voucherify_Remove_Coupon_Service $remove_coupon_service */ 12 private $remove_coupon_service; 11 13 12 14 /** … … 14 16 */ 15 17 public function __construct( 16 Voucherify_Redemption_Service $redemption_service 18 Voucherify_Redemption_Service $redemption_service, 19 Voucherify_Remove_Coupon_Service $remove_coupon_service 17 20 ) { 18 21 $this->redemption_service = $redemption_service; 22 $this->remove_coupon_service = $remove_coupon_service; 19 23 } 20 24 … … 78 82 $voucher_value = $redeemed_voucher->value; 79 83 $this->redemption_service->rollback_redemption( $voucher_value['redemption_id'] ); 84 $this->remove_coupon_service->remove_coupon_from_voucherify_session( $voucher_value['code'], is_admin() ); 80 85 $order->add_meta_data( '_voucherify_rolled_back_voucher', $voucher_value, false ); 81 86 $order->delete_meta_data_by_mid( $redeemed_voucher->id ); -
voucherify/trunk/src/class-voucherify-remove-coupon-service.php
r2491101 r2519268 36 36 */ 37 37 public function remove_coupon_from_voucherify_session( $coupon_code, $is_in_admin_panel = false ) { 38 $order = null; 38 39 if ( $is_in_admin_panel ) { 39 40 $order = vcrf_get_admin_order(); 40 41 $session_key = empty( $order ) ? '' : $order->get_meta( '_voucherify_session_key', true ); 41 42 } 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']; 43 44 } 44 45 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 } 46 51 } 47 $this->order_placing_session->clear( );52 $this->order_placing_session->clear($coupon_code); 48 53 } 49 54 } -
voucherify/trunk/src/class-voucherify-save-order-listener.php
r2504530 r2519268 59 59 $vcrf_current_order = $order; 60 60 } 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' ] ) ) { 62 87 return; 63 88 } … … 69 94 return; 70 95 } 96 71 97 $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 ); 72 125 } 73 126 -
voucherify/trunk/src/class-voucherify-validation-service.php
r2504530 r2519268 229 229 private function validate_voucher( $code ) { 230 230 $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 ) { 237 242 $this->save_session_key( $code, $response ); 238 243 } … … 296 301 } 297 302 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; 300 306 } 301 307 … … 417 423 //add coupon by admin 418 424 $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 ) 421 430 && isset( $response, $response->session, $response->session->key ) ) { 422 431 $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(); 423 435 } 424 436 } -
voucherify/trunk/src/class-voucherify.php
r2504530 r2519268 30 30 require_once "class-voucherify-remove-coupon-service.php"; 31 31 require_once "class-voucherify-voucher-amount-calculator.php"; 32 require_once "class-voucherify-unlock-order-listener.php"; 32 33 33 34 if ( ! class_exists( 'Voucherify' ) ) { … … 62 63 /** @var Voucherify_Remove_Coupon_Service $remove_coupon_service */ 63 64 private $remove_coupon_service; 65 /** @var Voucherify_Unlock_Order_Listener $unlock_order_listener */ 66 private $unlock_order_listener; 64 67 65 68 /** … … 79 82 $this->save_order_listener = new Voucherify_Save_Order_Listener( $this->validataion_service, 80 83 $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 ); 82 85 $this->promotion_service = new Voucherify_Promotion_Service(); 83 86 $this->all_promotions_fetcher = … … 89 92 $this->messaging_modificator = new Voucherify_Messaging_Modificator(); 90 93 $this->form_handler = new Voucherify_Form_Handler( $this->promotion_service ); 94 $this->unlock_order_listener = new Voucherify_Unlock_Order_Listener( $this->remove_coupon_service ); 91 95 } 92 96 … … 131 135 'on_before_order_save' 132 136 ], 10 ); 137 add_action( 'woocommerce_before_order_object_save', [ 138 $this->save_order_listener, 139 'on_before_order_save_after_redemption' 140 ], 20 ); 133 141 add_filter( 'woocommerce_thankyou_order_received_text', [ 134 142 $this->save_order_listener, … … 195 203 'add_partial_refund_rollback_checkbox' 196 204 ], 10, 1 ); 205 206 add_action( 'woocommerce_add_to_cart', [ $this, 'on_add_to_cart' ] ); 197 207 198 208 add_action( 'woocommerce_removed_coupon', … … 210 220 $this, 211 221 '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' 212 232 ], 10 ); 213 233 } … … 245 265 'on_before_order_save' 246 266 ], 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 247 273 remove_filter( 'woocommerce_thankyou_order_received_text', [ 248 274 $this->save_order_listener, … … 300 326 ] ); 301 327 328 remove_action( 'woocommerce_add_to_cart', [ $this, 'on_add_to_cart' ] ); 329 302 330 remove_action( 'woocommerce_before_calculate_totals', [ 303 331 $this->order_placing_session, … … 308 336 $this, 309 337 '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' 310 348 ] ); 311 349 … … 351 389 352 390 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 ); 353 399 } 354 400 … … 358 404 $this->validataion_service->remove_coupons(); 359 405 $cart->remove_coupons(); 406 } else { 407 $this->on_update_cart_action_cart_updated( true ); 360 408 } 361 409 } 362 410 363 411 public function on_update_cart_action_cart_updated( $cart_updated ) { 412 if ( ! isset( WC()->cart ) ) { 413 return $cart_updated; 414 } 415 364 416 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 ); 366 423 } 367 424 -
voucherify/trunk/voucherify.php
r2517904 r2519268 8 8 * Plugin URI: https://wordpress.org/plugins/voucherify/ 9 9 * Description: Integrates Voucherify API with woocommerce replacing core coupons functionality 10 * Version: 2.1. 310 * Version: 2.1.4 11 11 * Author: rspective 12 12 * Author URI: https://www.rspective.com/
Note: See TracChangeset
for help on using the changeset viewer.