V. Process Withdraw Orders in Admin

(Back end: Admin -> Withdrawal  -> Pending)

/**
* Protected constructor to prevent creating a new instance of the
 * *Singleton* via the `new` operator from outside of this class.
*/
protected function __construct() {
	add_action( 'wpj_admin_orders_after_tfoot_buttons', array( $this, 'display_process_payment_request_button' ) );


	if( isset( $_POST['processSamplePayRequest'] ) ) {
		add_action( 'wpj_admin_orders_before_content', array( $this, 'gateway_process_payment_request_action' ), 11 );
	}


	add_filter( 'wpj_withdrawals_gateways_filter', function( $gateways ) { array_push( $gateways, 'sample' ); return $gateways; }, 10, 1 );
	add_filter( 'wpj_enabled_withdrawals_gateways_filter', function( $gateways ) { array_push( $gateways, 'wpjobster_enable_sample_withdraw_automatic' ); return $gateways; }, 10, 1 );
}


/*
 * Display Process Payment Request Button in Admin withdrawal orders
 * This function is hooked to 'wpj_admin_orders_after_tfoot_buttons' action, which is used to display Process Payment Request Button in Admin withdrawal orders.
 */
public function display_process_payment_request_button( $payment_type ) {
	$wpjobster_sample_withdrawal_enable = get_option( 'wpjobster_enable_sample_withdraw_automatic' );
	if ( $payment_type == 'withdrawal' && WPJ_Form::get( 'status', '' ) == 'pending' && $wpjobster_sample_withdrawal_enable == 'yes' ) { ?>


		<input class="button-secondary" type="submit" value="<?php echo __( 'Process Sample Requests', 'wpjobster-sample' ); ?>" name="processSamplePayRequest" id="processSamplePayRequest" />


		<script>
			jQuery( document ).ready( function( $ ) {


				$( 'a.mark-order-completed' ).on( 'click', function( e ) {


					if ( $( this ).hasClass( 'sample-automatic-withdrawal' ) ) {
						e.preventDefault();


						$( this ).parents( 'tr' ).find( 'input[type="checkbox"]' ).prop( "checked", true );


						$( '#processSamplePayRequest' ).trigger( 'click' );
					}
				});


			});
		</script>


	<?php }
}


/*
 * Process Withdrawal Orders payment request action and set the withdrawal order as completed
 * This function is hooked to 'wpj_admin_orders_before_content' action, which is used to process Withdrawal Orders payment request action and set the withdrawal order as completed.
 */
public function gateway_process_payment_request_action() {
	if ( ! empty( $_POST['processSamplePayRequest'] ) ) {
		$wpjobster_sample_client_id = get_option( 'wpjobster_sample_client_id' );
		$wpjobster_sample_secret_key = get_option( 'wpjobster_sample_secret_key' );


		if ( $wpjobster_sample_client_id != '' && $wpjobster_sample_secret_key != '' ) {


			global $wpdb;
			$tm       = current_time( 'timestamp', 1 );
			$ids      = $_POST['requests'];
			$currency = wpjobster_get_currency_classic();


			$payee_ids = array();
			$mounts = array();
			$sample_payout_requests_info = array();


			foreach ( $ids as $id ) {
				$s   = "SELECT * FROM {$wpdb->prefix}job_withdraw WHERE id = '{$id}' AND methods LIKE '%Sample%' ";
				$row = $wpdb->get_results( $s );
				$row = $row[0];


				if ( ! empty($row) && $row->done == 0 ) {


					$sample_payee_id = !empty( user( $row->uid, 'sample_automatic_payee_id' ) ) ? user( $row->uid, 'sample_automatic_payee_id' ) : '';
					$payee_ids[] = $sample_payee_id;
					$mounts[] = $row->amount;
					$sample_payout_requests_info[$id]['payee_id'] = $sample_payee_id;
					$sample_payout_requests_info[$id]['amount']   = $row->amount;
					$sample_payout_requests_info[$id]['userid']   = $row->uid;
					$sample_payout_requests_info[$id]['uniqueid'] = $id;


				}
			}


			if ( ! empty( $payee_ids ) ) {


				foreach( $sample_payout_requests_info as $sample_payout_info ) {


					$fund_transfer_status = $this->fund_transfer( $sample_payout_info['payee_id'], $sample_payout_info['amount'], $sample_payout_info['uniqueid'] );


					if( isset( $fund_transfer_status ) && $fund_transfer_status != '' ) {
						$output_arr[$sample_payout_info['uniqueid']] = $fund_transfer_status;
					} else {
						$output_arr[$sample_payout_info['uniqueid']] = "INCOMPLETED";
					}


				}


				foreach ( $output_arr as $item_id => $output_msg ) {
					if ( $output_msg == "COMPLETED" ) {
						$id  = $item_id;
						$s   = "SELECT * FROM {$wpdb->prefix}job_withdraw WHERE id = '{$id}' AND methods LIKE '%Sample%' ";
						$row = $wpdb->get_results( $s );
						$row = $row[0];


						if ( $row->done == 0 ) {
							echo '
								<div class="notice notice-success is-dismissible">
									<p>' . sprintf( __( 'Payment completed for %s!', 'wpjobster-sample' ), $row->payeremail ) . '</p>
								</div>
							';


							$wpdb->query( "UPDATE {$wpdb->prefix}job_withdraw SET done='1', datedone='{$tm}' WHERE id='{$id}' AND methods LIKE '%Sample%' " );


							wpjobster_send_email_allinone_translated( 'withdraw_compl', $row->uid, false, false, false, $row->methods, wpjobster_get_show_price_classic( $row->amount ) );


							$details = $row->methods . ': ' . $row->payeremail;
							$reason = __( 'Withdrawal to', 'wpjobster-sample' ) . ' ' . $details;
							wpjobster_add_history_log( '0', $reason, $row->amount, $row->uid, '', '', 9, $details );
						}
					} else {
						echo '
							<div class="notice notice-error is-dismissible">
								<p>' . __( sprintf( "The order %s could not be processed because the seller do not have sample payee id", $item_id ), 'wpjobster-sample' ) . '</p>
							</div>
						';
					}
				}


			} else {
				echo '
					<div class="notice notice-error is-dismissible">
						<p>' . __( 'No Sample order selected!', 'wpjobster-sample' ) . '</p>
					</div>
					';
			}


		} else {
			echo '
					<div class="notice notice-error is-dismissible">
						<p>' . __( 'Sample client id, secret key values are blank!', 'wpjobster-sample' ) . '</p>
					</div>
				';
		}
	}
}


/*
 * Add API Code for processing automated payments and from gateway response return transaction status (e.g. success, fail)
 * This function is called in 'gateway_process_payment_request_action' function, which is used to add gateway API Code for processing automated payments and from gateway response return transaction status (e.g. success, fail).
 */
public function fund_transfer( $payee_id, $amount, $unique_id ) {
	//Put API code here for processing payout, fund transfer


	//From payout response get transcation status (success, failed) and Return status


	if ( $payee_id != '' ) {
		return 'COMPLETED';
	} else {
		return 'INCOMPLETED';
	}
}
<br>