Improve check for duplicate gateways

This commit is contained in:
Hillel Coren 2016-06-22 21:42:09 +03:00
parent dcdfb4f21c
commit 93c3521952
12 changed files with 74 additions and 35 deletions

View File

@ -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);

View File

@ -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)) {

View File

@ -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;

View File

@ -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()

View File

@ -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

View File

@ -2,7 +2,7 @@
class BitPayPaymentDriver extends BasePaymentDriver
{
protected function gatewayTypes()
public function gatewayTypes()
{
return [
GATEWAY_TYPE_BITCOIN

View File

@ -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;
}

View File

@ -2,7 +2,7 @@
class DwollaPaymentDriver extends BasePaymentDriver
{
protected function gatewayTypes()
public function gatewayTypes()
{
return [GATEWAY_TYPE_DWOLLA];
}

View File

@ -4,7 +4,7 @@ use Exception;
class PayPalExpressPaymentDriver extends BasePaymentDriver
{
protected function gatewayTypes()
public function gatewayTypes()
{
return [
GATEWAY_TYPE_PAYPAL

View File

@ -2,7 +2,7 @@
class PayPalProPaymentDriver extends BasePaymentDriver
{
protected function gatewayTypes()
public function gatewayTypes()
{
return [
GATEWAY_TYPE_CREDIT_CARD

View File

@ -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;
}

View File

@ -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;
}
/*