From 0c5d2911931a59b3f19688086803373b3f0c674e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 15 Apr 2022 09:51:39 +1000 Subject: [PATCH] Gateway fees for PayPal --- .../PayPalExpressPaymentDriver.php | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 75a569c84c..f6016c7e58 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -32,6 +32,8 @@ class PayPalExpressPaymentDriver extends BaseDriver private $omnipay_gateway; + private float $fee = 0; + const SYSTEM_LOG_TYPE = SystemLog::TYPE_PAYPAL; public function gatewayTypes() @@ -173,11 +175,17 @@ class PayPalExpressPaymentDriver extends BaseDriver public function generatePaymentDetails(array $data) { + + $_invoice = collect($this->payment_hash->data->invoices)->first(); + $invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($_invoice->invoice_id)); + + $this->fee = $this->feeCalc($invoice, $data['total']['amount_with_fee']); + return [ 'currency' => $this->client->getCurrencyCode(), 'transactionType' => 'Purchase', 'clientIp' => request()->getClientIp(), - 'amount' => $data['total']['amount_with_fee'], + 'amount' => $data['total']['amount_with_fee'] + $this->fee, 'returnUrl' => route('client.payments.response', [ 'company_gateway_id' => $this->company_gateway->id, 'payment_hash' => $this->payment_hash->hash, @@ -200,8 +208,6 @@ class PayPalExpressPaymentDriver extends BaseDriver $_invoice = collect($this->payment_hash->data->invoices)->first(); $invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($_invoice->invoice_id)); - $line_item = collect($invoice->line_items)->first(); - $items = []; $items[] = new Item([ @@ -211,8 +217,44 @@ class PayPalExpressPaymentDriver extends BaseDriver 'quantity' => 1, ]); + + if($this->fee > 0.1){ + + $items[] = new Item([ + 'name' => " ", + 'description' => ctrans('texts.gateway_fee_description'), + 'price' => $this->fee, + 'quantity' => 1, + ]); + + + } + return $items; } + private function feeCalc($invoice, $invoice_total) + { + + $invoice->service()->removeUnpaidGatewayFees(); + $invoice = $invoice->fresh(); + + $balance = floatval($invoice->balance); + + $_updated_invoice = $invoice->service()->addGatewayFee($this->company_gateway, GatewayType::PAYPAL, $invoice_total)->save(); + + if(floatval($_updated_invoice->balance) > $balance){ + + $fee = floatval($_updated_invoice->balance) - $balance; + + $this->payment_hash->fee_total = $fee; + $this->payment_hash->save(); + + return $fee; + } + + return 0; + } + }