/** * Handles all cost calculations for Stripe. * * Returns an associative array with a possible Percentage Rate, along with the calculated Tax Amount. * Tax calculations are based on State/Province, Country, and/or Zip Code. * Updated to support multiple data fields in it's return value. * * @package optimizeMember\Stripe * @since 140617 * * @param int|string $trial_sub_total Optional. A numeric Amount/cost of a possible Initial/Trial being offered. * @param int|string $sub_total Optional. A numeric Amount/cost of the purchase and/or Regular Period. * @param string $state Optional. The State/Province where the Customer is billed. * @param string $country Optional. The Country where the Customer is billed. * @param int|string $zip Optional. The Postal/Zip Code where the Customer is billed. * @param string $currency Optional. Expects a 3 character Currency Code. * @param string $desc Optional. Description of the sale. * * @return array Array of calculations. */ public static function cost($trial_sub_total = '', $sub_total = '', $state = '', $country = '', $zip = '', $currency = '', $desc = '') { $state = strtoupper(c_ws_plugin__optimizemember_pro_utilities::full_state($state, $country = strtoupper($country))); $rates = apply_filters('ws_plugin__optimizemember_pro_tax_rates_before_cost_calculation', strtoupper($GLOBALS['WS_PLUGIN__']['optimizemember']['o']['pro_tax_rates']), get_defined_vars()); $default = $GLOBALS['WS_PLUGIN__']['optimizemember']['o']['pro_default_tax']; $ps = _x('%', 'optimizemember-front percentage-symbol', 's2member'); $trial_tax = $tax = $trial_tax_per = $tax_per = $trial_total = $total = NULL; // Initialize. foreach (array('trial_sub_total' => $trial_sub_total, 'sub_total' => $sub_total) as $this_key => $this_sub_total) { $_default = $this_tax = $this_tax_per = $this_total = $configured_rates = $configured_rate = $location = $rate = $m = NULL; if (is_numeric($this_sub_total) && $this_sub_total > 0) { if ($default && preg_match('/%$/', $default)) { if (($_default = (double) $default) > 0) { $this_tax = round($this_sub_total / 100 * $_default, 2); $this_tax_per = $_default . $ps; } else { $this_tax = 0.0; $this_tax_per = $_default . $ps; } } else { if (($_default = (double) $default) > 0) { $this_tax = round($_default, 2); $this_tax_per = ''; // Flat. } else { $this_tax = 0.0; // No tax. $this_tax_per = ''; // Flat rate. } } if (strlen($country) === 2) { foreach (preg_split('/[' . "\r\n\t" . ']+/', $rates) as $rate) { if ($rate = trim($rate)) { list($location, $rate) = preg_split('/\\=/', $rate, 2); $location = trim($location); $rate = trim($rate); if ($location === $country) { $configured_rates[1] = $rate; } else { if ($state && $location === $state . '/' . $country) { $configured_rates[2] = $rate; } else { if ($state && preg_match('/^([A-Z]{2})\\/(' . preg_quote($country, '/') . ')$/', $location, $m) && strtoupper(c_ws_plugin__optimizemember_pro_utilities::full_state($m[1], $m[2])) . '/' . $m[2] === $state . '/' . $country) { $configured_rates[2] = $rate; } else { if ($zip && preg_match('/^([0-9]+)-([0-9]+)\\/(' . preg_quote($country, '/') . ')$/', $location, $m) && $zip >= $m[1] && $zip <= $m[2] && $country === $m[3]) { $configured_rates[3] = $rate; } else { if ($zip && $location === $zip . '/' . $country) { $configured_rates[4] = $rate; } } } } } } } if (is_array($configured_rates) && !empty($configured_rates)) { krsort($configured_rates); $configured_rate = array_shift($configured_rates); if (preg_match('/%$/', $configured_rate)) { if (($configured_rate = (double) $configured_rate) > 0) { $this_tax = round($this_sub_total / 100 * $configured_rate, 2); $this_tax_per = $configured_rate . $ps; } else { $this_tax = 0.0; // No tax. $this_tax_per = $configured_rate . $ps; } } else { if (($configured_rate = (double) $configured_rate) > 0) { $this_tax = round($configured_rate, 2); $this_tax_per = ''; // Flat rate. } else { $this_tax = 0.0; // No tax. $this_tax_per = ''; // Flat rate. } } } } $this_total = $this_sub_total + $this_tax; } else { $this_tax = 0.0; // No tax. $this_tax_per = ''; // Flat rate. $this_sub_total = 0.0; // 0.00. $this_total = 0.0; // 0.00. } if ($this_key === 'trial_sub_total') { $trial_tax = $this_tax; $trial_tax_per = $this_tax_per; $trial_sub_total = $this_sub_total; $trial_total = $this_total; } else { if ($this_key === 'sub_total') { $tax = $this_tax; $tax_per = $this_tax_per; $sub_total = $this_sub_total; $total = $this_total; } } } return array('trial_sub_total' => number_format($trial_sub_total, 2, '.', ''), 'sub_total' => number_format($sub_total, 2, '.', ''), 'trial_tax' => number_format($trial_tax, 2, '.', ''), 'tax' => number_format($tax, 2, '.', ''), 'trial_tax_per' => $trial_tax_per, 'tax_per' => $tax_per, 'trial_total' => number_format($trial_total, 2, '.', ''), 'total' => number_format($total, 2, '.', ''), 'cur' => $currency, 'cur_symbol' => c_ws_plugin__optimizemember_utils_cur::symbol($currency), 'desc' => $desc); }
/** * Handles all cost calculations for PayPal. * * Returns an associative array with a possible Percentage Rate, along with the calculated Tax Amount. * Tax calculations are based on State/Province, Country, and/or Zip Code. * Updated to support multiple data fields in it's return value. * * @package optimizeMember\PayPal * @since 1.5 * * @param int|str $trial_sub_total Optional. A numeric Amount/cost of a possible Initial/Trial being offered. * @param int|str $sub_total Optional. A numeric Amount/cost of the purchase and/or Regular Period. * @param str $state Optional. The State/Province where the Customer is billed. * @param str $country Optional. The Country where the Customer is billed. * @param int|str $zip Optional. The Postal/Zip Code where the Customer is billed. * @param str $currency Optional. Expects a 3 character Currency Code. * @param str $desc Optional. Description of the sale. * @return array Array of calculations. * * @todo Add support for `Zip + 4` syntax? */ public static function paypal_cost($trial_sub_total = FALSE, $sub_total = FALSE, $state = FALSE, $country = FALSE, $zip = FALSE, $currency = FALSE, $desc = FALSE) { $state = strtoupper(c_ws_plugin__optimizemember_pro_utilities::full_state($state, $country = strtoupper($country))); $rates = strtoupper($GLOBALS["WS_PLUGIN__"]["optimizemember"]["o"]["pro_tax_rates"]); $default = $GLOBALS["WS_PLUGIN__"]["optimizemember"]["o"]["pro_default_tax"]; $ps = _x("%", "s2member-front percentage-symbol", "s2member"); /**/ foreach (array("trial_sub_total" => $trial_sub_total, "sub_total" => $sub_total) as $this_key => $this_sub_total) { unset($_default, $this_tax, $this_tax_per, $this_total, $configured_rates, $configured_rate, $location, $rate, $m); /**/ if (is_numeric($this_sub_total) && $this_sub_total > 0) { if (preg_match("/%\$/", $default)) { if (($_default = (double) $default) > 0) { $this_tax = round($this_sub_total / 100 * $_default, 2); $this_tax_per = $_default . $ps; } else { $this_tax = 0.0; $this_tax_per = $_default . $ps; } } else { if (($_default = (double) $default) > 0) { $this_tax = round($_default, 2); $this_tax_per = ""; /* Flat. */ } else { $this_tax = 0.0; /* No Tax. */ $this_tax_per = ""; /* Flat rate. */ } } /**/ if (strlen($country) === 2) { foreach (preg_split("/[\r\n\t]+/", $rates) as $rate) { if ($rate = trim($rate)) { list($location, $rate) = preg_split("/\\=/", $rate, 2); $location = trim($location); $rate = trim($rate); /**/ if ($location === $country) { $configured_rates[1] = $rate; } else { if ($state && $location === $state . "/" . $country) { $configured_rates[2] = $rate; } else { if ($state && preg_match("/^([A-Z]{2})\\/(" . preg_quote($country, "/") . ")\$/", $location, $m) && strtoupper(c_ws_plugin__optimizemember_pro_utilities::full_state($m[1], $m[2])) . "/" . $m[2] === $state . "/" . $country) { $configured_rates[2] = $rate; } else { if ($zip && preg_match("/^([0-9]+)-([0-9]+)\\/(" . preg_quote($country, "/") . ")\$/", $location, $m) && $zip >= $m[1] && $zip <= $m[2] && $country === $m[3]) { $configured_rates[3] = $rate; } else { if ($zip && $location === $zip . "/" . $country) { $configured_rates[4] = $rate; } } } } } } } /**/ if (is_array($configured_rates) && !empty($configured_rates)) { krsort($configured_rates); $configured_rate = array_shift($configured_rates); /**/ if (preg_match("/%\$/", $configured_rate)) { if (($configured_rate = (double) $configured_rate) > 0) { $this_tax = round($this_sub_total / 100 * $configured_rate, 2); $this_tax_per = $configured_rate . $ps; } else { $this_tax = 0.0; /* No Tax. */ $this_tax_per = $configured_rate . $ps; } } else { if (($configured_rate = (double) $configured_rate) > 0) { $this_tax = round($configured_rate, 2); $this_tax_per = ""; /* Flat rate. */ } else { $this_tax = 0.0; /* No Tax. */ $this_tax_per = ""; /* Flat rate. */ } } } } /**/ $this_total = $this_sub_total + $this_tax; } else { $this_tax = 0.0; /* No Tax. */ $this_tax_per = ""; /* Flat rate. */ $this_sub_total = 0.0; /* 0.00. */ $this_total = 0.0; /* 0.00. */ } /**/ if ($this_key === "trial_sub_total") { $trial_tax = $this_tax; $trial_tax_per = $this_tax_per; $trial_sub_total = $this_sub_total; $trial_total = $this_total; } else { if ($this_key === "sub_total") { $tax = $this_tax; $tax_per = $this_tax_per; $sub_total = $this_sub_total; $total = $this_total; } } } /**/ return array("trial_sub_total" => number_format($trial_sub_total, 2, ".", ""), "sub_total" => number_format($sub_total, 2, ".", ""), "trial_tax" => number_format($trial_tax, 2, ".", ""), "tax" => number_format($tax, 2, ".", ""), "trial_tax_per" => $trial_tax_per, "tax_per" => $tax_per, "trial_total" => number_format($trial_total, 2, ".", ""), "total" => number_format($total, 2, ".", ""), "cur" => $currency, "cur_symbol" => c_ws_plugin__optimizemember_utils_cur::symbol($currency), "desc" => $desc); }