/** * Process payment. * * @return array */ public function process_payment($object_id, $user_id = 0, $payment_type = 'course', $atts = array()) { if (!$user_id) { $user_id = get_current_user_id(); } if (!$user_id) { return array('status' => '', 'redirect' => home_url('/')); } // Add payment. $payment = IB_Educator_Payment::get_instance(); $payment->user_id = $user_id; $payment->payment_type = $payment_type; $payment->payment_status = 'complete'; $payment->payment_gateway = $this->get_id(); $payment->amount = 0.0; $payment->currency = ib_edu_get_currency(); if ('course' == $payment_type) { $payment->course_id = $object_id; $payment->amount = ib_edu_get_course_price($object_id); } elseif ('membership' == $payment_type) { $payment->object_id = $object_id; $ms = IB_Educator_Memberships::get_instance(); $payment->amount = $ms->get_price($object_id); } if (!empty($atts['ip'])) { $payment->ip = $atts['ip']; } if (0.0 == $payment->amount) { $payment->save(); if ($payment->ID) { if ('course' == $payment->payment_type) { // Setup course entry. $entry = IB_Educator_Entry::get_instance(); $entry->course_id = $object_id; $entry->user_id = $user_id; $entry->payment_id = $payment->ID; $entry->entry_status = 'inprogress'; $entry->entry_date = date('Y-m-d H:i:s'); $entry->save(); } elseif ('membership' == $payment->payment_type) { // Setup membership. $ms->setup_membership($user_id, $object_id); } } } return array('status' => 'complete', 'redirect' => get_permalink($object_id), 'payment' => $payment); }
/** * Output the Stripe's payment dialog. * Step 2 in the payment process. */ public function pay_page() { $payment_id = absint(get_query_var('edu-pay')); if (!$payment_id) { return; } $user = wp_get_current_user(); if (0 == $user->ID) { return; } $payment = edr_get_payment($payment_id); if (!$payment->ID || $user->ID != $payment->user_id) { // The payment must exist and it must be associated with the current user. return; } if ('course' == $payment->payment_type) { $post = get_post($payment->course_id); } elseif ('membership' == $payment->payment_type) { $post = get_post($payment->object_id); } if (!$post) { return; } ?> <p id="ib-edu-payment-processing-msg"> <?php _e('The payment is getting processed...', 'ibeducator'); ?> </p> <script src="https://checkout.stripe.com/checkout.js"></script> <script> (function($) { var handler = StripeCheckout.configure({ key: <?php echo json_encode($this->get_option('publishable_key')); ?> , image: '', email: <?php echo json_encode($user->user_email); ?> , token: function(token) { $.ajax({ type: 'POST', cache: false, url: <?php echo json_encode(ib_edu_request_url('stripe_token')); ?> , data: { payment_id: <?php echo intval($payment->ID); ?> , token: token.id, _wpnonce: <?php echo json_encode(wp_create_nonce('ib_educator_stripe_token')); ?> }, success: function(response) { if (response === '1') { $('#ib-edu-payment-processing-msg').text(<?php echo json_encode(__('Redirecting to the payment summary page...', 'ibeducator')); ?> ); var redirectTo = <?php echo json_encode(ib_edu_get_endpoint_url('edu-thankyou', $payment->ID, get_permalink(ib_edu_page_id('payment')))); ?> ; document.location = redirectTo; } } }); } }); handler.open({ name: <?php echo json_encode(esc_html($post->post_title)); ?> , description: <?php echo json_encode(ib_edu_format_price($payment->amount, false, false)); ?> , currency: <?php echo json_encode(ib_edu_get_currency()); ?> , amount: <?php echo absint($payment->amount * 100); ?> }); $(window).on('popstate', function() { handler.close(); }); })(jQuery); </script> <?php }
</div> <!-- Currency --> <div class="ib-edu-field"> <div class="ib-edu-label"><label for="ib-edu-currency"><?php _e('Currency', 'ibeducator'); ?> </label></div> <div class="ib-edu-control"> <select id="ib-edu-currency" name="currency"> <option value=""><?php _e('Select Currency', 'ibeducator'); ?> </option> <?php $current_currency = empty($payment->currency) ? ib_edu_get_currency() : $payment->currency; $currencies = ib_edu_get_currencies(); foreach ($currencies as $key => $value) { $selected = $key == $current_currency ? ' selected="selected"' : ''; echo '<option value="' . esc_attr($key) . '"' . $selected . '>' . esc_html($value) . '</option>'; } ?> </select> </div> </div> </div> </div> <div id="payment-billing" class="postbox"> <div class="handlediv"><br></div> <h3 class="hndle"><span><?php
/** * Format price. * * @param float $price * @return string */ function ib_edu_format_price($price, $apply_filters = true, $symbol = true) { $settings = ib_edu_get_settings(); $currency = ib_edu_get_currency(); $decimal_point = !empty($settings['decimal_point']) ? esc_html($settings['decimal_point']) : '.'; $thousands_sep = !empty($settings['thousands_sep']) ? esc_html($settings['thousands_sep']) : ','; $formatted = number_format($price, 2, $decimal_point, $thousands_sep); $formatted = ib_edu_strip_zeroes($formatted, $decimal_point); if ($symbol) { $currency_symbol = ib_edu_get_currency_symbol($currency); } else { $currency_symbol = preg_replace('/[^a-z]+/i', '', $currency); } if (isset($settings['currency_position']) && 'after' == $settings['currency_position']) { $formatted = "{$formatted} {$currency_symbol}"; } else { $formatted = "{$currency_symbol} {$formatted}"; } if ($apply_filters) { return apply_filters('ib_educator_format_price', $formatted, $currency, $price); } return $formatted; }
<?php // Setup form object. require_once IBEDUCATOR_PLUGIN_DIR . 'includes/ib-educator-form.php'; $form = new IB_Educator_Form(); $form->default_decorators(); // Registration. $form->set_value('_ib_educator_register', get_post_meta($post->ID, '_ib_educator_register', true)); $form->add(array('type' => 'select', 'name' => '_ib_educator_register', 'label' => __('Registration', 'ibeducator'), 'options' => array('open' => __('Open', 'ibeducator'), 'closed' => __('Closed', 'ibeducator')), 'default' => 'open')); // Price. $form->set_value('_ibedu_price', ib_edu_get_course_price($post->ID)); $form->add(array('type' => 'text', 'name' => '_ibedu_price', 'class' => '', 'id' => 'ib-educator-price', 'label' => __('Price', 'ibeducator'), 'before' => esc_html(ib_edu_get_currency_symbol(ib_edu_get_currency())) . ' ')); // Tax Class. $edu_tax = IB_Educator_Tax::get_instance(); $form->set_value('_ib_educator_tax_class', $edu_tax->get_tax_class_for($post->ID)); $form->add(array('type' => 'select', 'name' => '_ib_educator_tax_class', 'label' => __('Tax Class', 'ibeducator'), 'options' => $edu_tax->get_tax_classes(), 'default' => 'default')); // Difficulty. $form->set_value('_ib_educator_difficulty', get_post_meta($post->ID, '_ib_educator_difficulty', true)); $form->add(array('type' => 'select', 'name' => '_ib_educator_difficulty', 'id' => 'ib-educator-difficulty', 'label' => __('Difficulty', 'ibeducator'), 'options' => array_merge(array('' => __('None', 'ibeducator')), ib_edu_get_difficulty_levels()))); // Prerequisite. $courses = array('' => __('None', 'ibeducator')); $tmp = get_posts(array('post_type' => 'ib_educator_course', 'post_status' => 'publish', 'posts_per_page' => -1)); foreach ($tmp as $course) { $courses[$course->ID] = $course->post_title; } $prerequisites = IB_Educator::get_instance()->get_prerequisites($post->ID); $form->set_value('_ib_educator_prerequisite', array_pop($prerequisites)); $form->add(array('type' => 'select', 'name' => '_ib_educator_prerequisite', 'id' => 'ib-educator-prerequisite', 'label' => __('Prerequisite', 'ibeducator'), 'options' => $courses)); wp_nonce_field('ib_educator_course_meta_box', 'ib_educator_course_meta_box_nonce'); $form->display();
/** * Output the form to the step 2 (pay page) of the payment page. */ public function pay_page() { $action_url = $this->get_option('test') ? $this->test_url : $this->live_url; $payment_id = absint(get_query_var('edu-pay')); if (!$payment_id) { return; } $user_id = get_current_user_id(); if (!$user_id) { return; } $payment = edr_get_payment($payment_id); // The payment must exist in the database // and it must belong to the current user. if (!$payment->ID || $user_id != $payment->user_id) { return; } if ('course' == $payment->payment_type) { $post = get_post($payment->course_id); } elseif ('membership' == $payment->payment_type) { $post = get_post($payment->object_id); } if (!$post) { return; } $amount = $payment->amount - $payment->tax; $return_url = ''; $payment_page_id = ib_edu_page_id('payment'); if ($payment_page_id) { $return_url = ib_edu_get_endpoint_url('edu-thankyou', $payment->ID ? $payment->ID : '', get_permalink($payment_page_id)); } echo '<form id="ib-edu-paypal-form" action="' . esc_url($action_url) . '" method="post">'; echo '<input type="hidden" name="cmd" value="_xclick">'; echo '<input type="hidden" name="charset" value="utf-8">'; echo '<input type="hidden" name="business" value="' . esc_attr($this->get_option('business_email')) . '">'; echo '<input type="hidden" name="return" value="' . esc_url($return_url) . '">'; echo '<input type="hidden" name="notify_url" value="' . esc_url(ib_edu_request_url('paypalipn')) . '">'; echo '<input type="hidden" name="currency_code" value="' . esc_attr(ib_edu_get_currency()) . '">'; echo '<input type="hidden" name="item_name" value="' . esc_attr($post->post_title) . '">'; echo '<input type="hidden" name="item_number" value="' . absint($payment->ID) . '">'; if ($payment->tax) { echo '<input type="hidden" name="tax" value="' . (double) $payment->tax . '">'; } echo '<input type="hidden" name="amount" value="' . (double) $amount . '">'; echo '<div id="paypal-form-buttons"><button type="submit">' . __('Continue', 'ibeducator') . '</button></div>'; echo '</form>'; echo '<div id="paypal-redirect-notice" style="display: none;">' . __('Redirecting to PayPal...', 'ibeducator') . '</div>'; echo '<script>(function() { function goToPayPal() { document.getElementById("paypal-form-buttons").style.display = "none"; document.getElementById("paypal-redirect-notice").style.display = "block"; document.getElementById("ib-edu-paypal-form").submit(); } if ( typeof jQuery === "undefined" ) { setTimeout(goToPayPal, 500); } else { jQuery(document).on("ready", function() { goToPayPal(); }); } })();</script>'; }
/** * Create payment. * * @param int $object_id ID of the object the payment is to be associated with. * @param int $user_id * @param string $payment_type * @return IB_Educator_Payment */ public function create_payment($object_id, $user_id, $payment_type, $atts = array()) { $payment = IB_Educator_Payment::get_instance(); $payment->user_id = $user_id; $payment->payment_type = $payment_type; $payment->payment_status = 'pending'; $payment->payment_gateway = $this->get_id(); $payment->currency = ib_edu_get_currency(); if ('course' == $payment_type) { $payment->course_id = $object_id; $payment->amount = ib_edu_get_course_price($object_id); } elseif ('membership' == $payment_type) { $payment->object_id = $object_id; $payment->amount = IB_Educator_Memberships::get_instance()->get_price($object_id); } $tax_data = null; if (ib_edu_collect_billing_data($object_id)) { // Save billing data. $billing = get_user_meta($user_id, '_ib_educator_billing', true); if (!is_array($billing)) { $billing = array(); } $payment->first_name = get_user_meta($user_id, 'first_name', true); $payment->last_name = get_user_meta($user_id, 'last_name', true); $payment->address = isset($billing['address']) ? $billing['address'] : ''; $payment->address_2 = isset($billing['address_2']) ? $billing['address_2'] : ''; $payment->city = isset($billing['city']) ? $billing['city'] : ''; $payment->state = isset($billing['state']) ? $billing['state'] : ''; $payment->postcode = isset($billing['postcode']) ? $billing['postcode'] : ''; $payment->country = isset($billing['country']) ? $billing['country'] : ''; // Calculate tax. $edu_tax = IB_Educator_Tax::get_instance(); $tax_data = $edu_tax->calculate_tax($edu_tax->get_tax_class_for($object_id), $payment->amount, $payment->country, $payment->state); $payment->tax = $tax_data['tax']; $payment->amount = $tax_data['total']; } if (!empty($atts['ip'])) { $payment->ip = $atts['ip']; } $payment->save(); // Save tax data. if ($tax_data) { foreach ($tax_data['taxes'] as $tax) { $payment->update_line(array('object_id' => $tax->ID, 'line_type' => 'tax', 'amount' => $tax->amount, 'name' => $tax->name)); } } return $payment; }