| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Checkout |
| 4 |
* |
| 5 |
* @author ThimPress <Nhamdv> |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 4.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
class LP_Checkout { |
| 13 |
|
| 14 |
/** |
| 15 |
* @var LP_Checkout object instance |
| 16 |
* @access protected |
| 17 |
*/ |
| 18 |
protected static $_instance = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* Payment method |
| 22 |
* |
| 23 |
* @var string|LP_Gateway_Abstract |
| 24 |
*/ |
| 25 |
public $payment_method = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var array|mixed|null|void |
| 29 |
*/ |
| 30 |
protected $checkout_fields = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* @var null |
| 34 |
*/ |
| 35 |
public $user_login = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var null |
| 39 |
*/ |
| 40 |
public $user_pass = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var null |
| 44 |
*/ |
| 45 |
public $order_comment = null; |
| 46 |
|
| 47 |
/** |
| 48 |
* Handle the errors when checking out. |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
public $errors = array(); |
| 53 |
|
| 54 |
/** |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
protected $_checkout_email = ''; |
| 58 |
|
| 59 |
/** |
| 60 |
* @var array |
| 61 |
* @since 4.0.0 |
| 62 |
*/ |
| 63 |
protected $login_data = array(); |
| 64 |
|
| 65 |
/** |
| 66 |
* @var array |
| 67 |
* @since 4.0.0 |
| 68 |
*/ |
| 69 |
protected $register_data = array(); |
| 70 |
|
| 71 |
/** |
| 72 |
* @var string |
| 73 |
* @since 4.0.0 |
| 74 |
*/ |
| 75 |
protected $guest_email = ''; |
| 76 |
|
| 77 |
/** |
| 78 |
* @var string |
| 79 |
* @since 4.0.0 |
| 80 |
*/ |
| 81 |
protected $checkout_action = ''; |
| 82 |
|
| 83 |
/** |
| 84 |
* @var array |
| 85 |
* @since 4.0.0 |
| 86 |
*/ |
| 87 |
protected $checkout_form_data = array(); |
| 88 |
|
| 89 |
/** |
| 90 |
* Constructor |
| 91 |
*/ |
| 92 |
public function __construct() { |
| 93 |
add_filter( 'learn-press/validate-checkout-field', array( $this, 'validate_fields' ), 10, 3 ); |
| 94 |
add_filter( 'learn-press/validate-checkout-fields', array( $this, 'check_validate_fields' ), 10, 3 ); |
| 95 |
//add_filter( 'learn-press/payment-successful-result', array( $this, 'process_customer' ), 10, 2 ); |
| 96 |
|
| 97 |
if ( ! is_null( LP()->session ) ) { |
| 98 |
$this->_checkout_email = LP()->session->get( 'checkout-email' ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Process customer when checking out. |
| 104 |
* |
| 105 |
* @param array $result |
| 106 |
* @param int $order_id |
| 107 |
* |
| 108 |
* @return mixed |
| 109 |
* @editor tungnx |
| 110 |
* @modify 4.1.4 - merge to function create_order |
| 111 |
*/ |
| 112 |
/*public function process_customer( $result, $order_id ) { |
| 113 |
try { |
| 114 |
if ( ! $this->is_enable_guest_checkout() ) { |
| 115 |
throw new Exception( '' ); |
| 116 |
} |
| 117 |
|
| 118 |
$order = learn_press_get_order( $order_id ); |
| 119 |
|
| 120 |
if ( ! $order ) { |
| 121 |
throw new Exception( '' ); |
| 122 |
} |
| 123 |
|
| 124 |
$user_id = 0; |
| 125 |
$checkout_option = LP_Request::get_string( 'checkout-email-option' ); |
| 126 |
|
| 127 |
switch ( $checkout_option ) { |
| 128 |
case 'new-account': |
| 129 |
if ( $this->checkout_email_exists() ) { |
| 130 |
throw new Exception( 'NEW ACCOUNT EMAIL IS EXISTED', 0 ); |
| 131 |
} |
| 132 |
|
| 133 |
$order->set_meta( '_create_account', 'yes' ); |
| 134 |
|
| 135 |
$user_id = $this->_create_account(); |
| 136 |
|
| 137 |
if ( ! is_wp_error( $user_id ) ) { |
| 138 |
wp_new_user_notification( $user_id, null, apply_filters( 'learn-press/email-create-new-user-when-checkout', 'user' ) ); |
| 139 |
} |
| 140 |
break; |
| 141 |
} |
| 142 |
|
| 143 |
if ( $user_id ) { |
| 144 |
$order->set_user_id( $user_id ); |
| 145 |
$order->save(); |
| 146 |
} |
| 147 |
} catch ( Exception $ex ) { |
| 148 |
if ( $ex->getCode() && $ex->getMessage() ) { |
| 149 |
$result['message'] = $ex->getMessage(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return $result; |
| 154 |
}*/ |
| 155 |
|
| 156 |
/** |
| 157 |
* @return int|WP_Error |
| 158 |
*/ |
| 159 |
protected function _create_account() { |
| 160 |
$email = $this->get_checkout_email(); |
| 161 |
$password = wp_generate_password( 12, true ); |
| 162 |
$user_id = wp_create_user( $email, $password, $email ); |
| 163 |
|
| 164 |
return $user_id; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @param array $errors |
| 169 |
* @param array $fields |
| 170 |
* @param LP_Checkout $checkout |
| 171 |
* |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
public function check_validate_fields( $errors, $fields, $checkout ) { |
| 175 |
if ( empty( $errors ) ) { |
| 176 |
switch ( $this->checkout_action ) { |
| 177 |
case 'checkout-login': |
| 178 |
$this->checkout_form_data['remember'] = isset( $_POST['rememberme'] ) ? true : false; |
| 179 |
|
| 180 |
$login_info = $this->checkout_form_data; |
| 181 |
|
| 182 |
$user = wp_signon( |
| 183 |
array( |
| 184 |
'user_login' => $login_info['username'], |
| 185 |
'user_password' => $login_info['password'], |
| 186 |
'remember' => $login_info['remember'], |
| 187 |
), |
| 188 |
is_ssl() |
| 189 |
); |
| 190 |
|
| 191 |
if ( is_wp_error( $user ) ) { |
| 192 |
$errors['login_error'] = $user->get_error_message(); |
| 193 |
} else { |
| 194 |
wp_set_current_user( $user->ID ); |
| 195 |
} |
| 196 |
break; |
| 197 |
case 'checkout-register': |
| 198 |
$default_fields = array(); |
| 199 |
|
| 200 |
if ( isset( $_POST['reg_first_name'] ) ) { |
| 201 |
$default_fields['first_name'] = LP_Helper::sanitize_params_submitted( $_POST['reg_first_name'] ); |
| 202 |
} |
| 203 |
|
| 204 |
if ( isset( $_POST['reg_last_name'] ) ) { |
| 205 |
$default_fields['last_name'] = LP_Helper::sanitize_params_submitted( $_POST['reg_last_name'] ); |
| 206 |
} |
| 207 |
|
| 208 |
if ( isset( $_POST['reg_display_name'] ) ) { |
| 209 |
$default_fields['display_name'] = LP_Helper::sanitize_params_submitted( $_POST['reg_display_name'] ); |
| 210 |
} |
| 211 |
|
| 212 |
$update_meta = isset( $_POST['_lp_custom_register_form'] ) ? LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register_form'] ) : array(); |
| 213 |
|
| 214 |
$user_id = LP_Forms_Handler::learnpress_create_new_customer( |
| 215 |
$this->checkout_form_data['reg_email'], |
| 216 |
$this->checkout_form_data['reg_username'], |
| 217 |
$this->checkout_form_data['reg_password'], |
| 218 |
$this->checkout_form_data['reg_password2'], |
| 219 |
$default_fields, |
| 220 |
$update_meta |
| 221 |
); |
| 222 |
|
| 223 |
if ( is_wp_error( $user_id ) ) { |
| 224 |
$errors['create_user_error'] = $user_id->get_error_message(); |
| 225 |
} else { |
| 226 |
wp_set_current_user( $user_id ); |
| 227 |
wp_set_auth_cookie( $user_id, true ); |
| 228 |
} |
| 229 |
break; |
| 230 |
case 'guest-checkout': |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return $errors; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Get email of user is being checkout. |
| 239 |
* |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
public function get_checkout_email() { |
| 243 |
if ( $this->_checkout_email ) { |
| 244 |
return $this->_checkout_email; |
| 245 |
} elseif ( is_user_logged_in() ) { |
| 246 |
$user = learn_press_get_current_user( false ); |
| 247 |
|
| 248 |
return $user->get_email(); |
| 249 |
} |
| 250 |
|
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* @return bool|int |
| 256 |
*/ |
| 257 |
public function checkout_email_exists() { |
| 258 |
$email = $this->get_checkout_email(); |
| 259 |
|
| 260 |
if ( ! $email ) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
$user = get_user_by( 'email', $email ); |
| 265 |
|
| 266 |
if ( ! $user ) { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
return $user->ID; |
| 271 |
} |
| 272 |
|
| 273 |
public function verify_nonce() { |
| 274 |
if ( empty( $_POST['learn-press-checkout-nonce'] ) ) { |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
if ( ! is_user_logged_in() ) { |
| 279 |
$actions = array( 'guest-checkout', 'checkout-register', 'checkout-login' ); |
| 280 |
} else { |
| 281 |
$actions = array( 'guest-checkout', 'checkout-register', 'user-logged' ); |
| 282 |
} |
| 283 |
|
| 284 |
foreach ( $actions as $action ) { |
| 285 |
if ( wp_verify_nonce( $_REQUEST['learn-press-checkout-nonce'], "learn-press-{$action}" ) ) { |
| 286 |
$this->checkout_action = $action; |
| 287 |
|
| 288 |
return true; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Checkout fields. |
| 297 |
* |
| 298 |
* @return array |
| 299 |
*/ |
| 300 |
public function get_checkout_fields() { |
| 301 |
if ( ! $this->verify_nonce() ) { |
| 302 |
$this->checkout_fields['invalid_request'] = new WP_Error( 'invalid_request', __( 'Your session has expired.', 'learnpress' ) ); |
| 303 |
} else { |
| 304 |
if ( $this->checkout_action === 'checkout-register' ) { |
| 305 |
$this->checkout_fields['reg_email'] = esc_html__( 'Email', 'learnpress' ); |
| 306 |
$this->checkout_fields['reg_username'] = esc_html__( 'Username', 'learnpress' ); |
| 307 |
$this->checkout_fields['reg_password'] = esc_html__( 'Password', 'learnpress' ); |
| 308 |
$this->checkout_fields['reg_password2'] = esc_html__( 'Confirm Password', 'learnpress' ); |
| 309 |
} elseif ( $this->checkout_action === 'checkout-login' ) { |
| 310 |
$this->checkout_fields['username'] = esc_html__( 'Username', 'learnpress' ); |
| 311 |
$this->checkout_fields['password'] = esc_html__( 'Password', 'learnpress' ); |
| 312 |
} elseif ( $this->checkout_action === 'guest-checkout' ) { |
| 313 |
$this->checkout_fields['guest_email'] = esc_html__( 'Email', 'learnpress' ); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
$this->checkout_fields = apply_filters( 'learn_press_checkout_fields', $this->checkout_fields ); |
| 318 |
|
| 319 |
return $this->checkout_fields; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Check if an order is pending or failed. |
| 324 |
* |
| 325 |
* @param $order_id |
| 326 |
* |
| 327 |
* @return LP_Order|bool |
| 328 |
*/ |
| 329 |
protected function _is_resume_order( $order_id ) { |
| 330 |
$order = learn_press_get_order( $order_id ); |
| 331 |
|
| 332 |
if ( $order_id > 0 && $order && $order->has_status( array( 'pending', 'failed' ) ) ) { |
| 333 |
return $order; |
| 334 |
} |
| 335 |
|
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Creates temp new order if needed |
| 341 |
* |
| 342 |
* @return mixed|WP_Error |
| 343 |
* @throws Exception |
| 344 |
*/ |
| 345 |
public function create_order() { |
| 346 |
global $wpdb; |
| 347 |
|
| 348 |
$order_id = apply_filters( 'learn-press/checkout/create-order', null, $this ); |
| 349 |
|
| 350 |
if ( $order_id ) { |
| 351 |
return $order_id; |
| 352 |
} |
| 353 |
$cart = LP()->cart; |
| 354 |
|
| 355 |
try { |
| 356 |
$wpdb->query( 'START TRANSACTION' ); |
| 357 |
|
| 358 |
// Insert or update the post data |
| 359 |
$order_id = absint( LP()->session->get( 'order_awaiting_payment' ) ); |
| 360 |
|
| 361 |
// Resume the unpaid order if its pending |
| 362 |
$order = $this->_is_resume_order( $order_id ); |
| 363 |
|
| 364 |
if ( $order ) { |
| 365 |
if ( is_wp_error( $order ) ) { |
| 366 |
throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'learnpress' ), 401 ) ); |
| 367 |
} |
| 368 |
|
| 369 |
$order->remove_order_items(); |
| 370 |
do_action( 'learn-press/checkout/resume-order', $order_id ); |
| 371 |
|
| 372 |
} else { |
| 373 |
$order = new LP_Order(); |
| 374 |
|
| 375 |
if ( is_wp_error( $order ) ) { |
| 376 |
throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'learnpress' ), 400 ) ); |
| 377 |
} |
| 378 |
|
| 379 |
$order_id = $order->get_id(); |
| 380 |
do_action( 'learn-press/checkout/new-order', $order_id ); |
| 381 |
} |
| 382 |
|
| 383 |
$user_id = 0; |
| 384 |
if ( is_user_logged_in() ) { |
| 385 |
$user_id = get_current_user_id(); |
| 386 |
} else { |
| 387 |
$checkout_option = LP_Helper::sanitize_params_submitted( $_POST['checkout-email-option'] ?? '' ); |
| 388 |
|
| 389 |
// Create new user if buy with Guest and tick "Create new Account" |
| 390 |
if ( $checkout_option === 'new-account' ) { |
| 391 |
if ( $this->checkout_email_exists() ) { |
| 392 |
throw new Exception( 'NEW ACCOUNT EMAIL IS EXISTED', 0 ); |
| 393 |
} |
| 394 |
|
| 395 |
$order->set_meta( '_create_account', 'yes' ); |
| 396 |
|
| 397 |
$user_id = $this->_create_account(); |
| 398 |
|
| 399 |
if ( ! is_wp_error( $user_id ) ) { |
| 400 |
wp_new_user_notification( $user_id, null, apply_filters( 'learn-press/email-create-new-user-when-checkout', 'user' ) ); |
| 401 |
} |
| 402 |
} else { // Set user id for Order if buy with Guest and email exists on the user |
| 403 |
$user = get_user_by( 'email', $this->get_checkout_email() ); |
| 404 |
|
| 405 |
if ( $order->is_guest() && $user ) { |
| 406 |
$user_id = $user->ID; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
if ( $user_id ) { |
| 411 |
$order->set_user_id( $user_id ); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
$order->set_customer_note( $this->order_comment ); |
| 416 |
$order->set_status( learn_press_default_order_status( 'lp-' ) ); |
| 417 |
$order->set_total( $cart->total ); |
| 418 |
$order->set_subtotal( $cart->subtotal ); |
| 419 |
$order->set_user_ip_address( learn_press_get_ip() ); |
| 420 |
$order->set_user_agent( learn_press_get_user_agent() ); |
| 421 |
$order->set_created_via( 'checkout' ); |
| 422 |
$order->set_user_id( apply_filters( 'learn-press/checkout/default-user', $user_id ) ); |
| 423 |
|
| 424 |
if ( $this->is_enable_guest_checkout() && $this->get_checkout_email() ) { |
| 425 |
$order->set_checkout_email( $this->get_checkout_email() ); |
| 426 |
} |
| 427 |
|
| 428 |
$order_id = $order->save(); |
| 429 |
|
| 430 |
// Store the line items to the new/resumed order |
| 431 |
foreach ( $cart->get_items() as $item ) { |
| 432 |
$item_type = get_post_type( $item['item_id'] ); |
| 433 |
|
| 434 |
if ( ! in_array( $item_type, learn_press_get_item_types_can_purchase() ) ) { |
| 435 |
continue; |
| 436 |
} |
| 437 |
|
| 438 |
$item_id = $order->add_item( $item ); |
| 439 |
|
| 440 |
if ( ! $item_id ) { |
| 441 |
throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'learnpress' ), 402 ) ); |
| 442 |
} |
| 443 |
|
| 444 |
do_action( 'learn-press/checkout/add-order-item-meta', $item_id, $item ); |
| 445 |
} |
| 446 |
|
| 447 |
$order->set_payment_method( $this->payment_method ); |
| 448 |
|
| 449 |
if ( ! empty( $this->user_id ) ) { |
| 450 |
do_action( 'learn-press/checkout/update-user-meta', $this->user_id ); |
| 451 |
} |
| 452 |
|
| 453 |
do_action( 'learn-press/checkout/update-order-meta', $order_id ); |
| 454 |
|
| 455 |
if ( ! $order_id || is_wp_error( $order_id ) ) { |
| 456 |
learn_press_add_message( __( 'Unable to checkout. Order creation failed.', 'learnpress' ) ); |
| 457 |
} |
| 458 |
|
| 459 |
$wpdb->query( 'COMMIT' ); |
| 460 |
|
| 461 |
} catch ( Exception $e ) { |
| 462 |
$wpdb->query( 'ROLLBACK' ); |
| 463 |
learn_press_add_message( $e->getMessage() ); |
| 464 |
|
| 465 |
return false; |
| 466 |
} |
| 467 |
|
| 468 |
return $order_id; |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Guest checkout is enable? |
| 473 |
* |
| 474 |
* @return mixed |
| 475 |
* @since 3.0.0 |
| 476 |
*/ |
| 477 |
public function is_enable_guest_checkout() { |
| 478 |
return apply_filters( 'learn-press/checkout/enable-guest', LP_Settings::instance()->get( 'guest_checkout', 'no' ) == 'yes' ); |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Enable user can login in checkout page? |
| 483 |
* |
| 484 |
* @return bool |
| 485 |
* @since 3.0.0 |
| 486 |
*/ |
| 487 |
public function is_enable_login() { |
| 488 |
return apply_filters( 'learn-press/checkout/enable-login', in_array( LP_Settings::instance()->get( 'enable_login_checkout' ), array( '', 'yes' ) ) ); |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* Enable user can register in checkout page? |
| 493 |
* |
| 494 |
* @return bool |
| 495 |
* @since 3.0.0 |
| 496 |
*/ |
| 497 |
public function is_enable_register() { |
| 498 |
return apply_filters( 'learn-press/checkout/enable-register', in_array( LP_Settings::instance()->get( 'enable_registration_checkout' ), array( '', 'yes' ) ) && get_option( 'users_can_register' ) ); |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Validate fields |
| 503 |
* |
| 504 |
* @param bool $validate |
| 505 |
* @param mixed $field |
| 506 |
* @param string $name |
| 507 |
* |
| 508 |
* @return bool|WP_Error |
| 509 |
*/ |
| 510 |
public function validate_fields( $validate, $field, $name ) { |
| 511 |
switch ( $this->checkout_action ) { |
| 512 |
case 'checkout-register': |
| 513 |
if ( $name === 'reg_username' ) { |
| 514 |
if ( empty( $_POST['reg_username'] ) ) { |
| 515 |
return new WP_Error( 'username_empty', sprintf( __( '%s is required field.', 'learnpress' ), $field ) ); |
| 516 |
} elseif ( username_exists( $_POST['reg_username'] ) ) { |
| 517 |
return new WP_Error( 'username_exists', sprintf( __( '%s is exists.', 'learnpress' ), $_POST['reg_username'] ) ); |
| 518 |
} elseif ( ! validate_username( $_POST['reg_username'] ) ) { |
| 519 |
return new WP_Error( 'username_invalid', sprintf( __( '%s is not a valid username.', 'learnpress' ), $_POST['reg_username'] ) ); |
| 520 |
} |
| 521 |
} elseif ( $name === 'reg_email' ) { |
| 522 |
if ( empty( $_POST['reg_email'] ) ) { |
| 523 |
return new WP_Error( 'email_empty', sprintf( __( '%s is required field.', 'learnpress' ), $field ) ); |
| 524 |
} elseif ( email_exists( $_POST['reg_email'] ) ) { |
| 525 |
return new WP_Error( 'email_exists', sprintf( __( '%s is exists.', 'learnpress' ), $_POST['reg_email'] ) ); |
| 526 |
} elseif ( ! is_email( $_POST['reg_email'] ) ) { |
| 527 |
return new WP_Error( 'email_invalid', sprintf( __( '%s is not a valid email.', 'learnpress' ), $_POST['reg_email'] ) ); |
| 528 |
} |
| 529 |
} elseif ( $name === 'reg_password' ) { |
| 530 |
if ( empty( $_POST['reg_password'] ) ) { |
| 531 |
return new WP_Error( 'password_empty', sprintf( __( '%s is required field.', 'learnpress' ), $field ) ); |
| 532 |
} |
| 533 |
} elseif ( $name === 'reg_password2' ) { |
| 534 |
if ( empty( $_POST['reg_password2'] ) ) { |
| 535 |
return new WP_Error( 'password2_empty', sprintf( __( '%s is required field.', 'learnpress' ), $field ) ); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
$this->checkout_form_data[ $name ] = LP_Helper::maybe_unserialize( $_POST[ $name ] ); |
| 540 |
|
| 541 |
break; |
| 542 |
case 'checkout-login': |
| 543 |
if ( $name === 'username' ) { |
| 544 |
if ( empty( $_POST['username'] ) ) { |
| 545 |
return new WP_Error( 'email_empty', sprintf( __( '%s is required field.', 'learnpress' ), $field ) ); |
| 546 |
} |
| 547 |
|
| 548 |
if ( ! username_exists( $_POST['username'] ) && ! email_exists( $_POST['username'] ) ) { |
| 549 |
return new WP_Error( 'username_exists', sprintf( __( '%s is not exists.', 'learnpress' ), $field ) ); |
| 550 |
} |
| 551 |
} elseif ( $name === 'password' ) { |
| 552 |
if ( empty( $_POST['password'] ) ) { |
| 553 |
return new WP_Error( 'email_empty', sprintf( __( '%s is required field.', 'learnpress' ), $field ) ); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
$this->checkout_form_data[ $name ] = LP_Helper::maybe_unserialize( $_POST[ $name ] ); |
| 558 |
break; |
| 559 |
case 'guest-checkout': |
| 560 |
if ( empty( $_POST['guest_email'] ) ) { |
| 561 |
return new WP_Error( 'email_empty', sprintf( __( '%s is required field.', 'learnpress' ), $field ) ); |
| 562 |
} elseif ( ! is_email( $_POST['guest_email'] ) ) { |
| 563 |
return new WP_Error( 'email_invalid', __( 'Your email is not a valid.', 'learnpress' ) ); |
| 564 |
} |
| 565 |
|
| 566 |
$this->guest_email = LP_Helper::maybe_unserialize( $_POST[ $name ] ); |
| 567 |
$this->_checkout_email = LP_Helper::maybe_unserialize( $_POST[ $name ] ); |
| 568 |
} |
| 569 |
|
| 570 |
return $validate; |
| 571 |
} |
| 572 |
|
| 573 |
/** |
| 574 |
* Process checkout from request. |
| 575 |
* |
| 576 |
* @return mixed |
| 577 |
* @throws Exception |
| 578 |
*/ |
| 579 |
public function process_checkout_handler() { |
| 580 |
if ( strtolower( $_SERVER['REQUEST_METHOD'] ) != 'post' ) { |
| 581 |
return false; |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* Set default fields from request. |
| 586 |
*/ |
| 587 |
$this->payment_method = LP_Request::get_string( 'payment_method' ); |
| 588 |
$this->user_login = LP_Request::get_string( 'username' ); |
| 589 |
$this->user_pass = LP_Request::get_string( 'password' ); |
| 590 |
$this->order_comment = LP_Request::get_string( 'order_comments' ); |
| 591 |
$this->_checkout_email = LP_Request::get_email( 'checkout-email' ); |
| 592 |
|
| 593 |
if ( LP_Request::get_int( 'terms_conditions_field', 0 ) ) { |
| 594 |
$this->checkout_fields['terms_conditions'] = LP_Request::get_string( 'terms_conditions', '' ); |
| 595 |
} |
| 596 |
|
| 597 |
if ( $this->_checkout_email ) { |
| 598 |
LP()->session->set( 'checkout-email', $this->_checkout_email ); |
| 599 |
} |
| 600 |
|
| 601 |
$this->process_checkout(); |
| 602 |
|
| 603 |
return true; |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
* Validate fields. |
| 608 |
* |
| 609 |
* @return bool |
| 610 |
*/ |
| 611 |
public function validate_checkout_fields() { |
| 612 |
$this->errors = array(); |
| 613 |
$fields = $this->get_checkout_fields(); |
| 614 |
|
| 615 |
if ( $fields ) { |
| 616 |
foreach ( $fields as $name => $field ) { |
| 617 |
if ( ! is_wp_error( $field ) ) { |
| 618 |
$error = apply_filters( 'learn-press/validate-checkout-field', true, $field, $name ); |
| 619 |
} else { |
| 620 |
$error = $field; |
| 621 |
} |
| 622 |
|
| 623 |
if ( is_wp_error( $error ) ) { |
| 624 |
$this->errors[ $name ] = $error; |
| 625 |
} elseif ( ! $error ) { |
| 626 |
$this->errors[ $name ] = new WP_Error( 'invalid_field', __( 'Invalid field', 'learnpress' ) ); |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
$this->errors = apply_filters( 'learn-press/validate-checkout-fields', $this->errors, $fields, $this ); |
| 632 |
|
| 633 |
return ! sizeof( $this->errors ); |
| 634 |
} |
| 635 |
|
| 636 |
/** |
| 637 |
* Validate checkout payment. |
| 638 |
* |
| 639 |
* @return bool |
| 640 |
* @throws Exception |
| 641 |
*/ |
| 642 |
public function validate_payment() { |
| 643 |
$cart = LP()->cart; |
| 644 |
$validate = true; |
| 645 |
|
| 646 |
if ( $cart->needs_payment() ) { |
| 647 |
if ( ! $this->payment_method instanceof LP_Gateway_Abstract ) { |
| 648 |
$available_gateways = LP_Gateways::instance()->get_available_payment_gateways(); |
| 649 |
|
| 650 |
if ( ! isset( $available_gateways[ $this->payment_method ] ) ) { |
| 651 |
$this->payment_method = ''; |
| 652 |
throw new Exception( __( 'No payment method is selected', 'learnpress' ), LP_ERROR_NO_PAYMENT_METHOD_SELECTED ); |
| 653 |
} else { |
| 654 |
$this->payment_method = $available_gateways[ $this->payment_method ]; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
if ( $this->payment_method ) { |
| 659 |
$validate = $this->payment_method->validate_fields(); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
return $validate; |
| 664 |
} |
| 665 |
|
| 666 |
/** |
| 667 |
* Process checkout. |
| 668 |
* |
| 669 |
* @throws Exception |
| 670 |
*/ |
| 671 |
public function process_checkout() { |
| 672 |
$has_error = false; |
| 673 |
|
| 674 |
try { |
| 675 |
|
| 676 |
if ( function_exists( 'set_time_limit' ) ) { |
| 677 |
@set_time_limit( 0 ); // @codingStandardsIgnoreLine |
| 678 |
} |
| 679 |
|
| 680 |
do_action( 'learn-press/before-checkout' ); |
| 681 |
|
| 682 |
$cart = LP()->cart; |
| 683 |
$result = false; |
| 684 |
|
| 685 |
if ( $cart->is_empty() ) { |
| 686 |
throw new Exception( __( 'Your cart is currently empty.', 'learnpress' ) ); |
| 687 |
} |
| 688 |
|
| 689 |
$messages = array(); |
| 690 |
|
| 691 |
foreach ( $cart->get_items() as $item ) { |
| 692 |
$item_type = get_post_type( $item['item_id'] ); |
| 693 |
|
| 694 |
if ( ! in_array( $item_type, learn_press_get_item_types_can_purchase() ) ) { |
| 695 |
throw new Exception( __( 'Type item buy invalid!', 'learnpress' ) ); |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
if ( ! $this->validate_checkout_fields() ) { |
| 700 |
foreach ( $this->errors as $key => $error ) { |
| 701 |
if ( is_wp_error( $error ) ) { |
| 702 |
$error = $error->get_error_message(); |
| 703 |
} |
| 704 |
$messages[ $key ] = array( $error, 'error' ); |
| 705 |
} |
| 706 |
} else { |
| 707 |
// maybe throw new exception |
| 708 |
$this->validate_payment(); |
| 709 |
|
| 710 |
// Create order. |
| 711 |
$order_id = $this->create_order(); |
| 712 |
|
| 713 |
if ( is_wp_error( $order_id ) ) { |
| 714 |
throw new Exception( $order_id->get_error_message() ); |
| 715 |
} |
| 716 |
|
| 717 |
// allow Third-party hook: send email and more... |
| 718 |
do_action( 'learn-press/checkout-order-processed', $order_id, $this ); |
| 719 |
|
| 720 |
if ( $this->payment_method ) { |
| 721 |
// Store the order is waiting f6or payment and each payment method should clear it |
| 722 |
LP()->session->order_awaiting_payment = $order_id; |
| 723 |
// Process Payment |
| 724 |
$result = $this->payment_method->process_payment( $order_id ); |
| 725 |
|
| 726 |
if ( isset( $result['result'] ) ) { |
| 727 |
if ( 'success' === $result['result'] ) { |
| 728 |
$result = apply_filters( 'learn-press/payment-successful-result', $result, $order_id ); |
| 729 |
} |
| 730 |
|
| 731 |
if ( learn_press_is_ajax() ) { |
| 732 |
learn_press_send_json( $result ); |
| 733 |
} else { |
| 734 |
wp_redirect( $result['redirect'] ); |
| 735 |
exit; |
| 736 |
} |
| 737 |
} |
| 738 |
} else { |
| 739 |
// ensure that no order is waiting for payment |
| 740 |
$order = new LP_Order( $order_id ); |
| 741 |
|
| 742 |
if ( $order && $order->payment_complete() ) { |
| 743 |
|
| 744 |
$is_guest_checkout = $this->guest_email ? true : false; |
| 745 |
$redirect = $order->get_checkout_order_received_url(); |
| 746 |
|
| 747 |
if ( ! $is_guest_checkout ) { |
| 748 |
$course_id = get_transient( 'checkout_enroll_course_id' ); |
| 749 |
|
| 750 |
if ( ! $course_id ) { |
| 751 |
if ( ! empty( $_REQUEST['enroll-course'] ) ) { |
| 752 |
$course_id = absint( $_REQUEST['enroll-course'] ); |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
if ( $course_id ) { |
| 757 |
delete_transient( 'checkout_enroll_course_id' ); |
| 758 |
$redirect = get_the_permalink( $course_id ); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
$result = array( |
| 763 |
'result' => 'success', |
| 764 |
'redirect' => $redirect, |
| 765 |
); |
| 766 |
|
| 767 |
if ( learn_press_is_ajax() ) { |
| 768 |
learn_press_send_json( $result ); |
| 769 |
} else { |
| 770 |
wp_redirect( $result['redirect'] ); |
| 771 |
exit; |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
} catch ( Exception $e ) { |
| 777 |
$has_error = $e->getMessage(); |
| 778 |
$messages[] = array( $has_error, 'error' ); |
| 779 |
} |
| 780 |
|
| 781 |
$is_error = sizeof( $messages ); |
| 782 |
|
| 783 |
$result = apply_filters( |
| 784 |
'learn-press/checkout-error', |
| 785 |
array( |
| 786 |
'result' => ! $is_error ? 'success' : 'fail', |
| 787 |
'messages' => $messages, |
| 788 |
) |
| 789 |
); |
| 790 |
|
| 791 |
learn_press_maybe_send_json( $result, 'learn_press_remove_messages' ); |
| 792 |
} |
| 793 |
|
| 794 |
/** |
| 795 |
* Get unique instance for this object |
| 796 |
* |
| 797 |
* @return LP_Checkout |
| 798 |
*/ |
| 799 |
public static function instance() { |
| 800 |
if ( empty( self::$_instance ) ) { |
| 801 |
self::$_instance = new LP_Checkout(); |
| 802 |
} |
| 803 |
|
| 804 |
return self::$_instance; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
|