Withdrawal::balance_withdrawal()


Source

File: classes/Withdrawal.php

	public function balance_withdrawal() {
		global $wpdb;

		//Checking nonce
		dozent_checking_nonce();

		do_action( 'dozent_withdraw_before' );

		$user_id = get_current_user_id();

		$withdraw_amount = dozent_input_text( 'withdrawal_amount' );

		$earning_sum  = dozent_get_earning_sum( $user_id );
		$min_withdraw = dozent_get_option( 'min_withdraw_amount' );

		$saved_withdraw_account = dozent_get_withdraw_method_by_user();
		$formatted_balance      = dozent_price( $earning_sum->balance );
		$formatted_min_amount   = dozent_price( $min_withdraw );

		if ( ! dozent_count( $saved_withdraw_account ) ) {
			$no_withdraw_method_msg = apply_filters( 'dozent_no_withdraw_method_msg',
				__( 'Set a withdraw method in order to process withdrawal', 'dozent' ) );
			dozent_set_flash_message( $no_withdraw_method_msg, 'warning' );
			dozent_redirect();
		}

		if ( $earning_sum->balance < $withdraw_amount ) {
			$insufficient_msg = apply_filters( 'dozent_withdraw_insufficient_balance_msg', sprintf( __( 'Insufficient balance to withdraw, your balance is %s %s %s ', 'dozent' ), '<strong>', $formatted_balance, '</strong>' ) );

			dozent_set_flash_message( $insufficient_msg, 'warning' );
			dozent_redirect();
		}

		if ( $withdraw_amount < $min_withdraw ) {
			$required_min_msg = apply_filters( 'dozent_required_min_amount_msg', sprintf( __( 'Minimum withdraw amount is %s %s %s ', 'dozent' ), '<strong>', $formatted_min_amount, '</strong>' ) );

			dozent_set_flash_message( $required_min_msg, 'warning' );
			dozent_redirect();
		}


		$date = date( "Y-m-d H:i:s", dozent_time() );

		$withdraw_data = apply_filters( 'dozent_pre_withdraw_data', array(
			'user_id'     => $user_id,
			'amount'      => $withdraw_amount,
			'method_data' => maybe_serialize( $saved_withdraw_account ),
			'status'      => 'pending',
			'created_at'  => $date,
		) );

		do_action( 'dozent_insert_withdraw_before', $withdraw_data );

		$wpdb->insert( $wpdb->prefix . "dozent_withdrawal", $withdraw_data );
		$withdraw_id = $wpdb->insert_id;

		if ( $withdraw_id ) {
			do_action( 'dozent_insert_withdraw_after', $withdraw_id, $withdraw_data );


			/**
			 * Withdrawal has been processed, redirecting back
			 */

			$withdrawal_msg = apply_filters( 'dozent_withdrawal_successful_msg',
				__( "We've received your withdrawal request, currently it's on under review", 'dozent' ) );
			dozent_set_flash_message( $withdrawal_msg );
			dozent_redirect();
		} else {

			//Something wrong

			$withdrawal_msg = apply_filters( 'dozent_withdrawal_failed_msg',
				__( "Something went wrong, please try again later", 'dozent' ) );
			dozent_set_flash_message( $withdrawal_msg, 'warning' );
			dozent_redirect();
		}

	}