From ca71ea562252e44bc7f219ce44dfdde3313386f7 Mon Sep 17 00:00:00 2001 From: Joshua Dwire Date: Thu, 8 Sep 2016 22:12:51 -0400 Subject: [PATCH] Restrict payments based on gateway type limits --- .../Controllers/ClientPortalController.php | 4 +- app/Models/PaymentType.php | 8 ++++ .../PaymentDrivers/BasePaymentDriver.php | 45 ++++++++++++++++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/ClientPortalController.php b/app/Http/Controllers/ClientPortalController.php index fa396a68240b..35da46b81651 100644 --- a/app/Http/Controllers/ClientPortalController.php +++ b/app/Http/Controllers/ClientPortalController.php @@ -173,8 +173,8 @@ class ClientPortalController extends BaseController foreach ($account->account_gateways as $accountGateway) { $paymentDriver = $accountGateway->paymentDriver($invitation); - $links = array_merge($links, $paymentDriver->tokenLinks()); - $links = array_merge($links, $paymentDriver->paymentLinks()); + $links = array_merge($links, $paymentDriver->tokenLinks($invitation->invoice)); + $links = array_merge($links, $paymentDriver->paymentLinks($invitation->invoice)); } return $links; diff --git a/app/Models/PaymentType.php b/app/Models/PaymentType.php index 01e96239fde0..811ed0a486e8 100644 --- a/app/Models/PaymentType.php +++ b/app/Models/PaymentType.php @@ -11,4 +11,12 @@ class PaymentType extends Eloquent * @var bool */ public $timestamps = false; + + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function gatewayType() + { + return $this->belongsTo('App\Models\GatewayType'); + } } diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 87eae2684e11..9b6f00f6e58b 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -2,12 +2,14 @@ use URL; use Session; +use Utils; use Request; use Omnipay; use Exception; use CreditCard; use DateTime; use App\Models\AccountGatewayToken; +use App\Models\AccountGatewaySettings; use App\Models\Account; use App\Models\Payment; use App\Models\PaymentMethod; @@ -733,7 +735,7 @@ class BasePaymentDriver return $this->createPayment($ref); } - public function tokenLinks() + public function tokenLinks($invoice) { if ( ! $this->customer()) { return []; @@ -747,6 +749,10 @@ class BasePaymentDriver continue; } + if ( !$this->invoiceMeetsGatewayTypeLimits($invoice, $paymentMethod->payment_type->gateway_type_id) ) { + continue; + } + $url = URL::to("/payment/{$this->invitation->invitation_key}/token/".$paymentMethod->public_id); if ($paymentMethod->payment_type_id == PAYMENT_TYPE_ACH) { @@ -770,24 +776,51 @@ class BasePaymentDriver return $links; } - public function paymentLinks() + public function paymentLinks($invoice) { $links = []; - foreach ($this->gatewayTypes() as $gatewayType) { - if ($gatewayType === GATEWAY_TYPE_TOKEN) { + foreach ($this->gatewayTypes() as $gatewayTypeId) { + if ($gatewayTypeId === GATEWAY_TYPE_TOKEN) { + continue; + } + + if ( !$this->invoiceMeetsGatewayTypeLimits($invoice, $gatewayTypeId) ) { continue; } $links[] = [ - 'url' => $this->paymentUrl($gatewayType), - 'label' => trans("texts.{$gatewayType}") + 'url' => $this->paymentUrl($gatewayTypeId), + 'label' => trans("texts.{$gatewayTypeId}") ]; } return $links; } + protected function invoiceMeetsGatewayTypeLimits( $invoice, $gatewayTypeId ) { + if ( !$gatewayTypeId ) { + return true; + } + + $accountGatewaySettings = AccountGatewaySettings::scope()->where('account_gateway_settings.gateway_type_id', + '=', $gatewayTypeId)->first(); + + if ($accountGatewaySettings) { + if ($accountGatewaySettings->min_limit && $invoice->balance < $accountGatewaySettings->min_limit) { + return false; + } + + if ($accountGatewaySettings->max_limit && $invoice->balance >= $accountGatewaySettings->max_limit + 1) { + // The max is limit_max + 0.99, so we add 1 to max_limit + // Then, if the balance is greater than or equal to that value, it's over the max. + return false; + } + } + + return true; + } + protected function paymentUrl($gatewayType) { $account = $this->account();