diff --git a/app/DataMapper/ClientSettings.php b/app/DataMapper/ClientSettings.php index e4d04f59ba73..4e299fb9e711 100644 --- a/app/DataMapper/ClientSettings.php +++ b/app/DataMapper/ClientSettings.php @@ -91,7 +91,7 @@ class ClientSettings extends BaseSettings public $design; - public $default_company_gateway_id; + public $payment_gateways; /** * Cast object values and return entire class * prevents missing properties from not being returned diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 26518c671867..cbf86fb348b3 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -113,7 +113,7 @@ class CompanySettings extends BaseSettings public $design; - public $default_company_gateway_id; + public $payment_gateways; /** * Cast object values and return entire class * prevents missing properties from not being returned diff --git a/app/Factory/PaymentFactory.php b/app/Factory/PaymentFactory.php index 57789bdc872d..42a120a33e2a 100644 --- a/app/Factory/PaymentFactory.php +++ b/app/Factory/PaymentFactory.php @@ -28,7 +28,7 @@ class PaymentFactory $payment->client_id = 0; $payment->client_contact_id = null; $payment->invitation_id = null; - $payment->account_gateway_id = null; + $payment->company_gateway_id = null; $payment->payment_type_id = null; $payment->is_deleted = false; $payment->amount = 0; diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index 17e2023c8643..265605703c9d 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -86,8 +86,10 @@ class InvoiceController extends Controller */ public function show(ShowInvoiceRequest $request, Invoice $invoice) { + + $data = [ - 'invoice' => $invoice + 'invoice' => $invoice, ]; return view('portal.default.invoices.show', $data); @@ -132,6 +134,10 @@ class InvoiceController extends Controller $formatted_total = Number::formatMoney($total, auth()->user()->client->currency(), auth()->user()->client->country, auth()->user()->client->getMergedSettings()); + $payment_methods = auth()->user()->client->getPaymentMethods($total); + + + $data = [ 'invoices' => $invoices, 'formatted_total' => $formatted_total, diff --git a/app/Models/Client.php b/app/Models/Client.php index 0216509fa790..749d64a513cb 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -152,17 +152,41 @@ class Client extends BaseModel return $this->morphMany(Document::class, 'documentable'); } - public function getPaymentMethods() + public function getPaymentMethods($amount) { $settings = $this->getMergedSettings(); /* If we have a single default gateway - pass this back now.*/ - if($settings->default_company_gateway_id); - return $settings->default_company_gateway_id; + if($settings->payment_gateways){ + $gateways = $this->company->company_gateways->whereIn('id', $settings->payment_gateways); + } + else + $gateways = $this->company->company_gateways; - /* If there is no default, then we pass back the Collection of gateways */ + //** Filter gateways based on limits + $gateways->filter(function ($method) use ($amount){ + if($method->min_limit !== null && $amount < $method->min_limit) + return false; - $gateways = $this->company->company_gateways; + if($method->max_limit !== null && $amount > $method->min_limit) + return false; + }); + + //** Get Payment methods from each gateway + $payment_methods = []; + + foreach($gateways as $gateway) + foreach($gateway->driver()->gatewayTypes() as $type) + $payment_methods[] = [$gateway->id => $type]; + + //** Reduce gateways so that only one TYPE is present in the list ie. cannot have multiple credit card options + $payment_methods_collections = collect($payment_methods); + $payment_methods_intersect = $payment_methods_collections->intersectByKeys( $payment_methods_collections->flatten(1)->unique() ); + + $multiplied = $collection->map(function ($item, $key) { + return $item * 2; + }); + } diff --git a/app/Models/Company.php b/app/Models/Company.php index 1b08c707244e..3d2527f630e5 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -124,7 +124,7 @@ class Company extends BaseModel */ public function company_gateways() { - return $this->hasMany(CompanyGateway::class); + return $this->hasMany(CompanyGateway::class)->orderBy('sort_id','ASC'); } /** diff --git a/app/Models/CompanyGatewaySetting.php b/app/Models/CompanyGatewaySetting.php deleted file mode 100644 index ec3fd8d0e4b4..000000000000 --- a/app/Models/CompanyGatewaySetting.php +++ /dev/null @@ -1,39 +0,0 @@ -belongsTo(Company::class); - } - - public function user() - { - return $this->belongsTo(User::class); - } - - public function company_gateway() - { - return $this->belongsTo(CompanyGateway::class); - } -} diff --git a/app/Models/GatewayType.php b/app/Models/GatewayType.php index 9a0a0dc0247f..3902b9a17acf 100644 --- a/app/Models/GatewayType.php +++ b/app/Models/GatewayType.php @@ -32,7 +32,7 @@ class GatewayType extends Model const APPLE_PAY = 11; const CUSTOM2 = 12; const CUSTOM3 = 13; - const TOKEN = 'token'); + const TOKEN = 'token'; public function gateway() { diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 2974378084c9..7d8ba25c3e9f 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -351,6 +351,7 @@ class CreateUsersTable extends Migration $table->unsignedInteger('company_id')->unique(); $table->unsignedInteger('user_id'); $table->unsignedInteger('gateway_id'); + $table->unsignedInteger('gateway_type_id')->nullable(); $table->unsignedInteger('accepted_credit_cards'); $table->boolean('require_cvv')->default(true); $table->boolean('show_address')->default(true)->nullable(); @@ -359,6 +360,17 @@ class CreateUsersTable extends Migration $table->text('config'); $table->unsignedInteger('sort_id')->default(0); + $table->decimal('min_limit', 13, 2)->nullable(); + $table->decimal('max_limit', 13, 2)->nullable(); + $table->decimal('fee_amount', 13, 2)->nullable(); + $table->decimal('fee_percent', 13, 2)->nullable(); + $table->decimal('fee_tax_name1', 13, 2)->nullable(); + $table->decimal('fee_tax_name2', 13, 2)->nullable(); + $table->decimal('fee_tax_rate1', 13, 2)->nullable(); + $table->decimal('fee_tax_rate2', 13, 2)->nullable(); + $table->unsignedInteger('fee_cap')->default(0); + $table->boolean('adjust_fee_percent'); + $table->timestamps(6); $table->softDeletes(); @@ -682,23 +694,23 @@ class CreateUsersTable extends Migration Schema::create('payments', function ($t) { $t->increments('id'); //$t->unsignedInteger('invoice_id')->nullable()->index(); //todo handle payments where there is no invoice OR we are paying MULTIPLE invoices + // *** this is handled by the use of the paymentables table. in here we store the + // entities which have been registered payments against $t->unsignedInteger('company_id')->index(); $t->unsignedInteger('client_id')->index(); + $t->unsignedInteger('user_id')->nullable(); $t->unsignedInteger('client_contact_id')->nullable(); $t->unsignedInteger('invitation_id')->nullable(); - $t->unsignedInteger('user_id')->nullable(); $t->unsignedInteger('company_gateway_id')->nullable(); $t->unsignedInteger('payment_type_id')->nullable(); $t->unsignedInteger('status_id')->index(); - - $t->timestamps(6); - $t->softDeletes(); - - $t->boolean('is_deleted')->default(false); $t->decimal('amount', 13, 2); $t->datetime('payment_date')->nullable(); $t->string('transaction_reference')->nullable(); $t->string('payer_id')->nullable(); + $t->timestamps(6); + $t->softDeletes(); + $t->boolean('is_deleted')->default(false); //$t->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade'); $t->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); @@ -711,7 +723,7 @@ class CreateUsersTable extends Migration }); - Schema::create('paymentables', function ($table) { + Schema::create('paymentables', function ($table) { //allows multiple invoices to one payment $table->unsignedInteger('payment_id'); $table->unsignedInteger('paymentable_id'); $table->string('paymentable_type'); @@ -903,28 +915,6 @@ class CreateUsersTable extends Migration $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade'); }); - - Schema::create('company_gateway_settings', function ($table){ - $table->increments('id'); - $table->unsignedInteger('company_id'); - $table->unsignedInteger('company_gateway_id')->nullable(); - $table->unsignedInteger('gateway_type_id')->nullable(); - $table->unsignedInteger('user_id')->nullable(); - $table->decimal('min_limit', 13, 2)->nullable(); - $table->decimal('max_limit', 13, 2)->nullable(); - $table->decimal('fee_amount', 13, 2)->nullable(); - $table->decimal('fee_percent', 13, 2)->nullable(); - $table->decimal('fee_tax_name1', 13, 2)->nullable(); - $table->decimal('fee_tax_name2', 13, 2)->nullable(); - $table->decimal('fee_tax_rate1', 13, 2)->nullable(); - $table->decimal('fee_tax_rate2', 13, 2)->nullable(); - $table->unsignedInteger('fee_cap')->default(0); - $table->boolean('adjust_fee_percent'); - - $table->timestamps(6); - $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade'); - $table->foreign('company_gateway_id')->references('id')->on('company_gateways')->onDelete('cascade'); - }); } /** diff --git a/tests/Unit/CollectionMergingTest.php b/tests/Unit/CollectionMergingTest.php index 203cb36c1d50..7df52cffdc73 100644 --- a/tests/Unit/CollectionMergingTest.php +++ b/tests/Unit/CollectionMergingTest.php @@ -81,6 +81,41 @@ class CollectionMergingTest extends TestCase $this->assertEquals($term['num_days'], 90); } + public function testUniqueValues() + { + $methods[] = [1 => 1]; + $methods[] = [1 => 2]; + $methods[] = [1 => 3]; + $methods[] = [1 => 4]; + $methods[] = [1 => 5]; + $methods[] = [1 => 6]; + + $other_methods[] = [2 => 1]; + $other_methods[] = [2 => 7]; + $other_methods[] = [2 => 8]; + $other_methods[] = [2 => 9]; + $other_methods[] = [2 => 10]; + + $array = array_merge($methods, $other_methods); + + $this->assertEquals(11, count($array)); + + $collection = collect($array); + + $intersect = $collection->intersectByKeys( $collection->flatten(1)->unique() ); + + $this->assertEquals(10,$intersect->count()); + + $third_methods[] = [3 => 1]; + $third_methods[] = [2 => 11]; + + $array = array_merge($array, $third_methods); + + $collection = collect($array); + $intersect = $collection->intersectByKeys( $collection->flatten(1)->unique() ); + $this->assertEquals(11,$intersect->count()); + + } }