简体   繁体   中英

ReflectionException: Class StripeBilling does not exist

I am trying to implement a Billing interface which uses Stripe. I have created the Billing interface, Stripe class and binded the interface using a Service Provider.

I receive a Class not found error when trying to run the code:

ReflectionException in Container.php line 737: Class Acme\\Billing\\StripeBilling does not exist

I can't figure out what the issue is, I've double checked for small issues like correct case etc.

Here is the code I've used:

app/Acme/Billing/BillingInterface.php

<?php 

namespace Acme\Billing;

interface BillingInterface {
    public function charge(array $data);
}

app/Acme/Billing/StripeBilling.php

<?php 

namespace Acme\Billing;

use Stripe;
use Stripe_Charge;
use Stripe_Customer;
use Stripe_InvalidRequestError;
use Stripe_CardError;
use Exception;

class StripeBilling implements BillingInterface {

    public function __construct()
    {
        Stripe::setApiKey(env('STRIPE_SECRET_KEY'))
    }

    public function charge(array $data)
    {

        try
        {
            return Stripe_Charge::create([
                'amount' => 1000, // £10
                'currency' => 'gbp',
                'description' => $data['email'],
                'card' => $data['token']
            ]);
        } 

        catch(Stripe_CardError $e)
        {
            dd('card was declined');

        }

    }
}

app/Providers/BillingServiceProvider.php (UPDATED)

class BillingServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('Billing\BillingInterface', 'Billing\StripeBilling');
    }
}

BasketController.php (ADDED)

public function store(Request $request)
{

    $billing = \App::make('Billing\BillingInterface');
    return $billing->charge([
        'email' => $request->email,
        'stripe-token' => $request->token,
    ]);

I have added App\\Providers\\BillingServiceProvider::class to my app.php file, and updated my composer.json to include Acme folder "Acme\\\\": "app/"

Your problem looks two-fold:

  1. The PSR-4 autoload definition in your composer.json file is incorrect.

    If your Acme folder lives inside the app folder, eg /dir/project_root/app/Acme/Billing/BillingInterface.php , then your composer.json definition should look like this:

     "psr-4": { "Acme\\\\": "app/Acme" } 

    This is the root cause of the error you are receiving, which is not a Laravel-specific error. The autoloader simply cannot find the class you are asking for, even though the requested fully qualified class name is correct.

  2. Your interface and class are not bound to the container properly (missing the Acme portion of the namespace).

    Because you've defined both of them in the Acme namespace, you need to ensure Acme is present in your service provider definitions. So your service provider should look like this:

     class BillingServiceProvider extends ServiceProvider { public function register() { $this->app->bind('Acme\\Billing\\BillingInterface', 'Acme\\Billing\\StripeBilling'); } } 

    (Or, better yet, use the ::class syntax for improved IDE support.)

    You will also need to make sure the fully qualified classname is correct when requesting the class in your controller: App::make('Acme\\Billing\\BillingInterface') . (I would recommend using dependency injection instead of this syntax, anyway.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM