| @@ -42,8 +42,15 @@ | ||
| 42 | 42 | * @var float |
| 43 | 43 | */ |
| 44 | 44 | public $gross; |
| 45 | 45 | /** |
| 46 | + * @author Leo Fajardo (@leorw) | |
| 47 | + * @since 2.3.0 | |
| 48 | + * | |
| 49 | + * @var string One of the following: `usd`, `gbp`, `eur`. | |
| 50 | + */ | |
| 51 | + public $currency; | |
| 52 | + /** | |
| 46 | 53 | * @var number |
| 47 | 54 | */ |
| 48 | 55 | public $bound_payment_id; |
| 49 | 56 | /** |
| @@ -72,10 +79,25 @@ | ||
| 72 | 79 | * @var int Payment source. |
| 73 | 80 | */ |
| 74 | 81 | public $source = 0; |
| 75 | 82 | |
| 83 | + /** | |
| 84 | + * Presentment payment information for customer-facing currency display. | |
| 85 | + * | |
| 86 | + * @var object|null | |
| 87 | + */ | |
| 88 | + public $presentment; | |
| 89 | + | |
| 76 | 90 | #endregion Properties |
| 77 | 91 | |
| 92 | + const CURRENCY_USD = 'usd'; | |
| 93 | + const CURRENCY_GBP = 'gbp'; | |
| 94 | + const CURRENCY_EUR = 'eur'; | |
| 95 | + const CURRENCY_ILS = 'ils'; | |
| 96 | + const CURRENCY_CAD = 'cad'; | |
| 97 | + const CURRENCY_AUD = 'aud'; | |
| 98 | + const CURRENCY_PLN = 'pln'; | |
| 99 | + | |
| 78 | 100 | /** |
| 79 | 101 | * @param object|bool $payment |
| 80 | 102 | */ |
| 81 | 103 | function __construct( $payment = false ) { |
| @@ -106,5 +128,100 @@ | ||
| 106 | 128 | */ |
| 107 | 129 | function is_migrated() { |
| 108 | 130 | return ( 0 != $this->source ); |
| 109 | 131 | } |
| 110 | - } | |
| 132 | + | |
| 133 | + /** | |
| 134 | + * @return bool | |
| 135 | + */ | |
| 136 | + private function has_presentment() | |
| 137 | + { | |
| 138 | + return is_object( $this->presentment ); | |
| 139 | + } | |
| 140 | + | |
| 141 | + /** | |
| 142 | + * @return float | |
| 143 | + */ | |
| 144 | + private function get_display_gross() | |
| 145 | + { | |
| 146 | + return $this->has_presentment() ? | |
| 147 | + (float) $this->presentment->gross : | |
| 148 | + (float) $this->gross; | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * @return float | |
| 153 | + */ | |
| 154 | + private function get_display_vat() | |
| 155 | + { | |
| 156 | + return $this->has_presentment() ? | |
| 157 | + (float) $this->presentment->vat : | |
| 158 | + (float) $this->vat; | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * @return string | |
| 163 | + */ | |
| 164 | + private function get_display_currency() | |
| 165 | + { | |
| 166 | + return $this->has_presentment() ? | |
| 167 | + $this->presentment->currency : | |
| 168 | + $this->currency; | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 172 | + * Returns the gross in this format: | |
| 173 | + * `{symbol}{amount | 2 decimal digits} {currency | uppercase}` | |
| 174 | + * | |
| 175 | + * Examples: £9.99 GBP, -£9.99 GBP. | |
| 176 | + * | |
| 177 | + * @author Leo Fajardo (@leorw) | |
| 178 | + * @since 2.3.0 | |
| 179 | + * | |
| 180 | + * @return string | |
| 181 | + */ | |
| 182 | + function formatted_gross() | |
| 183 | + { | |
| 184 | + $price = $this->get_display_gross() + $this->get_display_vat(); | |
| 185 | + | |
| 186 | + return ( | |
| 187 | + ( $price < 0 ? '-' : '' ) . | |
| 188 | + $this->get_symbol() . | |
| 189 | + number_format( abs( $price ), 2, '.', ',' ) . ' ' . | |
| 190 | + strtoupper( $this->get_display_currency() ) | |
| 191 | + ); | |
| 192 | + } | |
| 193 | + | |
| 194 | + /** | |
| 195 | + * A map between supported currencies with their symbols. | |
| 196 | + * | |
| 197 | + * @var array<string,string> | |
| 198 | + */ | |
| 199 | + static $CURRENCY_2_SYMBOL; | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * @author Leo Fajardo (@leorw) | |
| 203 | + * @since 2.3.0 | |
| 204 | + * | |
| 205 | + * @return string | |
| 206 | + */ | |
| 207 | + private function get_symbol() { | |
| 208 | + if ( ! isset( self::$CURRENCY_2_SYMBOL ) ) { | |
| 209 | + // Lazy load. | |
| 210 | + self::$CURRENCY_2_SYMBOL = array( | |
| 211 | + self::CURRENCY_USD => '$', | |
| 212 | + self::CURRENCY_GBP => '£', | |
| 213 | + self::CURRENCY_EUR => '€', | |
| 214 | + self::CURRENCY_ILS => '₪', | |
| 215 | + self::CURRENCY_CAD => '$', | |
| 216 | + self::CURRENCY_AUD => '$', | |
| 217 | + self::CURRENCY_PLN => 'zł', | |
| 218 | + ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + $currency = $this->get_display_currency(); | |
| 222 | + | |
| 223 | + return isset( self::$CURRENCY_2_SYMBOL[ $currency ] ) | |
| 224 | + ? self::$CURRENCY_2_SYMBOL[ $currency ] | |
| 225 | + : strtoupper( $currency ) . ' '; | |
| 226 | + } | |
| 227 | + } | |