我正在关注 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-付款和处理/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>
最后,我想将结帐按钮移到 div 表单之外,所以我制作了这样的按钮(在设计页面中,而不是上面的示例)
</form> <br>
<form id="myform"><p class="center">
<button form="myform" input type="submit" value="Checkout">
</p></form>
该按钮有效,但它没有在我的(WIP)设计页面上将值显示为标签。