From 490e11572d73b1d5daf1e133356a33e29cd20aec Mon Sep 17 00:00:00 2001 From: Ferks-FK Date: Thu, 18 May 2023 14:33:42 +0000 Subject: [PATCH] Use casts instead of converting values --- .../PaymentGateways/PayPal/PayPalExtension.php | 6 +----- app/Models/Coupon.php | 17 ++++++++++++++--- app/Models/ShopProduct.php | 7 +++++++ app/Traits/Coupon.php | 12 ------------ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/app/Extensions/PaymentGateways/PayPal/PayPalExtension.php b/app/Extensions/PaymentGateways/PayPal/PayPalExtension.php index 1586804b..7667c08d 100644 --- a/app/Extensions/PaymentGateways/PayPal/PayPalExtension.php +++ b/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()); } diff --git a/app/Models/Coupon.php b/app/Models/Coupon.php index d7f0efb3..20657db5 100644 --- a/app/Models/Coupon.php +++ b/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'); } } diff --git a/app/Models/ShopProduct.php b/app/Models/ShopProduct.php index cfe3f783..12b870a2 100644 --- a/app/Models/ShopProduct.php +++ b/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(); diff --git a/app/Traits/Coupon.php b/app/Traits/Coupon.php index be4da4ad..091fa855 100644 --- a/app/Traits/Coupon.php +++ b/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); }