1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00

Fixes for getPaymentMethods

This commit is contained in:
David Bomba 2021-01-18 13:12:48 +11:00
parent d5ad84e024
commit 69bc2f0183
2 changed files with 23 additions and 17 deletions

View File

@ -12,6 +12,7 @@
namespace App\Services\Client; namespace App\Services\Client;
use App\Models\Client; use App\Models\Client;
use App\Services\Client\PaymentMethod;
use App\Utils\Number; use App\Utils\Number;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
@ -63,6 +64,10 @@ class ClientService
->sortBy('created_at'); ->sortBy('created_at');
} }
public function getPaymentMethods(float $amount)
{
return (new PaymentMethod($this->client, $amount))->run();
}
public function save() :Client public function save() :Client
{ {

View File

@ -11,11 +11,8 @@
namespace App\Services\Client; namespace App\Services\Client;
use App\DataMapper\InvoiceItem; use App\Models\Client;
use App\Events\Invoice\InvoiceWasPaid;
use App\Events\Invoice\InvoiceWasUpdated;
use App\Models\CompanyGateway; use App\Models\CompanyGateway;
use App\Models\Credit;
use App\Models\GatewayType; use App\Models\GatewayType;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
@ -36,18 +33,21 @@ class PaymentMethod
private $payment_urls = []; private $payment_urls = [];
public function __construct(Credit $client, float $amount) public function __construct(Client $client, float $amount)
{ {
$this->client = $client; $this->client = $client;
$this->amount = $amount; $this->amount = $amount;
} }
public function run() :Credit public function run()
{ {
$this->getGateways() $this->getGateways()
->getMethods() ->getMethods()
->buildUrls(); ->buildUrls();
return $this->getPaymentUrls();
} }
public function getPaymentUrls() public function getPaymentUrls()
@ -131,12 +131,12 @@ class PaymentMethod
{ {
// we should prefilter $gateway->driver($this)->gatewayTypes() // we should prefilter $gateway->driver($this)->gatewayTypes()
// and only include the enabled payment methods on the gateway // and only include the enabled payment methods on the gateway
$this->$this->payment_methods = []; $this->payment_methods = [];
foreach ($this->gateways as $gateway) { foreach ($this->gateways as $gateway) {
foreach ($gateway->driver($this)->gatewayTypes() as $type) { foreach ($gateway->driver($this->client)->gatewayTypes() as $type) {
if (isset($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) { if (isset($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $type)) {
if ($this->validGatewayForAmount($gateway->fees_and_limits->{$type}, $amount)) { if ($this->validGatewayForAmount($gateway->fees_and_limits->{$type}, $this->amount)) {
$this->payment_methods[] = [$gateway->id => $type]; $this->payment_methods[] = [$gateway->id => $type];
} }
} else { } else {
@ -156,9 +156,9 @@ class PaymentMethod
//note we have to use GatewayType::CREDIT_CARD as alias for CUSTOM //note we have to use GatewayType::CREDIT_CARD as alias for CUSTOM
foreach ($this->gateways as $gateway) { foreach ($this->gateways as $gateway) {
foreach ($gateway->driver($this)->gatewayTypes() as $type) { foreach ($gateway->driver($this->client)->gatewayTypes() as $type) {
if (isset($gateway->fees_and_limits) && property_exists($gateway->fees_and_limits, $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)) { if ($this->validGatewayForAmount($gateway->fees_and_limits->{GatewayType::CREDIT_CARD}, $this->amount)) {
$this->payment_methods->push([$gateway->id => $type]); $this->payment_methods->push([$gateway->id => $type]);
} }
} else { } else {
@ -167,16 +167,17 @@ class PaymentMethod
} }
} }
return $this;
} }
private function buildUrls() private function buildUrls()
{ {
foreach ($payment_methods_intersect as $key => $child_array) { foreach ($this->payment_methods as $key => $child_array) {
foreach ($child_array as $gateway_id => $gateway_type_id) { foreach ($child_array as $gateway_id => $gateway_type_id) {
$gateway = CompanyGateway::find($gateway_id); $gateway = CompanyGateway::find($gateway_id);
$fee_label = $gateway->calcGatewayFeeLabel($amount, $this); $fee_label = $gateway->calcGatewayFeeLabel($this->amount, $this->client);
if(!$gateway_type_id){ if(!$gateway_type_id){
@ -201,7 +202,7 @@ class PaymentMethod
// Show credits as only payment option if both statements are true. // Show credits as only payment option if both statements are true.
if ( if (
$this->client->service()->getCreditBalance() > $amount $this->client->service()->getCreditBalance() > $this->amount
&& $this->client->getSetting('use_credits_payment') == 'always') { && $this->client->getSetting('use_credits_payment') == 'always') {
$payment_urls = []; $payment_urls = [];
} }
@ -225,11 +226,11 @@ class PaymentMethod
return true; 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) { if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $fees_and_limits->min_limit != -1 && $this->amount < $fees_and_limits->min_limit) {
return false; 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) { if ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $fees_and_limits->max_limit != -1 && $this->amount > $fees_and_limits->max_limit) {
return false; return false;
} }