Withdrawal


Source

File: classes/Withdrawal.php

class Withdrawal {

	public $available_withdraw_methods;
	public $get_options;
	public $withdraw_methods;


	public static function instance() {
		static $instance = null;

		if ( null === $instance ) {
			$instance = new self();
		}

		return $instance;
	}

	public function __construct() {
		$this->get_options                = $this->get_options();
		$this->withdraw_methods           = $this->withdraw_methods();
		$this->available_withdraw_methods = $this->available_withdraw_methods();

		add_action( 'dozent/option/field_group/after/dozent_withdraw/withdraw_methods',
			[ $this, 'withdraw_admin_options' ] );
		add_action( 'dozent_option_save_after', [ $this, 'withdraw_option_save' ] );

		add_dozent_action( 'save_withdraw_account', [ $this, 'save_withdraw_account' ] );
		add_dozent_action( 'balance_withdrawal', [ $this, 'balance_withdrawal' ] );
	}

	public function withdraw_methods() {
		$methods = [

			'bank_transfer_withdraw' => [

				'method_name'       => __( 'Bank Transfer', 'dozent' ),
				'desc'              => __( 'Get your payment directly into your bank account', 'dozent' ),
				'admin_form_fields' => [
					'instruction' => [
						'type'  => 'textarea',
						'label' => __( 'Instruction', 'dozent' ),
						'desc'  => __( 'Write instruction for the instructor to fill bank information', 'dozent' ),
					],
				],

				'form_fields' => [
					'account_name' => [
						'type'  => 'text',
						'label' => __( 'Account Name', 'dozent' ),
					],

					'account_number' => [
						'type'  => 'text',
						'label' => __( 'Account Number', 'dozent' ),
					],

					'bank_name' => [
						'type'  => 'text',
						'label' => __( 'Bank Name', 'dozent' ),
					],
					'iban'      => [
						'type'  => 'text',
						'label' => __( 'IBAN', 'dozent' ),
					],
					'swift'     => [
						'type'  => 'text',
						'label' => __( 'BIC / SWIFT', 'dozent' ),
					],
				],
			],

			'echeck_withdraw' => [
				'method_name' => __( 'E-Check', 'dozent' ),
				'form_fields' => [
					'physical_address' => [
						'type'  => 'textarea',
						'label' => __( 'Your Physical Address', 'dozent' ),
						'desc'  => __( 'We will send you an E-Check to this address directly.', 'dozent' ),
					],
				],
			],

			'paypal_withdraw' => [
				'method_name' => __( 'PayPal', 'dozent' ),
				'form_fields' => [
					'paypal_email' => [
						'type'  => 'email',
						'label' => __( 'PayPal E-Mail Address', 'dozent' ),
						'desc'  => __( 'Write your paypal email address to get payout directly to your paypal account',
							'dozent' ),
					],
				],
			],
		];

		$withdraw_methods = apply_filters( 'dozent_withdraw_methods', $methods );

		return $withdraw_methods;
	}

	/**
	 * @return mixed|array
	 *
	 * Return only enabled methods
	 */
	public function available_withdraw_methods() {
		$withdraw_options = $this->get_options();
		$methods          = $this->withdraw_methods();

		foreach ( $methods as $method_id => $method ) {
			$is_enable = (bool) dozent_array_get( $method_id . ".enabled", $withdraw_options );

			if ( ! $is_enable ) {
				unset( $methods[ $method_id ] );
			}
		}

		return $methods;
	}

	public function get_options() {
		return (array) maybe_unserialize( get_option( 'dozent_withdraw_options' ) );
	}

	public function withdraw_admin_options() {
		include DOZENT_ABSPATH . 'views/options/withdraw/withdraw_admin_options_generator.php';
	}

	/**
	 * Save Withdraw method
	 *
	 * @since v.1.0.0
	 */

	public function withdraw_option_save() {
		do_action( 'dozent_withdraw_options_save_before' );

		$option = (array) dozent_input_array_field( 'dozent_withdraw_options' );
		$option = apply_filters( 'dozent_withdraw_options_input', $option );
		update_option( 'dozent_withdraw_options', $option );

		do_action( 'dozent_withdraw_options_save_after' );
	}

	/**
	 * Save Withdraw Method Data
	 *
	 * @since v.1.0.0
	 */

	public function save_withdraw_account() {
		//Checking nonce
		dozent_checking_nonce();

		$user_id = get_current_user_id();

		$method = dozent_input_text( 'dozent_selected_withdraw_method' );
		if ( ! $method ) {
			dozent_redirect();
		}

		$method_data               = dozent_input_array_field( "withdraw_method_field." . $method );
		$available_withdraw_method = dozent_withdrawal_methods();

		if ( dozent_count( $method_data ) ) {
			$saved_data                         = [];
			$saved_data['withdraw_method_key']  = $method;
			$saved_data['withdraw_method_name'] = dozent_array_get( $method . ".method_name", $available_withdraw_method );

			foreach ( $method_data as $input_name => $value ) {
				$saved_data[ $input_name ]['value'] = sanitize_text_field( $value );
				$saved_data[ $input_name ]['label'] = dozent_array_get( $method . ".form_fields.{$input_name}.label",
					$available_withdraw_method );
			}

			update_user_meta( $user_id, '_dozent_withdraw_method_data', $saved_data );
		}

		dozent_set_flash_message( __( 'Withdraw preference has been saved', 'dozent' ) );
		dozent_redirect();
	}

	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();
		}

	}


}

Methods