【问题标题】:I am get an error in test payment "Class 'Stripe' not found"我在测试付款中收到错误“找不到类‘条纹’”
【发布时间】:2015-07-13 21:30:37
【问题描述】:

我在点击提交付款按钮时收到此错误消息。

致命错误:在第 10 行的 /opt/lampp/htdocs/stripe-php/stripe_api.php 中找不到类“Stripe”

但我将 Stripe.php 文件包含在我的代码中 require 'init.php' 中。那么为什么我会遇到这种类型的错误请建议我?

我的代码在这里

stripe_api.php :

<?php

   //require(dirname(__FILE__) . '/init.php');
   require 'init.php';

   $error = '';
   $success = '';

  if ($_POST) {
   Stripe::setApiKey("pk_test_knsJUL8pXsPgKOPIrVtleSab");

   try {
      if (!isset($_POST['stripeToken']))
          throw new Exception("The Stripe Token was not generated correctly");
        Stripe_Charge::create(array("amount" => 5,
                                "currency" => "usd",
                                "card" => $_POST['stripeToken']));
             $success = 'Your payment was successful.';
     }
   catch (Exception $e) {
       $error = $e->getMessage();
   }
 }

代码在这里

 <!DOCTYPE html>
 <html lang="en">
  <head>
      <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>payment</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">
        // this identifies your website in the createToken call below
        Stripe.setPublishableKey('pk_test_knsJUL8pXsPgKOPIrVtleSab');

        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({
                    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
            });
        });
    </script>
  </head>
 <body>
    <h1>Charge $10 with Stripe</h1>
    <!-- to display errors returned by createToken -->
    <span class="payment-errors"><?= $error; ?></span>
    <span class="payment-success"><?= $success; ?></span>
    <form action="" method="POST" id="payment-form">
        <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>
        <button type="submit" class="submit-button">Submit Payment</button>
    </form>
</body>

init.php 文件内容

 code here
   <?php

     // Stripe singleton
     require(dirname(__FILE__) . '/lib/Stripe.php');

     // Utilities
     require(dirname(__FILE__) . '/lib/Util/RequestOptions.php');
     require(dirname(__FILE__) . '/lib/Util/Set.php');
     require(dirname(__FILE__) . '/lib/Util/Util.php');

     // HttpClient
     require(dirname(__FILE__) . '/lib/HttpClient/ClientInterface.php');
     require(dirname(__FILE__) . '/lib/HttpClient/CurlClient.php');

     // Errors
     require(dirname(__FILE__) . '/lib/Error/Base.php');
     require(dirname(__FILE__) . '/lib/Error/Api.php');
     require(dirname(__FILE__) . '/lib/Error/ApiConnection.php');
     require(dirname(__FILE__) . '/lib/Error/Authentication.php');
     require(dirname(__FILE__) . '/lib/Error/Card.php');
     require(dirname(__FILE__) . '/lib/Error/InvalidRequest.php');
     require(dirname(__FILE__) . '/lib/Error/RateLimit.php');

     // Plumbing
     require(dirname(__FILE__) . '/lib/Object.php');
     require(dirname(__FILE__) . '/lib/ApiRequestor.php');
     require(dirname(__FILE__) . '/lib/ApiResource.php');
     require(dirname(__FILE__) . '/lib/SingletonApiResource.php');
     require(dirname(__FILE__) . '/lib/AttachedObject.php');
     require(dirname(__FILE__) . '/lib/ExternalAccount.php');

     // Stripe API Resources
     require(dirname(__FILE__) . '/lib/Account.php');
     require(dirname(__FILE__) . '/lib/AlipayAccount.php');
     require(dirname(__FILE__) . '/lib/ApplicationFee.php');
     require(dirname(__FILE__) . '/lib/ApplicationFeeRefund.php');
     require(dirname(__FILE__) . '/lib/Balance.php');
     require(dirname(__FILE__) . '/lib/BalanceTransaction.php');
     require(dirname(__FILE__) . '/lib/BankAccount.php');
     require(dirname(__FILE__) . '/lib/BitcoinReceiver.php');
     require(dirname(__FILE__) . '/lib/BitcoinTransaction.php');
     require(dirname(__FILE__) . '/lib/Card.php');
     require(dirname(__FILE__) . '/lib/Charge.php');
     require(dirname(__FILE__) . '/lib/Collection.php');
     require(dirname(__FILE__) . '/lib/Coupon.php');
     require(dirname(__FILE__) . '/lib/Customer.php');
     require(dirname(__FILE__) . '/lib/Event.php');
     require(dirname(__FILE__) . '/lib/FileUpload.php');
     require(dirname(__FILE__) . '/lib/Invoice.php');
     require(dirname(__FILE__) . '/lib/InvoiceItem.php');
     require(dirname(__FILE__) . '/lib/Plan.php');
     require(dirname(__FILE__) . '/lib/Recipient.php');
     require(dirname(__FILE__) . '/lib/Refund.php');
     require(dirname(__FILE__) . '/lib/Subscription.php');
     require(dirname(__FILE__) . '/lib/Token.php');
     require(dirname(__FILE__) . '/lib/Transfer.php');
     require(dirname(__FILE__) . '/lib/TransferReversal.php');
 ?>

【问题讨论】:

  • 你能把init.php的内容贴出来吗?
  • **init.php文件内容**
  • 看不到 init.php 文件内容,编辑你的问题
  • 现在你可以看到....请检查
  • 尝试在您的 api.php 文件顶部添加 use Stripe\Stripe。现在 PHP 在当前命名空间中找不到 Stripe 类。

标签: javascript php html stripe-payments


【解决方案1】:

PHP 找不到 Stripe 类。

可能的原因:

  1. 你没有包含它。您需要有includerequire 语句来加载类,或使用自动加载器。无论哪种方式,这段代码都可能会出现在您尚未向我们展示的init.php 中,因此我无法确定您到目前为止做了什么。

  2. 我碰巧知道 Stripe 的类是有命名空间的,所以如果你打算将它直接引用为 Stripe,那么你需要在代码顶部有一个 use 语句来定义引用。

    类似这样的:

     use Stripe\Stripe;
     use Stripe\Charge;
    

    您需要在每个代码文件中使用这些行来引用 Stripe 类名称。或者,在您的代码中使用它们的完整命名空间引用它们。

    例如\Stripe\Stripe::setApiKey('....');

【讨论】:

    猜你喜欢
    • 2020-02-12
    • 2021-05-11
    • 2023-02-03
    • 2015-01-03
    • 1970-01-01
    • 2022-07-19
    • 2013-09-15
    • 2022-11-28
    • 2021-07-08
    相关资源
    最近更新 更多