/**
  * Ensure that only one instance of LP_Gateways is loadded
  * @return LP_Gateways|null
  */
 static function instance()
 {
     if (is_null(self::$_instance)) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 /**
  * @return mixed
  */
 function get_sections()
 {
     $gateways = LP_Gateways::instance()->get_gateways();
     $sections = array();
     if ($gateways) {
         foreach ($gateways as $id => $gateway) {
             $sections[$id] = !empty($gateway->method_title) ? $gateway->method_title : ucfirst($gateway->id);
         }
     }
     return $sections;
 }
 /**
  * @return mixed
  */
 function get_sections()
 {
     $gateways = LP_Gateways::instance()->get_gateways();
     $sections = array();
     if ($gateways) {
         foreach ($gateways as $id => $gateway) {
             $sections[$id] = array('id' => $id, 'title' => !empty($gateway->method_title) ? $gateway->method_title : ucfirst($gateway->id), 'description' => !empty($gateway->method_description) ? $gateway->method_description : '');
         }
     }
     return $sections;
 }
function learn_press_handle_purchase_request()
{
    LP_Gateways::instance()->get_available_payment_gateways();
    $method_var = 'learn-press-transaction-method';
    $requested_transaction_method = empty($_REQUEST[$method_var]) ? false : $_REQUEST[$method_var];
    learn_press_do_transaction($requested_transaction_method);
}
Example #5
0
<?php

_deprecated_function(__FILE__, '1.0');
?>
<form id="learn_press_payment_form" name="learn_press_payment_form" method="post">
	<?php 
if (!learn_press_is_free_course()) {
    $gateways = LP_Gateways::instance()->get_available_payment_gateways();
    ?>
		<ul id="lpr-payment-tab" class="nav nav-tabs" role="tablist">
			<?php 
    do_action('learn_press_before_payment_loop');
    $i = 0;
    foreach ($gateways as $slug => $gateway) {
        $gateway = apply_filters('learn_press_print_payment_' . $slug, $gateway);
        if (!$gateway) {
            continue;
        }
        ?>
				<li>
					<label><input type="radio" name="payment_method" value="<?php 
        echo $slug;
        ?>
" /> <?php 
        echo $gateway->get_title();
        ?>
					</label>

					<div class="learn_press_payment_form" id="learn_press_payment_form_<?php 
        echo $slug;
        ?>
 /**
  * Process checkout
  *
  * @throws Exception
  */
 function process_checkout()
 {
     try {
         if (strtolower($_SERVER['REQUEST_METHOD']) != 'post') {
             return;
         }
         // Prevent timeout
         @set_time_limit(0);
         do_action('learn_press_before_checkout_process');
         $success = true;
         if (LP()->cart->is_empty()) {
             learn_press_send_json(array('result' => 'success', 'redirect' => learn_press_get_page_link('checkout')));
         }
         if (empty($_REQUEST['payment_method'])) {
             $success = false;
             learn_press_add_notice(__('Please select a payment method', 'learn_press'), 'error');
         } else {
             $this->payment_method = $_REQUEST['payment_method'];
             if ($this->checkout_fields) {
                 foreach ($this->checkout_fields as $name => $field) {
                     if (!apply_filters('learn_press_checkout_validate_field', true, array('name' => $name, 'text' => $field), $this)) {
                         $success = false;
                     }
                 }
             }
             if (isset($this->checkout_fields['user_login']) && isset($this->checkout_fields['user_password'])) {
                 $creds = array();
                 $creds['user_login'] = $_POST['user_login'];
                 $creds['user_password'] = $_POST['user_password'];
                 $creds['remember'] = true;
                 $user = wp_signon($creds, false);
                 if (is_wp_error($user)) {
                     learn_press_add_notice($user->get_error_message(), 'error');
                     $success = false;
                 }
             }
             LP()->session->set('chosen_payment_method', $this->payment_method);
         }
         if ($success && LP()->cart->needs_payment()) {
             // Payment Method
             $available_gateways = LP_Gateways::instance()->get_available_payment_gateways();
             if (!isset($available_gateways[$this->payment_method])) {
                 $this->payment_method = '';
                 learn_press_add_notice(__('Invalid payment method.', 'learn_press'), 'error');
             } else {
                 $this->payment_method = $available_gateways[$this->payment_method];
                 $success = $this->payment_method->validate_fields();
             }
         } else {
             $available_gateways = array();
         }
         $order_id = $this->create_order();
         if ($success && $this->payment_method && $order_id) {
             LP()->session->order_awaiting_payment = $order_id;
             // Process Payment
             $result = $this->payment_method->process_payment($order_id);
             $success = !empty($result['result']) ? $result['result'] == 'success' : false;
             // Redirect to success/confirmation/payment page
             if ($success) {
                 $result = apply_filters('learn_press_payment_successful_result', $result, $order_id);
                 //LP()->cart->empty_cart();
                 if (is_ajax()) {
                     learn_press_send_json($result);
                 } else {
                     wp_redirect($result['redirect']);
                     exit;
                 }
             }
         }
     } catch (Exception $e) {
         if (!empty($e)) {
             learn_press_add_notice($e->getMessage(), 'error');
         }
         $success = false;
     }
     $error_messages = '';
     if (!$success) {
         ob_start();
         learn_press_print_notices();
         $error_messages = ob_get_clean();
     }
     $result = array('result' => $success ? 'success' : 'fail', 'messages' => $error_messages, 'redirect' => '');
     return $result;
 }
 /**
  * Output payment methods
  *
  * @param LP_Checkout object
  */
 function learn_press_order_payment($checkout)
 {
     $available_gateways = LP_Gateways::instance()->get_available_payment_gateways();
     learn_press_get_template('checkout/payment.php', array('available_gateways' => $available_gateways));
 }
Example #8
0
 /**
  * Init LearnPress when WP initialises
  */
 function init()
 {
     if ($this->is_request('frontend')) {
         $this->cart = LP_Cart::instance();
     }
     $this->get_session();
     $this->get_user();
     $this->gateways = LP_Gateways::instance()->get_available_payment_gateways();
     //$this->question_factory = LP_Question_Factory::instance();
     LP_Emails::init_email_notifications();
 }
Example #9
0
 /**
  * Init LearnPress when WP initialises
  */
 function init()
 {
     if ($this->is_request('frontend')) {
         $this->cart = LP_Cart::instance();
     }
     $this->get_session();
     $this->get_user();
     $this->gateways = LP_Gateways::instance()->get_available_payment_gateways();
     //$this->question_factory = LP_Question_Factory::instance();
     LP_Emails::init_email_notifications();
     if (get_transient('learn_press_install') == 'yes') {
         flush_rewrite_rules();
         delete_transient('learn_press_install');
     }
 }
 static function do_ajax($var)
 {
     if (!defined('DOING_AJAX')) {
         define('DOING_AJAX', true);
     }
     LP_Gateways::instance()->get_available_payment_gateways();
     $result = false;
     switch ($var) {
         case 'checkout':
             $result = LP()->checkout->process_checkout();
             break;
         case 'enroll-course':
             $course_id = learn_press_get_request('enroll-course');
             if (!$course_id) {
                 throw new Exception(__('Invalid course', 'learn_press'));
             }
             $insert_id = LP()->user->enroll($course_id);
             $response = array('result' => 'fail', 'redirect' => apply_filters('learn_press_enroll_course_failure_redirect_url', get_the_permalink($course_id)));
             if ($insert_id) {
                 $response['result'] = 'success';
                 $response['redirect'] = apply_filters('learn_press_enrolled_course_redirect_url', get_the_permalink($course_id));
                 $message = apply_filters('learn_press_enrolled_course_message', sprintf(__('Congrats! You have enrolled <strong>%s</strong>', 'learn_press'), get_the_title($course_id)), $course_id, LP()->user->id);
                 learn_press_add_notice($message);
             } else {
                 $message = apply_filters('learn_press_enroll_course_failed_message', sprintf(__('Sorry! The course <strong>%s</strong> you want to enroll has failed! Please contact site\'s administrator for more information.', 'learn_press'), get_the_title($course_id)), $course_id, LP()->user->id);
                 learn_press_add_notice($message, 'error');
             }
             if (is_ajax()) {
                 learn_press_send_json($response);
             }
             if ($response['redirect']) {
                 wp_redirect($response['redirect']);
             }
             break;
         case 'checkout-login':
             $result = array('result' => 'success');
             ob_start();
             if (empty($_REQUEST['user_login'])) {
                 $result['result'] = 'fail';
                 learn_press_add_notice(__('Please enter username', 'learn_press'), 'error');
             }
             if (empty($_REQUEST['user_password'])) {
                 $result['result'] = 'fail';
                 learn_press_add_notice(__('Please enter password', 'learn_press'), 'error');
             }
             if ($result['result'] == 'success') {
                 $creds = array();
                 $creds['user_login'] = $_REQUEST['user_login'];
                 $creds['user_password'] = $_REQUEST['user_password'];
                 $creds['remember'] = true;
                 $user = wp_signon($creds, false);
                 if (is_wp_error($user)) {
                     $result['result'] = 'fail';
                     learn_press_add_notice($user->get_error_message(), 'error');
                 } else {
                     $result['redirect'] = learn_press_get_page_link('checkout');
                 }
             }
             learn_press_print_notices();
             $messages = ob_get_clean();
             if ($result['result'] == 'fail') {
                 $result['messages'] = $messages;
             }
             break;
         case 'add-to-cart':
             $result = LP()->cart->add_to_cart(learn_press_get_request('add-course-to-cart'));
     }
     learn_press_send_json($result);
 }