Reputation: 293
So far I've used composer which puts the lib underneath the vendor folder. It goes like this : vendor\stripe\stripe-php\lib
From there I'm bit lost on how I should declare the namespace (i guess in config/web.php underneath components). The namespace declared in the Stripe.php class file is Stripe.
In the controllers I would like to use it like shown in the examples on the Stripe site.
```
// 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" => "[email protected]")
);
} catch(\StripeLib\Error\Card $e) {
// The card has been declined
echo "The card has been declined. Please, try again.";
}
```
Upvotes: 1
Views: 3006
Reputation: 810
I struggled with this. I added the Stripe library to my composer file
"stripe/stripe-php" : "2.*"
that created /vendor/stripe/stripe-php
The stripe library uses just Stripe as the namespace. So then in my controller I put
use Stripe\Stripe;
use Stripe\Charge;
Then I was able to do things like
Stripe::setApiKey(Yii::$app->params['sk_test'])
and
try {
$charge = Charge::create(array(
"amount" => round(100*100,0), // amount in cents, again
"currency" => "usd",
"card" => $token,
"description" => 'description',
));
$paid = true;
} // end try
So far so good ...
Upvotes: 3
Reputation: 293
Ok, here how it goes. After including the lib using composer, you can find the stripe class under vendor/stripe/stripe-php/lib/stripe. So, in my controller I did this
use app\stripe\stripe-php\lib\Stripe;
But it didn't work because of the hyphen so I had to rename the folder stripe-php into stripephp (I know that is not good AT ALL) because it won't be updated with composer i guess.
Finally, I have this at the beginning of my controller
use app\stripe\stripephp\lib\Stripe;
Then in my action I use something like this
\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
...
Dont forget to put your private/public keys in your config file.
If by any mean you have a better solution (more elegant) please, you're welcome.
Upvotes: 0