PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.1
1.7.1 1.7.0 1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 All 51 releases
fluent-cart / app / Http / Requests / FrontendRequests / CouponRequest.php

CouponRequest.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.7.1, at app/Http/Requests/FrontendRequests/CouponRequest.php

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