Welcome Guest, Not a member yet? Register   Sign In
Stripe Library
#1

I'm having problems migrating my CI3 Stripe Library to CI4.

I've run composer require stripe/stripe-php and a truck load of code has ended up in my vendor folder (as expected).

I've created a StripePayments.php file in app/Libraries with the following code

Code:
<?php

namespace App\Libraries;

class StripePayments
{
  protected $stripe;

  public function __construct()
  {
    $this->stripe = new \Stripe\StripeClient([
      'api_key' => getenv('stripe.secretkey'),
      'stripe_version' => '2020-08-27'
    ]);
  }

But my intelephense vscode linter extension is telling me "Nope, not gonna happen"  Sad

I've googled and searched github for a couple of hours now.

Hopefully someone here will be able to point me in the right direction Big Grin
Reply
#2

I believe that you need to add a use clause just above the class name.

PHP Code:
use Stripe\StripeClient

No sure if that is what is callusing your warning.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(05-14-2021, 01:32 AM)InsiteFX Wrote: I believe that you need to add a use clause just above the class name.

PHP Code:
use Stripe\StripeClient

No sure if that is what is callusing your warning.

Hi InsiteFX,

Thanks very much for your help again. Big Grin

Unfortunately I'm still getting a thumbs down from intelephense. It is displaying Undefined type 'Stripe\StripeClient' for the Stripe instantiation in the constructor

Code:
<?php

namespace App\Libraries;

use Stripe\StripeClient;

class StripePayments
{
  protected $stripe;

  public function __construct()
  {
    $this->stripe = new \Stripe\StripeClient([
      'api_key' => getenv('stripe.secretkey'),
      'stripe_version' => '2020-08-27'
    ]);
  }

Do you think I should add anything to  app\Config\Autoload.php ?
Reply
#4

If you add their library then you need to add it to the class map in the autoload.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Hi InsiteFX,

I have a success  Big Grin

Turns out I can ignore the undefined type 'Stripe\StripeClient' error flagged by intelephense, although I suspect intelephense will be strictly correct. btw - the intelephense vcs extension is awesome, if anyone is interested  Angel

I'm not an OOP guru, but have grasp of the main concepts. I'm going to post some of my code here , if anyone else is looking to add Stripe to their app.

As per the docs, I installed Stripe via composer.

I have a FrontController that extends the baseController and then I have a "Front" folder (under app/Controllers) of controllers that extend the FrontController - one of them is called Main.php

Here is the beginning of Main.php showing that I've temporarily hijacked the about page to test my Stripe connection.

Code:
<?php

namespace App\Controllers\Front;

use App\Controllers\FrontController;

class Main extends FrontController
{

  public function __construct()
  {
    parent::__construct();
  }

  public function index()
  {
    $this->loadPage('homepage');
  }

  public function about()
  {
    $stripe = new \App\Libraries\StripePayments();

    var_dump($stripe->listCustomers());
    die;

I have a StripePayments.php file under app/Libraries

Here is the beginning of the code showing listCustomers and getBalance functions

Code:
<?php

namespace App\Libraries;

use Stripe;

class StripePayments
{
  protected $stripe;

  public function __construct()
  {
    $this->stripe = new Stripe\StripeClient([
      'api_key' => getenv('stripe.secretkey'),
      'stripe_version' => '2020-08-27'
    ]);
  }

  public function listCustomers(array $params = [])
  {
    $result = $this->stripe->customers->all($params);
    return $result->toArray();
  }

  public function getBalance()
  {
    $result = $this->stripe->balance->retrieve();
    return $result->toArray();
  }


I did not need to add anything to app/Config/Autoload.php

Hope this helps others. Feel free to tell me I "this is not the way" or I should be using services  Tongue
Reply
#6

I personally use the phpStorm editor, doe's all the updates right from the editor.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
Reply




Theme © iAndrew 2016 - Forum software by © MyBB