fork download
  1. <?php
  2.  
  3. /**
  4.  * Check if an invoice should be marked as paid based on the transaction amount
  5.  * Considers a threshold of 1% below invoice amount to account for payment fees
  6.  *
  7.  * @param float $txAmount The actual transaction/payment amount
  8.  * @param float $invAmount The invoice amount
  9.  * @return bool True if the payment meets the threshold criteria
  10.  */
  11. function checkInvoicePayed($txAmount, $invAmount) {
  12. $threshold = $invAmount * 0.99; // 1% threshold
  13. return $txAmount >= $threshold;
  14. }
  15.  
  16. /**
  17.  * Calculate the invoice amount for which a given transaction amount
  18.  * would be at exactly the 1% threshold boundary
  19.  *
  20.  * @param float $txAmount The transaction amount to test
  21.  * @return float The calculated invoice amount
  22.  */
  23. function challengeCheckInvoicePayed($txAmount) {
  24. // If txAmount = invAmount * 0.99
  25. // Then invAmount = txAmount / 0.99
  26. return $txAmount / 0.99;
  27. }
  28.  
  29. /**
  30.  * Test the payment checking functions with various edge cases
  31.  */
  32. function runTests() {
  33. $testCases = [
  34. ['tx' => 100, 'expected_inv' => 101.01],
  35. ['tx' => 99, 'expected_inv' => 100],
  36. ['tx' => 0.99, 'expected_inv' => 1],
  37. ['tx' => 1000, 'expected_inv' => 1010.10],
  38. ];
  39.  
  40. foreach ($testCases as $test) {
  41. $calculatedInv = challengeCheckInvoicePayed($test['tx']);
  42. $isPassed = checkInvoicePayed($test['tx'], $calculatedInv);
  43.  
  44. echo sprintf(
  45. "Test case: TX=%.2f, Calculated INV=%.2f, Expected INV=%.2f, Is at threshold: %s\n",
  46. $test['tx'],
  47. $calculatedInv,
  48. $test['expected_inv'],
  49. $isPassed ? 'Yes' : 'No'
  50. );
  51.  
  52. // Verify the boundary condition
  53. $slightlyMore = checkInvoicePayed($test['tx'], $calculatedInv - 0.01);
  54. $slightlyLess = checkInvoicePayed($test['tx'], $calculatedInv + 0.01);
  55.  
  56. if ($slightlyMore !== true || $slightlyLess !== false) {
  57. echo "WARNING: Boundary condition failed for TX amount " . $test['tx'] . "\n";
  58. }
  59. }
  60. }
  61.  
  62. // Run the tests
  63. runTests();
Success #stdin #stdout 0.02s 25772KB
stdin
Standard input is empty
stdout
Test case: TX=100.00, Calculated INV=101.01, Expected INV=101.01, Is at threshold: Yes
Test case: TX=99.00, Calculated INV=100.00, Expected INV=100.00, Is at threshold: Yes
Test case: TX=0.99, Calculated INV=1.00, Expected INV=1.00, Is at threshold: Yes
Test case: TX=1000.00, Calculated INV=1010.10, Expected INV=1010.10, Is at threshold: Yes