From 9e8abe314b6237894e2125ba1da4c99722dbe00e Mon Sep 17 00:00:00 2001 From: 1day2die Date: Fri, 5 Nov 2021 17:38:05 +0100 Subject: [PATCH] Refactor as J22j suggested --- .../Controllers/Admin/PaymentController.php | 34 +++++-------------- app/Models/PaypalProduct.php | 17 ++++++++++ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index ef1cf7b2..4c52a310 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/app/Http/Controllers/Admin/PaymentController.php @@ -28,24 +28,6 @@ use PayPalHttp\HttpException; class PaymentController extends Controller { - - public function getTaxPercent(){ - $tax = Configuration::getValueByKey("SALES_TAX"); - if ( $tax < 0 ) { - return 0; - } - return $tax; - } - - public function getTaxValue(PaypalProduct $paypalProduct){ - return $paypalProduct->price*$this->getTaxPercent()/100; - } - - public function getTotalPrice(PaypalProduct $paypalProduct){ - return $paypalProduct->price+($this->getTaxValue($paypalProduct)); - } - - /** * @return Application|Factory|View */ @@ -65,9 +47,9 @@ class PaymentController extends Controller { return view('store.checkout')->with([ 'product' => $paypalProduct, - 'taxvalue' => $this->getTaxValue($paypalProduct), - 'taxpercent' => $this->getTaxPercent(), - 'total' => $this->getTotalPrice($paypalProduct) + 'taxvalue' => $paypalProduct->getTaxValue(), + 'taxpercent' => $paypalProduct->getTaxPercent(), + 'total' => $paypalProduct->getTotalPrice() ]); } @@ -87,7 +69,7 @@ class PaymentController extends Controller "reference_id" => uniqid(), "description" => $paypalProduct->description, "amount" => [ - "value" => $this->getTotalPrice($paypalProduct), + "value" => $paypalProduct->getTotalPrice(), 'currency_code' => strtoupper($paypalProduct->currency_code), 'breakdown' =>[ 'item_total' => @@ -98,7 +80,7 @@ class PaymentController extends Controller 'tax_total' => [ 'currency_code' => strtoupper($paypalProduct->currency_code), - 'value' => $this->getTaxValue($paypalProduct), + 'value' => $paypalProduct->getTaxValue(), ] ] ] @@ -197,9 +179,9 @@ class PaymentController extends Controller 'status' => $response->result->status, 'amount' => $paypalProduct->quantity, 'price' => $paypalProduct->price, - 'tax_value' => $this->getTaxValue($paypalProduct), - 'tax_percent' => $this->getTaxPercent(), - 'total_price' => $this->getTotalPrice($paypalProduct), + 'tax_value' => $paypalProduct->getTaxValue(), + 'tax_percent' => $paypalProduct->getTaxPercent(), + 'total_price' => $paypalProduct->getTotalPrice(), 'currency_code' => $paypalProduct->currency_code, 'payer' => json_encode($response->result->payer), ]); diff --git a/app/Models/PaypalProduct.php b/app/Models/PaypalProduct.php index f0aef5d9..82177a4a 100644 --- a/app/Models/PaypalProduct.php +++ b/app/Models/PaypalProduct.php @@ -6,6 +6,7 @@ use Hidehalo\Nanoid\Client; use Illuminate\Database\Eloquent\Model; use NumberFormatter; use Spatie\Activitylog\Traits\LogsActivity; +use App\Models\Configuration; class PaypalProduct extends Model { @@ -48,4 +49,20 @@ class PaypalProduct extends Model $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); return $formatter->formatCurrency($value, $this->currency_code); } + + public function getTaxPercent(){ + $tax = Configuration::getValueByKey("SALES_TAX"); + if ( $tax < 0 ) { + return 0; + } + return $tax; + } + + public function getTaxValue(){ + return $this->price*$this->getTaxPercent()/100; + } + + public function getTotalPrice(){ + return $this->price+($this->getTaxValue()); + } }