Restrict payments based on gateway type limits

This commit is contained in:
Joshua Dwire 2016-09-08 22:12:51 -04:00
parent 3c4ad52463
commit ca71ea5622
3 changed files with 49 additions and 8 deletions

View File

@ -173,8 +173,8 @@ class ClientPortalController extends BaseController
foreach ($account->account_gateways as $accountGateway) { foreach ($account->account_gateways as $accountGateway) {
$paymentDriver = $accountGateway->paymentDriver($invitation); $paymentDriver = $accountGateway->paymentDriver($invitation);
$links = array_merge($links, $paymentDriver->tokenLinks()); $links = array_merge($links, $paymentDriver->tokenLinks($invitation->invoice));
$links = array_merge($links, $paymentDriver->paymentLinks()); $links = array_merge($links, $paymentDriver->paymentLinks($invitation->invoice));
} }
return $links; return $links;

View File

@ -11,4 +11,12 @@ class PaymentType extends Eloquent
* @var bool * @var bool
*/ */
public $timestamps = false; public $timestamps = false;
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function gatewayType()
{
return $this->belongsTo('App\Models\GatewayType');
}
} }

View File

@ -2,12 +2,14 @@
use URL; use URL;
use Session; use Session;
use Utils;
use Request; use Request;
use Omnipay; use Omnipay;
use Exception; use Exception;
use CreditCard; use CreditCard;
use DateTime; use DateTime;
use App\Models\AccountGatewayToken; use App\Models\AccountGatewayToken;
use App\Models\AccountGatewaySettings;
use App\Models\Account; use App\Models\Account;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentMethod; use App\Models\PaymentMethod;
@ -733,7 +735,7 @@ class BasePaymentDriver
return $this->createPayment($ref); return $this->createPayment($ref);
} }
public function tokenLinks() public function tokenLinks($invoice)
{ {
if ( ! $this->customer()) { if ( ! $this->customer()) {
return []; return [];
@ -747,6 +749,10 @@ class BasePaymentDriver
continue; continue;
} }
if ( !$this->invoiceMeetsGatewayTypeLimits($invoice, $paymentMethod->payment_type->gateway_type_id) ) {
continue;
}
$url = URL::to("/payment/{$this->invitation->invitation_key}/token/".$paymentMethod->public_id); $url = URL::to("/payment/{$this->invitation->invitation_key}/token/".$paymentMethod->public_id);
if ($paymentMethod->payment_type_id == PAYMENT_TYPE_ACH) { if ($paymentMethod->payment_type_id == PAYMENT_TYPE_ACH) {
@ -770,24 +776,51 @@ class BasePaymentDriver
return $links; return $links;
} }
public function paymentLinks() public function paymentLinks($invoice)
{ {
$links = []; $links = [];
foreach ($this->gatewayTypes() as $gatewayType) { foreach ($this->gatewayTypes() as $gatewayTypeId) {
if ($gatewayType === GATEWAY_TYPE_TOKEN) { if ($gatewayTypeId === GATEWAY_TYPE_TOKEN) {
continue;
}
if ( !$this->invoiceMeetsGatewayTypeLimits($invoice, $gatewayTypeId) ) {
continue; continue;
} }
$links[] = [ $links[] = [
'url' => $this->paymentUrl($gatewayType), 'url' => $this->paymentUrl($gatewayTypeId),
'label' => trans("texts.{$gatewayType}") 'label' => trans("texts.{$gatewayTypeId}")
]; ];
} }
return $links; 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) protected function paymentUrl($gatewayType)
{ {
$account = $this->account(); $account = $this->account();