Mark as paid now adds funds if the Payment Provider is "Custom". (#1487)

* When you press the "Mark as paid" Button on the invoice, you can now enter a transaction id.
If the Payment gateway of the Invoice is "Custom" and it's enabled, the transaction ID will be used to add transactions that add funds in the amount of the invoice to the customer. Then, it will be marked as paid, just like with any other payment provider.

* Added processTransaction method to the Custom Payment Gateway.
* Fixed tests by removing unnecessary tests for Payment Adapters that don't have the processTransaction method.


---------

Co-authored-by: Christoph Schläpfer <chris+github@cleverly.ch>
Co-authored-by: Belle Aerni <belleaerni@gmail.com>
Co-authored-by: Jaap Marcus <9754650+jaapmarcus@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2023-09-28 13:14:06 +02:00
1 parent bf3c40719e
commit f2f9701359
7 files changed
+118 -12

No files matched your search

+1 -1
View File
@@ -355,11 +355,11 @@
"TINYINT",
"hostings",
"rulesets",
"newtx",
"pwreset",
"pagebottom",
"forceable",
"phpstan"
],
"ignorePaths": [
"tests/**",
+48 -6
View File
@@ -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;
}
}
}
+28 -2
View File
@@ -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);
}
/**
+4
View File
@@ -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 {
@@ -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);
}
@@ -141,12 +141,44 @@
</svg>
<span>{{ 'Send reminder'|trans }}</span>
</a>
<a href="{{ 'api/admin/invoice/mark_as_paid'|link({ 'id': invoice.id, 'execute': 1, 'CSRFToken': CSRFToken }) }}" class="btn btn-primary api-link" data-api-reload="Invoice marked as paid">
<a href="#" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#transactionModal">
<svg class="icon" width="24" height="24">
<use xlink:href="#check" />
</svg>
<span>{{ 'Mark as paid'|trans }}</span>
</a>
<div class="modal fade" id="transactionModal" tabindex="-1" aria-labelledby="transactionModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form class="modal-content api-form" method="post" action="{{ 'api/admin/invoice/mark_as_paid'|link({ 'id': invoice.id, 'execute': 1, 'CSRFToken': CSRFToken }) }}" name="transactionForm" data-api-reload="1">
<div class="modal-header">
<h5 class="modal-title" id="transactionModalLabel">{{ 'Enter Custom Transaction ID'|trans }}:</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="hidden" name="CSRFToken" value="{{ CSRFToken }}">
<input type="hidden" name="id" value="{{ invoice.id }}">
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="transaction-id">{{ 'Transaction ID'|trans }}:</label>
<div class="col">
<input class="form-control" type="text" name="transactionId" id="transaction-id">
</div>
</div>
<div class="mb-3 row">
<div class="col">
{{ 'Add the transaction ID of the payment to mark the invoice as paid.' |trans }}<br />
{{ 'This should be the transaction ID you use to reconcile your bank statement.' |trans }}
</div>
</div>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-link link-secondary" data-bs-dismiss="modal">{{ 'Cancel'|trans }}</a>
<input type="submit" class="btn btn-primary" value="{{ 'Submit'|trans }}">
</div>
</form>
</div>
</div>
{% endif %}
{% if invoice.status == 'paid' %}
@@ -698,6 +730,7 @@
return false;
});
});
</script>
{% endautoescape %}
{% endblock %}
+3 -1
View File
@@ -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(