skip to Main Content

I have installed a package for Laravel 7, the package in question is this http://paypal.github.io/PayPal-PHP-SDK/ to manage payments with Paypal.

I created everything, controller, web route, everything.
But the moment I go to the page to test I can’t find the class.

Target class [AppHttpControllersPaypalController] does not exist.

PaypalController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use PayPalRestApiContext;
use PayPalAuthOAuthTokenCredential;

// use to process billing agreements
use PayPalApiAgreement;
use PayPalApiPayer;
use PayPalApiPlan;
use PayPalApiShippingAddress;

class PaypalController extends Controller
{
    private $apiContext;
    private $mode;
    private $client_id;
    private $secret;
    private $plan_id;
    
    // Create a new instance with our paypal credentials
    public function __construct()
    {
        // Detect if we are running in live mode or sandbox
        if(config('paypal.settings.mode') == 'live'){
            $this->client_id = config('paypal.live_client_id');
            $this->secret = config('paypal.live_secret');
            $this->plan_id = env('PAYPAL_LIVE_PLAN_ID', '');
        } else {
            $this->client_id = config('paypal.sandbox_client_id');
            $this->secret = config('paypal.sandbox_secret');
            $this->plan_id = env('PAYPAL_SANDBOX_PLAN_ID', '');
        }
        
        // Set the Paypal API Context/Credentials
        $this->apiContext = new ApiContext(new OAuthTokenCredential($this->client_id, $this->secret));
        $this->apiContext->setConfig(config('paypal.settings'));
    }

    public function paypalRedirect(){
        // Create new agreement
        $agreement = new Agreement();
        $agreement->setName('App Name Monthly Subscription Agreement')
          ->setDescription('Basic Subscription')
          ->setStartDate(CarbonCarbon::now()->addMinutes(5)->toIso8601String());

        // Set plan id
        $plan = new Plan();
        $plan->setId($this->plan_id);
        $agreement->setPlan($plan);

        // Add payer type
        $payer = new Payer();
        $payer->setPaymentMethod('paypal');
        $agreement->setPayer($payer);

        try {
          // Create agreement
          $agreement = $agreement->create($this->apiContext);

          // Extract approval URL to redirect user
          $approvalUrl = $agreement->getApprovalLink();

          return redirect($approvalUrl);
        } catch (PayPalExceptionPayPalConnectionException $ex) {
          echo $ex->getCode();
          echo $ex->getData();
          die($ex);
        } catch (Exception $ex) {
          die($ex);
        }

    }

    public function paypalReturn(Request $request){

        $token = $request->token;
        $agreement = new PayPalApiAgreement();

        try {
            // Execute agreement
            $result = $agreement->execute($token, $this->apiContext);
            $user = Auth::user();
            $user->role = 'subscriber';
            $user->paypal = 1;
            if(isset($result->id)){
                $user->paypal_agreement_id = $result->id;
            }
            $user->save();

            echo 'New Subscriber Created and Billed';

        } catch (PayPalExceptionPayPalConnectionException $ex) {
            echo 'You have either cancelled the request or your session has expired';
        }
    }
}

Routes

Route::get('/subscribe/paypal', 'PaypalController@paypalRedirect');
Route::get('/subscribe/paypal/return', 'PaypalController@paypalReturn');

I can’t understand what the problem is! Thank you all

2

Answers


  1. Just run:

    composer dump-autoload
    

    to regenerate all classes, see: https://getcomposer.org/doc/03-cli.md

    Login or Signup to reply.
  2. Mention the Laravel 7 route:

    Route::post('create_paypal_plan','AppHttpControllersPaypallController@create_plan');
    

    just like this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search