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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / ecommerce / BillingController.php
tutor / ecommerce Last commit date
Cart 10 months ago PaymentGateways 8 months ago AdminMenu.php 9 months ago BillingController.php 1 year ago CartController.php 1 year ago CheckoutController.php 7 months ago CouponController.php 11 months ago Ecommerce.php 1 year ago EmailController.php 11 months ago HooksHandler.php 7 months ago OptionKeys.php 1 year ago OrderActivitiesController.php 1 year ago OrderController.php 7 months ago PaymentHandler.php 9 months ago Settings.php 9 months ago Tax.php 9 months ago currency.php 1 year ago
BillingController.php
246 lines
1 <?php
2 /**
3 * Manage Billing
4 *
5 * @package Tutor\Ecommerce
6 * @author Themeum
7 * @link https://themeum.com
8 * @since 3.0.0
9 */
10
11 namespace Tutor\Ecommerce;
12
13 use TUTOR\BaseController;
14 use Tutor\Helpers\HttpHelper;
15 use Tutor\Helpers\ValidationHelper;
16 use Tutor\Models\BillingModel;
17 use Tutor\Traits\JsonResponse;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22
23 /**
24 * BillingController class
25 *
26 * @since 3.0.0
27 */
28 class BillingController extends BaseController {
29
30
31 /**
32 * Billing model
33 *
34 * @since 3.0.0
35 *
36 * @var BillingModel
37 */
38 private $model;
39
40 /**
41 * Trait for sending JSON response
42 */
43 use JsonResponse;
44
45 /**
46 * Constructor.
47 *
48 * Initializes the Billing class, sets the page title, and optionally registers
49 * hooks for handling AJAX requests related to billing data.
50 *
51 * @param bool $register_hooks Whether to register hooks for handling requests. Default is true.
52 *
53 * @since 3.0.0
54 *
55 * @return void
56 */
57 public function __construct( $register_hooks = true ) {
58 $this->model = new BillingModel();
59
60 if ( $register_hooks ) {
61 add_filter( 'tutor_dashboard/nav_items/settings/nav_items', array( $this, 'register_nav' ) );
62 add_filter( 'load_dashboard_template_part_from_other_location', array( $this, 'load_template' ) );
63
64 /**
65 * Handle AJAX request for saving billing info if current user.
66 *
67 * @since 3.0.0
68 */
69 add_action( 'wp_ajax_tutor_save_billing_info', array( $this, 'save_billing_info' ) );
70
71 /**
72 * Handle AJAX request for getting billing info if current user.
73 *
74 * @since 3.0.0
75 */
76 add_action( 'wp_ajax_tutor_get_billing_info', array( $this, 'get_billing_info' ) );
77 }
78 }
79
80 /**
81 * Register billing nav menu for settings
82 *
83 * @since 3.0.0
84 *
85 * @param array $tabs setting navigation tabs.
86 *
87 * @return array
88 */
89 public static function register_nav( $tabs ) {
90 $billing_url = tutor_utils()->get_tutor_dashboard_page_permalink( 'settings/billing' );
91
92 $new_tab = array(
93 'url' => esc_url( $billing_url ),
94 'title' => __( 'Billing', 'tutor' ),
95 'role' => false,
96 );
97
98 $tabs['billing'] = $new_tab;
99
100 return $tabs;
101 }
102
103 /**
104 * Load billing template for settings
105 *
106 * Based on query_vars filter template path
107 *
108 * @since 3.0.0
109 *
110 * @param string $location default file location.
111 *
112 * @return string
113 */
114 public static function load_template( $location ) {
115 $page_name = get_query_var( 'pagename' );
116 $dashboard_sub_page = get_query_var( 'tutor_dashboard_sub_page' );
117
118 $dashboard_page_id = (int) tutor_utils()->get_option( 'tutor_dashboard_page_id' );
119 $dashboard_page = get_post( $dashboard_page_id );
120
121 // Current page is dashboard & sub page is billing.
122 if ( $page_name === $dashboard_page->post_name && 'billing' === $dashboard_sub_page ) {
123 $template = tutor()->path . 'templates/ecommerce/billing.php';
124
125 if ( file_exists( $template ) ) {
126 $location = $template;
127 }
128 }
129
130 return $location;
131 }
132
133 /**
134 * Get the customer model object
135 *
136 * @since 3.0.0
137 *
138 * @return object
139 */
140 public function get_model() {
141 return $this->model;
142 }
143
144 /**
145 * Save billing information for the current user.
146 *
147 * @since 3.0.0
148 *
149 * @return void
150 */
151 public function save_billing_info() {
152 if ( ! tutor_utils()->is_nonce_verified() ) {
153 $this->json_response(
154 tutor_utils()->error_message( 'nonce' ),
155 null,
156 HttpHelper::STATUS_BAD_REQUEST
157 );
158 }
159
160 $data = $this->get_allowed_fields( $_POST );
161
162 $user_id = get_current_user_id();
163 $data['user_id'] = $user_id;
164
165 $validation = $this->validate( $data );
166 if ( ! $validation->success ) {
167 $this->json_response(
168 __( 'Invalid inputs', 'tutor' ),
169 $validation->errors,
170 HttpHelper::STATUS_UNPROCESSABLE_ENTITY
171 );
172 }
173
174 $billing_info = $this->get_billing_info();
175
176 if ( $billing_info ) {
177 $response = $this->model->update( $data, array( 'user_id' => $user_id ) );
178 } else {
179 $response = $this->model->insert( $data );
180 }
181
182 if ( ! $response ) {
183 $this->json_response(
184 __( 'Failed to save billing info', 'tutor' ),
185 null,
186 HttpHelper::STATUS_INTERNAL_SERVER_ERROR
187 );
188 }
189
190 $this->json_response( __( 'Billing info saved successfully', 'tutor' ) );
191 }
192
193 /**
194 * Get billing information for the current user.
195 *
196 * @since 3.0.0
197 *
198 * @param int $user_id User id.
199 *
200 * @return mixed The user's billing information.
201 */
202 public function get_billing_info( $user_id = 0 ) {
203 $user_id = tutor_utils()->get_user_id( $user_id );
204 return $this->model->get_info( $user_id );
205 }
206
207 /**
208 * Validate input data based on predefined rules.
209 *
210 * This protected method validates the provided data array against a set of
211 * predefined validation rules. The rules specify that 'order_id' is required
212 * and must be numeric. The method will skip validation rules for fields that
213 * are not present in the data array.
214 *
215 * @since 3.0.0
216 *
217 * @param array $data The data array to validate.
218 *
219 * @return object The validation result. It returns validation object.
220 */
221 protected function validate( array $data ) {
222
223 $validation_rules = array(
224 'user_id' => 'required|numeric',
225 'first_name' => 'required',
226 'last_name' => 'required',
227 'email' => 'required|email',
228 'phone' => 'required',
229 'zip_code' => 'required',
230 'address' => 'required',
231 'country' => 'required',
232 'state' => 'required',
233 'city' => 'required',
234 );
235
236 // Skip validation rules for not available fields in data.
237 foreach ( $validation_rules as $key => $value ) {
238 if ( ! array_key_exists( $key, $data ) ) {
239 unset( $validation_rules[ $key ] );
240 }
241 }
242
243 return ValidationHelper::validate( $validation_rules, $data );
244 }
245 }
246