From 1bd3deca4f1125478a086d6a3181b75837be40ea Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 31 Aug 2020 14:27:47 +1000 Subject: [PATCH] Invoice payments with gateway fees --- .../ClientPortal/PaymentController.php | 1 + app/Services/Payment/UpdateInvoicePayment.php | 22 ++++++---- ...40557_add_is_public_to_documents_table.php | 1 + tests/Feature/CompanyGatewayApiTest.php | 40 +++++++++++++++++-- 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index e07161bee3..03d59ebe1c 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -156,6 +156,7 @@ class PaymentController extends Controller $payment_hash->hash = Str::random(128); $payment_hash->data = $payable_invoices; $payment_hash->fee_total = $fee_totals; + $payment_hash->fee_invoice_id = $first_invoice->id; $payment_hash->save(); $totals = [ diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 8fd45d241c..914a277ea2 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -47,26 +47,32 @@ class UpdateInvoicePayment collect($paid_invoices)->each(function ($paid_invoice) use($invoices) { $invoice = $invoices->first(function ($inv) use($paid_invoice) { - return $paid_invoice['invoice_id'] == $inv->hashed_id; + return $paid_invoice->invoice_id == $inv->hashed_id; }); + if($invoice->id == $this->payment_hash->fee_invoice_id) + $paid_amount = $paid_invoice->amount + $this->payment_hash->fee_total; + else + $paid_amount = $paid_invoice->amount; + $this->payment ->ledger() - ->updatePaymentBalance($paid_invoice->amount*-1); + ->updatePaymentBalance($paid_amount*-1); $this->payment ->client ->service() - ->updateBalance($paid_invoice->amount*-1) - ->updatePaidToDate($paid_invoice->amount) + ->updateBalance($paid_amount*-1) + ->updatePaidToDate($paid_amount) ->save(); - $invoice->pivot->amount = $paid_invoice->amount; - $invoice->pivot->save(); + /*i think to interact with this correct - we need to do this form $payment->invoice()->pivot*/ + // $invoice->pivot->amount = $paid_amount; + // $invoice->pivot->save(); $invoice->service() //caution what if we amount paid was less than partial - we wipe it! ->clearPartial() - ->updateBalance($paid_invoice->amount*-1) + ->updateBalance($paid_amount*-1) ->save(); }); @@ -93,7 +99,7 @@ class UpdateInvoicePayment // $this->payment->save(); // $this->payment->delete(); // } - } + return $this->payment; } 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 b9f3c1d386..9eda7ef60b 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 @@ -39,6 +39,7 @@ class AddIsPublicToDocumentsTable extends Migration $table->increments('id'); $table->string('hash', 255); $table->decimal('fee_total', 16, 4); + $table->unsignedInteger('fee_invoice_id')->nullable(); $table->mediumText('data'); $table->timestamps(6); }); diff --git a/tests/Feature/CompanyGatewayApiTest.php b/tests/Feature/CompanyGatewayApiTest.php index 4441a9f1fb..8c7e728ac9 100644 --- a/tests/Feature/CompanyGatewayApiTest.php +++ b/tests/Feature/CompanyGatewayApiTest.php @@ -314,7 +314,41 @@ class CompanyGatewayApiTest extends TestCase $company_gateway = CompanyGateway::find($id); - $this->assertEquals(11, $company_gateway->calcGatewayFee(10)); + $this->assertEquals(11, $company_gateway->calcGatewayFee(10, true)); + } + + public function testFeesAndLimitsFeePercentAndAmountAndTaxCalcuationInclusiveTaxes() + { + //{"1":{"min_limit":1,"max_limit":1000000,"fee_amount":10,"fee_percent":2,"fee_tax_name1":"","fee_tax_name2":"","fee_tax_name3":"","fee_tax_rate1":0,"fee_tax_rate2":0,"fee_tax_rate3":0,"fee_cap":10,"adjust_fee_percent":true}} + $fee = new FeesAndLimits; + $fee->fee_amount = 10; + // $fee->fee_percent = 2; + $fee->fee_tax_name1 = 'GST'; + $fee->fee_tax_rate1 = '10.0'; + + $fee_arr[1] = (array)$fee; + + $data = [ + 'config' => 'random config', + 'gateway_key' => '3b6621f970ab18887c4f6dca78d3f8bb', + 'fees_and_limits' => $fee_arr, + ]; + + /* POST */ + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token + ])->post('/api/v1/company_gateways', $data); + + + $response->assertStatus(200); + + $arr = $response->json(); + $id = $this->decodePrimaryKey($arr['data']['id']); + + $company_gateway = CompanyGateway::find($id); + + $this->assertEquals(10, $company_gateway->calcGatewayFee(10)); } public function testFeesAndLimitsFeePercentAndAmountAndDoubleTaxCalcuation() @@ -351,7 +385,7 @@ class CompanyGatewayApiTest extends TestCase $company_gateway = CompanyGateway::find($id); - $this->assertEquals(12, $company_gateway->calcGatewayFee(10)); + $this->assertEquals(12, $company_gateway->calcGatewayFee(10,true)); } @@ -389,6 +423,6 @@ class CompanyGatewayApiTest extends TestCase $company_gateway = CompanyGateway::find($id); - $this->assertEquals(1, $company_gateway->calcGatewayFee(10)); + $this->assertEquals(1, $company_gateway->calcGatewayFee(10,true)); } }