diff --git a/cspell.json b/cspell.json index cced40997..474a8cafc 100644 --- a/cspell.json +++ b/cspell.json @@ -355,11 +355,11 @@ "TINYINT", "hostings", "rulesets", + "newtx", "pwreset", "pagebottom", "forceable", "phpstan" - ], "ignorePaths": [ "tests/**", diff --git a/src/library/Payment/Adapter/Custom.php b/src/library/Payment/Adapter/Custom.php index 4ad030996..ee366d7af 100644 --- a/src/library/Payment/Adapter/Custom.php +++ b/src/library/Payment/Adapter/Custom.php @@ -34,12 +34,14 @@ class Payment_Adapter_Custom 'width' => '50px', ), 'form' => array( - 'single' => array('textarea', array( - 'label' => 'Enter your text for single payment information', + 'single' => array( + 'textarea', array( + 'label' => 'Enter your text for single payment information', ), ), - 'recurrent' => array('textarea', array( - 'label' => 'Enter your text for subscription information', + 'recurrent' => array( + 'textarea', array( + 'label' => 'Enter your text for subscription information', ), ), ), @@ -66,8 +68,48 @@ class Payment_Adapter_Custom return $systemService->renderString($vars['_tpl'], true, $vars); } - public function processTransaction(Api_Handler $api_admin, int $id, array $data, int $gateway_id) + /** + * Processes a transaction using a custom payment adapter. + * + * @param \Api_Handler $api_admin The API admin object. + * @param int $id The ID of the transaction to process. + * @param array $data The data associated with the transaction. + * @param int $gateway_id The ID of the payment gateway to use. + * + * @return bool Returns true if the transaction was processed successfully, false otherwise. + */ + public function processTransaction(\Api_Handler $api_admin, int $id, array $data, int $gateway_id) { - return true; + try { + // Get the transaction and invoice associated with the transaction + $tx = $this->di['db']->getExistingModelById('Transaction', $id); + $invoice = $this->di['db']->getExistingModelById('Invoice', $tx->invoice_id); + + // Load the payment gateway and client associated with the transaction + $gateway = $this->di['db']->load('PayGateway', $tx->gateway_id); + $clientService = $this->di['mod_service']('Client'); + $client = $clientService->get(['id' => $invoice->client_id]); + + // Calculate the total amount of the invoice + $invoiceService = $this->di['mod_service']('Invoice'); + $invoiceTotal = $invoiceService->getTotalWithTax($invoice); + + // Add funds to the client's account and mark the invoice as paid + $tx_desc = $gateway->title . ' transaction No: ' . $tx->txn_id; + $clientService->addFunds($client, $invoiceTotal, $tx_desc, []); + $invoiceService->markAsPaid($invoice, true, true); + + // Update the transaction status and details + $tx->status = 'succeeded'; + $tx->amount = $invoiceTotal; + $tx->note = $gateway->title . ' transaction No: ' . $tx->txn_id; + $tx->currency = $invoice->currency; + $tx->updated_at = date('Y-m-d H:i:s'); + + // Store the updated transaction and use it's return to indicate a sucsess or failure. + return $this->di['db']->store($tx); + } catch (\Exception $e) { + return false; + } } } diff --git a/src/modules/Invoice/Api/Admin.php b/src/modules/Invoice/Api/Admin.php index 5941f3f83..868163039 100644 --- a/src/modules/Invoice/Api/Admin.php +++ b/src/modules/Invoice/Api/Admin.php @@ -63,9 +63,35 @@ class Admin extends \Api_Abstract if (isset($data['execute']) && $data['execute']) { $execute = true; } - $model = $this->_getInvoice($data); + $invoice = $this->_getInvoice($data); + $gateway_id = [ 'id' => $invoice->gateway_id ]; + $payGateway = $this->gateway_get($gateway_id); + $charge = false; - return $this->getService()->markAsPaid($model, false, $execute); + // Check if the payment type is "Custom Payment", Add the transaction and process it. + if ($payGateway['code'] == 'Custom' && $payGateway['enabled'] == 1) { + // create transaction + $transactionService = $this->di['mod_service']('Invoice', 'Transaction'); + $newtx = $transactionService->create([ + 'invoice_id' => $invoice->id, + 'bb_invoice_id' => $invoice->id, + 'gateway_id' => $invoice->gateway_id, + 'bb_gateway_id' => $invoice->gateway_id, + 'currency' => $invoice->currency, + 'status' => 'received', + 'txn_id' => $data['transactionId'], + ]); + + try { + return $transactionService->processTransaction($newtx); + } catch (\Exception $e) + { + $this->di['logger']->info('Error processing transaction: '.$e->getMessage()); + } + + } + + return $this->getService()->markAsPaid($invoice, $charge, $execute); } /** diff --git a/src/modules/Invoice/Service.php b/src/modules/Invoice/Service.php index f311c21a1..4e3723dde 100644 --- a/src/modules/Invoice/Service.php +++ b/src/modules/Invoice/Service.php @@ -376,6 +376,8 @@ class Service implements InjectionAwareInterface $systemService = $this->di['mod_service']('system'); $ctable = $this->di['mod_service']('Currency'); + + $invoice->serie = $systemService->getParamValue('invoice_series_paid'); $invoice->approved = true; $invoice->currency_rate = $ctable->getRateByCode($invoice->currency); @@ -388,6 +390,8 @@ class Service implements InjectionAwareInterface $this->di['events_manager']->fire(['event' => 'onAfterAdminInvoicePaymentReceived', 'params' => ['id' => $invoice->id]]); + + if ($execute) { foreach ($invoiceItems as $item) { try { diff --git a/src/modules/Invoice/ServiceTransaction.php b/src/modules/Invoice/ServiceTransaction.php index 4ca932ff8..df70d54d6 100644 --- a/src/modules/Invoice/ServiceTransaction.php +++ b/src/modules/Invoice/ServiceTransaction.php @@ -363,7 +363,6 @@ class ServiceTransaction implements InjectionAwareInterface } $ipn = json_decode($tx->ipn, 1); - return $adapter->processTransaction($this->di['api_system'], $id, $ipn, $tx->gateway_id); } diff --git a/src/modules/Invoice/html_admin/mod_invoice_invoice.html.twig b/src/modules/Invoice/html_admin/mod_invoice_invoice.html.twig index 4c2424efd..8843afe54 100644 --- a/src/modules/Invoice/html_admin/mod_invoice_invoice.html.twig +++ b/src/modules/Invoice/html_admin/mod_invoice_invoice.html.twig @@ -141,12 +141,44 @@ {{ 'Send reminder'|trans }} - + {{ 'Mark as paid'|trans }} +
+ + {% endif %} {% if invoice.status == 'paid' %} @@ -698,6 +730,7 @@ return false; }); }); + {% endautoescape %} {% endblock %} diff --git a/tests/modules/Invoice/Api/AdminTest.php b/tests/modules/Invoice/Api/AdminTest.php index 030c4e43f..15497c914 100644 --- a/tests/modules/Invoice/Api/AdminTest.php +++ b/tests/modules/Invoice/Api/AdminTest.php @@ -103,7 +103,7 @@ class AdminTest extends \BBTestCase { $di = new \Pimple\Container(); $di['validator'] = $validatorMock; $di['db'] = $dbMock; - + $di['mod_service'] = $di->protect(function ($name) use ($serviceMock) { return $serviceMock; }); $this->api->setDi($di); $this->api->setService($serviceMock); @@ -111,6 +111,8 @@ class AdminTest extends \BBTestCase { $this->assertTrue($result); } + + public function testprepare() { $data = array(