1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Invoice payments with gateway fees

This commit is contained in:
David Bomba 2020-08-31 14:27:47 +10:00
parent eb867522df
commit 1bd3deca4f
4 changed files with 53 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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