'pdo_sqlite', 'memory' => true]); $dbal->executeStatement('CREATE TABLE currency (id INTEGER PRIMARY KEY, code VARCHAR(3) NOT NULL, is_default INTEGER NOT NULL)'); $dbal->executeStatement("INSERT INTO currency (id, code, is_default) VALUES (1, 'USD', 0), (2, 'aud', 1)"); $invoice = new \Model_Invoice(); $invoice->id = 42; $invoice->status = \Model_Invoice::STATUS_UNPAID; $invoice->currency = 'AUD'; $di = new \Pimple\Container(); $di['dbal'] = $dbal; $di['db'] = new class ($invoice) { public function __construct(private \Model_Invoice $invoice) { } /** @param array $params */ public function findOne(string $type, string $where, array $params): ?\Model_Invoice { return $type === 'Invoice' && $params[':id'] === $this->invoice->id ? $this->invoice : null; } }; // FOSSBilling's invoice service: the total with tax, as a float. $di['mod_service'] = static fn (string $name): object => new class { public function getTotalWithTax(\Model_Invoice $invoice): float { return 20.0; } }; Runtime::setDi($di); $currencies = Runtime::currencies(); self::assertSame(['AUD', 'USD'], $currencies); $amount = (new Host())->amountFor('42'); self::assertSame(['currency' => 'AUD', 'value' => '20.00', 'description' => 'Invoice #42'], $amount); $quote = (new Service(new FakeWallet(), new StaticPriceProvider(), [], $currencies))->prepareCheckout(['amount' => $amount]); // The static price is BTC/AUD 80000.00: A$20.00 is 25,000 sats. self::assertSame(25_000_000, $quote['amount_msats']); self::assertSame(['currency' => 'AUD', 'value' => '20.00'], $quote['fiat_quote']['fiat']); } public function testACurrencyThePriceFeedsNeverQuoteIsDroppedAndNotOffered(): void { // Kenyan shillings, the default, beside AUD and USD: the feeds quote no KES. $dbal = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]); $dbal->executeStatement('CREATE TABLE currency (id INTEGER PRIMARY KEY, code VARCHAR(3) NOT NULL, is_default INTEGER NOT NULL)'); $dbal->executeStatement("INSERT INTO currency (id, code, is_default) VALUES (1, 'USD', 0), (2, 'KES', 1), (3, 'AUD', 0)"); $gateway = new \Model_PayGateway(); $stored = []; $di = new \Pimple\Container(); $di['dbal'] = $dbal; $di['db'] = new class ($gateway, $stored) { /** @param list<\Model_PayGateway> $stored */ public function __construct(private \Model_PayGateway $gateway, public array &$stored) { } /** @param array $params */ public function findOne(string $type, string $where, array $params): ?\Model_PayGateway { return $type === 'PayGateway' ? $this->gateway : null; } public function store(\Model_PayGateway $bean): void { $this->stored[] = $bean; } }; Runtime::setDi($di); self::assertSame(['USD', 'AUD'], Runtime::currencies()); self::assertSame(['KES'], Runtime::unquotedCurrencies()); // FOSSBilling's invoice and order pages offer the gateway only in // its accepted currencies: every currency but KES. Runtime::syncAcceptedCurrencies(); self::assertSame(['USD', 'AUD'], json_decode((string) $gateway->accepted_currencies, true)); self::assertCount(1, $stored); // A list the admin ticked stays theirs, less what the feeds never quote. $gateway->accepted_currencies = json_encode(['aud', 'KES']); Runtime::syncAcceptedCurrencies(); self::assertSame(['AUD'], json_decode((string) $gateway->accepted_currencies, true)); // Nothing changes, nothing is written. Runtime::syncAcceptedCurrencies(); self::assertCount(2, $stored); } } }