我正在测试使用条形付款的简单结帐表单。我遵循了教程示例(https://stripe.com/docs/tutorials/forms),除了被拒绝的卡错误外,一切正常。根据api,使用4000000000000002来模拟被拒绝的卡。它只是崩溃了我的charge.php文件。这是来自html表单和charge.php文件的代码。
此处的实时版本:http://www.getwebshark.com/trial/step3
form.html -
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 11 February 2007), see www.w3.org" />
<meta http-equiv="Content-type" content="text/html; charset=us-ascii" />
<title>Stripe Getting Started Form</title>
<script type="text/javascript" src="https://js.stripe.com/v1/">
</script><!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script type="text/javascript" src=
"https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<script type="text/javascript">
//<![CDATA[
// this identifies your website in the createToken call below
Stripe.setPublishableKey('ihidthis');
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$ = $("#payment-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() {
$("#payment-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({
name: $('.card-name').val(),
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
return false; // submit from callback
});
});
if (window.location.protocol === 'file:') {
alert("stripe.js does not work when included in pages served over file:// URLs. Try serving this page over a webserver. Contact support@stripe.com if you need assistance.");
}
//]]>
</script>
</head>
<body>
<h1>Trial $4.99 with recurring $74.98 after 18 days</h1>
<!-- to display errors returned by createToken -->
<form action="charge.php" method="post" id="payment-form" name="payment-form">
<div class="form-row">
<label>Name on Card</label> <input type="text" size="20" autocomplete="off" class=
"card-name" />
</div>
<div class="form-row">
<label>Email</label> <input type="text" size="20" autocomplete="off" name=
"email" />
</div>
<div class="form-row">
<label>Card Number</label> <input type="text" size="20" autocomplete="off" class=
"card-number" />
</div>
<div class="form-row">
<label>CVC</label> <input type="text" size="4" autocomplete="off" class=
"card-cvc" />
</div>
<div class="form-row">
<label>Expiration (MM/YYYY)</label> <input type="text" size="2" class=
"card-expiry-month" /> <span>/</span> <input type="text" size="4" class=
"card-expiry-year" />
</div>
<div class="form-row">
<label>Shipping address</label> <input type="text" size="20" autocomplete="off"
name="address" />
</div>
<div class="form-row">
<label>City</label> <input type="text" size="20" autocomplete="off" name="city" />
</div>
<div class="form-row">
<label>State</label> <input type="text" size="20" autocomplete="off" name=
"state" />
</div>
<div class="form-row">
<label>Zip Code</label> <input type="text" size="20" autocomplete="off" name=
"zip" />
</div><button type="submit" class="submit-button">Submit Payment</button>
</form>
</body>
</html>
charge.php -
<?php
require_once('stripe-php/lib/Stripe.php');
Stripe::setApiKey("ihidthis");
// get the single use token from the form submitted by stripeResponseHandler
// set your secret key: remember to change this to your live secret key in production
// see your keys here https://manage.stripe.com/account
// get the credit card details submitted by the form
$token = $_POST['stripeToken'];
$email = $_POST['email'];
//combine these variables and post them all to stripe's customer description
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$description = $address . " " . $city . " " . $state . " " . $zip;
$customer = Stripe_Customer::create(array(
"card" => $token,
"plan" => "001",
"description" => $description,
"email" => $email
));
Stripe_Charge::create(array(
"amount" => 499, # amount in cents, again
"currency" => "usd",
"customer" => $customer->id
));
echo '<h1>Thank you for your business!</h1>';
?>
答案 0 :(得分:3)
Nicolas在下面是正确的,但由于我使用一次性费用并签署了一份定期计划,我需要在try catch块中包含客户和费用。
try {
$customer = Stripe_Customer::create(array(
"card" => $token,
"plan" => "001",
"description" => $description,
"email" => $email)
);
Stripe_Charge::create(array(
"amount" => 499,
"currency" => "usd",
"customer" => $customer->id)
);
$success = 'Your payment was successful.';
} catch (Exception $e) {
$error = $e->getMessage();
}
答案 1 :(得分:1)
您需要捕获第二页上返回的错误。第一页上的回复仅验证卡号。
try {
Stripe_Charge::create(array(
"amount" => 499,
"currency" => "usd",
"customer" => $customer->id)
);
$success = 'Your payment was successful.';
} catch (Exception $e) {
$error = $e->getMessage();
}