diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index d27a0eaf2027..cab7f4b66a27 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -136,12 +136,12 @@ class PaymentController extends Controller $payment_method_id = request()->input('payment_method_id'); $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->hash = Str::random(128); $payment_hash->data = $payable_invoices; - $payment_hash->fees = $fee_totals; + $payment_hash->fee_total = $fee_totals; $payment_hash->save(); $totals = [ @@ -156,6 +156,7 @@ class PaymentController extends Controller 'invoices' => $payable_invoices, 'token' => auth()->user()->client->gateway_token($gateway->id, $payment_method_id), 'payment_method_id' => $payment_method_id, + 'amount_with_fee' => $invoice_totals + $fee_totals, ]; return $gateway diff --git a/app/Models/CompanyGateway.php b/app/Models/CompanyGateway.php index 368a0522ba70..7501c48aa7c3 100644 --- a/app/Models/CompanyGateway.php +++ b/app/Models/CompanyGateway.php @@ -289,20 +289,22 @@ class CompanyGateway extends BaseModel $pre_tax_fee = $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}"); - } + //we shouldn't calculate the taxes - they'll be done when we re-process the invoice + + // 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) { - $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100; - info("fee after adding fee tax 2 = {$fee}"); - } + // if ($fees_and_limits->fee_tax_rate2) { + // $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100; + // info("fee after adding fee tax 2 = {$fee}"); + // } - if ($fees_and_limits->fee_tax_rate3) { - $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100; - info("fee after adding fee tax 3 = {$fee}"); - } + // if ($fees_and_limits->fee_tax_rate3) { + // $fee += $pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100; + // info("fee after adding fee tax 3 = {$fee}"); + // } if($fees_and_limits->fee_cap > 0 && ($fee > $fees_and_limits->fee_cap)) $fee = $fees_and_limits->fee_cap; diff --git a/app/Models/PaymentHash.php b/app/Models/PaymentHash.php index 9fd79df9269b..2031b2f1aba7 100644 --- a/app/Models/PaymentHash.php +++ b/app/Models/PaymentHash.php @@ -16,12 +16,14 @@ use Illuminate\Database\Eloquent\Model; class PaymentHash extends Model { + protected $guarded = ['id']; + protected $casts = [ 'data' => 'object' ]; public function invoices() { - return $this->data->invoices; + return $this->data; } } diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index f7a89f5d34b2..a0d3ce0d86df 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -171,7 +171,7 @@ class BaseDriver extends AbstractPaymentDriver { /*Payment meta data*/ $payment_hash = $request->getPaymentHash(); - + /*Payment invoices*/ $payment_invoices = $payment_hash->invoices(); @@ -179,10 +179,10 @@ class BaseDriver extends AbstractPaymentDriver $fee_total = $payment_hash->fee_total; /*Sum of invoice amounts*/ - $invoice_totals = array_sum(array_column($payable_invoices,'amount')); + $invoice_totals = array_sum(array_column($payment_invoices,'amount')); /*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*/ if($fee_total != 0){ diff --git a/app/PaymentDrivers/BasePaymentDriver.php b/app/PaymentDrivers/BasePaymentDriver.php index 0585aaf2ee5b..c9b6a0c6051d 100644 --- a/app/PaymentDrivers/BasePaymentDriver.php +++ b/app/PaymentDrivers/BasePaymentDriver.php @@ -13,6 +13,7 @@ namespace App\PaymentDrivers; use App\Factory\PaymentFactory; +use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Models\Client; use App\Models\ClientContact; use App\Models\CompanyGateway; @@ -286,4 +287,39 @@ class BasePaymentDriver 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 + } + + } } + diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index b0a585db6cd5..4fb0e34a5e23 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -19,6 +19,7 @@ use App\Models\ClientGatewayToken; use App\Models\GatewayType; use App\Models\Invoice; use App\Models\Payment; +use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\SystemLog; use App\PaymentDrivers\StripePaymentDriver; @@ -92,7 +93,7 @@ class CreditCard 'amount' => $this->stripe->convertToStripeAmount($data['amount_with_fee'], $this->stripe->client->currency()->precision), 'currency' => $this->stripe->client->getCurrencyCode(), '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']) { @@ -113,6 +114,8 @@ class CreditCard { $server_response = json_decode($request->input('gateway_response')); + $payment_hash = PaymentHash::whereRaw("BINARY `hash`= ?", [$request->input('payment_hash')])->firstOrFail(); + $state = [ 'payment_method' => $server_response->payment_method, 'payment_status' => $server_response->status, @@ -122,7 +125,7 @@ class CreditCard '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) ->get(); @@ -138,6 +141,10 @@ class CreditCard $state['customer'] = $state['payment_intent']->customer; if ($state['payment_status'] == 'succeeded') { + + /* Add gateway fees if needed! */ + $this->stripe->appendGatewayFeeToInvoice($request); + return $this->processSuccessfulPayment($state); } diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index 07838ce12484..024c1d1f0f06 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -43,7 +43,7 @@ class AddGatewayFee extends AbstractService 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(); @@ -71,7 +71,7 @@ class AddGatewayFee extends AbstractService private function processGatewayFee($gateway_fee) { $invoice_item = new InvoiceItem; - $invoice_item->type_id = 4; + $invoice_item->type_id = '4'; $invoice_item->product_key = ctrans('texts.surcharge'); $invoice_item->notes = ctrans('texts.online_payment_surcharge'); $invoice_item->quantity = 1; diff --git a/resources/views/portal/default/gateways/stripe/credit_card.blade.php b/resources/views/portal/default/gateways/stripe/credit_card.blade.php index 97344bff21c6..fd9c9bd57491 100644 --- a/resources/views/portal/default/gateways/stripe/credit_card.blade.php +++ b/resources/views/portal/default/gateways/stripe/credit_card.blade.php @@ -11,7 +11,7 @@ {!! Former::hidden('gateway_response')->id('gateway_response') !!} {!! 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('payment_method_id')->value($gateway->getCompanyGatewayId()) !!} {!! Former::close() !!} diff --git a/resources/views/portal/ninja2020/gateways/stripe/credit_card.blade.php b/resources/views/portal/ninja2020/gateways/stripe/credit_card.blade.php index b3df4b2759c2..77c91ba8e5c3 100644 --- a/resources/views/portal/ninja2020/gateways/stripe/credit_card.blade.php +++ b/resources/views/portal/ninja2020/gateways/stripe/credit_card.blade.php @@ -11,9 +11,8 @@ @csrf - @foreach($invoices as $invoice) - - @endforeach + +