在PHP上使用最大现金返还的优惠券

I want to make a discounted php formula with a voucher but must have a maximum cashback.

example: 20% discount with a maximum of 40000 cashback.

case: if inputted with the price of 100000 with a 20% voucher, the cashback earned is 20000, but if the inputted price is 500000 with a voucher of 20% also then the cashback is only 40000.

help me to solve it, I have searched Google but found no solution.

I have this script :

my script :

<?php
    require_once 'conn.php';
    $coupon_code = $_POST['coupon'];
    $price = $_POST['price'];
    $totalorder = $_POST ['totalorder'];

    $query = mysqli_query($conn, "SELECT * FROM `coupon` WHERE `coupon_code` = '$coupon_code' && `status` = 'Valid'") or die(mysqli_error());
    $count = mysqli_num_rows($query);
    $fetch = mysqli_fetch_array($query);
    $array = array();
    if($count > 0){
        $discount = $fetch['discount'] / 100;
        $total = $discount * $price;
        $array['discount'] = $fetch['discount'];
        $array['price'] = ($price - $total) * $totalorder;
        echo json_encode($array);
    }else{
        echo "error";
    }
?>

I hope this is what you expected :

<?php
function voucher_calc($price, $max, $voucher){
    $discount = $price * $voucher;
    if($discount >= $max){
        return $max;
    }else{
        return $discount;
    }
}

$price = 500000;
$voucher = 0.2;
$max = 40000;

echo voucher_calc($price, $max, $voucher);