Browse Source

Refactor as J22j suggested

1day2die 3 years ago
parent
commit
9e8abe314b
2 changed files with 25 additions and 26 deletions
  1. 8 26
      app/Http/Controllers/Admin/PaymentController.php
  2. 17 0
      app/Models/PaypalProduct.php

+ 8 - 26
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),
                 ]);

+ 17 - 0
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());
+    }
 }