mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Merge pull request #4707 from turbo124/v5-develop
Refactor getPaymentMethods()
This commit is contained in:
commit
d5ad84e024
@ -228,6 +228,9 @@ class BaseDriver extends AbstractPaymentDriver
|
||||
|
||||
$payment->service()->updateInvoicePayment($this->payment_hash);
|
||||
|
||||
if ($this->client->getSetting('client_online_payment_notification'))
|
||||
$payment->service()->sendEmail();
|
||||
|
||||
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
|
||||
|
||||
return $payment->service()->applyNumber()->save();
|
||||
|
@ -202,6 +202,11 @@ class BaseRepository
|
||||
|
||||
/* Model now persisted, now lets do some child tasks */
|
||||
|
||||
/* If client currency differs from the company default currency, then insert the client exchange rate on the model.*/
|
||||
if($client->currency()->id != (int) $model->company->settings->currency_id)
|
||||
$model->exchange_rate = $client->currency()->exchange_rate;
|
||||
|
||||
/* Save any documents */
|
||||
if (array_key_exists('documents', $data))
|
||||
$this->saveDocuments($data['documents'], $model);
|
||||
|
||||
|
240
app/Services/Client/PaymentMethod.php
Normal file
240
app/Services/Client/PaymentMethod.php
Normal file
@ -0,0 +1,240 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Services\Client;
|
||||
|
||||
use App\DataMapper\InvoiceItem;
|
||||
use App\Events\Invoice\InvoiceWasPaid;
|
||||
use App\Events\Invoice\InvoiceWasUpdated;
|
||||
use App\Models\CompanyGateway;
|
||||
use App\Models\Credit;
|
||||
use App\Models\GatewayType;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
use App\Utils\Ninja;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
|
||||
class PaymentMethod
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
private $client;
|
||||
|
||||
private $amount;
|
||||
|
||||
private $gateways;
|
||||
|
||||
private $payment_methods;
|
||||
|
||||
private $payment_urls = [];
|
||||
|
||||
public function __construct(Credit $client, float $amount)
|
||||
{
|
||||
$this->client = $client;
|
||||
$this->amount = $amount;
|
||||
}
|
||||
|
||||
public function run() :Credit
|
||||
{
|
||||
|
||||
$this->getGateways()
|
||||
->getMethods()
|
||||
->buildUrls();
|
||||
}
|
||||
|
||||
public function getPaymentUrls()
|
||||
{
|
||||
return $this->payment_urls;
|
||||
}
|
||||
|
||||
public function getPaymentMethods()
|
||||
{
|
||||
return $this->payment_methods;
|
||||
}
|
||||
|
||||
private function getGateways()
|
||||
{
|
||||
|
||||
$company_gateways = $this->client->getSetting('company_gateway_ids');
|
||||
|
||||
//we need to check for "0" here as we disable a payment gateway for a client with the number "0"
|
||||
if ($company_gateways || $company_gateways == '0') {
|
||||
|
||||
$transformed_ids = $this->transformKeys(explode(',', $company_gateways));
|
||||
|
||||
$this->gateways = $this->client
|
||||
->company
|
||||
->company_gateways
|
||||
->whereIn('id', $transformed_ids)
|
||||
->where('gateway_key', '!=', '54faab2ab6e3223dbe848b1686490baa')
|
||||
->sortby(function ($model) use ($transformed_ids) { //company gateways are sorted in order of priority
|
||||
return array_search($model->id, $transformed_ids);// this closure sorts for us
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
$this->gateways = $this->client
|
||||
->company
|
||||
->company_gateways
|
||||
->where('gateway_key', '!=', '54faab2ab6e3223dbe848b1686490baa')
|
||||
->where('is_deleted', false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
private function getCustomGateways()
|
||||
{
|
||||
|
||||
$company_gateways = $this->client->getSetting('company_gateway_ids');
|
||||
|
||||
//we need to check for "0" here as we disable a payment gateway for a client with the number "0"
|
||||
if ($company_gateways || $company_gateways == '0') {
|
||||
|
||||
$transformed_ids = $this->transformKeys(explode(',', $company_gateways));
|
||||
|
||||
$this->gateways = $this->client
|
||||
->company
|
||||
->company_gateways
|
||||
->whereIn('id', $transformed_ids)
|
||||
->where('gateway_key', '=', '54faab2ab6e3223dbe848b1686490baa')
|
||||
->sortby(function ($model) use ($transformed_ids) { //company gateways are sorted in order of priority
|
||||
return array_search($model->id, $transformed_ids);// this closure sorts for us
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
$this->gateways = $this->client
|
||||
->company
|
||||
->company_gateways
|
||||
->where('gateway_key', '=', '54faab2ab6e3223dbe848b1686490baa')
|
||||
->where('is_deleted', false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function getMethods()
|
||||
{
|
||||
// we should prefilter $gateway->driver($this)->gatewayTypes()
|
||||
// and only include the enabled payment methods on the gateway
|
||||
$this->$this->payment_methods = [];
|
||||
|
||||
foreach ($this->gateways as $gateway) {
|
||||
foreach ($gateway->driver($this)->gatewayTypes() as $type) {
|
||||
if (isset($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) {
|
||||
if ($this->validGatewayForAmount($gateway->fees_and_limits->{$type}, $amount)) {
|
||||
$this->payment_methods[] = [$gateway->id => $type];
|
||||
}
|
||||
} else {
|
||||
$this->payment_methods[] = [$gateway->id => $type];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//transform from Array to Collection
|
||||
$payment_methods_collections = collect($this->payment_methods);
|
||||
|
||||
//** Plucks the remaining keys into its own collection
|
||||
$this->payment_methods = $payment_methods_collections->intersectByKeys($payment_methods_collections->flatten(1)->unique());
|
||||
|
||||
/* Loop through custom gateways if any exist and append them to the methods collection*/
|
||||
$this->getCustomGateways();
|
||||
|
||||
//note we have to use GatewayType::CREDIT_CARD as alias for CUSTOM
|
||||
foreach ($this->gateways as $gateway) {
|
||||
foreach ($gateway->driver($this)->gatewayTypes() as $type) {
|
||||
if (isset($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) {
|
||||
if ($this->validGatewayForAmount($gateway->fees_and_limits->{GatewayType::CREDIT_CARD}, $amount)) {
|
||||
$this->payment_methods->push([$gateway->id => $type]);
|
||||
}
|
||||
} else {
|
||||
$this->payment_methods->push([$gateway->id => NULL]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function buildUrls()
|
||||
{
|
||||
|
||||
foreach ($payment_methods_intersect as $key => $child_array) {
|
||||
foreach ($child_array as $gateway_id => $gateway_type_id) {
|
||||
$gateway = CompanyGateway::find($gateway_id);
|
||||
|
||||
$fee_label = $gateway->calcGatewayFeeLabel($amount, $this);
|
||||
|
||||
if(!$gateway_type_id){
|
||||
|
||||
$this->payment_urls[] = [
|
||||
'label' => $gateway->getConfigField('name') . $fee_label,
|
||||
'company_gateway_id' => $gateway_id,
|
||||
'gateway_type_id' => GatewayType::CREDIT_CARD,
|
||||
];
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->payment_urls[] = [
|
||||
'label' => $gateway->getTypeAlias($gateway_type_id) . $fee_label,
|
||||
'company_gateway_id' => $gateway_id,
|
||||
'gateway_type_id' => $gateway_type_id,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (($this->client->getSetting('use_credits_payment') == 'option' || $this->client->getSetting('use_credits_payment') == 'always') && $this->client->service()->getCreditBalance() > 0) {
|
||||
|
||||
// Show credits as only payment option if both statements are true.
|
||||
if (
|
||||
$this->client->service()->getCreditBalance() > $amount
|
||||
&& $this->client->getSetting('use_credits_payment') == 'always') {
|
||||
$payment_urls = [];
|
||||
}
|
||||
|
||||
$payment_urls[] = [
|
||||
'label' => ctrans('texts.apply_credit'),
|
||||
'company_gateway_id' => CompanyGateway::GATEWAY_CREDIT,
|
||||
'gateway_type_id' => GatewayType::CREDIT,
|
||||
];
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
private function validGatewayForAmount($fees_and_limits_for_payment_type, $amount) :bool
|
||||
{
|
||||
if (isset($fees_and_limits_for_payment_type)) {
|
||||
$fees_and_limits = $fees_and_limits_for_payment_type;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $fees_and_limits->min_limit != -1 && $amount < $fees_and_limits->min_limit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $fees_and_limits->max_limit != -1 && $amount > $fees_and_limits->max_limit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -375,11 +375,6 @@ class InvoiceService
|
||||
if (!isset($this->invoice->terms)) {
|
||||
$this->invoice->terms = $settings->invoice_terms;
|
||||
}
|
||||
|
||||
if(!isset($this->invoice->public_notes)) {
|
||||
$this->invoice->public_notes = $settings->public_notes;
|
||||
}
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -74,9 +74,8 @@ class MarkPaid extends AbstractService
|
||||
->applyNumber()
|
||||
->save();
|
||||
|
||||
if ($this->invoice->client->getSetting('client_manual_payment_notification')) {
|
||||
EmailPayment::dispatch($payment, $payment->company, $payment->client->primary_contact()->first());
|
||||
}
|
||||
if ($this->invoice->client->getSetting('client_manual_payment_notification'))
|
||||
$payment->service()->sendEmail();
|
||||
|
||||
/* Update Invoice balance */
|
||||
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
|
||||
|
Loading…
x
Reference in New Issue
Block a user