| @@ -1,9 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCart\App\Http\Requests; |
| 4 | 4 | |
| 5 | +use FluentCart\App\Helpers\Helper; | |
| 5 | 6 | use FluentCart\Framework\Foundation\RequestGuard; |
| 7 | +use FluentCart\Framework\Support\Arr; | |
| 6 | 8 | |
| 7 | 9 | class OrderRequest extends RequestGuard |
| 8 | 10 | { |
| 9 | 11 | |
| @@ -25,9 +27,13 @@ | ||
| 25 | 27 | 'discount_tax' => 'numeric', |
| 26 | 28 | 'manual_discount_total' => 'numeric', |
| 27 | 29 | 'coupon_discount_total' => 'numeric', |
| 28 | 30 | 'shipping_tax' => 'numeric', |
| 29 | - 'shipping_total' => 'numeric', | |
| 31 | + // min/max close the silent-corruption window: 1e19 passes `numeric` | |
| 32 | + // but wraps to a negative BIGINT through a float-to-int cast, and a | |
| 33 | + // negative shipping charge has no meaning. The bound matches the | |
| 34 | + // Helper::roundCent() guard (float's exact-integer range). | |
| 35 | + 'shipping_total' => 'numeric|min:0|max:9000000000000000', | |
| 30 | 36 | 'tax_total' => 'numeric', |
| 31 | 37 | 'total_amount' => 'numeric', |
| 32 | 38 | 'rate' => 'numeric', |
| 33 | 39 | 'note' => 'nullable|sanitizeTextArea|maxLength:5000', |
| @@ -81,28 +87,57 @@ | ||
| 81 | 87 | 'tax_lines.*.tax_amount' => 'nullable|numeric|min:0', |
| 82 | 88 | 'tax_lines.*.label' => 'nullable|sanitizeText', |
| 83 | 89 | 'tax_lines.*.is_compound'=> 'nullable', |
| 84 | 90 | |
| 85 | - 'applied_coupon' => 'nullable|array', | |
| 86 | - "applied_coupon.*.id" => 'nullable|numeric|min:1', | |
| 87 | - "applied_coupon.*.order_id" => 'nullable|numeric|min:1', | |
| 88 | - "applied_coupon.*.coupon_id" => 'required|numeric|min:1', | |
| 89 | - //"applied_coupon.*.title" => 'required|string|max:100', | |
| 90 | - "applied_coupon.*.code" => 'required|sanitizeText|maxLength:100', | |
| 91 | - //"applied_coupon.*.status" => 'required|string|max:100', | |
| 92 | - //"applied_coupon.*.type" => 'required|string|max:100', | |
| 93 | - "applied_coupon.*.amount" => 'nullable|numeric', | |
| 94 | - "applied_coupon.*.discounted_amount" => 'required|numeric', | |
| 95 | - "applied_coupon.*.discount" => 'nullable|numeric', | |
| 96 | - "applied_coupon.*.stackable" => 'required|numeric', | |
| 97 | - "applied_coupon.*.priority" => 'nullable|numeric', | |
| 98 | - "applied_coupon.*.max_uses" => 'nullable|numeric', | |
| 99 | - "applied_coupon.*.use_count" => 'nullable|numeric', | |
| 100 | - "applied_coupon.*.max_per_customer" => 'nullable|numeric|min:1', | |
| 101 | - "applied_coupon.*.min_purchase_amount" => 'nullable|numeric', | |
| 102 | - "applied_coupon.*.max_discount_amount" => 'nullable|numeric', | |
| 103 | - "applied_coupon.*.notes" => 'nullable|sanitizeTextArea|maxLength:100', | |
| 104 | - 'trigger' => 'nullable|string', | |
| 91 | + // `applied_coupon` is the admin order screen handing back, untouched, what | |
| 92 | + // POST coupons/apply returned: a map KEYED BY COUPON CODE whose rows are | |
| 93 | + // CouponServiceAdmin discount data (see ensureCouponExistInDiscountData()), | |
| 94 | + // NOT fct_applied_coupons rows. AdminOrderProcessor::insertAppliedCoupons() | |
| 95 | + // reads the code keys plus `id` and `discount` and builds its insert rows | |
| 96 | + // from the Coupon model, so those two are the whole load-bearing contract; | |
| 97 | + // everything else in the map is display metadata. | |
| 98 | + // | |
| 99 | + // The previous rules described fct_applied_coupons columns (coupon_id, code, | |
| 100 | + // discounted_amount, stackable) that no caller has ever sent. They were inert | |
| 101 | + // while the validator skipped absent wildcard children, and became a hard | |
| 102 | + // 422 on every coupon order once it started materializing them. | |
| 103 | + // | |
| 104 | + // The per-row closure is the backstop, not decoration: whether the wildcard | |
| 105 | + // rules below can fire at all depends on the validator materializing absent | |
| 106 | + // children, so on its own `applied_coupon.*.id => required` is silently | |
| 107 | + // unenforced on older framework builds. insertAppliedCoupons() subscripts | |
| 108 | + // ['id'] unguarded, so an entry without one writes a null coupon_id. | |
| 109 | + 'applied_coupon' => ['nullable', 'array', function ($attribute, $value) { | |
| 110 | + if (!is_array($value)) { | |
| 111 | + return null; // the `array` rule already reports this | |
| 112 | + } | |
| 113 | + | |
| 114 | + foreach ($value as $code => $row) { | |
| 115 | + $couponId = is_array($row) ? Arr::get($row, 'id') : null; | |
| 116 | + | |
| 117 | + if (!is_numeric($couponId) || (int) $couponId < 1) { | |
| 118 | + return sprintf( | |
| 119 | + /* translators: %1$s: the coupon code the admin applied to the order. */ | |
| 120 | + __('The applied coupon "%1$s" is missing its coupon id.', 'fluent-cart'), | |
| 121 | + sanitize_text_field((string) $code) | |
| 122 | + ); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + return null; | |
| 127 | + }], | |
| 128 | + "applied_coupon.*.id" => 'required|numeric|min:1', | |
| 129 | + // Bounded for the same reason as shipping_total above: sanitize() routes this | |
| 130 | + // through Helper::roundCent(), which throws outside float's exact-integer | |
| 131 | + // range, and a negative coupon discount has no meaning. | |
| 132 | + "applied_coupon.*.discount" => 'required|numeric|min:0|max:9000000000000000', | |
| 133 | + "applied_coupon.*.title" => 'nullable|sanitizeText|maxLength:192', | |
| 134 | + "applied_coupon.*.type" => 'nullable|sanitizeText|maxLength:100', | |
| 135 | + "applied_coupon.*.amount" => 'nullable|numeric', | |
| 136 | + "applied_coupon.*.actual_amount" => 'nullable|numeric', | |
| 137 | + "applied_coupon.*.unit_amount" => 'nullable|numeric', | |
| 138 | + "applied_coupon.*.actual_quantity" => 'nullable|numeric', | |
| 139 | + 'trigger' => 'nullable|string', | |
| 105 | 140 | ]; |
| 106 | 141 | } |
| 107 | 142 | |
| 108 | 143 | |
| @@ -138,9 +173,19 @@ | ||
| 138 | 173 | 'discount_tax' => 'floatval', |
| 139 | 174 | 'manual_discount_total' => 'floatval', |
| 140 | 175 | 'coupon_discount_total' => 'floatval', |
| 141 | 176 | 'shipping_tax' => 'floatval', |
| 142 | - 'shipping_total' => 'floatval', | |
| 177 | + // Cents column: normalize at the boundary so every consumer of this request | |
| 178 | + // receives a whole-cent int. floatval alone let a client-computed 19.99 * 100 | |
| 179 | + // arrive as 1998.9999999999998, which any later int cast would truncate. | |
| 180 | + // | |
| 181 | + // Wrapped in a closure, NOT passed as [Helper::class, 'roundCent']: an array | |
| 182 | + // value in this map is a LIST of callbacks, iterated one by one | |
| 183 | + // (vendor/wpfluent/framework/src/WPFluent/Support/Sanitizer.php:456-464), so the | |
| 184 | + // array-callable form would try to call Helper() as a function. | |
| 185 | + 'shipping_total' => function ($value) { | |
| 186 | + return Helper::roundCent($value); | |
| 187 | + }, | |
| 143 | 188 | 'tax_total' => 'floatval', |
| 144 | 189 | 'tax_behavior' => 'intval', |
| 145 | 190 | 'total_amount' => 'floatval', |
| 146 | 191 | 'rate' => 'sanitize_text_field', |
| @@ -201,27 +246,23 @@ | ||
| 201 | 246 | "tax_lines.*.is_compound"=> function ($value) { |
| 202 | 247 | return (bool) $value; |
| 203 | 248 | }, |
| 204 | 249 | |
| 205 | - "applied_coupon.*.id" => 'intval', | |
| 206 | - "applied_coupon.*.order_id" => 'intval', | |
| 207 | - "applied_coupon.*.coupon_id" => 'intval', | |
| 208 | - "applied_coupon.*.title" => 'sanitize_text_field', | |
| 209 | - "applied_coupon.*.discount" => 'intval', | |
| 210 | - "applied_coupon.*.code" => 'sanitize_text_field', | |
| 211 | - "applied_coupon.*.status" => 'sanitize_text_field', | |
| 212 | - "applied_coupon.*.type" => 'sanitize_text_field', | |
| 213 | - "applied_coupon.*.amount" => 'intval', | |
| 214 | - "applied_coupon.*.discounted_amount" => 'intval', | |
| 215 | - "applied_coupon.*.stackable" => 'intval', | |
| 216 | - "applied_coupon.*.priority" => 'intval', | |
| 217 | - "applied_coupon.*.max_uses" => 'intval', | |
| 218 | - "applied_coupon.*.use_count" => 'intval', | |
| 219 | - "applied_coupon.*.max_per_customer" => 'intval', | |
| 220 | - "applied_coupon.*.min_purchase_amount" => 'intval', | |
| 221 | - "applied_coupon.*.max_discount_amount" => 'intval', | |
| 222 | - "applied_coupon.*.notes" => 'sanitize_text_field', | |
| 223 | - 'trigger' => 'sanitize_text_field', | |
| 250 | + // Mirrors rules(): the coupons/apply discount-data shape, keyed by coupon code. | |
| 251 | + "applied_coupon.*.id" => 'intval', | |
| 252 | + // Already cents (CouponServiceAdmin rounds the distributed discount to two | |
| 253 | + // decimals in cents) — normalize the float artifact without scaling. A bare | |
| 254 | + // intval() here truncates, so a 9.99 discount would persist a cent short. | |
| 255 | + "applied_coupon.*.discount" => function ($value) { | |
| 256 | + return Helper::roundCent($value); | |
| 257 | + }, | |
| 258 | + "applied_coupon.*.title" => 'sanitize_text_field', | |
| 259 | + "applied_coupon.*.type" => 'sanitize_text_field', | |
| 260 | + "applied_coupon.*.amount" => 'intval', | |
| 261 | + "applied_coupon.*.actual_amount" => 'floatval', | |
| 262 | + "applied_coupon.*.unit_amount" => 'intval', | |
| 263 | + "applied_coupon.*.actual_quantity" => 'intval', | |
| 264 | + 'trigger' => 'sanitize_text_field', | |
| 224 | 265 | ]; |
| 225 | 266 | |
| 226 | 267 | } |
| 227 | 268 | } |