条纹 Class 'Stripe\Charge

Stripe Class 'Stripe\Charge

我想实现一个 Stripe 支付表单,但我收到一个致命错误 Class 'Stripe\Charge。 class 在那里(见我的文件夹树),我试过了:

$charge =  \Stripe\Charge::create(array(

$charge =  \Charge::create(array(

但两者均无效。 我的 php 代码:

require_once('Stripe/lib/Stripe.php');
\Stripe\Stripe::setApiKey("my_key"); //Replace with your Secret Key

$charge =  \Stripe\Charge::create(array(
    "amount" => 1500,
    "currency" => "usd",
    "card" => $_POST['stripeToken'],
    "description" => "Charge for Facebook Login code."
));

您使用的是旧版 (1.x) 的 Stripe PHP 库。在那个版本中,所有 类 都被命名为 Stripe_Class.

在版本 2.0.0 中,语法更改为使用命名空间,现在所有 类 都命名为 \Stripe\Class.

如果可以的话,我建议您升级到最新版本(3.13.0)。您可以在这里找到它:https://github.com/stripe/stripe-php/releases。 Stripe 文档和 API 参考中的所有示例都使用更新的语法。

可以使用

use \Stripe\Stripe;
\Stripe\Stripe::setApiKey("my_key");
 public function chargeCard($token)
 {
    try
    {
        $charge=\Stripe\Charge::create(array(
            "amount" => 200,//Amount in cent.(100 cent equal to .00).it is smallest currency unit
            "currency" => "usd",//united state dollar .but we can use different countries currency code
            "source" => $token, // obtained createtoken function
            "description" => "Charge for testing"//desc of payment purpose,Automatic receipt emails will include the description of the charge(s)
          )
          );
      }
      catch(\Stripe\Card $e){
        echo $e->getMessage();
      }
  }

这对我有用:

$customer = Stripe_Customer::create(array(
      'email' => 'customer-email',
      'source'  => 'stripe_token'
  ));

  $charge = Stripe_Charge::create(array(
    "amount" => $order_amount, // amount in cents
    "customer"=>$customer->id,
    "currency" => "usd",
    "description" => "payinguser@example.com"
  ));