php - Stripe.js/stripe.php - 所有卡错误都有效(过期、cvc、不正确的#),卡被拒绝除外

标签 php javascript arrays

我正在测试使用 stripe 进行付款的简单结帐表单。我按照教程示例 (https://stripe.com/docs/tutorials/forms) 进行操作,除了拒绝卡错误外,一切正常。根据 api,使用 4000000000000002 模拟被拒绝的卡。它只是让我的 charge.php 文件崩溃。这是来自 html 表单和 charge.php 文件的代码。

现场版在这里:http://www.getwebshark.com/trial/step3

表单.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>';
?>

最佳答案

Nicolas 在下面是正确的,但由于我使用的是一次性收费并为客户签了定期计划,所以我需要在 try catch block 中同时包含客户和收费。

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();
}

关于php - Stripe.js/stripe.php - 所有卡错误都有效(过期、cvc、不正确的#),卡被拒绝除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9706128/

相关文章:

javascript - 当从多个组件获取受控输入时,如何最大限度地提高 React 的性能?

java - 如何处理反转字符串上的标点符号和大写?

arrays - Swift3 iOS -Firebase 如何将节点数据分离到不同的数组和字典中

php - 为什么这不更新信息

javascript - Struts2上传多个文件前如何显示选中的文件名?

Php - 在 rewriteRule 中允许空间

javascript - 在 DOM 中向下和横向遍历

php - 通过 jQuery Ajax 传递 PHP 数组

php - 如果超过 3 个错误提交,显示验证码

mongodb - pecl 无法在 php CLI 模式下工作(涉及 Gearman 中的 mongoDB)