mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-13 06:32:40 +01:00
Working on gateway fees
This commit is contained in:
parent
a4d0e7759e
commit
ac08a146a1
@ -136,12 +136,12 @@ class PaymentController extends Controller
|
|||||||
$payment_method_id = request()->input('payment_method_id');
|
$payment_method_id = request()->input('payment_method_id');
|
||||||
|
|
||||||
$invoice_totals = array_sum(array_column($payable_invoices,'amount'));
|
$invoice_totals = array_sum(array_column($payable_invoices,'amount'));
|
||||||
$fee_totals = $gateway->calcGatewayFee($invoice_totals);
|
$fee_totals = round($gateway->calcGatewayFee($invoice_totals), $invoices->first()->client->currency()->precision);
|
||||||
|
|
||||||
$payment_hash = new PaymentHash;
|
$payment_hash = new PaymentHash;
|
||||||
$payment_hash->hash = Str::random(128);
|
$payment_hash->hash = Str::random(128);
|
||||||
$payment_hash->data = $payable_invoices;
|
$payment_hash->data = $payable_invoices;
|
||||||
$payment_hash->fees = $fee_totals;
|
$payment_hash->fee_total = $fee_totals;
|
||||||
$payment_hash->save();
|
$payment_hash->save();
|
||||||
|
|
||||||
$totals = [
|
$totals = [
|
||||||
@ -156,6 +156,7 @@ class PaymentController extends Controller
|
|||||||
'invoices' => $payable_invoices,
|
'invoices' => $payable_invoices,
|
||||||
'token' => auth()->user()->client->gateway_token($gateway->id, $payment_method_id),
|
'token' => auth()->user()->client->gateway_token($gateway->id, $payment_method_id),
|
||||||
'payment_method_id' => $payment_method_id,
|
'payment_method_id' => $payment_method_id,
|
||||||
|
'amount_with_fee' => $invoice_totals + $fee_totals,
|
||||||
];
|
];
|
||||||
|
|
||||||
return $gateway
|
return $gateway
|
||||||
|
@ -289,20 +289,22 @@ class CompanyGateway extends BaseModel
|
|||||||
|
|
||||||
$pre_tax_fee = $fee;
|
$pre_tax_fee = $fee;
|
||||||
|
|
||||||
if ($fees_and_limits->fee_tax_rate1) {
|
//we shouldn't calculate the taxes - they'll be done when we re-process the invoice
|
||||||
$fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate1 / 100;
|
|
||||||
info("fee after adding fee tax 1 = {$fee}");
|
// if ($fees_and_limits->fee_tax_rate1) {
|
||||||
}
|
// $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate1 / 100;
|
||||||
|
// info("fee after adding fee tax 1 = {$fee}");
|
||||||
|
// }
|
||||||
|
|
||||||
if ($fees_and_limits->fee_tax_rate2) {
|
// if ($fees_and_limits->fee_tax_rate2) {
|
||||||
$fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100;
|
// $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100;
|
||||||
info("fee after adding fee tax 2 = {$fee}");
|
// info("fee after adding fee tax 2 = {$fee}");
|
||||||
}
|
// }
|
||||||
|
|
||||||
if ($fees_and_limits->fee_tax_rate3) {
|
// if ($fees_and_limits->fee_tax_rate3) {
|
||||||
$fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100;
|
// $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100;
|
||||||
info("fee after adding fee tax 3 = {$fee}");
|
// info("fee after adding fee tax 3 = {$fee}");
|
||||||
}
|
// }
|
||||||
|
|
||||||
if($fees_and_limits->fee_cap > 0 && ($fee > $fees_and_limits->fee_cap))
|
if($fees_and_limits->fee_cap > 0 && ($fee > $fees_and_limits->fee_cap))
|
||||||
$fee = $fees_and_limits->fee_cap;
|
$fee = $fees_and_limits->fee_cap;
|
||||||
|
@ -16,12 +16,14 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
class PaymentHash extends Model
|
class PaymentHash extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'data' => 'object'
|
'data' => 'object'
|
||||||
];
|
];
|
||||||
|
|
||||||
public function invoices()
|
public function invoices()
|
||||||
{
|
{
|
||||||
return $this->data->invoices;
|
return $this->data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,7 +171,7 @@ class BaseDriver extends AbstractPaymentDriver
|
|||||||
{
|
{
|
||||||
/*Payment meta data*/
|
/*Payment meta data*/
|
||||||
$payment_hash = $request->getPaymentHash();
|
$payment_hash = $request->getPaymentHash();
|
||||||
|
|
||||||
/*Payment invoices*/
|
/*Payment invoices*/
|
||||||
$payment_invoices = $payment_hash->invoices();
|
$payment_invoices = $payment_hash->invoices();
|
||||||
|
|
||||||
@ -179,10 +179,10 @@ class BaseDriver extends AbstractPaymentDriver
|
|||||||
$fee_total = $payment_hash->fee_total;
|
$fee_total = $payment_hash->fee_total;
|
||||||
|
|
||||||
/*Sum of invoice amounts*/
|
/*Sum of invoice amounts*/
|
||||||
$invoice_totals = array_sum(array_column($payable_invoices,'amount'));
|
$invoice_totals = array_sum(array_column($payment_invoices,'amount'));
|
||||||
|
|
||||||
/*Hydrate invoices*/
|
/*Hydrate invoices*/
|
||||||
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payable_invoices, 'invoice_id')))->get();
|
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id')))->get();
|
||||||
|
|
||||||
/*Append gateway fee to invoice line item of first invoice*/
|
/*Append gateway fee to invoice line item of first invoice*/
|
||||||
if($fee_total != 0){
|
if($fee_total != 0){
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
namespace App\PaymentDrivers;
|
namespace App\PaymentDrivers;
|
||||||
|
|
||||||
use App\Factory\PaymentFactory;
|
use App\Factory\PaymentFactory;
|
||||||
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Models\ClientContact;
|
use App\Models\ClientContact;
|
||||||
use App\Models\CompanyGateway;
|
use App\Models\CompanyGateway;
|
||||||
@ -286,4 +287,39 @@ class BasePaymentDriver
|
|||||||
|
|
||||||
return $payment;
|
return $payment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a successful payment is made, we need to append the gateway fee
|
||||||
|
* to an invoice
|
||||||
|
*
|
||||||
|
* @param PaymentResponseRequest $request The incoming payment request
|
||||||
|
* @return void Success/Failure
|
||||||
|
*/
|
||||||
|
public function appendGatewayFeeToInvoice(PaymentResponseRequest $request) :void
|
||||||
|
{
|
||||||
|
/*Payment meta data*/
|
||||||
|
$payment_hash = $request->getPaymentHash();
|
||||||
|
|
||||||
|
/*Payment invoices*/
|
||||||
|
$payment_invoices = $payment_hash->invoices();
|
||||||
|
|
||||||
|
/*Fee charged at gateway*/
|
||||||
|
$fee_total = $payment_hash->fee_total;
|
||||||
|
|
||||||
|
/*Sum of invoice amounts*/
|
||||||
|
$invoice_totals = array_sum(array_column($payment_invoices,'amount'));
|
||||||
|
|
||||||
|
/*Hydrate invoices*/
|
||||||
|
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_invoices, 'invoice_id')))->get();
|
||||||
|
|
||||||
|
/*Append gateway fee to invoice line item of first invoice*/
|
||||||
|
if($fee_total != 0){
|
||||||
|
$invoices->first()->service()->addGatewayFee($this->company_gateway, $invoice_totals)->save();
|
||||||
|
|
||||||
|
//We need to update invoice balance / client balance at this point so
|
||||||
|
//that payment record sanity is preserved //todo
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ use App\Models\ClientGatewayToken;
|
|||||||
use App\Models\GatewayType;
|
use App\Models\GatewayType;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
|
use App\Models\PaymentHash;
|
||||||
use App\Models\PaymentType;
|
use App\Models\PaymentType;
|
||||||
use App\Models\SystemLog;
|
use App\Models\SystemLog;
|
||||||
use App\PaymentDrivers\StripePaymentDriver;
|
use App\PaymentDrivers\StripePaymentDriver;
|
||||||
@ -92,7 +93,7 @@ class CreditCard
|
|||||||
'amount' => $this->stripe->convertToStripeAmount($data['amount_with_fee'], $this->stripe->client->currency()->precision),
|
'amount' => $this->stripe->convertToStripeAmount($data['amount_with_fee'], $this->stripe->client->currency()->precision),
|
||||||
'currency' => $this->stripe->client->getCurrencyCode(),
|
'currency' => $this->stripe->client->getCurrencyCode(),
|
||||||
'customer' => $this->stripe->findOrCreateCustomer(),
|
'customer' => $this->stripe->findOrCreateCustomer(),
|
||||||
'description' => $data['invoices']->pluck('id'), //todo more meaningful description here:
|
'description' => collect($data['invoices'])->pluck('id'), //todo more meaningful description here:
|
||||||
];
|
];
|
||||||
|
|
||||||
if ($data['token']) {
|
if ($data['token']) {
|
||||||
@ -113,6 +114,8 @@ class CreditCard
|
|||||||
{
|
{
|
||||||
$server_response = json_decode($request->input('gateway_response'));
|
$server_response = json_decode($request->input('gateway_response'));
|
||||||
|
|
||||||
|
$payment_hash = PaymentHash::whereRaw("BINARY `hash`= ?", [$request->input('payment_hash')])->firstOrFail();
|
||||||
|
|
||||||
$state = [
|
$state = [
|
||||||
'payment_method' => $server_response->payment_method,
|
'payment_method' => $server_response->payment_method,
|
||||||
'payment_status' => $server_response->status,
|
'payment_status' => $server_response->status,
|
||||||
@ -122,7 +125,7 @@ class CreditCard
|
|||||||
'server_response' => $server_response,
|
'server_response' => $server_response,
|
||||||
];
|
];
|
||||||
|
|
||||||
$invoices = Invoice::whereIn('id', $this->stripe->transformKeys($state['hashed_ids']))
|
$invoices = Invoice::whereIn('id', $this->stripe->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))
|
||||||
->whereClientId($this->stripe->client->id)
|
->whereClientId($this->stripe->client->id)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
@ -138,6 +141,10 @@ class CreditCard
|
|||||||
$state['customer'] = $state['payment_intent']->customer;
|
$state['customer'] = $state['payment_intent']->customer;
|
||||||
|
|
||||||
if ($state['payment_status'] == 'succeeded') {
|
if ($state['payment_status'] == 'succeeded') {
|
||||||
|
|
||||||
|
/* Add gateway fees if needed! */
|
||||||
|
$this->stripe->appendGatewayFeeToInvoice($request);
|
||||||
|
|
||||||
return $this->processSuccessfulPayment($state);
|
return $this->processSuccessfulPayment($state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ class AddGatewayFee extends AbstractService
|
|||||||
|
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
$gateway_fee = $this->company_gateway->calcGatewayFee($this->amount);
|
$gateway_fee = round($this->company_gateway->calcGatewayFee($this->amount), $this->invoice->client->currency()->precision);
|
||||||
|
|
||||||
$this->cleanPendingGatewayFees();
|
$this->cleanPendingGatewayFees();
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ class AddGatewayFee extends AbstractService
|
|||||||
private function processGatewayFee($gateway_fee)
|
private function processGatewayFee($gateway_fee)
|
||||||
{
|
{
|
||||||
$invoice_item = new InvoiceItem;
|
$invoice_item = new InvoiceItem;
|
||||||
$invoice_item->type_id = 4;
|
$invoice_item->type_id = '4';
|
||||||
$invoice_item->product_key = ctrans('texts.surcharge');
|
$invoice_item->product_key = ctrans('texts.surcharge');
|
||||||
$invoice_item->notes = ctrans('texts.online_payment_surcharge');
|
$invoice_item->notes = ctrans('texts.online_payment_surcharge');
|
||||||
$invoice_item->quantity = 1;
|
$invoice_item->quantity = 1;
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
{!! Former::hidden('gateway_response')->id('gateway_response') !!}
|
{!! Former::hidden('gateway_response')->id('gateway_response') !!}
|
||||||
{!! Former::hidden('store_card')->id('store_card') !!}
|
{!! Former::hidden('store_card')->id('store_card') !!}
|
||||||
{!! Former::hidden('hashed_ids')->value($hashed_ids) !!}
|
{!! Former::hidden('payment_hash')->value($payment_hash) !!}
|
||||||
{!! Former::hidden('company_gateway_id')->value($payment_method_id) !!}
|
{!! Former::hidden('company_gateway_id')->value($payment_method_id) !!}
|
||||||
{!! Former::hidden('payment_method_id')->value($gateway->getCompanyGatewayId()) !!}
|
{!! Former::hidden('payment_method_id')->value($gateway->getCompanyGatewayId()) !!}
|
||||||
{!! Former::close() !!}
|
{!! Former::close() !!}
|
||||||
|
@ -11,9 +11,8 @@
|
|||||||
@csrf
|
@csrf
|
||||||
<input type="hidden" name="gateway_response">
|
<input type="hidden" name="gateway_response">
|
||||||
<input type="hidden" name="store_card">
|
<input type="hidden" name="store_card">
|
||||||
@foreach($invoices as $invoice)
|
<input type="hidden" name="payment_hash" value="{{$payment_hash}}">
|
||||||
<input type="hidden" name="hashed_ids[]" value="{{ $invoice->hashed_id }}">
|
|
||||||
@endforeach
|
|
||||||
<input type="hidden" name="company_gateway_id" value="{{ $gateway->getCompanyGatewayId() }}">
|
<input type="hidden" name="company_gateway_id" value="{{ $gateway->getCompanyGatewayId() }}">
|
||||||
<input type="hidden" name="payment_method_id" value="{{ $payment_method_id }}">
|
<input type="hidden" name="payment_method_id" value="{{ $payment_method_id }}">
|
||||||
</form>
|
</form>
|
||||||
|
Loading…
Reference in New Issue
Block a user