diff --git a/app/Http/Controllers/AccountGatewayController.php b/app/Http/Controllers/AccountGatewayController.php index ccb291988429..aec0212b098c 100644 --- a/app/Http/Controllers/AccountGatewayController.php +++ b/app/Http/Controllers/AccountGatewayController.php @@ -101,8 +101,9 @@ class AccountGatewayController extends BaseController $data['title'] = trans('texts.add_gateway'); if ($otherProviders) { - $data['primaryGateways'] = Gateway::primary($accountGatewaysIds)->orderBy('name', 'desc')->get(); - $data['secondaryGateways'] = Gateway::secondary($accountGatewaysIds)->orderBy('name')->get(); + $availableGatewaysIds = $account->availableGatewaysIds(); + $data['primaryGateways'] = Gateway::primary($availableGatewaysIds)->orderBy('name', 'desc')->get(); + $data['secondaryGateways'] = Gateway::secondary($availableGatewaysIds)->orderBy('name')->get(); $data['hiddenFields'] = Gateway::$hiddenFields; return View::make('accounts.account_gateway', $data); diff --git a/app/Models/Account.php b/app/Models/Account.php index 3ea45aaeab5f..df02a059d4aa 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -9,6 +9,7 @@ use Cache; use App; use File; use App\Models\Document; +use App\Models\AccountGateway; use App\Events\UserSettingsChanged; use Illuminate\Support\Facades\Storage; use Illuminate\Database\Eloquent\SoftDeletes; @@ -418,6 +419,39 @@ class Account extends Eloquent return false; } + public function availableGatewaysIds() + { + if ( ! $this->relationLoaded('account_gateways')) { + $this->load('account_gateways'); + } + + $gatewayTypes = []; + $gatewayIds = []; + + foreach ($this->account_gateways as $accountGateway) { + $paymentDriver = $accountGateway->paymentDriver(); + $gatewayTypes = array_unique(array_merge($gatewayTypes, $paymentDriver->gatewayTypes())); + } + + foreach (Cache::get('gateways') as $gateway) { + $paymentDriverClass = AccountGateway::paymentDriverClass($gateway->provider); + $paymentDriver = new $paymentDriverClass(); + $available = true; + + foreach ($gatewayTypes as $type) { + if ($paymentDriver->handles($type)) { + $available = false; + break; + } + } + if ($available) { + $gatewayIds[] = $gateway->id; + } + } + + return $gatewayIds; + } + public function paymentDriver($invitation = false, $gatewayType = false) { if ($accountGateway = $this->getGatewayByType($gatewayType)) { diff --git a/app/Models/AccountGateway.php b/app/Models/AccountGateway.php index e2690b9b6c38..54cbdfcbd833 100644 --- a/app/Models/AccountGateway.php +++ b/app/Models/AccountGateway.php @@ -37,20 +37,26 @@ class AccountGateway extends EntityModel return $arrayOfImages; } - public function paymentDriver($invitation = false, $gatewayType = false) + public static function paymentDriverClass($provider) { $folder = "App\\Ninja\\PaymentDrivers\\"; - $class = $folder . $this->gateway->provider . 'PaymentDriver'; + $class = $folder . $provider . 'PaymentDriver'; $class = str_replace('_', '', $class); if (class_exists($class)) { - return new $class($this, $invitation, $gatewayType); + return $class; } else { - $baseClass = $folder . "BasePaymentDriver"; - return new $baseClass($this, $invitation, $gatewayType); + return $folder . "BasePaymentDriver"; } } + public function paymentDriver($invitation = false, $gatewayType = false) + { + $class = static::paymentDriverClass($this->gateway->provider); + + return new $class($this, $invitation, $gatewayType); + } + public function isGateway($gatewayId) { return $this->gateway_id == $gatewayId; diff --git a/app/Models/Gateway.php b/app/Models/Gateway.php index 57064bb73f91..49b59d2c050d 100644 --- a/app/Models/Gateway.php +++ b/app/Models/Gateway.php @@ -25,6 +25,8 @@ class Gateway extends Eloquent GATEWAY_DWOLLA, GATEWAY_STRIPE, GATEWAY_BRAINTREE, + GATEWAY_AUTHORIZE_NET, + GATEWAY_MOLLIE, ]; // allow adding these gateway if another gateway @@ -81,25 +83,16 @@ class Gateway extends Eloquent { $query->where('payment_library_id', '=', 1) ->where('id', '!=', GATEWAY_WEPAY) - ->whereIn('id', Gateway::$preferred) - ->whereNotIn('id', $accountGatewaysIds); - - // if the user has a credit card gateway only show alternate options - if (static::hasStandardGateway($accountGatewaysIds)) { - $query->whereNotIn('id', array_diff(static::$preferred, static::$alternate)); - } + ->whereIn('id', static::$preferred) + ->whereIn('id', $accountGatewaysIds); } public function scopeSecondary($query, $accountGatewaysIds) { - // if the user has a credit card don't show an secondary options - if (static::hasStandardGateway($accountGatewaysIds)) { - $query->where('id', '=', 0); - } else { - $query->where('payment_library_id', '=', 1) - ->where('id', '!=', GATEWAY_WEPAY) - ->whereNotIn('id', static::$preferred); - } + $query->where('payment_library_id', '=', 1) + ->where('id', '!=', GATEWAY_WEPAY) + ->whereNotIn('id', static::$preferred) + ->whereIn('id', $accountGatewaysIds); } public function getHelp() diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index eded20357157..21522702b89b 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -30,7 +30,7 @@ class BasePaymentDriver protected $customerReferenceParam; protected $transactionReferenceParam; - public function __construct($accountGateway, $invitation = false, $gatewayType = false) + public function __construct($accountGateway = false, $invitation = false, $gatewayType = false) { $this->accountGateway = $accountGateway; $this->invitation = $invitation; @@ -47,7 +47,7 @@ class BasePaymentDriver return $this->gatewayType === $gatewayType; } - protected function gatewayTypes() + public function gatewayTypes() { return [ GATEWAY_TYPE_CREDIT_CARD diff --git a/app/Ninja/PaymentDrivers/BitPayPaymentDriver.php b/app/Ninja/PaymentDrivers/BitPayPaymentDriver.php index 420c876a7090..df1821acdb96 100644 --- a/app/Ninja/PaymentDrivers/BitPayPaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BitPayPaymentDriver.php @@ -2,7 +2,7 @@ class BitPayPaymentDriver extends BasePaymentDriver { - protected function gatewayTypes() + public function gatewayTypes() { return [ GATEWAY_TYPE_BITCOIN diff --git a/app/Ninja/PaymentDrivers/BraintreePaymentDriver.php b/app/Ninja/PaymentDrivers/BraintreePaymentDriver.php index 13fa2bbf7e24..d714b8161c9c 100644 --- a/app/Ninja/PaymentDrivers/BraintreePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BraintreePaymentDriver.php @@ -9,14 +9,14 @@ class BraintreePaymentDriver extends BasePaymentDriver protected $customerReferenceParam = 'customerId'; protected $sourceReferenceParam = 'paymentMethodToken'; - protected function gatewayTypes() + public function gatewayTypes() { $types = [ GATEWAY_TYPE_CREDIT_CARD, GATEWAY_TYPE_TOKEN, ]; - if ($this->accountGateway->getPayPalEnabled()) { + if ($this->accountGateway && $this->accountGateway->getPayPalEnabled()) { $types[] = GATEWAY_TYPE_PAYPAL; } diff --git a/app/Ninja/PaymentDrivers/DwollaPaymentDriver.php b/app/Ninja/PaymentDrivers/DwollaPaymentDriver.php index bfec26068a3f..26f5626c8c3d 100644 --- a/app/Ninja/PaymentDrivers/DwollaPaymentDriver.php +++ b/app/Ninja/PaymentDrivers/DwollaPaymentDriver.php @@ -2,7 +2,7 @@ class DwollaPaymentDriver extends BasePaymentDriver { - protected function gatewayTypes() + public function gatewayTypes() { return [GATEWAY_TYPE_DWOLLA]; } diff --git a/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php index 88e3639efa4d..15ad415fdec8 100644 --- a/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/Ninja/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -4,7 +4,7 @@ use Exception; class PayPalExpressPaymentDriver extends BasePaymentDriver { - protected function gatewayTypes() + public function gatewayTypes() { return [ GATEWAY_TYPE_PAYPAL diff --git a/app/Ninja/PaymentDrivers/PayPalProPaymentDriver.php b/app/Ninja/PaymentDrivers/PayPalProPaymentDriver.php index 9acf75e7cdfe..0c8344cb5705 100644 --- a/app/Ninja/PaymentDrivers/PayPalProPaymentDriver.php +++ b/app/Ninja/PaymentDrivers/PayPalProPaymentDriver.php @@ -2,7 +2,7 @@ class PayPalProPaymentDriver extends BasePaymentDriver { - protected function gatewayTypes() + public function gatewayTypes() { return [ GATEWAY_TYPE_CREDIT_CARD diff --git a/app/Ninja/PaymentDrivers/StripePaymentDriver.php b/app/Ninja/PaymentDrivers/StripePaymentDriver.php index a197ac6a34a4..c733796a6462 100644 --- a/app/Ninja/PaymentDrivers/StripePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/StripePaymentDriver.php @@ -8,14 +8,14 @@ class StripePaymentDriver extends BasePaymentDriver { protected $customerReferenceParam = 'customerReference'; - protected function gatewayTypes() + public function gatewayTypes() { $types = [ GATEWAY_TYPE_CREDIT_CARD, GATEWAY_TYPE_TOKEN ]; - if ($this->accountGateway->getAchEnabled()) { + if ($this->accountGateway && $this->accountGateway->getAchEnabled()) { $types[] = GATEWAY_TYPE_BANK_TRANSFER; } diff --git a/app/Ninja/PaymentDrivers/WePayPaymentDriver.php b/app/Ninja/PaymentDrivers/WePayPaymentDriver.php index c79b216db58e..6a8b226ea2da 100644 --- a/app/Ninja/PaymentDrivers/WePayPaymentDriver.php +++ b/app/Ninja/PaymentDrivers/WePayPaymentDriver.php @@ -6,13 +6,18 @@ use Exception; class WePayPaymentDriver extends BasePaymentDriver { - protected function gatewayTypes() + public function gatewayTypes() { - return [ + $types = [ GATEWAY_TYPE_CREDIT_CARD, - GATEWAY_TYPE_BANK_TRANSFER, GATEWAY_TYPE_TOKEN ]; + + if ($this->accountGateway && $this->accountGateway->getAchEnabled()) { + $types[] = GATEWAY_TYPE_BANK_TRANSFER; + } + + return $types; } /*