diff --git a/src/modules/Invoice/ServicePayGateway.php b/src/modules/Invoice/ServicePayGateway.php index eaa5bb844..fb5cc08e2 100644 --- a/src/modules/Invoice/ServicePayGateway.php +++ b/src/modules/Invoice/ServicePayGateway.php @@ -37,7 +37,7 @@ class ServicePayGateway implements InjectionAwareInterface return $this->di; } - public function getSearchQuery(array $data) + public function getSearchQuery(array $data): array { $sql = 'SELECT * FROM pay_gateway @@ -119,7 +119,7 @@ class ServicePayGateway implements InjectionAwareInterface return $adapters; } - public function install($code) + public function install($code): bool { $available = $this->getAvailable(); if (!in_array($code, $available)) { @@ -167,7 +167,7 @@ class ServicePayGateway implements InjectionAwareInterface return $result; } - public function copy(\Model_PayGateway $model) + public function copy(\Model_PayGateway $model): int { $new = $this->di['db']->dispense('PayGateway'); $new->name = $model->name . ' (Copy)'; @@ -182,7 +182,7 @@ class ServicePayGateway implements InjectionAwareInterface return $newId; } - public function update(\Model_PayGateway $model, array $data) + public function update(\Model_PayGateway $model, array $data): bool { $model->name = $data['title'] ?? $model->name; if (isset($data['config']) && is_array($data['config'])) { @@ -203,7 +203,7 @@ class ServicePayGateway implements InjectionAwareInterface return true; } - public function delete(\Model_PayGateway $model) + public function delete(\Model_PayGateway $model): bool { $id = $model->id; $this->di['db']->trash($model); @@ -227,20 +227,13 @@ class ServicePayGateway implements InjectionAwareInterface } else { $gateway = $this->toApiArray($gtw); $adapter = $this->getPaymentAdapter($gtw); - if (array_key_exists('logo', $adapter->getConfig())) { - $gateway['logo'] = $adapter->getConfig()['logo']; - if ($this->filesystem->exists(Path::join(PATH_LIBRARY, 'Payment', 'Adapter', $adapter->getConfig()['logo']['logo']))) { - $gateway['logo']['logo'] = $this->di['tools']->url("/library/Payment/Adapter/{$adapter->getConfig()['logo']['logo']}"); - } else { - if ($this->filesystem->exists(Path::join(PATH_DATA, 'assets', 'gateways', $adapter->getConfig()['logo']['logo']))) { - $gateway['logo']['logo'] = $this->di['tools']->url("/data/assets/gateways/{$adapter->getConfig()['logo']['logo']}"); - } else { - $gateway['logo']['logo'] = $this->di['tools']->url('/data/assets/gateways/default.png'); - } - } - } else { - $gateway['logo']['logo'] = $this->di['tools']->url('/data/assets/gateways/default.png'); + $config = $adapter->getConfig(); + + if (!empty($config['logo'])) { + $gateway['logo'] = $config['logo']; + $gateway['logo']['logo'] = $this->resolveGatewayLogo($config['logo']); } + $result[] = $gateway; } } @@ -248,7 +241,25 @@ class ServicePayGateway implements InjectionAwareInterface return $result; } - public function canPerformRecurrentPayment(\Model_PayGateway $model) + public function resolveGatewayLogo(array $logoConfig): string + { + $filename = $logoConfig['logo'] ?? 'default.png'; + + $libraryPath = Path::join(PATH_LIBRARY, 'Payment', 'Adapter', $filename); + $dataPath = Path::join(PATH_DATA, 'assets', 'gateways', $filename); + + if ($this->filesystem->exists($libraryPath)) { + return $this->di['tools']->url("/library/Payment/Adapter/{$filename}"); + } + + if ($this->filesystem->exists($dataPath)) { + return $this->di['tools']->url("/data/assets/gateways/{$filename}"); + } + + return $this->di['tools']->url('/data/assets/gateways/default.png'); + } + + public function canPerformRecurrentPayment(\Model_PayGateway $model): bool { return (bool) $model->allow_recurrent; } @@ -279,7 +290,7 @@ class ServicePayGateway implements InjectionAwareInterface $class = $this->getAdapterClassName($pg); - if (!class_exists($class ?? '')) { + if (!class_exists($class)) { throw new \FOSSBilling\Exception('Payment gateway :adapter was not found.', [':adapter' => $class]); } @@ -292,7 +303,7 @@ class ServicePayGateway implements InjectionAwareInterface return $adapter; } - private function _getAllowTuple(\Model_PayGateway $model) + private function _getAllowTuple(\Model_PayGateway $model): array { $adapter_config = $this->getAdapterConfig($model); $single = $adapter_config['supports_one_time_payments'] ?? false; @@ -304,7 +315,7 @@ class ServicePayGateway implements InjectionAwareInterface ]; } - public function getAdapterConfig(\Model_PayGateway $pg) + public function getAdapterConfig(\Model_PayGateway $pg): array { $class = $this->getAdapterClassName($pg); if (!$this->filesystem->exists(Path::join(PATH_LIBRARY, 'Payment', 'Adapter', "{$pg->gateway}.php"))) { @@ -326,19 +337,19 @@ class ServicePayGateway implements InjectionAwareInterface return call_user_func([$class, 'getConfig']); } - public function getAdapterClassName(\Model_PayGateway $pg) + public function getAdapterClassName(\Model_PayGateway $pg): string { $class = "Payment_Adapter_{$pg->gateway}"; - if (!class_exists($class)) { - include Path::join(PATH_LIBRARY, 'Payment', 'Adapter', $pg->gateway, "{$pg->gateway}.php"); - return "Payment_Adapter_{$pg->gateway}"; - } else { - return $class; + if (!class_exists($class)) { + $file = Path::join(PATH_LIBRARY, 'Payment', 'Adapter', $pg->gateway, "{$pg->gateway}.php"); + include $file; } + + return $class; } - public function getAcceptedCurrencies(\Model_PayGateway $model) + public function getAcceptedCurrencies(\Model_PayGateway $model): array { if ($model->accepted_currencies === null || empty($model->accepted_currencies)) { $currencyService = $this->di['mod_service']('Currency'); @@ -349,7 +360,7 @@ class ServicePayGateway implements InjectionAwareInterface return json_decode($model->accepted_currencies ?? '', true); } - public function getFormElements(\Model_PayGateway $model) + public function getFormElements(\Model_PayGateway $model): array { $config = $this->getAdapterConfig($model); if (isset($config['form']) && is_array($config['form'])) { @@ -359,7 +370,7 @@ class ServicePayGateway implements InjectionAwareInterface return []; } - public function getDescription(\Model_PayGateway $model) + public function getDescription(\Model_PayGateway $model): ?string { $config = $this->getAdapterConfig($model); @@ -369,7 +380,7 @@ class ServicePayGateway implements InjectionAwareInterface /** * @param \Model_Invoice $model */ - public function getCallbackUrl(\Model_PayGateway $pg, $model = null) + public function getCallbackUrl(\Model_PayGateway $pg, $model = null): string { $p = [ 'gateway_id' => $pg->id, @@ -384,7 +395,7 @@ class ServicePayGateway implements InjectionAwareInterface /** * @param \Model_Invoice $model */ - private function getReturnUrl(\Model_PayGateway $pg, $model = null) + private function getReturnUrl(\Model_PayGateway $pg, $model = null): string { if ($model instanceof \Model_Invoice) { return $this->di['url']->link("/invoice/{$model->hash}", ['status' => 'ok', 'restore_session' => session_id()]); @@ -396,7 +407,7 @@ class ServicePayGateway implements InjectionAwareInterface /** * @param \Model_Invoice $model */ - private function getCancelUrl(\Model_PayGateway $pg, $model = null) + private function getCancelUrl(\Model_PayGateway $pg, $model = null): string { if ($model instanceof \Model_Invoice) { return $this->di['url']->link("/invoice/{$model->hash}", ['status' => 'cancel', 'restore_session' => session_id()]); @@ -408,7 +419,7 @@ class ServicePayGateway implements InjectionAwareInterface /** * @param \Model_Invoice $model */ - private function getCallbackRedirect(\Model_PayGateway $pg, $model = null) + private function getCallbackRedirect(\Model_PayGateway $pg, $model = null): string { $p = [ 'gateway_id' => $pg->id, diff --git a/src/modules/Invoice/ServiceTransaction.php b/src/modules/Invoice/ServiceTransaction.php index 1c49f77f8..a1e05fd20 100644 --- a/src/modules/Invoice/ServiceTransaction.php +++ b/src/modules/Invoice/ServiceTransaction.php @@ -28,7 +28,7 @@ class ServiceTransaction implements InjectionAwareInterface return $this->di; } - public function processReceivedATransactions() + public function processReceivedATransactions(): bool { $this->di['logger']->info('Executed action to process received transactions'); $received = $this->getReceived(); @@ -40,7 +40,7 @@ class ServiceTransaction implements InjectionAwareInterface return true; } - public function update(\Model_Transaction $model, array $data) + public function update(\Model_Transaction $model, array $data): bool { $this->di['events_manager']->fire(['event' => 'onBeforeAdminTransactionUpdate', 'params' => ['id' => $model->id]]); @@ -116,7 +116,7 @@ class ServiceTransaction implements InjectionAwareInterface return $newId; } - public function delete(\Model_Transaction $model) + public function delete(\Model_Transaction $model): bool { $id = $model->id; $this->di['db']->trash($model); @@ -161,7 +161,7 @@ class ServiceTransaction implements InjectionAwareInterface return $result; } - public function getSearchQuery(array $data) + public function getSearchQuery(array $data): array { $sql = 'SELECT m.* FROM transaction as m @@ -251,7 +251,7 @@ class ServiceTransaction implements InjectionAwareInterface return [$sql, $params]; } - public function counter() + public function counter(): array { $sql = 'SELECT status, count(id) as counter FROM transaction @@ -271,7 +271,7 @@ class ServiceTransaction implements InjectionAwareInterface ]; } - public function getStatusPairs() + public function getStatusPairs(): array { return [ \Model_Transaction::STATUS_RECEIVED => 'Received', @@ -281,7 +281,7 @@ class ServiceTransaction implements InjectionAwareInterface ]; } - public function getStatuses() + public function getStatuses(): array { return [ \Model_Transaction::STATUS_RECEIVED => 'Received', @@ -291,7 +291,7 @@ class ServiceTransaction implements InjectionAwareInterface ]; } - public function getGatewayStatuses() + public function getGatewayStatuses(): array { return [ \Payment_Transaction::STATUS_PENDING => 'Pending validation', @@ -300,7 +300,7 @@ class ServiceTransaction implements InjectionAwareInterface ]; } - public function getTypes() + public function getTypes(): array { return [ \Payment_Transaction::TXTYPE_PAYMENT => 'Payment', @@ -378,7 +378,7 @@ class ServiceTransaction implements InjectionAwareInterface return $this->di['db']->getAll($sql, $params); } - public function process($tx) + public function process(\Model_Transaction $tx): \Model_Transaction { $transaction = $this->di['db']->load('Transaction', $tx->id); @@ -414,7 +414,7 @@ class ServiceTransaction implements InjectionAwareInterface return $transaction; } - private function _isProcessed(\Model_Transaction $tx) + private function _isProcessed(\Model_Transaction $tx): bool { if ($tx->status == \Model_Transaction::STATUS_PROCESSED) { $tx->error = null; @@ -438,7 +438,7 @@ class ServiceTransaction implements InjectionAwareInterface return false; } - private function hasProcessedTransaction(\Model_Transaction $tx) + private function hasProcessedTransaction(\Model_Transaction $tx): bool { if (!$tx->txn_id) { return false; @@ -449,7 +449,7 @@ class ServiceTransaction implements InjectionAwareInterface return empty($res); } - private function _markAsProcessed(\Model_Transaction $tx) + private function _markAsProcessed(\Model_Transaction $tx): void { $tx->error = null; $tx->error_code = null; @@ -458,7 +458,7 @@ class ServiceTransaction implements InjectionAwareInterface $this->di['db']->store($tx); } - private function _parseIpnAndApprove(\Model_Transaction &$tx) + private function _parseIpnAndApprove(\Model_Transaction &$tx): \Model_Transaction { if ($tx->status == \Model_Transaction::STATUS_APPROVED) { return $tx; @@ -466,6 +466,7 @@ class ServiceTransaction implements InjectionAwareInterface $invoiceService = $this->di['mod_service']('Invoice'); $payGatewayService = $this->di['mod_service']('Invoice', 'PayGateway'); + $ipn = json_decode($tx->ipn ?? '', true) ?? []; if (empty($tx->gateway_id)) { @@ -569,7 +570,7 @@ class ServiceTransaction implements InjectionAwareInterface } } - private function _refund(\Model_Transaction $tx) + private function _refund(\Model_Transaction $tx): \Model_Transaction { if ($this->_isProcessed($tx)) { return $tx; @@ -588,7 +589,7 @@ class ServiceTransaction implements InjectionAwareInterface return $tx; } - private function _subscribe(\Model_Transaction $tx) + private function _subscribe(\Model_Transaction $tx): \Model_Transaction { if ($this->_isProcessed($tx)) { return $tx; @@ -623,7 +624,7 @@ class ServiceTransaction implements InjectionAwareInterface return $tx; } - private function _unsubscribe(\Model_Transaction $tx) + private function _unsubscribe(\Model_Transaction $tx): \Model_Transaction { if ($this->_isProcessed($tx)) { return $tx; @@ -642,7 +643,7 @@ class ServiceTransaction implements InjectionAwareInterface return $tx; } - private function _validateApprovedTransaction(\Model_Transaction $tx) + private function _validateApprovedTransaction(\Model_Transaction $tx): void { if ($tx->status != \Model_Transaction::STATUS_APPROVED) { throw new \FOSSBilling\Exception('Only approved transaction can be processed'); @@ -665,7 +666,7 @@ class ServiceTransaction implements InjectionAwareInterface } } - public function debitTransaction(\Model_Transaction $tx) + public function debitTransaction(\Model_Transaction $tx): void { $proforma = $this->di['db']->load('Invoice', $tx->invoice_id); $client = $this->di['db']->load('Client', $proforma->client_id); diff --git a/tests-legacy/modules/Invoice/ServicePayGatewayTest.php b/tests-legacy/modules/Invoice/ServicePayGatewayTest.php index 1b615d8a9..20551c3ad 100644 --- a/tests-legacy/modules/Invoice/ServicePayGatewayTest.php +++ b/tests-legacy/modules/Invoice/ServicePayGatewayTest.php @@ -167,11 +167,11 @@ class ServicePayGatewayTest extends \BBTestCase 'title' => null, 'allow_single' => null, 'allow_recurrent' => null, - 'accepted_currencies' => null, + 'accepted_currencies' => [], 'supports_one_time_payments' => false, 'supports_subscriptions' => false, 'config' => [], - 'form' => null, + 'form' => [], 'description' => null, 'enabled' => null, 'test_mode' => null, @@ -339,7 +339,7 @@ class ServicePayGatewayTest extends \BBTestCase ->getMock(); $serviceMock->expects($this->atLeastOnce()) ->method('getAdapterClassName') - ->willReturn(null); + ->willReturn(''); $urlMock = $this->getMockBuilder('\Box_Url')->getMock(); $urlMock->expects($this->atLeastOnce())