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

Viewed 1701

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?

1 Answers

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.

Related