到目前为止,我使用了composer,它将lib放在供应商文件夹下。它是这样的:vendor '' stripe '' stripe php''lib
从那以后,我有点不知道应该如何声明命名空间(我想是在组件下面的config/web.php中)。在Stripe.php类文件中声明的名称空间是Stripe。
在控制器中,我想使用它,如Stripe网站上的示例所示。
```
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = 'StripeLib'Charge::create(array(
"amount" => 1000, // amount in cents, again
"currency" => "eur",
"source" => $token,
"description" => "payinguser@example.com")
);
} catch('StripeLib'Error'Card $e) {
// The card has been declined
echo "The card has been declined. Please, try again.";
}
```
我很难解决这个问题。我将Stripe库添加到我的作曲家文件中
"stripe/stripe-php" : "2.*"
创建了/vender/stripe/sstripe php
stripe库仅使用stripe作为命名空间。然后我把放在我的控制器里
use Stripe'Stripe;
use Stripe'Charge;
然后我可以做之类的事情
Stripe::setApiKey(Yii::$app->params['sk_test'])
和
try {
$charge = Charge::create(array(
"amount" => round(100*100,0), // amount in cents, again
"currency" => "usd",
"card" => $token,
"description" => 'description',
));
$paid = true;
} // end try
到目前为止还不错。。。
好的,现在开始吧。在使用composer包含lib之后,您可以在vendor/stripe/stripephp/lib/stripe下找到stripe类。所以,在我的控制器中,我做了这个
use app'stripe'stripe-php'lib'Stripe;
但由于连字符的原因,它不起作用,所以我不得不将文件夹stripe.php重命名为stripephp(我知道这一点都不好),因为我想它不会用composer更新。
最后,我在我的控制器的开头有这个
use app'stripe'stripephp'lib'Stripe;
然后在我的行动中,我使用类似于的东西
'Stripe'Stripe::setApiKey(Yii::$app->stripe->privateKey);
//we got the token, we must then charge the customer
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = 'Stripe'Charge::create(array(
"amount" => 1000, // amount in cents, again
...
别忘了把你的私钥/公钥放在你的配置文件里。
如果你有更好的解决方案(更优雅),欢迎你。