CouponService
in package
Service for coupon validation, discount calculation, and usage tracking.
Tags
Table of Contents
Properties
Methods
- __construct() : mixed
- Initialize the coupon service.
- applyCoupon() : void
- Increment the usage count after a successful order.
- calculateDiscount() : float
- Calculate the discount amount for a coupon and subtotal.
- formatMessage() : string
- Build a human-readable discount message.
- validate() : array<string|int, mixed>
- Validate a coupon code for a business and subtotal.
- result() : array<string|int, mixed>
- Build a standard validation result array.
Properties
$couponModel
private
Coupon
$couponModel
Coupon model
Methods
__construct()
Initialize the coupon service.
public
__construct() : mixed
applyCoupon()
Increment the usage count after a successful order.
public
applyCoupon(int $couponId) : void
Parameters
- $couponId : int
-
Coupon ID
calculateDiscount()
Calculate the discount amount for a coupon and subtotal.
public
calculateDiscount(array<string|int, mixed> $coupon, float $subtotal) : float
Parameters
- $coupon : array<string|int, mixed>
-
Coupon record
- $subtotal : float
-
Order subtotal
Return values
float —Discount amount (never exceeds subtotal)
formatMessage()
Build a human-readable discount message.
public
formatMessage(array<string|int, mixed> $coupon, float $discount) : string
Parameters
- $coupon : array<string|int, mixed>
-
Coupon record
- $discount : float
-
Calculated discount amount
Return values
stringvalidate()
Validate a coupon code for a business and subtotal.
public
validate(int $businessId, string $code, float $subtotal) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $code : string
-
Coupon code
- $subtotal : float
-
Order subtotal before discount
Return values
array<string|int, mixed> —['valid' => bool, 'coupon' => array|null, 'discount' => float, 'error' => string|null]
result()
Build a standard validation result array.
private
result(bool $valid, array<string|int, mixed>|null $coupon, float $discount, string|null $error) : array<string|int, mixed>
Parameters
- $valid : bool
-
Whether the coupon is valid
- $coupon : array<string|int, mixed>|null
-
Coupon record or null
- $discount : float
-
Calculated discount
- $error : string|null
-
Error message or null