Working on payment methods

This commit is contained in:
David Bomba 2019-09-09 12:19:19 +10:00
parent 0856fc2dfd
commit 8a90d46287
10 changed files with 95 additions and 79 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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;
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;
});
$gateways = $this->company->company_gateways;
}

View File

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

View File

@ -1,39 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Models;
use App\Models\Client;
use App\Models\Company;
use App\Models\CompanyGateway;
use App\Models\Gateway;
use App\Models\GatewayType;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
class CompanyGatewaySetting extends BaseModel
{
public function company()
{
return $this->belongsTo(Company::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function company_gateway()
{
return $this->belongsTo(CompanyGateway::class);
}
}

View File

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

View File

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

View File

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