2015-07-13 51 views
2

當點擊提交付款按鈕時,我收到此錯誤消息。我在測試付款中遇到錯誤「Class'Stripe'not found」

Fatal error: Class 'Stripe' not found in /opt/lampp/htdocs/stripe-php/stripe_api.php on line 10

但我在需要'init.php'的代碼中包含Stripe.php文件。那麼,爲什麼我得到這種類型的錯誤,請給我建議?

我的代碼在這裏在這裏

<!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> 

content of init.php file

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'); 
?> 
+1

你可以張貼的init.php的內容是什麼? –

+0

** init.php文件的內容** –

+0

無法看到init.php文件的內容,編輯您的問題 –

回答

2

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

代碼PHP找不到Stripe類。

可能的原因:

  1. 您還沒有它。您需要有一個includerequire語句來加載該類,或者使用自動加載器。無論哪種方式,這個代碼可能會在你沒有向我們顯示的init.php中進行,所以我不能確定你迄今爲止做了什麼。

  2. 我碰巧知道Stripe的類是命名空間的,所以如果你打算直接引用它作爲Stripe,那麼你需要在代碼的頂部有一個use語句來定義引用。

    事情是這樣的:

    use Stripe\Stripe; 
    use Stripe\Charge; 
    

    您需要在每一個代碼文件中的這些行,你用的參考條紋類的名稱。或者,在您的代碼中使用完整的名稱空間引用它們。

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

+0

謝謝!!! @辛巴 –