Money_Utility.php
164 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Utilities; |
| 25 | |
| 26 | defined( 'ABSPATH' ) || exit; |
| 27 | |
| 28 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 29 | |
| 30 | /** |
| 31 | * Helper for converting money values. |
| 32 | * |
| 33 | * Square deals in cents, and Woo deals in floats so this methods help conversion between the two. |
| 34 | * |
| 35 | * @since 2.0.0 |
| 36 | */ |
| 37 | class Money_Utility { |
| 38 | |
| 39 | |
| 40 | /** |
| 41 | * Converts a WooCommerce amount to a Square money object. |
| 42 | * |
| 43 | * @since 2.0.0 |
| 44 | * |
| 45 | * @param float $amount amount to convert |
| 46 | * @param string $currency currency code |
| 47 | * |
| 48 | * @return \SquareConnect\Model\Money |
| 49 | */ |
| 50 | public static function amount_to_money( $amount, $currency ) { |
| 51 | |
| 52 | return new \SquareConnect\Model\Money( |
| 53 | array( |
| 54 | 'amount' => self::amount_to_cents( $amount, $currency ), |
| 55 | 'currency' => $currency, |
| 56 | ) |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Converts a float amount to cents. |
| 63 | * |
| 64 | * @since 2.0.0 |
| 65 | * |
| 66 | * @param float $amount float amount to convert |
| 67 | * @param string $currency currency code for the amount |
| 68 | * @return int |
| 69 | */ |
| 70 | public static function amount_to_cents( $amount, $currency = '' ) { |
| 71 | |
| 72 | if ( ! $currency ) { |
| 73 | $currency = get_woocommerce_currency(); |
| 74 | } |
| 75 | |
| 76 | $cents_factor = 10 ** self::get_currency_decimals( $currency ); |
| 77 | return (int) ( round( $cents_factor * $amount ) ); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Converts an amount in cents to a float. |
| 83 | * |
| 84 | * @since 2.0.0 |
| 85 | * |
| 86 | * @param int $cents amount in cents |
| 87 | * @param string $currency currency code for the amount |
| 88 | * @return float |
| 89 | */ |
| 90 | public static function cents_to_float( $cents, $currency = '' ) { |
| 91 | |
| 92 | if ( ! $currency ) { |
| 93 | $currency = get_woocommerce_currency(); |
| 94 | } |
| 95 | |
| 96 | $cents_factor = 10 ** self::get_currency_decimals( $currency ); |
| 97 | return (float) ( $cents / $cents_factor ); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Gets the standard number of decimals for the given currency. |
| 103 | * |
| 104 | * @since 2.0.2 |
| 105 | * |
| 106 | * @param string $currency currency code |
| 107 | * @return int |
| 108 | */ |
| 109 | public static function get_currency_decimals( $currency ) { |
| 110 | |
| 111 | $other_currencies = array( |
| 112 | 'BIF' => 0, |
| 113 | 'CLP' => 0, |
| 114 | 'DJF' => 0, |
| 115 | 'GNF' => 0, |
| 116 | 'HUF' => 0, |
| 117 | 'JPY' => 0, |
| 118 | 'KMF' => 0, |
| 119 | 'KRW' => 0, |
| 120 | 'MGA' => 0, |
| 121 | 'PYG' => 0, |
| 122 | 'RWF' => 0, |
| 123 | 'VND' => 0, |
| 124 | 'VUV' => 0, |
| 125 | 'XAF' => 0, |
| 126 | 'XOF' => 0, |
| 127 | 'XPF' => 0, |
| 128 | ); |
| 129 | |
| 130 | if ( Framework\SV_WC_Plugin_Compatibility::is_wc_version_gte( '3.5' ) ) { |
| 131 | |
| 132 | $locale_info = include( WC()->plugin_path() . '/i18n/locale-info.php' ); |
| 133 | |
| 134 | $currencies = wp_list_pluck( $locale_info, 'num_decimals', 'currency_code' ); |
| 135 | |
| 136 | // ensure the values set in local-info.php always override the above |
| 137 | $currencies = array_merge( $other_currencies, $currencies ); |
| 138 | |
| 139 | } else { |
| 140 | |
| 141 | $currencies = array(); |
| 142 | $currency_codes = get_woocommerce_currencies(); |
| 143 | |
| 144 | foreach ( $currency_codes as $code => $name ) { |
| 145 | $currencies[ $code ] = 2; |
| 146 | } |
| 147 | |
| 148 | $currencies = array_merge( $currencies, $other_currencies ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Filters the number of decimals to use for a given currency when converting to or from its smallest denomination. |
| 153 | * |
| 154 | * @since 2.0.2 |
| 155 | * |
| 156 | * @param int $decimals number of decimals |
| 157 | * @param string $currency currency code |
| 158 | */ |
| 159 | return apply_filters( 'wc_square_currency_decimals', isset( $currencies[ $currency ] ) ? $currencies[ $currency ] : 2, $currency ); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | } |
| 164 |