From 1a7d0d3cbd648aae11b088fc665350a5fb48852e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 27 Aug 2020 22:12:39 +1000 Subject: [PATCH] Working on gateway fees across multiple invoices --- .../ClientPortal/PaymentController.php | 25 ++++++++---- .../Payments/PaymentResponseRequest.php | 39 +++++++++++++++++++ app/Jobs/Invoice/EmailInvoice.php | 1 + app/Models/Company.php | 2 +- app/Models/CompanyGateway.php | 28 +++++++++++++ app/Models/PaymentHash.php | 1 - ...40557_add_is_public_to_documents_table.php | 1 + resources/views/index/index.blade.php | 5 +++ .../authorize/credit_card_payment.blade.php | 4 +- 9 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 app/Http/Requests/ClientPortal/Payments/PaymentResponseRequest.php diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index ea2ee36a47..9ff34d666c 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -14,6 +14,7 @@ namespace App\Http\Controllers\ClientPortal; use App\Filters\PaymentFilters; use App\Http\Controllers\Controller; +use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Jobs\Invoice\InjectSignature; use App\Models\CompanyGateway; use App\Models\Invoice; @@ -142,17 +143,18 @@ class PaymentController extends Controller $payment_methods = auth()->user()->client->getPaymentMethods(array_sum(array_column($payable_invoices, 'amount_with_fee'))); $payment_method_id = request()->input('payment_method_id'); - $payment_hash = new PaymentHash; - $payment_hash->hash = Str::random(128); - $payment_hash->data = $payable_invoices; - $payment_hash->save(); - $invoice_totals = array_sum(array_column($payable_invoices,'amount')); $fee_totals = $gateway->calcGatewayFee($invoice_totals); + $payment_hash = new PaymentHash; + $payment_hash->hash = Str::random(128); + $payment_hash->data = $payable_invoices; + $payment_hash->fees = $fee_totals; + $payment_hash->save(); + $totals = [ 'invoice_totals' => $invoice_totals, - 'fee_totals' => $fee_totals, + 'fee_total' => $fee_totals, 'amount_with_fee' => $invoice_totals + $fee_totals, ]; @@ -170,10 +172,17 @@ class PaymentController extends Controller ->processPaymentView($data); } - public function response(Request $request) + public function response(PaymentResponseRequest $request) { - $gateway = CompanyGateway::find($request->input('company_gateway_id')); + $gateway = CompanyGateway::find($request->input('company_gateway_id'))->firstOrFail(); + $payment_hash = $request->getPaymentHash(); + $payment_invoices = $payment_hash->invoices(); + $fee_total = $payment_hash->fee_total; + + $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payable_invoices, 'invoice_id')))->get(); + + $invoice_count = $invoices->count(); //REFACTOR - Entry point for the gateway response - we don't need to do anything at this point. // // - Inside each gateway driver, we should use have a generic code path (in BaseDriver.php)for successful/failed payment diff --git a/app/Http/Requests/ClientPortal/Payments/PaymentResponseRequest.php b/app/Http/Requests/ClientPortal/Payments/PaymentResponseRequest.php new file mode 100644 index 0000000000..0302bf7dd1 --- /dev/null +++ b/app/Http/Requests/ClientPortal/Payments/PaymentResponseRequest.php @@ -0,0 +1,39 @@ + 'required', + 'payment_hash' => 'required', + ]; + } + + public function getPaymentHash() + { + $input = $this->all(); + + return PaymentHash::whereRaw("BINARY `hash`= ?", [$input['payment_hash']])->first(); + } +} diff --git a/app/Jobs/Invoice/EmailInvoice.php b/app/Jobs/Invoice/EmailInvoice.php index 4d7ee03800..93b9b04239 100644 --- a/app/Jobs/Invoice/EmailInvoice.php +++ b/app/Jobs/Invoice/EmailInvoice.php @@ -91,6 +91,7 @@ class EmailInvoice extends BaseMailerJob implements ShouldQueue catch (\Swift_TransportException $e) { event(new InvoiceWasEmailedAndFailed($this->invoice_invitation->invoice, $this->company, $e->getMessage(), Ninja::eventVars())); + } if (count(Mail::failures()) > 0) { diff --git a/app/Models/Company.php b/app/Models/Company.php index 9027999579..edb4d55a65 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -393,7 +393,7 @@ class Company extends BaseModel public function system_logs() { - return $this->hasMany(SystemLog::class); + return $this->hasMany(SystemLog::class)->orderBy('id', 'DESC')->take(50); } public function tokens_hashed() diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 586187ad8d..7db6f56bb6 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -293,6 +293,34 @@ class CompanyGateway extends BaseModel return $fee; } + + public function calcGatewayFeeObject($amount, $invoice_count) + { + $total_gateway_fee = $this->calcGatewayFee($amount); + + $fee_object = new \stdClass; + + $fees_and_limits = $this->getFeesAndLimits(); + + if(!$fees_and_limits) + return $fee_object; + + $fee_component_amount = $fees_and_limits->fee_amount ?: 0; + $fee_component_percent = $fees_and_limits->fee_percent ? ($amount * $fees_and_limits->fee_percent / 100) : 0; + + $combined_fee_component = $fee_component_amount + $fee_component_percent; + + $fee_component_tax_name1 = $fees_and_limits->fee_tax_name1 ?: ''; + $fee_component_tax_rate1 = $fees_and_limits->fee_tax_rate1 ? ($combined_fee_component * $fees_and_limits->fee_tax_rate1 / 100) : 0; + + $fee_component_tax_name2 = $fees_and_limits->fee_tax_name2 ?: ''; + $fee_component_tax_rate2 = $fees_and_limits->fee_tax_rate2 ? ($combined_fee_component * $fees_and_limits->fee_tax_rate2 / 100) : 0; + + $fee_component_tax_name3 = $fees_and_limits->fee_tax_name3 ?: ''; + $fee_component_tax_rate3 = $fees_and_limits->fee_tax_rate3 ? ($combined_fee_component * $fees_and_limits->fee_tax_rate3 / 100) : 0; + + } + public function resolveRouteBinding($value) { return $this diff --git a/app/Models/PaymentHash.php b/app/Models/PaymentHash.php index d9c2c25410..9fd79df926 100644 --- a/app/Models/PaymentHash.php +++ b/app/Models/PaymentHash.php @@ -20,7 +20,6 @@ class PaymentHash extends Model 'data' => 'object' ]; - public function invoices() { return $this->data->invoices; diff --git a/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php b/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php index 92c6b9627c..1070bc3051 100644 --- a/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php +++ b/database/migrations/2020_08_18_140557_add_is_public_to_documents_table.php @@ -38,6 +38,7 @@ class AddIsPublicToDocumentsTable extends Migration Schema::create('payment_hashes', function ($table) { $table->increments('id'); $table->string('hash', 255); + $table->decimal('fee_total', 16, 4); $table->mediumText('data'); $table->timestamps(6); }); diff --git a/resources/views/index/index.blade.php b/resources/views/index/index.blade.php index b032dc2e70..ccfe1573d8 100644 --- a/resources/views/index/index.blade.php +++ b/resources/views/index/index.blade.php @@ -11,6 +11,11 @@