3

I'm messing around with Omnipay and I received this message:

Fatal error: Uncaught Error: Class 'Omnipay\Omnipay' not found

The directory listing:

  • composer.json
  • composer.lock
  • test.php
  • vendor

test.php

<?php
use Omnipay\Omnipay;

$gateway = Omnipay::create('Stripe');
$gateway->setApiKey('abc123');

$formData = array('number' => '4242424242424242', 'expiryMonth' => '6', 'expiryYear' => '2016', 'cvv' => '123');
$response = $gateway->purchase(array('amount' => '10.00', 'currency' => 'USD', 'card' => $formData))->send();

if ($response->isRedirect())
{
 // redirect to offsite payment gateway
 $response->redirect();
}
elseif ($response->isSuccessful())
{
 // payment was successful: update database
 print_r($response);
}
else
{
 // payment failed: display message to customer
 echo $response->getMessage();
}
?>

I don't code PHP in this fashion and the website directions are vague at this point. It looks like an excellent way to save time but...I don't code this way. What am I missing?

Leith
  • 3,139
  • 1
  • 27
  • 38
John
  • 1
  • 13
  • 98
  • 177
  • There are a lot of possible causes for this. Have you run composer update? Does your vendor/autoload load OK in your PHP classes. Are you using a framework of some kind that does the autoloading for you, and if so which one? – delatbabel Jul 27 '17 at 08:20
  • @delatbabel The whole point of running Omnipay for me was to simplify things and then I received an email reply telling me to "just use Composer" as if I couldn't determine the payment processor myself. That is a dependency and dependencies are weaknesses and an unnecessary waste of resources. Omnipay might be amazing but it only created another complicated mess. I'll learn from the code but I won't implement it so long as it dumps more dependencies. – John Jul 27 '17 at 14:29
  • Composer is a dependency of just about every PHP application these days. – delatbabel Jul 28 '17 at 05:36
  • @delatbabel Fair enough, that is why I write all my code from scratch which in turn is why my software is 10x faster on the exact same hardware. – John Jul 28 '17 at 06:32

1 Answers1

2

If you're using Composer, you need to make sure to include the Composer auto-loader - without it, your test.php script has no idea about anything Composer is doing.

As per their documentation, put this at the top of your file:

require __DIR__ . '/vendor/autoload.php';

Assuming you've run composer install or composer update to download the dependencies, your test.php script will then run the Composer auto-loader and make them available for your use statement.

Leith
  • 3,139
  • 1
  • 27
  • 38