2

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!

Pecans
  • 307
  • 5
  • 16

2 Answers2

2

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,
]);
Friderich Weber
  • 290
  • 2
  • 12
  • That does it! Thank you so much! Makes sense now that I see it, assumed I might have to instantiate an instance of the Stripe object and assign it to a variable in my controller. – Pecans May 23 '18 at 20:30
1

You have to include autoload.php in the controller.

include("path-to-autoload.php-in-vendor");
pspatel
  • 508
  • 2
  • 7
  • 18
  • I got the include to work and point to the right file (although I changed it to a require_once) but still can't get the Stripe() method to actually load in my controller. I checked and it seems Stripe IS being loaded in my composer/autoload_psr4.php like so: ` 'Stripe\\' => array($vendorDir . '/stripe/stripe-php/lib').` Any idea how to load the Stripe class now? Thanks for your help so far! – Pecans May 23 '18 at 19:03
  • Could you solve this? – Learning and sharing Nov 24 '21 at 04:30