在PHP中使用Stripe API创建客户时,为什么我的数组不返回任何数据?

时间:2019-04-09 00:15:21

标签: stripe-payments php-5.6

我正在尝试在Stripe中创建客户并收费。 但是,当我尝试在$ customer数组变量中对数组数据进行print_r时,会得到一个空白页。

我还尝试将字符串回显到charge.php页面,以确保Charge页面有效(回显“ page working”);并且确实如此。所以我知道这不是问题的一部分。

权限似乎还可以。

这些是有关文件的权限 -rw-rw-r-- 1个沙箱管理员682 Apr 7 15:55 charge.php -rw-rw-r-- 1个沙箱管理员1612 Apr 7 17:01 index.php

我已经多次检查代码,没有运气。 有人可以帮助我了解我可能做错了什么吗?

注意:下面显示的代码均位于相应文件类型的单独文件中,即所有PHP代码位于不同的.php文件中,而JS位于.js文件中。 注释标记不在实际代码中。它们只是每个文件在此对话开始的位置的指示。

     <!-- index.php code below -->
     <html>
     <body>
        <div class="container">
        <h2 class="my-4 text-center">Title of Product Page Here</h2>
            <form action="./charge.php" method="post" id="payment-form">
            <div class="form-row">
            <input type="text" class="form-control mb-3 StripeElement StripeElement--empty" name="first_name" placeholder="First Name">
            <input type="text" class="form-control mb-3 StripeElement StripeElement--empty" name="last_name" placeholder="Last Name">
            <input type="email" class="form-control mb-3 StripeElement StripeElement--empty" name="email" placeholder="Email Address">
                <div id="card-element" class="form-control">
                <!-- A Stripe Element will be inserted here. -->
                </div>

                <!-- Used to display form errors. -->
                <div id="card-errors" role="alert"></div>
            </div>

            <button>Submit Payment</button>
            </form>
        </div>

        <script src="https://js.stripe.com/v3/"></script>
        <script src="js/charge.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </body>
    </html>

    <!-- charge.php code below -->

    <?php
    require_once('vendor/autoload.php');
    \Stripe\Stripe::setApiKey('sk_test_kOCozPXbhBGR30zsrrTXSGyg00wzUJG39W');

    // Sanitize 
    $POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
    $first_name = $POST['first_name'];
    $last_name = $POST['last_name'];
    $email = $POST['email'];
    $token = $POST['stripeToken'];

    // Create customer 
    $customer = \Stripe\Customer::create(array(
        "email" => $email,
        "source" => $token
    ));

    // Charge customer
    $charge = \Stripe\Charge::create(array(
        "amount" => 5000,
        "currency" => "usd",
        "description" => "Audio Loops Pack 2019",
        "customer" => $customer->id
     ));

    print_r($customer);


    <!-- charge.js code below -->

    // Create a Stripe client.
    var stripe = Stripe('pk_test_OHd7EQLWyzTxvcjBRWVfK6vG00XRu3oMIJ');

    // Create an instance of Elements.
    var elements = stripe.elements();

    // Custom styling can be passed to options when creating an Element.
    // (Note that this demo uses a wider set of styles than the guide below.)
    var style = {
      base: {
        color: '#32325d',
        fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
        fontSmoothing: 'antialiased',
        fontSize: '16px',
        '::placeholder': {
          color: '#aab7c4'
        }
      },
      invalid: {
        color: '#fa755a',
        iconColor: '#fa755a'
      }
    };

    //Style button 
    document.querySelector('#payment-form button').classList ='btn btn-primary btn-block mt-4';

    // Create an instance of the card Element.
    var card = elements.create('card', {style: style});

    // Add an instance of the card Element into the `card-element` <div>.
    card.mount('#card-element');

    // Handle real-time validation errors from the card Element.
    card.addEventListener('change', function(event) {
      var displayError = document.getElementById('card-errors');
      if (event.error) {
        displayError.textContent = event.error.message;
      } else {
        displayError.textContent = '';
      }
    });

    // Handle form submission.
    var form = document.getElementById('payment-form');
    form.addEventListener('submit', function(event) {
      event.preventDefault();

      stripe.createToken(card).then(function(result) {
        if (result.error) {
          // Inform the user if there was an error.
          var errorElement = document.getElementById('card-errors');
          errorElement.textContent = result.error.message;
        } else {
          // Send the token to your server.
          stripeTokenHandler(result.token);
        }
      });
    });

    // Submit the form with the token ID.
    function stripeTokenHandler(token) {
      // Insert the token ID into the form so it gets submitted to the server
      var form = document.getElementById('payment-form');
      var hiddenInput = document.createElement('input');
      hiddenInput.setAttribute('type', 'hidden');
      hiddenInput.setAttribute('name', 'stripeToken');
      hiddenInput.setAttribute('value', token.id);
      form.appendChild(hiddenInput);

      // Submit the form
      form.submit();
    }

我希望客户和费用阵列返回数据,但屏幕上什么也没有,甚至没有空的阵列。

0 个答案:

没有答案