/** * Edit payment gateway action. */ public static function edit_payment_gateway() { if (!isset($_POST['gateway_id'])) { return; } $gateway_id = sanitize_title($_POST['gateway_id']); // Verify nonce. check_admin_referer('ib_educator_payments_settings'); // Get available gateways. $gateways = IB_Educator_Main::get_gateways(); // Does the requested gateway exist? if (!isset($gateways[$gateway_id])) { return; } // Capability check. if (!current_user_can('manage_educator')) { return; } $saved = $gateways[$gateway_id]->save_admin_options(); $message = ''; if (true === $saved) { $message = 'saved'; } else { $message = 'not_saved'; } wp_redirect(admin_url('admin.php?page=ib_educator_admin&tab=payment&gateway_id=' . $gateway_id . '&edu-message=' . $message)); }
<!-- Payment Method --> <div class="ib-edu-field edu-block"> <div class="ib-edu-label"> <label for="ib-edu-amount"><?php _e('Payment Method', 'ibeducator'); ?> </label> </div> <div class="ib-edu-control"> <select name="payment_gateway"> <option value="">— <?php _e('Select', 'ibeducator'); ?> —</option> <?php $gateways = IB_Educator_Main::get_gateways(); foreach ($gateways as $gateway) { echo '<option value="' . esc_attr($gateway->get_id()) . '" ' . selected($payment->payment_gateway, $gateway->get_id()) . '>' . esc_html($gateway->get_title()) . '</option>'; } ?> </select> </div> </div> <!-- Transaction ID --> <div class="ib-edu-field edu-block"> <div class="ib-edu-label"> <label for="ib-edu-txn_id"><?php _e('Transaction ID', 'ibeducator'); ?> </label>
/** * Pay for a course. */ public static function payment() { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'ibedu_submit_payment')) { return; } do_action('ib_educator_before_payment'); // Get post id and payment type (course or membership). $post_id = 0; // either course id or membership id $payment_type = 'course'; if (isset($_POST['course_id'])) { $post_id = absint($_POST['course_id']); } elseif (isset($_POST['membership_id'])) { $post_id = absint($_POST['membership_id']); $payment_type = 'membership'; } $post = get_post($post_id); if (!$post) { return; } $user_id = get_current_user_id(); $errors = new WP_Error(); // Check the course prerequisites. if ('course' == $payment_type) { // Registration allowed? if ('closed' == ib_edu_registration($post_id)) { return; } // Check prerequisites. $api = IB_Educator::get_instance(); if (!$api->check_prerequisites($post_id, $user_id)) { $prerequisites_html = ''; $prerequisites = $api->get_prerequisites($post_id); $courses = get_posts(array('post_type' => 'ib_educator_course', 'post_status' => 'publish', 'include' => $prerequisites)); if (!empty($courses)) { foreach ($courses as $course) { $prerequisites_html .= '<br><a href="' . esc_url(get_permalink($course->ID)) . '">' . esc_html($course->post_title) . '</a>'; } } $errors->add('prerequisites', sprintf(__('You have to complete the prerequisites for this course: %s', 'ibeducator'), $prerequisites_html)); ib_edu_message('payment_errors', $errors); return; } } // Get the payment method. $payment_method = ''; $gateways = IB_Educator_Main::get_gateways(); if (!isset($_POST['payment_method']) || !array_key_exists($_POST['payment_method'], $gateways)) { $errors->add('empty_payment_method', __('Please select a payment method.', 'ibeducator')); } else { $payment_method = $_POST['payment_method']; } /** * Filter the validation of the payment form. * * @param WP_Error $errors */ $errors = apply_filters('ib_educator_register_form_validate', $errors, $post); // Attempt to register the user. if ($errors->get_error_code()) { ib_edu_message('payment_errors', $errors); return; } elseif (!$user_id) { $user_data = apply_filters('ib_educator_register_user_data', array('role' => 'student'), $post); $user_id = wp_insert_user($user_data); if (is_wp_error($user_id)) { ib_edu_message('payment_errors', $user_id); return; } else { // Setup the password change nag. update_user_option($user_id, 'default_password_nag', true, true); // Send the new user notifications. wp_new_user_notification($user_id, $user_data['user_pass']); do_action('ib_educator_new_student', $user_id, $post); // Log the user in. wp_set_auth_cookie($user_id); } } else { do_action('ib_educator_update_student', $user_id, $post); } $can_pay = true; if ('course' == $payment_type) { $access_status = IB_Educator::get_instance()->get_access_status($post_id, $user_id); // Student can pay for a course only if he/she completed this course or didn't register for it yet. $can_pay = in_array($access_status, array('course_complete', 'forbidden')); } if ($can_pay) { // Process payment. $atts = array(); if (ib_edu_get_option('payment_ip', 'settings')) { $atts['ip'] = $_SERVER['REMOTE_ADDR']; } $result = $gateways[$payment_method]->process_payment($post_id, $user_id, $payment_type, $atts); /** * Fires when the payment record has been created. * * The payment may not be confirmed yet. * * @param null|IB_Educator_Payment */ do_action('ib_educator_payment_processed', isset($result['payment']) ? $result['payment'] : null); // Go to the next step(e.g. thank you page). wp_safe_redirect($result['redirect']); exit; } }