/** * Get payment info table. * * @param WP_Post $object * @param array $args * @return string */ public static function payment_info($object, $args = array()) { // Get price. if (!isset($args['price'])) { if ('ib_educator_course' == $object->post_type) { $args['price'] = ib_edu_get_course_price($object->ID); } elseif ('ib_edu_membership' == $object->post_type) { $args['price'] = IB_Educator_Memberships::get_instance()->get_price($object->ID); } } // Get tax data. $tax_enabled = ib_edu_get_option('enable', 'taxes'); if ($tax_enabled) { $edu_tax = IB_Educator_Tax::get_instance(); $tax_data = $edu_tax->calculate_tax($edu_tax->get_tax_class_for($object->ID), $args['price'], $args['country'], $args['state']); } else { $tax_data = array('taxes' => array(), 'subtotal' => $args['price'], 'tax' => 0.0, 'total' => $args['price']); } // Items list. $output = '<table class="edu-payment-table">'; $output .= '<thead><tr><th>' . __('Item', 'ibeducator') . '</th><th>' . __('Price', 'ibeducator') . '</th></tr></thead>'; if ('ib_educator_course' == $object->post_type) { $output .= '<tbody><tr><td>'; if (ib_edu_get_option('payment_lecturer', 'settings')) { $output .= sprintf(__('%s with %s', 'ibeducator'), '<a href="' . esc_url(get_permalink($object->ID)) . '" target="_blank">' . esc_html($object->post_title) . '</a>', esc_html(get_the_author_meta('display_name', $object->post_author))); } else { $output .= '<a href="' . esc_url(get_permalink($object->ID)) . '" target="_blank">' . esc_html($object->post_title) . '</a>'; } $output .= '<input type="hidden" id="payment-object-id" name="course_id" value="' . intval($object->ID) . '"></td>'; $output .= '<td>' . ib_edu_format_price($tax_data['subtotal'], false) . '</td></tr></tbody>'; } elseif ('ib_edu_membership' == $object->post_type) { $output .= '<tbody><tr><td>' . esc_html($object->post_title); $output .= '<input type="hidden" id="payment-object-id" name="membership_id" value="' . intval($object->ID) . '"></td>'; $ms = IB_Educator_Memberships::get_instance(); $membership_meta = $ms->get_membership_meta($object->ID); $output .= '<td>' . $ms->format_price($tax_data['subtotal'], $membership_meta['duration'], $membership_meta['period']) . '</td></tr></tbody>'; } $output .= '</table>'; // Summary. $output .= '<dl class="edu-payment-summary edu-dl">'; if ($tax_data['tax'] > 0.0) { $output .= '<dt class="payment-subtotal">' . __('Subtotal', 'ibeducator') . '</dt><dd>' . ib_edu_format_price($tax_data['subtotal'], false) . '</dd>'; foreach ($tax_data['taxes'] as $tax) { $output .= '<dt class="payment-tax">' . esc_html($tax->name) . '</dt><dd>' . ib_edu_format_price($tax->amount, false) . '</dd>'; } } $output .= '<dt class="payment-total">' . __('Total', 'ibeducator') . '</dt><dd>' . ib_edu_format_price($tax_data['total'], false) . '</dd>'; $output .= '</dl>'; return $output; }
/** * 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; }
<?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();
/** * Render tax rates app. */ public function render_tax_classes() { $countries = IB_Educator_Countries::get_instance()->get_countries(); ?> <div id="edu-tax-classes-container"></div> <!-- TEMPLATE: TaxClassView --> <script id="edu-tax-class" type="text/html"> <td><%= description %></td> <td> <button class="button edit-tax-class"><?php _e('Edit', 'ibeducator'); ?> </button> <button class="button edit-rates"><?php _e('Rates', 'ibeducator'); ?> </button> <button class="button delete-tax-class"><?php _e('Delete', 'ibeducator'); ?> </button> </td> </script> <!-- TEMPLATE: TaxClassesView --> <script id="edu-tax-classes" type="text/html"> <table class="edu-tax-classes-table edu-table"> <thead> <tr> <th><?php _e('Tax Class', 'ibeducator'); ?> </th> <th><?php _e('Options', 'ibeducator'); ?> </th> </tr> </thead> <tbody></tbody> </table> <p class="actions"> <button class="button add-new-class"><?php _e('Add New', 'ibeducator'); ?> </button> </p> </script> <!-- TEMPLATE: EditTaxClassView --> <script id="edu-edit-tax-class" type="text/html"> <h4 class="title-add-new"><?php _e('Add New Tax Rate', 'ibeducator'); ?> </h4> <h4 class="title-edit"><?php _e('Edit Tax Rate', 'ibeducator'); ?> </h4> <p> <label><?php _e('Short Name', 'ibeducator'); ?> </label> <input type="text" class="short-name" value="<%= name %>"> </p> <p> <label><?php _e('Description', 'ibeducator'); ?> </label> <input type="text" class="description" value="<%= description %>"> </p> <p> <button class="button button-primary save-tax-class"><?php _e('Save', 'ibeducator'); ?> </button> <button class="button cancel"><?php _e('Cancel', 'ibeducator'); ?> </button> </p> </script> <!-- TEMPLATE: view tax rate --> <script id="edu-tax-rate" type="text/html"> <td class="handle"><div class="ib-edu-sort-y dashicons dashicons-sort"></div></td> <td class="country"><%= country_name %></td> <td class="state"><%= state_name %></td> <td class="name"><%= name %></td> <td class="rate"><%= rate %></td> <td class="priority"><%= priority %></td> <td class="options"> <a class="edit-rate ib-edu-action" href="#"><?php _e('Edit', 'ibeducator'); ?> </a> <span>|</span> <a class="delete-rate ib-edu-action" href="#"><?php _e('Delete', 'ibeducator'); ?> </a> </td> </script> <!-- TEMPLATE: edit tax rate --> <script id="edu-tax-rate-edit" type="text/html"> <td class="handle"><div class="ib-edu-sort-y dashicons dashicons-sort"></div></td> <td class="country"> <select class="country"> <option value=""></option> <?php foreach ($countries as $code => $country) { echo '<option value="' . esc_attr($code) . '">' . esc_html($country) . '</option>'; } ?> </select> </td> <td class="state"></td> <td class="name"><input type="text" value="<%= name %>"></td> <td class="rate"><input type="number" value="<%= rate %>"></td> <td class="priority"><input type="number" value="<%= priority %>"></td> <td class="options"> <a class="save-rate ib-edu-action" href="#"><?php _e('Save', 'ibeducator'); ?> </a> <span>|</span> <a class="delete-rate ib-edu-action" href="#"><?php _e('Delete', 'ibeducator'); ?> </a> </td> </script> <!-- TEMPLATE: TaxRatesView --> <script id="edu-tax-rates" type="text/html"> <table class="edu-tax-rates-table edu-table"> <thead> <tr> <th></th> <th><?php _e('Country', 'ibeducator'); ?> </th> <th><?php _e('State', 'ibeducator'); ?> </th> <th><?php _e('Name', 'ibeducator'); ?> </th> <th><?php _e('Rate (%)', 'ibeducator'); ?> </th> <th><?php _e('Priority', 'ibeducator'); ?> </th> <th><?php _e('Options', 'ibeducator'); ?> </th> </tr> </thead> <tbody> <tr class="loading"> <td colspan="7"> <?php _e('Loading', 'ibeducator'); ?> </td> </tr> </tbody> <tfoot> <tr> <td colspan="7"> <button class="button button-primary add-new-rate"><?php _e('Add New', 'ibeducator'); ?> </button> <button class="button save-order" disabled="disabled"><?php _e('Save Order', 'ibeducator'); ?> </button> <button class="button cancel"><?php _e('Close', 'ibeducator'); ?> </button> </td> </tr> </tfoot> </table> </script> <script> var eduTaxAppNonce = <?php echo json_encode(wp_create_nonce('ib_educator_tax_rates')); ?> ; var eduGetStatesNonce = <?php echo json_encode(wp_create_nonce('ib_edu_get_states')); ?> ; var eduTaxClasses = <?php $json = '['; $classes = IB_Educator_Tax::get_instance()->get_tax_classes(); $i = 0; foreach ($classes as $name => $description) { if ($i > 0) { $json .= ','; } $json .= '{name:' . json_encode(esc_html($name)) . ',description:' . json_encode(esc_html($description)) . '}'; ++$i; } $json .= ']'; echo $json; ?> ; var eduTaxAppErrors = { name: <?php echo json_encode(__('The name is invalid.', 'ibeducator')); ?> , nameNotUnique: <?php echo json_encode(__('Tax class with this name exists.', 'ibeducator')); ?> , description: <?php echo json_encode(__('Description cannot be empty.', 'ibeducator')); ?> , ratesNotSaved: <?php echo json_encode(__('Rates could not be saved.', 'ibeducator')); ?> }; </script> <?php }