代码之家  ›  专栏  ›  技术社区  ›  deek

我的PHP信用卡表单无法识别某些输入和错误

  •  0
  • deek  · 技术社区  · 12 年前

    我正在学习John Conde关于Authorize.net使用PHP和错误检测的信用卡输入表单的教程。

    它非常完美,但我决定添加输入框来输入付款金额,并删除了不必要的发货地址要求;

    现在,当提交的表单输入不正确或为空时,它们不再变为红色,“金额”框也不会真正识别它是空的还是已填充的。错误框仍然会弹出,用于提交不良信用卡。

    这是页面(去掉了简化故障排除的设计);

    http://teetimelawncare.com/payment-form.php

    编辑:删除了与信用卡无关的代码以及州和年份到期日期等内容,使其更小。最底部的PHP代码用于红色错误弹出框,当用户错误填写表单时,该框会显示给用户。

    如果有人想比较的话,我在教程的这一部分: http://community.developer.authorize.net/t5/The-Authorize-Net-Developer-Blog/Handling-Online-Payments-Part-5-Processing-Payment-and-Handling/ba-p/10768

    代码:

    <?php
        $errors = array();
        if ('POST' === $_SERVER['REQUEST_METHOD'])
        {
            $credit_card           = sanitize($_POST['credit_card']);
            $expiration_month      = (int) sanitize($_POST['expiration_month']);
            $expiration_year       = (int) sanitize($_POST['expiration_year']);
            $cvv                   = sanitize($_POST['cvv']);
            $cardholder_first_name = sanitize($_POST['cardholder_first_name']);
            $cardholder_last_name  = sanitize($_POST['cardholder_last_name']);
            $billing_address       = sanitize($_POST['billing_address']);
            $billing_address2      = sanitize($_POST['billing_address2']);
            $billing_city          = sanitize($_POST['billing_city']);
            $billing_state         = sanitize($_POST['billing_state']);
            $billing_zip           = sanitize($_POST['billing_zip']);
            $telephone             = sanitize($_POST['telephone']);
            $email                 = sanitize($_POST['email']);
            $account  = sanitize($_POST['account']);
            $amount   = sanitize($_POST['amount']);
    
    
            if (!validateCreditcard_number($credit_card))
            {
                $errors['credit_card'] = "Please enter a valid credit card number";
            }
            if (!validateCreditCardExpirationDate($expiration_month, $expiration_year))
            {
                $errors['expiration_month'] = "Please enter a valid exopiration date for your credit card";
            }
            if (!validateCVV($credit_card, $cvv))
            {
                $errors['cvv'] = "Please enter the security code (CVV number) for your credit card";
            }
            if (empty($cardholder_first_name))
            {
                $errors['cardholder_first_name'] = "Please provide the card holder's first name";
            }
            if (empty($cardholder_last_name))
            {
                $errors['cardholder_last_name'] = "Please provide the card holder's last name";
            }
            if (empty($billing_address))
            {
                $errors['billing_address'] = 'Please provide your billing address.';
            }
            if (empty($billing_city))
            {
                $errors['billing_city'] = 'Please provide the city of your billing address.';
            }
            if (empty($billing_state))
            {
                $errors['billing_state'] = 'Please provide the state for your billing address.';
            }
            if (!preg_match("/^\d{5}$/", $billing_zip))
            {
                $errors['billing_zip'] = 'Make sure your billing zip code is 5 digits.';
            }
            if (empty($telephone))
            {
                $errors['telephone'] = 'Please provide a telephone number where we can reach you if necessary.';
            }
            if (!filter_var($email, FILTER_VALIDATE_EMAIL))
            {
                $errors['email'] = 'Please provide a valid email address';
            }
            if (empty($account))
            {
                $errors['account'] = 'Please provide the Your Customer ID Number from your billing statement.';
            }
            if (empty($amount))
            {
                $errors['amount'] = 'Please enter a payment amount.';
            }
            // If there are no errors let's process the payment
            if (count($errors) === 0)
            {
                // Format the expiration date
                $expiration_date = sprintf("%04d-%02d", $expiration_year, $expiration_month);
    
                // Include the SDK
                require_once('./config.php');
    
                // Process the transaction using the AIM API
                $transaction = new AuthorizeNetAIM;
                $transaction->setSandbox(AUTHORIZENET_SANDBOX);
                $transaction->setFields(
                    array(
                    'amount' => $amount,
                    'card_num' => $credit_card,
                    'exp_date' => $expiration_date,
                    'first_name' => $cardholder_first_name,
                    'last_name' => $cardholder_last_name,
                    'address' => $billing_address,
                    'city' => $billing_city,
                    'state' => $billing_state,
                    'zip' => $billing_zip,
                    'email' => $email,
                    'card_code' => $cvv,
                    'Customer ID Number' => $account,
    
                    )
                );
                $response = $transaction->authorizeAndCapture();
                if ($response->approved)
                {
                    // Transaction approved. Collect pertinent transaction information for saving in the database.
                    $transaction_id     = $response->transaction_id;
                    $authorization_code = $response->authorization_code;
                    $avs_response       = $response->avs_response;
                    $cavv_response      = $response->cavv_response;
    
                    // Put everything in a database for later review and order processing
                    // How you do this depends on how your application is designed
                    // and your business needs.
    
                    // Once we're finished let's redirect the user to a receipt page
                    header('Location: thank-you-page.php');
                    exit;
                }
                else if ($response->declined)
                {
                    // Transaction declined. Set our error message.
                    $errors['declined'] = 'Your credit card was declined by your bank. Please try another form of payment.';
                }
                else
                {
                    // And error has occurred. Set our error message.
                    $errors['error'] = 'We encountered an error while processing your payment. Your credit card was not charged. Please try again or contact customer service to place your order.';
    
        }
    ?>
    <!DOCTYPE HTML>
    <html>
        <head>
            <title>Payment Form</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <meta http-equiv="Content-Language" content="en-us">
            <style type="text/css">
                #errormessage
                {
                    background-color: #FFE7E7;
                    border: 3px solid #CC0033;
                    color: #000000;
                    margin: 20px ;
                    padding: 10px;
                    width: 420px;
                    -moz-border-radius: 6px;
                    -webkit-border-radius: 6px;
                    border-radius: 6px;
                    -moz-box-shadow: 5px 5px 5px #ccc;
                    -webkit-box-shadow: 5px 5px 5px #ccc;
                    box-shadow: 5px 5px 5px #ccc;
                    background: -webkit-gradient(linear, 0 0, 0 bottom, from(#FFEAEA), to(#FFB3B3));
                    background: -moz-linear-gradient(#FFEAEA, #FFB3B3);
                    background: linear-gradient(#FFEAEA, #FFB3B3);
                    float: left;
                }
                .labelerror
                {
                    color: #ff0000;
                    font-weight: bold;
                }
                h3 {
        font-size: 1.6em;
        line-height: 10px;
        padding-left: 17px;
        padding-top: 8px;
        -webkit-font-smoothing: antialiased;;
    
    }
                #credit
                {
                Position: relative;
                margin-left: 14px;
                height:620px;
                width:400px;
                 -webkit-border-radius: 6px;
                    border-radius: 6px;
                    -moz-box-shadow: 5px 5px 5px #ccc;
                    -webkit-box-shadow: 5px 5px 5px #ccc;
                    box-shadow: 5px 5px 5px #ccc;
                    float: left;
                }
                #amount1
                {
                margin: 5px;
                height:620px;
                position: relative;
                width:400px;
                 -webkit-border-radius: 6px;
                    border-radius: 6px;
                    -moz-box-shadow: 5px 5px 5px #ccc;
                    -webkit-box-shadow: 5px 5px 5px #ccc;
                    box-shadow: 5px 5px 5px #ccc; 
                    float: left;
                    }
            </style>
        </head>
        <body>
    
     <div id="amount1">  <h3> Payment Amount</h3><p>
                   <form id="myform"> <label for="amount"<?php if (in_array('amount', $errors)) echo ' class="labelerror"'; ?>> $</label>
                    <input type="text" name="amount" id="amount" maxlength="5" value=""></form>
                </p>  <br><div id="phpdisplay"> <form action="payment-form.php" method="get" enctype="application/x-www-form-urlencoded" target="_self" id="search">
         <strong>Get your current balance by searching<br> your Customer ID number</strong><br>(Don't Know? Ask us on live chat or check your billing invoice):<br> <input type="text" name="term" /><br />
        <input type="submit" name="btn" value="Search" />
        </form>
    
    
    </form></div>
    <div id="credit">
    <h3> Credit Card Information</h3>
            <form id="myform" action="/payment-form.php" method="post">
    
    
     <p>
                    <label for="credit_card"<?php if (in_array('credit_card', $errors)) echo ' class="labelerror"'; ?>>Credit Card Number</label>
                    <input type="text" name="credit_card" id="credit_card" autocomplete="off" maxlength="19" value="">
                </p>
                <p>
                    <label for="expiration_month"<?php if (in_array('expiration_month', $errors)) echo ' class="labelerror"'; ?>>Expiration Date</label>
                    <select name="expiration_month" id="expiration_month">
    
    
                        <option value="12">12</option>
                    </select>
                    <select name="expiration_year" id="expiration_year">
                        <option value="0"> </option>
    
                        <option value="2019">2019</option>
                        <option value="2020">2020</option>
                        <option value="2021">2021</option>
                    </select>
                </p>
                <p>
                    <label for="cvv"<?php if (in_array('cvv', $errors)) echo ' class="labelerror"'; ?>>Security Code</label>
                    <input type="text" name="cvv" id="cvv" autocomplete="off" value="" maxlength="4">
                </p>
                <p>
                    <label for="cardholder_first_name"<?php if (in_array('cardholder_first_name', $errors)) echo ' class="labelerror"'; ?>>Cardholder's First Name</label>
                    <input type="text" name="cardholder_first_name" id="cardholder_first_name" maxlength="30" value="">
                </p>
                <p>
                    <label for="cardholder_last_name"<?php if (in_array('cardholder_last_name', $errors)) echo ' class="labelerror"'; ?>>Cardholder's Last Name</label>
                    <input type="text" name="cardholder_last_name" id="cardholder_last_name" maxlength="30" value="">
                </p>
                <p>
                    <label for="billing_address"<?php if (in_array('billing_address', $errors)) echo ' class="labelerror"'; ?>>Billing Address</label>
                    <input type="text" name="billing_address" id="billing_address" maxlength="45" value="">
                </p>
                <p>
                    <label for="billing_address2"<?php if (in_array('billing_address2', $errors)) echo ' class="labelerror"'; ?>>Suite/Apt #</label>
                    <input type="text" name="billing_address2" id="billing_address2" maxlength="45" value="">
                </p>
                <p>
                    <label for="billing_city"<?php if (in_array('billing_city', $errors)) echo ' class="labelerror"'; ?>>City</label>
                    <input type="text" name="billing_city" id="billing_city" maxlength="25" value="">
                </p>
                <p>
                    <label for="billing_state"<?php if (in_array('billing_state', $errors)) echo ' class="labelerror"'; ?>>State</label>
                    <select id="billing_state" name="billing_state">
                        <option value="0"> </option>
                        <option value="AL">Alabama</option>
                        <option value="AK">Alaska</option>
                        <option value="AZ">Arizona</option>
                        <option value="AR">Arkansas</option>
    
    
                    </select>
                </p>
                <p>
                    <label for="billing_zip"<?php if (in_array('billing_zip', $errors)) echo ' class="labelerror"'; ?>>Zip Code</label>
                    <input type="text" name="billing_zip" id="billing_zip" maxlength="5" value="">
                </p>
                <p>
                    <label for="telephone"<?php if (in_array('telephone', $errors)) echo ' class="labelerror"'; ?>>Telephone Number</label>
                    <input type="text" name="telephone" id="telephone" maxlength="20" value="">
                </p>
                <p>
                    <label for="email"<?php if (in_array('email', $errors)) echo ' class="labelerror"'; ?>>Email Address</label>
                    <input type="text" name="email" id="email" maxlength="20" value="">
                </p>
                <p>
                    <label for="account"<?php if (in_array('account', $errors)) echo ' class="labelerror"'; ?>>Customer ID number</label>
                    <input type="text" name="account" id="account" maxlength="6" value="">
                </p>
    
                <p>
                    <input type="submit" value="Checkout">
                </p>
            </form></div><?php
        if (count($errors))
        {
    ?>
            <div id="errormessage">
                <h2>
                    There was an error with your submission. Please make the necessary corrections and try again.
                </h2>
                <ul>
    <?php
                foreach ($errors as $error)
                {
    ?>
                    <li><?php echo $error; ?></li>
    <?php
                }
    ?>
                </ul>
            </div>
    <?php
        }
    ?>
        </body>
    </html>
    

    最后,我想把checkout按钮移到div表单之外,所以我做了这样的按钮(在设计的页面中,而不是上面的例子)

    </form> <br>
        <form id="myform"><p class="center">
                    <button form="myform" input type="submit" value="Checkout">
                </p></form>
    

    按钮可以工作,但它不会将值显示为我(WIP)设计的页面上的标签。

    2 回复  |  直到 12 年前
        1
  •  2
  •   Tieson T.    12 年前

    这个:

    <button form="myform" input type="submit" value="Checkout">
    

    不是如何 <button> 元素被构造。看起来你试图改变 <input /> 。这很可能是你想要的:

    <button form="myform" type="submit">Checkout</button>
    

    看起来你也在复制 id 以两种不同的形式,这是无效的。移除 身份证件 在包装提交按钮的表单上,或者将其更改为其他内容。

        2
  •  2
  •   Community CDub    8 年前

    在我看来,这实际上是几个问题。由于有几个问题,如果我做错了什么,有人会指出,我可能会把事情搞混。

    回复:“金额”框实际上可以识别它是空的还是满的--

    你不能把金额分成它自己的形式,然后让它和其他形式元素中的其他元素一起使用。您想要发布的所有内容都必须使用相同的表单元素。(除非你使用html5表单属性,但我认为IE还不支持这一点。如果我错了,请有人纠正我。即使这样,如果我回忆正确,你也不会添加更多的表单元素。)请参阅: Is it possible to wrap html form elements in multiple form tags? 有关更多详细信息,请参阅已接受答案中的注释。

    关于没有错误更改的方框--

    <label for="billing_address2"<?php if (in_array('billing_address2', $errors)) echo ' class="labelerror"'; ?>>Suite/Apt #</label>
    

    可能应该是:

    <label for="billing_address2"<?php if (in_array('billing_address2', array_keys($errors))) echo ' class="labelerror"'; ?>>Suite/Apt #</label>
    

    您的数组使用元素名称进行键控,因此in_array应该搜索errors数组的键。(请注意,这将更改标签颜色,而不是输入框本身。如果您希望框本身发生更改,请将类设置代码放在框上。)

    按钮位于另一个答案中:

    <button form="myform" type="submit">Checkout</button>
    

    表单元素之外的HTML5。同样,不确定IE是否支持这一点。假设您的目标浏览器支持form属性,则无需将其包装在form元素btw中。

    <button type="submit">Checkout</button>
    

    内部形式。