Search code examples
phpcakephpcakephp-2.0stripe-payments

How to auto import all Stripe classes into CakePHP


How do I import all necessary classes from Stripe PHP SDK (2.1.0) into CakePHP (2.6.1)?

I have added a submodule for the Stripe and cloned it into app/Vendor/Stripe

My code looks like this:

<?php

App::uses('Stripe', 'Vendor/Stripe/lib');

// Secret API Key for Stripe
\Stripe\Stripe::setApiKey('sk_test_xxxxxxxxxxxxxxxxxxx');


class BillingController extends AppController {

// My methods

}


?>

and I get:

Fatal Error
Error: Class 'Stripe\Stripe' not found  
File: /Projects/Web/admin.example.io/app/Controller/BillingController.php   
Line: 6

Solution

  • You should use import() while using vendor , not uses() like

    App::import('Vendor', 'Stripe', array('file' => 'Stripe' . DS .'lib' . DS . 'Stripe.php'));
    

    Here DS is constant for /.

    You should follow cakephp naming convention like import() Must be used while using Vendor package . While using other classes such as controller you can use uses() but not here in your situation.

    Updated :

    Now steps , you need to follow as it is more specific to your problem :

    1.Install using composer.

    2.Now import installed package/vendor using

    App::import('Vendor', 'autoload', array('file' => 'Stripe' . DS .'vendor' . DS . 'autoload.php'));