我有一个表格,我正在尝试按照此示例(https://gist.github.com/boucher/1750375)处理付款。我正在使用PHP和Stripe.js但不断收到此错误:
"exp_month"参数应为整数(而是未定义)
下面是我正在使用的代码。我错过了一些明显的东西吗?
<?php
session_start();
$name = htmlspecialchars($_POST['name']);
$description = "Test Transaction";
$amount = trim($_POST['amount']);
$email = htmlspecialchars(trim($_POST['email']));
$receipt_error = "Email receipt did not send!";
require_once 'includes/stripe-php-1.12.0/lib/Stripe.php';
if ($_POST) {
Stripe::setApiKey("sk_test_XXXXXX");
$error = '';
$success = '';
try {
if (!isset($_POST['stripeToken']))
throw new Exception("The Stripe Token was not generated correctly");
Stripe_Charge::create(array("amount" => $amount * 100,
"description" => $description,
"currency" => "usd",
"card" => $_POST['stripeToken']));
//PREPARE EMAIL
$to = $email;
$subject = "Receipt";
$headers =
'From: XXXX ' . "'r'n" .
'Reply-To: XXXX ' . "'r'n" .
'Bcc: XXXX' . "'r'n" .
'Content-type: text/html' . "'r'n" .
'X-Mailer: PHP/' . phpversion();
include_once('includes/email_receipt.php'); //Sent $message
//Send the email!
if(mail($to,$subject,$message,$headers)){
header("Location: thank-you.php");
}
else{
die($receipt_error);
}
}
catch (Exception $e) {
$error = $e->getMessage();
}
}
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" ></script>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script type="text/javascript">
// this identifies your website in the createToken call below
Stripe.setPublishableKey('pk_test_XXX');
function stripeResponseHandler(status, response) {
if (response.error) {
// re-enable the submit button
$('.submit_button').removeAttr("disabled");
// show the errors on the form
$(".payment-errors").html(response.error.message);
} else {
var form$ = $("#donation_form");
// token contains id, last4, and card type
var token = response['id'];
// insert the token into the form so it gets submitted to the server
form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
// and submit
form$.get(0).submit();
}
}
$(document).ready(function() {
$("#donation_form").submit(function(event) {
// disable the submit button to prevent repeated clicks
$('.submit_button').attr("disabled", "disabled");
// createToken returns immediately - the supplied callback submits the form if there are no errors
Stripe.createToken({
number: $('.number').val(),
name: $('.name').val(),
description: $('.description').val(),
cvc: $('.cvc').val(),
exp_month: $('.exp_month').val(),
exp_year: $('.exp_year').val()
}, stripeResponseHandler);
return false; // submit from callback
});
});
</script>
<form method="post" action="" class="reply-input" id="donation_form" >
<div class="input-block">
<label for="amount" class="label_comment"><strong>Donation Amount</strong>*</label>
<input type="text" name="amount" value="<?php echo $_POST['amount']; ?>" placeholder="$" data-stripe="amount" id="amount" required="">
</div>
<div class="input-block">
<label for="name" class="label_comment"><strong>Cardholder Name</strong>*</label>
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" data-stripe="name" id="name" required="">
</div>
<div class="input-block">
<label for="email" class="label_comment"><strong>Email</strong>*</label>
<input type="email" name="email" value="<?php echo $_POST['email']; ?>" data-stripe="email" id="email" required="">
</div>
<div class="input-block">
<label for="credit_card" class="label_comment"><strong>Credit Card Number</strong>*</label>
<input type="number" name="credit-card" value="" id="credit_card" data-stripe="number" required="">
</div>
<div class="input-block">
<label for="cvc" class="label_comment"><strong>CVV</strong>*</label>
<input type="number" name="cvc" value="" data-stripe="cvc" id="cvv" required="" maxlength="4">
</div>
<div class="input-block">
<label for="exp_month" class="label_comment"><strong>Expiration Month</strong>*</label>
<div class="clear"></div>
<select name='exp_month' id='exp_month' data-stripe="exp-month">
<option value=''>Month</option>
<option value='01'>Janaury</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='04'>April</option>
<option value='05'>May</option>
<option value='06'>June</option>
<option value='07'>July</option>
<option value='08'>August</option>
<option value='09'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<select name='exp_year' id='exp_year' data-stripe="exp-year">
<option value="">Year</option>
<?php
for($i=0;$i<21;$i++){
echo "<option value='".(date('Y')+$i)."'>".(date('y')+$i)."</option>'n";
}
?>
</select>
</div>
<input type="submit" class="btn btn-success btn-large submit_button" value="Click to Donate Today">
<div class="clear"></div>
</form>
在您的代码中,当 HTML 中没有带有".exp_month"的类时,您将按类名".exp_month"进行查询。如果您尝试按 id 查询,它应该是 $('#exp_month')