Browse Source

Use casts instead of converting values

Ferks-FK 2 years ago
parent
commit
490e11572d

+ 1 - 5
app/Extensions/PaymentGateways/PayPal/PayPalExtension.php

@@ -47,13 +47,9 @@ class PayPalExtension extends AbstractExtension
         $discount = PartnerDiscount::getDiscount();
         $couponCode = $request->input('couponCode');
         $isValidCoupon = $this->validateCoupon($request->user(), $couponCode, $request->shopProduct);
-
-        if (is_string($shopProduct->price)) {
-            $shopProduct->price = floatval($shopProduct->price);
-        }
-
         $price = $shopProduct->price;
 
+		// Coupon Discount.
         if ($isValidCoupon->getStatusCode() == 200) {
             $price = $this->calcDiscount($price, $isValidCoupon->getData());
         }

+ 14 - 3
app/Models/Coupon.php

@@ -10,6 +10,9 @@ class Coupon extends Model
 {
     use HasFactory;
 
+    /**
+     * @var string[]
+     */
     protected $fillable = [
         'code',
         'type',
@@ -19,6 +22,16 @@ class Coupon extends Model
         'expires_at'
     ];
 
+    /**
+     * @var string[]
+     */
+    protected $casts = [
+        'value' => 'float',
+        'uses' => 'integer',
+        'max_uses' => 'integer',
+        'expires_at' => 'timestamp'
+    ];
+
     /**
      * Returns the date format used by the coupons.
      *
@@ -41,9 +54,7 @@ class Coupon extends Model
         }
 
         if (!is_null($this->expires_at)) {
-            $expires_at = Carbon::createFromTimeString($this->expires_at)->timestamp;
-
-            if ($expires_at <= Carbon::now()->timestamp) {
+            if ($this->expires_at <= Carbon::now()->timestamp) {
                 return __('EXPIRED');
             }
         }

+ 7 - 0
app/Models/ShopProduct.php

@@ -36,6 +36,13 @@ class ShopProduct extends Model
         'disabled',
     ];
 
+    /**
+     * @var string[]
+     */
+    protected $casts = [
+        'price' => 'float'
+    ];
+
     public static function boot()
     {
         parent::boot();

+ 0 - 12
app/Traits/Coupon.php

@@ -22,14 +22,6 @@ trait Coupon
         ], 404);
 
         if (!is_null($coupon)) {
-            if (is_string($coupon->value)) {
-                $coupon->value = floatval($coupon->value);
-            }
-
-            if (is_string($shopProduct->price)) {
-                $shopProduct->price = floatval($shopProduct->price);
-            }
-
             if ($coupon->getStatus() == 'USES_LIMIT_REACHED') {
                 $response = response()->json([
                     'isValid' => false,
@@ -81,10 +73,6 @@ trait Coupon
     {
 
         if ($data->isValid) {
-            if (is_string($productPrice)) {
-                $productPrice = floatval($productPrice);
-            }
-
             if ($data->couponType === 'percentage') {
                 return $productPrice - ($productPrice * $data->couponValue / 100);
             }