javascript - 创建 Stripe token 时遇到问题 : 402 (Payment Required) error

标签 javascript jquery payment-gateway stripe-payments

大家好,

下面是控制台中抛出的错误代码:

   (index):3 POST https://api.stripe.com/v1/tokens 402 (Payment Required)

(index):3 POST https://api.stripe.com/v1/tokens 402 (Payment Required)c @ (index):3e @ (index):3a @ (index):3Stripe.isDoubleLoaded.Stripe.xhr @ (index):3Stripe.a._rawRequest @ (index):2Stripe.a.request @ (index):2Stripe.token.a.create @ (index):2Stripe.card.b.createToken @ (index):2Stripe.a._channelListener @ (index):2incoming @ (index):2f @ (index):2

下面是 JavaScript 代码

// Generate the user token
$(function() {


      // Once the user has submited the form
      $(".submit").click(function(e) {

                  // Prevent the form from submiting by default
          e.preventDefault();

          // Ensure that checkbox is checked
          if($('#checkbox').is(":checked")) {

            // Prevent the form from submiting by default
            var $form = $('#payment-form');

            // Request a token from Stripe:
            Stripe.card.createToken($form, stripeResponseHandler);

            // Prevent the form from being submitted:
            return false;
          }
          else {

        // Display an error message to the user by writting directly into the header error tag
        document.getElementById('checkboxError').innerHTML = "You must kindly accept the terms and conditions to continue.";
          }
       });

      // Ensure that checkbox is checked
      if($('#checkbox').is(":checked")) {

        var appendedStripeToken = false;

        function stripeResponseHandler(status, response) {
          // Grab the form:
          var $form = $('#payment-form');

          if (response.error) { // Problem!

            // Scroll to the billing section
            $("#billingError").scrollTop();

            // Show the errors on the form:
            $form.find('.payment-errors').text(response.error.message);
            $form.find('.submit').prop('disabled', false); // Re-enable submission

          } else { // Token was created!

            // Get the token ID:
            var token = response.id;
            handleCall(token);

          }

          // What to do after the token has been generated.
          function handleCall(token) { 
           var $form = $('#payment-form');
            if (!appendedStripeToken) { 
                // Insert the token into the form so it gets submitted to the server
                appendedStripeToken = true; 
                phpCall(); 
            } 
          }

          // Post the package name, the token, and the user name information to the billing.php page
          function phpCall() {
           if( appendedStripeToken === true ){
             $.ajax({
              type: "POST",
              data: {run: true, packageName: $('#packageName').val(), token: token, userName: $('#userName').val(),customerName: $('#customerName').val() },
              url: '/app/functions/billing.php',
              success: function (response) {//response is value returned from php (for    your example it's "bye bye"
                            $('#payment-form').prop('disabled', true); // TODO: give your html-submit-input-tag an "id" attribute
              window.location = response;
                }
               });
             }
            } 
          }

      } 


});

下面是HTML代码

              <!-- Package name to be submitted to server -->
              <input type="hidden" id="packageName" value="{{ packageName|capitalize }}">

              <!-- Username to be submitted to server -->
              <input type="hidden" id="userName" value="{{ userName }}">
               <input type="hidden" id="customerName" value="{{ customerName }}">



              <div class="form-row">
                <label for="cardHolder">Cardholder Name</label><br>
                  <input type="text" id="cardHolder" size="20" data-stripe="name">
                </label>
              </div>
              <br><br>


              <div class="form-row">
                <label for="cardNumber">Card Number </label><br>
                  <input type="text" id="cardNumber" size="20" data-stripe="number">
                </label>
              </div>
              <br>
              <img src="/public/images/credit-card/visa.png" class="card-visa">
              <img src="/public/images/credit-card/mastercard.png" class="card-mastercard">
                <img src="/public/images/credit-card/american-express.png" class="card-aexpress">




              <br>

              <div class="form-row">
                <label for="cardExpiration"> Expiration (MM/YY)</label><br>
                  <input class="expirationNumber" type="text" size="2" id="cardExpiration" data-stripe="exp_month">
                </label>
                <span> / </span>
                <input class="expirationNumber" type="text" size="2" data-stripe="exp_year">
              </div>
              <br><br>

              <div class="form-row">
                <label for="cardCVC">CVC</label><br>
                  <input type="text" id="cardCVC" size="4" data-stripe="cvc">
                </label>
              </div>

              </div>
            </div>

             <input type="checkbox" id="checkbox"> 
             <label for="checkbox">By purchasing this package you are agreeing to our Terms & Conditions</label><br><br>
              <h4 id="checkboxError"></h4>
               <button type="submit" class="submit btn-tangerine">Submit Payment</button>

              </form>

任何帮助将不胜感激!

我认为主要错误在于以下行:

Stripe.card.createToken($form, stripeResponseHandler);

假设发生的事情非常简单。当提供所有正确的信息时, token 就会被创建,然后 token 与其他信息一起通过 ajax 发布到服务器,其中 PHP 代码将使用这些信息创建费用。

最佳答案

当我添加测试 API key 进行付款时,我遇到了同样的问题,然后网站工作正常,当我添加实时 key 时,网站在控制台上显示相同的错误。 但问题是,我正在使用测试信用卡号测试实时 key 。也许你也犯了同样的错误

抱歉英语不好。

关于javascript - 创建 Stripe token 时遇到问题 : 402 (Payment Required) error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37714527/

相关文章:

JavaScript SRC 路径不起作用

javascript - 如何使用 Jquery/Javascript/css 在每次点击时动态旋转圆圈?

php - Omnipay MiGS : how to handle users closing the browser after a successful transaction?

security - TLS 足够安全吗?需要在 PA-DSS 支付应用程序中滚动哈希吗?

php - 我可以在 adobe AIR 桌面应用程序中集成支付网关吗?

php - Ajax 自动更新和服务器负载

javascript - 在不插入多余标签的情况下关闭 contenteditable 中的 <ul/> 标签

Javascript正则表达式加粗多个字符串

javascript - 在 jQuery zIndex 中创建多个 div

javascript - 使用 jQuery/JS,我可以将 "tie"和一个 JS 对象的属性一起直接赋给表单输入元素的值吗?