Reputation: 307
I'm trying to load the Stripe PHP API (installed via Composer) into my CodeIgniter 4 app. This is what my files look like:
Composer.json
{
"description": "The CodeIgniter framework",
"name": "codeigniter/framework",
"type": "project",
"homepage": "https://codeigniter.com",
"license": "MIT",
"support": {
"forum": "http://forum.codeigniter.com/",
"wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
"slack": "https://codeigniterchat.slack.com",
"source": "https://github.com/bcit-ci/CodeIgniter"
},
"require": {
"php": ">=5.3.7",
"stripe/stripe-php": "^6.7"
},
"suggest": {
"paragonie/random_compat": "Provides better randomness in PHP 5.x"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*",
"phpunit/phpunit": "4.* || 5.*"
}
}
Autoload.php (only the code that matters)
...
$classmap = ['Stripe' => '../../vendor/stripe/stripe-php/lib/Stripe.php'];
...
MyController.php
public function construct() {
parent::__construct($request, $response, $logger = null);
$this->db = \Config\Services::db();
$this->stripe = Stripe();
}
Running this code gives me: Call to undefined function App\Controllers\Base\Stripe(). I'm pretty sure I set everything up correctly and I'm just not calling Stripe from my controller correctly. I took a look at the CodeIgniter 4 docs but couldn't find any help and since CI4 is so new there's not much online. Any help greatly appreciated!
Upvotes: 2
Views: 2106
Reputation: 290
Once you have it autoloaded, you don't need to pass it in a variable, just use it.
So Stripe is available through
\Stripe\[function]
From: https://stripe.com/docs/charges with extra commentary
// set your API key
\Stripe\Stripe::setApiKey("sk_test_your_key");
// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];
// you set a variable when expecting a result/return
$charge = \Stripe\Charge::create([
'amount' => 999,
'currency' => 'usd',
'description' => 'Example charge',
'source' => $token,
]);
Upvotes: 2
Reputation: 518
You have to include autoload.php in the controller.
include("path-to-autoload.php-in-vendor");
Upvotes: 1