Example #1
0
 /**
  * Get insntance of this class (singleton).
  *
  * @return Edr_TaxManager
  */
 public static function get_instance()
 {
     if (!self::$instance) {
         self::$instance = new self();
     }
     return self::$instance;
 }
Example #2
0
<?php

$ms = Edr_Memberships::get_instance();
// Get membership meta.
$meta = $ms->get_membership_meta($post->ID);
// Get membership periods.
$membership_periods = $ms->get_periods();
// Setup form object.
$form = new Edr_Form();
$form->default_decorators();
// Price.
$form->set_value('_ib_educator_price', $meta['price']);
$form->add(array('type' => 'text', 'name' => '_ib_educator_price', 'id' => 'ib-educator-price', 'label' => __('Price', 'ibeducator'), 'before' => esc_html(ib_edu_get_currency_symbol(ib_edu_get_currency())) . ' '));
// Tax Class.
$edu_tax = Edr_TaxManager::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'));
// Duration.
$period = '<select name="_ib_educator_period">';
foreach ($membership_periods as $mp_value => $mp_name) {
    $period .= '<option value="' . esc_attr($mp_value) . '"' . selected($meta['period'], $mp_value, false) . '>' . esc_html($mp_name) . '</option>';
}
$period .= '</select>';
$form->set_value('_ib_educator_duration', $meta['duration']);
$form->add(array('type' => 'text', 'name' => '_ib_educator_duration', 'id' => 'ib-educator-duration', 'class' => 'small-text', 'label' => __('Duration', 'ibeducator'), 'after' => " {$period}"));
// Categories.
$categories = array('' => __('Select Categories', 'ibeducator'));
$terms = get_terms('ib_educator_category');
if ($terms && !is_wp_error($terms)) {
    foreach ($terms as $term) {
        $categories[$term->term_id] = $term->name;
Example #3
0
 /**
  * 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'] = Edr_Memberships::get_instance()->get_price($object->ID);
         }
     }
     // Get tax data.
     $tax_enabled = ib_edu_get_option('enable', 'taxes');
     if ($tax_enabled) {
         $edu_tax = Edr_TaxManager::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 = Edr_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;
 }
Example #4
0
 /**
  * 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 = edr_get_payment();
     $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 = Edr_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 = Edr_TaxManager::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;
 }
Example #5
0
    /**
     * Render tax rates app.
     */
    public function render_tax_classes()
    {
        $countries = Edr_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 = Edr_TaxManager::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 
    }