self::money( (float) $total ), 'currency' => $store, ); } if ( 'BAM' === $store ) { return array( 'amount' => self::money( (float) $total / self::BAM_PER_EUR ), 'currency' => 'EUR', ); } $fresh = $rate_saved > 0 && $now - $rate_saved <= self::STOP_AFTER_DAY * DAY_IN_SECONDS; if ( ! in_array( $store, self::MERCHANT_RATE, true ) || (float) $rate <= 0 || ! $fresh ) { return null; } return array( 'amount' => self::money( (float) $total / (float) $rate ), 'currency' => 'USD' === $settle_in ? 'USD' : 'EUR', ); } /** * @param string $store Store currency code. * @param int $rate_saved Unix time the merchant rate was saved. * @param int $now Current Unix time. * @return bool True when a merchant rate is older than the warning age. */ public static function rate_is_old( $store, $rate_saved, $now ) { return in_array( $store, self::MERCHANT_RATE, true ) && $now - $rate_saved > self::WARN_AFTER_DAY * DAY_IN_SECONDS; } /** * AgentaOS reports EUR and USD sales with its internal codes EURC and USDC in some responses. * * @param string $code Currency code from AgentaOS. * @return string The fiat code: EUR, USD or the code as given, upper case. */ public static function fiat( $code ) { $code = strtoupper( trim( (string) $code ) ); $map = array( 'EURC' => 'EUR', 'USDC' => 'USD', ); return isset( $map[ $code ] ) ? $map[ $code ] : $code; } /** * @param string $store Store currency code. * @return bool True when the store currency is one this plugin can charge (with a rate where needed). */ public static function is_known( $store ) { return in_array( $store, self::DIRECT, true ) || 'BAM' === $store || in_array( $store, self::MERCHANT_RATE, true ); } /** * Rounds half up to cents; the tiny offset absorbs binary float error (1.005 -> 1.01). * * @param float $value Amount. * @return string */ private static function money( $value ) { return number_format( round( $value + 1e-9, 2 ), 2, '.', '' ); } }