| @@ -9,11 +9,9 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace TUTOR; |
| 12 | 12 | |
| 13 | -use Exception; | |
| 14 | 13 | use Tutor\Models\WithdrawModel; |
| 15 | -use Tutor\Traits\JsonResponse; | |
| 16 | 14 | |
| 17 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | 16 | exit; |
| 19 | 17 | } |
| @@ -23,9 +21,8 @@ | ||
| 23 | 21 | * |
| 24 | 22 | * @since 1.0.0 |
| 25 | 23 | */ |
| 26 | 24 | class Withdraw { |
| 27 | - use JsonResponse; | |
| 28 | 25 | |
| 29 | 26 | /** |
| 30 | 27 | * Withdraw method |
| 31 | 28 | * |
| @@ -223,15 +220,13 @@ | ||
| 223 | 220 | wp_send_json_success( array( 'msg' => $msg ) ); |
| 224 | 221 | } |
| 225 | 222 | |
| 226 | 223 | /** |
| 227 | - * Handle withdraw request form submit. | |
| 224 | + * Handle withdraw request form submit | |
| 228 | 225 | * |
| 229 | 226 | * @since 1.0.0 |
| 230 | 227 | * |
| 231 | 228 | * @return void |
| 232 | - * | |
| 233 | - * @throws Exception If any validation fails. | |
| 234 | 229 | */ |
| 235 | 230 | public function tutor_make_an_withdraw() { |
| 236 | 231 | global $wpdb; |
| 237 | 232 | |
| @@ -236,120 +231,93 @@ | ||
| 236 | 231 | global $wpdb; |
| 237 | 232 | |
| 238 | 233 | tutor_utils()->checking_nonce(); |
| 239 | 234 | |
| 240 | - $user_id = get_current_user_id(); | |
| 241 | - if ( ! tutor_utils()->is_instructor( $user_id ) ) { | |
| 242 | - wp_send_json_error( array( 'msg' => tutor_utils()->error_message() ) ); | |
| 243 | - } | |
| 235 | + $user_id = get_current_user_id(); | |
| 236 | + $withdraw_amount = Input::post( 'tutor_withdraw_amount' ); | |
| 244 | 237 | |
| 245 | - $lock_name = 'tutor_withdraw_lock_' . $user_id; | |
| 246 | - $locked = $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, 10)', $lock_name ) ); | |
| 238 | + $earning_summary = WithdrawModel::get_withdraw_summary( $user_id ); | |
| 239 | + $min_withdraw = tutor_utils()->get_option( 'min_withdraw_amount' ); | |
| 247 | 240 | |
| 248 | - if ( 1 !== (int) $locked ) { | |
| 241 | + if ( ( $earning_summary->total_pending + $withdraw_amount ) > $earning_summary->available_for_withdraw ) { | |
| 249 | 242 | wp_send_json_error( |
| 250 | 243 | array( |
| 251 | - 'msg' => __( 'Another withdrawal request is in progress. Please try again.', 'tutor' ), | |
| 244 | + 'msg' => wp_sprintf( | |
| 245 | + /* translators: 1: total pending withdraw request 2: available for withdraw */ | |
| 246 | + __( "You have total %1\$s pending withdraw request. You can't make more than %2\$s withdraw request at a time", 'tutor' ), | |
| 247 | + $earning_summary->total_pending, | |
| 248 | + $earning_summary->available_for_withdraw | |
| 249 | + ), | |
| 252 | 250 | ) |
| 253 | 251 | ); |
| 254 | 252 | } |
| 255 | 253 | |
| 256 | - try { | |
| 257 | - $withdraw_amount = (float) Input::post( 'amount' ); | |
| 258 | - $earning_summary = WithdrawModel::get_withdraw_summary( $user_id ); | |
| 259 | - $min_withdraw = (float) tutor_utils()->get_option( 'min_withdraw_amount' ); | |
| 254 | + $saved_withdraw_account = WithdrawModel::get_user_withdraw_method(); | |
| 255 | + $formatted_min_withdraw_amount = tutor_utils()->tutor_price( $min_withdraw ); | |
| 260 | 256 | |
| 261 | - if ( ( $earning_summary->total_pending + $withdraw_amount ) > $earning_summary->available_for_withdraw ) { | |
| 262 | - throw new Exception( | |
| 263 | - wp_sprintf( | |
| 264 | - /* translators: 1: total pending withdraw request 2: available for withdraw */ | |
| 265 | - __( "You have total %1\$s pending withdraw request. You can't make more than %2\$s withdraw request at a time", 'tutor' ), | |
| 266 | - $earning_summary->total_pending, | |
| 267 | - $earning_summary->available_for_withdraw | |
| 268 | - ) | |
| 269 | - ); | |
| 270 | - } | |
| 257 | + if ( ! tutor_utils()->count( $saved_withdraw_account ) ) { | |
| 258 | + $no_withdraw_method = apply_filters( 'tutor_no_withdraw_method_msg', __( 'Please save withdraw method ', 'tutor' ) ); | |
| 259 | + wp_send_json_error( array( 'msg' => $no_withdraw_method ) ); | |
| 260 | + } | |
| 271 | 261 | |
| 272 | - $saved_withdraw_account = WithdrawModel::get_user_withdraw_method(); | |
| 273 | - $formatted_min_withdraw_amount = tutor_utils()->tutor_price( $min_withdraw ); | |
| 262 | + if ( ( ! is_numeric( $withdraw_amount ) && ! is_float( $withdraw_amount ) ) || $withdraw_amount < $min_withdraw ) { | |
| 263 | + /* translators: 1: strong tag start 2: min withdrawal amount 3: strong tag end */ | |
| 264 | + $required_min_withdraw = apply_filters( 'tutor_required_min_amount_msg', sprintf( __( 'Minimum withdrawal amount is %1$s %2$s %3$s ', 'tutor' ), '<strong>', $formatted_min_withdraw_amount, '</strong>' ) ); | |
| 265 | + wp_send_json_error( array( 'msg' => $required_min_withdraw ) ); | |
| 266 | + } | |
| 274 | 267 | |
| 275 | - if ( ! tutor_utils()->count( $saved_withdraw_account ) ) { | |
| 276 | - $no_withdraw_method = apply_filters( 'tutor_no_withdraw_method_msg', __( 'Please save withdraw method ', 'tutor' ) ); | |
| 277 | - throw new Exception( $no_withdraw_method ); | |
| 278 | - } | |
| 268 | + if ( $earning_summary->available_for_withdraw < $withdraw_amount ) { | |
| 269 | + $insufficient_balence = apply_filters( 'tutor_withdraw_insufficient_balance_msg', __( 'Insufficient balance.', 'tutor' ) ); | |
| 279 | 270 | |
| 280 | - if ( ( ! is_numeric( $withdraw_amount ) && ! is_float( $withdraw_amount ) ) || $withdraw_amount < $min_withdraw ) { | |
| 281 | - /* translators: 1: strong tag start 2: min withdrawal amount 3: strong tag end */ | |
| 282 | - $required_min_withdraw = apply_filters( 'tutor_required_min_amount_msg', sprintf( __( 'Minimum withdrawal amount is %1$s %2$s %3$s ', 'tutor' ), '<strong>', $formatted_min_withdraw_amount, '</strong>' ) ); | |
| 283 | - throw new Exception( $required_min_withdraw ); | |
| 284 | - } | |
| 271 | + wp_send_json_error( array( 'msg' => $insufficient_balence ) ); | |
| 272 | + } | |
| 285 | 273 | |
| 286 | - if ( $earning_summary->available_for_withdraw < $withdraw_amount ) { | |
| 287 | - $insufficient_balence = apply_filters( 'tutor_withdraw_insufficient_balance_msg', __( 'Insufficient balance.', 'tutor' ) ); | |
| 288 | - throw new Exception( $insufficient_balence ); | |
| 289 | - } | |
| 274 | + $date = gmdate( 'Y-m-d H:i:s', tutor_time() ); | |
| 290 | 275 | |
| 291 | - $date = gmdate( 'Y-m-d H:i:s', tutor_time() ); | |
| 276 | + $withdraw_data = apply_filters( | |
| 277 | + 'tutor_pre_withdraw_data', | |
| 278 | + array( | |
| 279 | + 'user_id' => $user_id, | |
| 280 | + 'amount' => $withdraw_amount, | |
| 281 | + 'method_data' => maybe_serialize( $saved_withdraw_account ), | |
| 282 | + 'status' => 'pending', | |
| 283 | + 'created_at' => $date, | |
| 284 | + ) | |
| 285 | + ); | |
| 292 | 286 | |
| 293 | - $withdraw_data = apply_filters( | |
| 294 | - 'tutor_pre_withdraw_data', | |
| 295 | - array( | |
| 296 | - 'user_id' => $user_id, | |
| 297 | - 'amount' => $withdraw_amount, | |
| 298 | - 'method_data' => maybe_serialize( $saved_withdraw_account ), | |
| 299 | - 'status' => 'pending', | |
| 300 | - 'created_at' => $date, | |
| 301 | - ) | |
| 302 | - ); | |
| 287 | + $date = gmdate( 'Y-m-d H:i:s', tutor_time() ); | |
| 303 | 288 | |
| 304 | - do_action( 'tutor_insert_withdraw_before', $withdraw_data ); | |
| 289 | + $withdraw_data = apply_filters( | |
| 290 | + 'tutor_pre_withdraw_data', | |
| 291 | + array( | |
| 292 | + 'user_id' => $user_id, | |
| 293 | + 'amount' => $withdraw_amount, | |
| 294 | + 'method_data' => maybe_serialize( $saved_withdraw_account ), | |
| 295 | + 'status' => 'pending', | |
| 296 | + 'created_at' => $date, | |
| 297 | + ) | |
| 298 | + ); | |
| 305 | 299 | |
| 306 | - $inserted = $wpdb->insert( $wpdb->prefix . 'tutor_withdraws', $withdraw_data ); | |
| 307 | - if ( false === $inserted ) { | |
| 308 | - throw new Exception( __( 'Unable to process withdrawal request. Please try again.', 'tutor' ) ); | |
| 309 | - } | |
| 300 | + do_action( 'tutor_insert_withdraw_before', $withdraw_data ); | |
| 310 | 301 | |
| 311 | - $withdraw_id = $wpdb->insert_id; | |
| 302 | + $wpdb->insert( $wpdb->prefix . 'tutor_withdraws', $withdraw_data ); | |
| 303 | + $withdraw_id = $wpdb->insert_id; | |
| 312 | 304 | |
| 313 | - do_action( 'tutor_insert_withdraw_after', $withdraw_id, $withdraw_data ); | |
| 305 | + do_action( 'tutor_insert_withdraw_after', $withdraw_id, $withdraw_data ); | |
| 314 | 306 | |
| 315 | - /** | |
| 316 | - * Getting earning and balance data again | |
| 317 | - */ | |
| 318 | - $earning = WithdrawModel::get_withdraw_summary( $user_id ); | |
| 319 | - $new_available_balance = tutor_utils()->tutor_price( $earning->available_for_withdraw ); | |
| 307 | + /** | |
| 308 | + * Getting earning and balance data again | |
| 309 | + */ | |
| 310 | + $earning = WithdrawModel::get_withdraw_summary( $user_id ); | |
| 311 | + $new_available_balance = tutor_utils()->tutor_price( $earning->available_for_withdraw ); | |
| 320 | 312 | |
| 321 | - do_action( 'tutor_withdraw_after' ); | |
| 313 | + do_action( 'tutor_withdraw_after' ); | |
| 322 | 314 | |
| 323 | - $response = array( | |
| 324 | - 'msg' => apply_filters( 'tutor_withdraw_successful_msg', __( 'Withdrawal Request Sent!', 'tutor' ) ), | |
| 315 | + $withdraw_successfull_msg = apply_filters( 'tutor_withdraw_successful_msg', __( 'Withdrawal Request Sent!', 'tutor' ) ); | |
| 316 | + wp_send_json_success( | |
| 317 | + array( | |
| 318 | + 'msg' => $withdraw_successfull_msg, | |
| 325 | 319 | 'available_balance' => $new_available_balance, |
| 326 | - ); | |
| 327 | - | |
| 328 | - } catch ( Exception $e ) { | |
| 329 | - | |
| 330 | - $response = array( | |
| 331 | - 'error' => true, | |
| 332 | - 'msg' => $e->getMessage(), | |
| 333 | - ); | |
| 334 | - | |
| 335 | - } finally { | |
| 336 | - | |
| 337 | - $wpdb->query( | |
| 338 | - $wpdb->prepare( | |
| 339 | - 'SELECT RELEASE_LOCK(%s)', | |
| 340 | - $lock_name | |
| 341 | - ) | |
| 342 | - ); | |
| 343 | - } | |
| 344 | - | |
| 345 | - if ( ! empty( $response['error'] ) ) { | |
| 346 | - wp_send_json_error( | |
| 347 | - array( | |
| 348 | - 'msg' => $response['msg'], | |
| 349 | - ) | |
| 350 | - ); | |
| 351 | - } | |
| 352 | - | |
| 353 | - wp_send_json_success( $response ); | |
| 320 | + ) | |
| 321 | + ); | |
| 354 | 322 | } |
| 355 | 323 | } |