PluginProbe
Tutor LMS – eLearning and online course solution / 2.0.1
Tutor LMS – eLearning and online course solution v2.0.1
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
tutor / classes / Withdraw.php

Withdraw.php in Tutor LMS – eLearning and online course solution 2.0.1, at classes/Withdraw.php

262 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Withdraw class
4 *
5 * @author: themeum
6 * @author_uri: https://themeum.com
7 * @package Tutor
8 * @since v.1.0.0
9 */
10
11 namespace TUTOR;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 class Withdraw {
18
19 public $withdraw_methods;
20
21 public function __construct() {
22 add_action( 'wp_ajax_tutor_save_withdraw_account', array( $this, 'tutor_save_withdraw_account' ) );
23 add_action( 'wp_ajax_tutor_make_an_withdraw', array( $this, 'tutor_make_an_withdraw' ) );
24
25 add_filter( 'tutor_withdrawal_methods_all', array( $this, 'withdraw_methods_all' ) );
26 add_filter( 'tutor_withdrawal_methods_available', array( $this, 'withdraw_methods_available' ) );
27 }
28
29 public function withdraw_methods_all(){
30
31 $this->migrate_withdrawal_method_data();
32
33 $methods = array(
34 'bank_transfer_withdraw' => array(
35 'method_name' => __( 'Bank Transfer', 'tutor' ),
36 'image' => tutor()->url . 'assets/images/payment-bank.png',
37 'desc' => __( 'Get your payment directly into your bank account', 'tutor' ),
38
39 'form_fields' => array(
40 'account_name' => array(
41 'type' => 'text',
42 'label' => __( 'Account Name', 'tutor' ),
43 ),
44 'account_number' => array(
45 'type' => 'text',
46 'label' => __( 'Account Number', 'tutor' ),
47 ),
48 'bank_name' => array(
49 'type' => 'text',
50 'label' => __( 'Bank Name', 'tutor' ),
51 ),
52 'iban' => array(
53 'type' => 'text',
54 'label' => __( 'IBAN', 'tutor' ),
55 ),
56 'swift' => array(
57 'type' => 'text',
58 'label' => __( 'BIC / SWIFT', 'tutor' ),
59 ),
60
61 ),
62 ),
63
64 'echeck_withdraw' => array(
65 'method_name' => __( 'E-Check', 'tutor' ),
66 'image' => tutor()->url . 'assets/images/payment-echeck.png',
67 'form_fields' => array(
68 'physical_address' => array(
69 'type' => 'text',
70 'label' => __( 'Your Physical Address', 'tutor' ),
71 'desc' => __( 'We will send you an E-Check to this address directly.', 'tutor' ),
72 ),
73 ),
74 ),
75
76 'paypal_withdraw' => array(
77 'method_name' => __( 'PayPal', 'tutor' ),
78 'image' => tutor()->url . 'assets/images/payment-paypal.png',
79 'form_fields' => array(
80 'paypal_email' => array(
81 'type' => 'email',
82 'label' => __( 'PayPal E-Mail Address', 'tutor' ),
83 'desc' => __( 'We will use this email address to send the money to your Paypal account', 'tutor' ),
84 ),
85
86 ),
87 ),
88 );
89
90 return apply_filters( 'tutor_withdraw_methods', $methods );
91 }
92
93 private function migrate_withdrawal_method_data() {
94 $old_data = get_option( 'tutor_withdraw_options', null );
95
96 if ( ! $old_data ) {
97 // Return if already migrated
98 return;
99 }
100
101 $withdraw_options = (array) maybe_unserialize( $old_data );
102 $new_methods_array = array();
103
104 foreach ( $withdraw_options as $key => $option ) {
105 if ( is_array( $option ) ) {
106
107 // Set enable state
108 if (isset( $option['enabled'] ) ) {
109 $option['enabled'] ? $new_methods_array[] = $key : 0;
110 }
111
112 // Set instruction
113 if ( isset( $option['instruction'] ) ) {
114 tutor_utils()->update_option( 'tutor_' . $key . '_instruction', $option['instruction'] );
115 }
116 }
117 }
118
119 // Update new
120 tutor_utils()->update_option( 'tutor_withdrawal_methods', $new_methods_array );
121
122 // Delete old
123 delete_option( 'tutor_withdraw_options' );
124 }
125
126 /**
127 * @return mixed|array
128 *
129 * Return only enabled methods
130 */
131 public function withdraw_methods_available() {
132 $methods = $this->withdraw_methods_all();
133 $withdraw_options = tutor_utils()->get_option( 'tutor_withdrawal_methods', array() );
134
135 foreach ( $methods as $method_id => $method ) {
136 if ( ! in_array( $method_id, $withdraw_options ) ) {
137 // Remove the unavailable methods from array
138 unset( $methods[$method_id] );
139 }
140 }
141
142 return $methods;
143 }
144
145 /**
146 * Save Withdraw Method Data
147 *
148 * @since v.1.2.0
149 */
150
151 public function tutor_save_withdraw_account() {
152 //Checking nonce
153 tutor_utils()->checking_nonce();
154
155 $user_id = get_current_user_id();
156 $method = sanitize_text_field( tutor_utils()->avalue_dot( 'tutor_selected_withdraw_method', $_POST ) );
157 if ( ! $method ) {
158 wp_send_json_error();
159 }
160
161 $method_data = tutor_utils()->avalue_dot( "withdraw_method_field.".$method, $_POST );
162 $available_withdraw_method = $this->withdraw_methods_all();
163
164 if ( tutor_utils()->count( $method_data ) ) {
165 $saved_data = array();
166 $saved_data['withdraw_method_key'] = $method;
167 $saved_data['withdraw_method_name'] = tutor_utils()->avalue_dot( $method.".method_name", $available_withdraw_method );
168
169 foreach ( $method_data as $input_name => $value ) {
170 $saved_data[ $input_name ]['value'] = esc_sql( sanitize_text_field( $value ) ) ;
171 $saved_data[ $input_name ]['label'] = tutor_utils()->avalue_dot( $method.".form_fields.{$input_name}.label", $available_withdraw_method );
172 }
173
174 update_user_meta( $user_id, '_tutor_withdraw_method_data', $saved_data );
175 update_user_meta( $user_id, '_tutor_withdraw_selected_method', $method );
176 update_user_meta( $user_id, '_tutor_withdraw_method_data_'.$method, $saved_data );
177 }
178
179 $msg = apply_filters( 'tutor_withdraw_method_set_success_msg', __( 'Withdrawal information saved!', 'tutor' ) );
180 wp_send_json_success(array( 'msg' => $msg ) );
181 }
182
183 public function tutor_make_an_withdraw() {
184 global $wpdb;
185
186 // Checking nonce
187 tutor_utils()->checking_nonce();
188
189 $user_id = get_current_user_id();
190 $withdraw_amount = sanitize_text_field( tutor_utils()->avalue_dot( 'tutor_withdraw_amount', $_POST ) );
191
192 $earning_sum = tutor_utils()->get_earning_sum();
193 $min_withdraw = tutor_utils()->get_option( 'min_withdraw_amount' );
194
195 $saved_withdraw_account = tutor_utils()->get_user_withdraw_method();
196 $formatted_balance = tutor_utils()->tutor_price( $earning_sum->balance );
197 $formatted_min_withdraw_amount = tutor_utils()->tutor_price( $min_withdraw );
198
199 $saved_withdraw_account = tutor_utils()->get_user_withdraw_method();
200 $formatted_balance = tutor_utils()->tutor_price( $earning_sum->balance );
201 $formatted_min_withdraw_amount = tutor_utils()->tutor_price( $min_withdraw );
202
203 if ( ! tutor_utils()->count( $saved_withdraw_account ) ) {
204 $no_withdraw_method = apply_filters( 'tutor_no_withdraw_method_msg', __( 'Please save withdraw method ', 'tutor' ) );
205 wp_send_json_error( array( 'msg' => $no_withdraw_method ) );
206 }
207
208 if ( ( ! is_numeric( $withdraw_amount ) && ! is_float( $withdraw_amount ) ) || $withdraw_amount < $min_withdraw ) {
209 $required_min_withdraw = apply_filters( 'tutor_required_min_amount_msg', sprintf( __( 'Minimum withdrawal amount is %s %s %s ', 'tutor' ) , '<strong>', $formatted_min_withdraw_amount, '</strong>' ) );
210 wp_send_json_error( array( 'msg' => $required_min_withdraw ) );
211 }
212
213 if ( $earning_sum->balance < $withdraw_amount ) {
214 $insufficient_balence = apply_filters( 'tutor_withdraw_insufficient_balance_msg', __( 'Insufficient balance.', 'tutor' ) );
215
216 wp_send_json_error( array( 'msg' => $insufficient_balence ) );
217 }
218
219 $date = date( 'Y-m-d H:i:s', tutor_time() );
220
221 $withdraw_data = apply_filters(
222 'tutor_pre_withdraw_data',
223 array(
224 'user_id' => $user_id,
225 'amount' => $withdraw_amount,
226 'method_data' => maybe_serialize( $saved_withdraw_account ),
227 'status' => 'pending',
228 'created_at' => $date,
229 )
230 );
231
232 $date = date( "Y-m-d H:i:s", tutor_time() );
233
234 $withdraw_data = apply_filters( 'tutor_pre_withdraw_data', array(
235 'user_id' => $user_id,
236 'amount' => $withdraw_amount,
237 'method_data' => maybe_serialize( $saved_withdraw_account ),
238 'status' => 'pending',
239 'created_at' => $date,
240 ));
241
242 do_action( 'tutor_insert_withdraw_before', $withdraw_data );
243
244 $wpdb->insert( $wpdb->prefix."tutor_withdraws", $withdraw_data );
245 $withdraw_id = $wpdb->insert_id;
246
247 do_action( 'tutor_insert_withdraw_after', $withdraw_id, $withdraw_data );
248
249
250 /**
251 * Getting earning and balance data again
252 */
253 $earning = tutor_utils()->get_earning_sum();
254 $new_available_balance = tutor_utils()->tutor_price( $earning->balance );
255
256 do_action( 'tutor_withdraw_after' );
257
258 $withdraw_successfull_msg = apply_filters( 'tutor_withdraw_successful_msg', __( 'Withdrawal Request Sent!', 'tutor' ) );
259 wp_send_json_success( array( 'msg' => $withdraw_successfull_msg, 'available_balance' => $new_available_balance ) );
260 }
261 }
262