Withdrawal
Source
File: classes/Withdrawal.php
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 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
- __construct
- available_withdraw_methods
- balance_withdrawal
- get_options
- instance
- save_withdraw_account — Save Withdraw Method Data
- withdraw_admin_options
- withdraw_methods
- withdraw_option_save — Save Withdraw method