| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Requests\FrontendRequests; |
| 4 |
|
| 5 |
use FluentCart\Framework\Foundation\RequestGuard; |
| 6 |
|
| 7 |
class CouponRequest extends RequestGuard |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* @return array |
| 12 |
*/ |
| 13 |
public function rules(): array |
| 14 |
{ |
| 15 |
return [ |
| 16 |
'order_uuid' => 'nullable|sanitizeText|maxLength:100', |
| 17 |
'id' => 'nullable|numeric', |
| 18 |
'coupon_code' => 'required|sanitizeText', |
| 19 |
'order_items' => 'required|array', |
| 20 |
"order_items.*.id" => 'numeric|min:1', |
| 21 |
"order_items.*.order_id" => 'numeric|min:1', |
| 22 |
"order_items.*.post_id" => 'numeric|min:1', |
| 23 |
"order_items.*.variation_id" => 'numeric|min:1', |
| 24 |
"order_items.*.type" => 'nullable|sanitizeText|maxLength:100', |
| 25 |
"order_items.*.quantity" => 'numeric|min:1', |
| 26 |
"order_items.*.title" => 'nullable|sanitizeText|maxLength:100', |
| 27 |
"order_items.*.price" => 'numeric', |
| 28 |
"order_items.*.unit_price" => 'numeric', |
| 29 |
"order_items.*.item_cost" => 'numeric', |
| 30 |
"order_items.*.item_total" => 'numeric', |
| 31 |
"order_items.*.tax_amount" => 'numeric', |
| 32 |
// nullable: the admin UI serializes variation line items with subtotal null |
| 33 |
// (cartService.js computes unit_price*quantity - cost, and productService.js |
| 34 |
// never sets `cost` on variation rows, so NaN JSON-encodes as null). The bare |
| 35 |
// numeric rule 422'd every coupon apply on such orders. CouponServiceAdmin |
| 36 |
// falls back to unit_price*quantity for non-numeric subtotals, keeping the |
| 37 |
// min/max purchase-amount enforcement this field was whitelisted for. |
| 38 |
"order_items.*.subtotal" => 'nullable|numeric', |
| 39 |
"order_items.*.discount_total" => 'numeric', |
| 40 |
"order_items.*.total" => 'numeric', |
| 41 |
"order_items.*.line_total" => 'numeric', |
| 42 |
"order_items.*.cart_index" => 'nullable|numeric', |
| 43 |
"order_items.*.rate" => 'nullable|numeric', |
| 44 |
"order_items.*.line_meta" => 'nullable', |
| 45 |
"order_items.*.other_info" => 'nullable|array', |
| 46 |
'applied_coupons' => 'nullable|array', |
| 47 |
|
| 48 |
'customer_email' => 'nullable|sanitizeText|email', |
| 49 |
|
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function messages(): array |
| 57 |
{ |
| 58 |
return [ |
| 59 |
'coupon_code.required' => esc_html__('Coupon code is required', 'fluent-cart'), |
| 60 |
'order_items.required' => esc_html__('Item selection is required', 'fluent-cart'), |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
public function sanitize(): array |
| 68 |
{ |
| 69 |
return [ |
| 70 |
'order_uuid' => 'sanitize_text_field', |
| 71 |
'id' => 'intval', |
| 72 |
'coupon_code' => 'sanitize_text_field', |
| 73 |
"order_items.*.id" => 'intval', |
| 74 |
"order_items.*.order_id" => 'intval', |
| 75 |
"order_items.*.post_id" => 'intval', |
| 76 |
"order_items.*.variation_id" => 'intval', |
| 77 |
"order_items.*.type" => 'sanitize_text_field', |
| 78 |
"order_items.*.quantity" => 'intval', |
| 79 |
"order_items.*.title" => 'sanitize_text_field', |
| 80 |
"order_items.*.price" => 'floatval', |
| 81 |
"order_items.*.unit_price" => 'floatval', |
| 82 |
"order_items.*.item_cost" => 'floatval', |
| 83 |
"order_items.*.item_total" => 'floatval', |
| 84 |
"order_items.*.tax_amount" => 'floatval', |
| 85 |
// floatval(null) would coerce to 0.0 and silently zero the items total the |
| 86 |
// min/max purchase gates sum — keep null as null so the service layer can |
| 87 |
// recompute it from unit_price * quantity instead. |
| 88 |
"order_items.*.subtotal" => function ($value) { |
| 89 |
return is_numeric($value) ? floatval($value) : null; |
| 90 |
}, |
| 91 |
"order_items.*.discount_total" => 'floatval', |
| 92 |
"order_items.*.total" => 'floatval', |
| 93 |
"order_items.*.line_total" => 'floatval', |
| 94 |
"order_items.*.cart_index" => 'intval', |
| 95 |
"order_items.*.rate" => 'floatval', |
| 96 |
"order_items.*.line_meta" => function ($value) { |
| 97 |
return is_array($value) ? $value : sanitize_text_field((string) $value); |
| 98 |
}, |
| 99 |
"order_items.*.other_info" => function ($value) { |
| 100 |
return is_array($value) ? $value : sanitize_text_field((string) $value); |
| 101 |
}, |
| 102 |
'applied_coupons.*' => 'intval', |
| 103 |
|
| 104 |
'customer_email' => function ($value) { |
| 105 |
if(empty($value)) { |
| 106 |
return ''; |
| 107 |
} |
| 108 |
|
| 109 |
return sanitize_email($value); |
| 110 |
}, |
| 111 |
]; |
| 112 |
} |
| 113 |
} |
| 114 |
|