Last active
November 6, 2017 01:27
-
-
Save eighty20results/948ad9784d9d156c3503ad0c128797d9 to your computer and use it in GitHub Desktop.
Calculate GST for Australian billing addresses/residents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Paid Memberships Pro - Australian GST | |
* Plugin URI: https://eighty20results.com/paid-memberships-pro/do-it-for-me/ | |
* Description: Calculate and display GST for Australian customers at checkout | |
* Version: 1.0 | |
* Author: Paid Memberships Pro / Thomas Sjolshagen <thomas@eighty20results.com> | |
* Author URI: https://eighty20results.com/thomas-sjolshagen/ | |
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
* License: GPLv2 or later | |
* | |
* Copyright (c) 2017 - Eighty / 20 Results by Wicked Strong Chicks. | |
* ALL RIGHTS RESERVED | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
/** Credit: Jason @ Stranger Studios (Original Developer) */ | |
/* | |
Tax solution for Australia | |
This solution assume the GST tax rate of 10% from March 15th, 2017. Ask your accountant how much tax you must charge. | |
More info: https://www.business.gov.au/info/run/tax/register-for-goods-and-services-tax-gst | |
Edit as needed, then save this file in your plugins folder and activate it through the plugins page in the WP dashboard. | |
*/ | |
if ( ! defined( "PMPRO_AGST_TAX_RATE" ) ) { | |
define( "AGST_PCT_TAX_RATE", 10 ); // As of March 15th, 2017 | |
} | |
/** | |
* Calculate the tax multiplier (from the constant) | |
* | |
* @return float | |
*/ | |
function agst_get_tax_rate() { | |
return (float) ( intval( AGST_PCT_TAX_RATE ) / 100 ); | |
} | |
/** | |
* Calculate the tax amount as part of the pmpro_tax filter | |
* | |
* @filter: pmpro_tax | |
* | |
* @param float $tax | |
* @param array $values | |
* @param MemberOrder $order | |
* | |
* @return float | |
*/ | |
function agst_pmpro_tax( $tax, $values, $order ) { | |
$tax = round( (float) $values[ 'price' ] * agst_get_tax_rate(), 2 ); | |
return $tax; | |
} | |
add_filter( 'pmpro_tax', 'agst_pmpro_tax', 10, 3 ); | |
/** | |
* Generate the Text to show as the Level Cost w/ GST included | |
* | |
* @param string $level_cost_text | |
* @param stdClass $level | |
* | |
* @return string | |
*/ | |
function agst_pmpro_level_cost_text( $level_cost_text, $level ) { | |
global $pmpro_currencies; | |
global $pmpro_currency_symbol; | |
$initial_price_tax = 0.00; | |
$billing_amount_tax = 0.00; | |
$new_cost_text = ''; | |
if ( ! empty( $level->initial_payment )) { | |
$initial_price_tax = round( (float) $level->initial_payment * agst_get_tax_rate(), 2 ); | |
$initial_amount = number_format_i18n( ((float)($level->initial_payment) + $initial_price_tax),2 ); | |
$new_cost_text = sprintf( __( 'You will be billed %s %.2f immediately. ', 'pmpro-australia-gst' ), $pmpro_currency_symbol, $initial_amount ); | |
} | |
if ( ! empty( $level->billing_amount) ) { | |
$billing_amount_tax = round( (float) $level->billing_amount * agst_get_tax_rate(), 2 ); | |
$billing_amount = number_format_i18n( ((float)($level->billing_amount) + $billing_amount_tax),2 ); | |
$new_cost_text .= sprintf( __( 'Then %s %.2f every %d %s(s). ', 'pmpro-australia-gst' ), $pmpro_currency_symbol, $billing_amount, $level->cycle_number, $level->cycle_period ); | |
} | |
if ( !empty( $new_cost_text ) ) { | |
$new_cost_text .= sprintf( __( "(amount(s) include %d%% GST)", 'pmpro-australia-gst' ), AGST_PCT_TAX_RATE ); | |
return $new_cost_text; | |
} else { | |
return $level_cost_text; | |
} | |
} | |
add_filter( "pmpro_level_cost_text", "agst_pmpro_level_cost_text", 10, 2 ); | |
/** | |
* Set the default billing country to be Australia | |
* | |
* @param string $country - Country code (2 letters) | |
* | |
* @return string | |
*/ | |
function agst_pmpro_default_country( $country ) { | |
return 'AU'; | |
} | |
add_filter( 'pmpro_default_country', 'agst_pmpro_default_country' ); | |
/** | |
* Add a checkbox for residency to the checkout page | |
*/ | |
function agst_pmpro_checkout_boxes() { | |
// Should we include taxes? | |
$add_tax = ( isset( $_REQUEST['taxregion'] ) || isset( $_SESSION['taxregion'] ) ); | |
?> | |
<table id="pmpro_pricing_fields" class="pmpro_checkout" width="100%" cellpadding="0" cellspacing="0" border="0"> | |
<thead> | |
<tr> | |
<th> | |
<?php _e( 'Australian Resident', 'pmpro-australia-gst' ); ?> | |
</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td> | |
<div> | |
<input id="taxregion" name="taxregion" type="checkbox" value="1" <?php checked( $add_tax, true ); ?> /> | |
<label for="taxregion" | |
class="pmpro_normal pmpro_clickable"><?php _e( 'Check this box if your billing address is in Australia.', 'pmpro-australian-gst' ); ?></label> | |
</div> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<?php | |
} | |
add_action( "pmpro_checkout_boxes", "agst_pmpro_checkout_boxes" ); | |
/** | |
* Add the pmpro_tax filter if the new member is in a tax region | |
*/ | |
function agst_region_tax_check() { | |
// Grab info from request & assign it to a session variable | |
if ( isset( $_REQUEST['taxregion'] ) ) { | |
//Add the tax calculation filter | |
if ( ! empty( $_REQUEST['taxregion'] ) ) { | |
add_filter( "pmpro_tax", "agst_pmpro_tax", 10, 3 ); | |
} | |
} else if ( ! empty( $_SESSION['taxregion'] ) ) { | |
//add the filter | |
add_filter( "pmpro_tax", "agst_pmpro_tax", 10, 3 ); | |
} else { | |
//check state and country | |
if ( ! empty( $_REQUEST['bcountry'] ) ) { | |
$bcountry = trim( strtolower( $_REQUEST['bcountry'] ) ); | |
if ( $bcountry == "au" ) { | |
//billing address is in AU | |
add_filter( "pmpro_tax", "agst_pmpro_tax", 10, 3 ); | |
} | |
} | |
} | |
} | |
add_action( "pmpro_checkout_preheader", "agst_region_tax_check", 5 ); | |
/** | |
* Save to the session variable once the session is known to exist | |
* The session is (minimally) initialized in the `pmpro_checkout_preheader` action. | |
* Unless there's no session support... | |
*/ | |
function agst_set_session_data() { | |
if ( isset( $_REQUEST['taxregion'] ) ) { | |
// Save to the session variable | |
$_SESSION['taxregion'] = $_REQUEST['taxregion']; | |
} | |
} | |
add_action( 'pmpro_checkout_preheader', 'agst_set_session_data', 20 ); | |
/** | |
* Remove the taxregion session variable after the checkout | |
*/ | |
function agst_pmpro_after_checkout() { | |
if ( isset( $_SESSION['taxregion'] ) ) { | |
unset( $_SESSION['taxregion'] ); | |
} | |
} | |
add_action( "pmpro_after_checkout", "agst_pmpro_after_checkout", 10 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment