omnipay/common
Framework-agnostic core for Omnipay payment gateways. Provides shared interfaces, request/response handling, HTTP client integration, and common utilities used by gateway drivers so apps can add and swap payment providers with a consistent API.
## Getting Started
### Minimal Setup in Laravel
1. **Installation**
```bash
composer require omnipay/common
No additional Laravel-specific configuration is needed—Omnipay is framework-agnostic.
First Use Case: Basic Purchase Flow
use Omnipay\Omnipay;
// Initialize a gateway (e.g., Stripe, PayPal) using its Omnipay driver
$gateway = Omnipay::create('Stripe'); // Replace with your provider
$gateway->setApiKey(config('services.stripe.key')); // Inject config
// Create a purchase request
$request = $gateway->purchase([
'amount' => '10.00',
'currency' => 'USD',
'description' => 'Laravel Product',
]);
// Send the request and handle the response
try {
$response = $request->send();
if ($response->isSuccessful()) {
// Success: Log transaction, redirect user, etc.
return redirect()->route('checkout.success');
}
// Handle failure (e.g., redirect to error page)
return back()->withErrors(['payment' => $response->getMessage()]);
} catch (\Exception $e) {
// Log and handle exceptions (e.g., network issues)
Log::error('Payment failed: ' . $e->getMessage());
}
Key Entry Points
vendor/omnipay/common/src/Omnipay/GatewayInterface for contracts.Omnipay\Common\Message\RequestInterface for standardized flows (e.g., purchase(), authorize()).Omnipay\Common\Message\ResponseInterface for response handling (e.g., isSuccessful(), getTransactionReference()).// services.php
'gateways' => [
'stripe' => \Omnipay\Stripe\Gateway::class,
'paypal' => \Omnipay\PayPal\Gateway::class,
],
// In a service class
$gateway = app(config('services.gateways.' . $provider));
$gateway->setApiKey(config("services.{$provider}.key"));
AppServiceProvider for dependency injection:
$this->app->bind(\Omnipay\Stripe\Gateway::class, function ($app) {
$gateway = \Omnipay::create('Stripe');
$gateway->setApiKey(config('services.stripe.key'));
return $gateway;
});
authorize() + capture()).
// Authorize first
$authorizeRequest = $gateway->authorize(['amount' => '10.00', 'currency' => 'USD']);
$authorizeResponse = $authorizeRequest->send();
if ($authorizeResponse->isSuccessful()) {
// Capture later (e.g., after inventory check)
$captureRequest = $gateway->capture([
'amount' => '10.00',
'currency' => 'USD',
'transactionId' => $authorizeResponse->getTransactionId(),
]);
$captureResponse = $captureRequest->send();
}
CapturePaymentJob::dispatch($gateway, $transactionId, $amount);
$gateway->setHttpClient(new \GuzzleHttp\Client([
'timeout' => 30,
'headers' => ['User-Agent' => 'Laravel/' . app()->version()],
]));
$gateway->setHttpClient(app(\Illuminate\Http\Client\PendingRequest::class)
->withOptions(['timeout' => 30])
->tap(function ($request) {
$request->withHeaders(['X-Custom-Header' => 'value']);
}));
getParameters() and validate() for dynamic input handling.
$request = $gateway->purchase($parameters);
if (!$request->validate()) {
throw new \InvalidArgumentException($request->getMessage());
}
$validated = request()->validate([
'amount' => 'required|numeric',
'currency' => 'required|string|size:3',
]);
Route::post('/webhook/stripe', function (Request $request) {
$gateway = app(\Omnipay\Stripe\Gateway::class);
$response = $gateway->completePurchase()->send();
// Handle webhook-specific logic
if ($response->isSuccessful()) {
event(new PaymentSucceeded($response->getTransactionReference()));
}
});
Gateway Initialization:
setApiKey()).
Fix: Use Laravel config (config('services.stripe.key')) and validate in AppServiceProvider boot.Omnipay\Stripe\Gateway vs. Omnipay\PayPal\Gateway).
Fix: Autoload via Omnipay::create('Stripe') to avoid typos.Request Validation:
validate() may not catch all Laravel validation rules (e.g., required fields).
Fix: Validate with Laravel’s validator first, then pass to Omnipay.Response Handling:
isSuccessful() means the payment was captured (it may just be authorized).
Fix: Check getTransactionReference() and implement idempotency keys.getMessage() for failed responses.
Fix: Log errors and redirect users with user-friendly messages:
return back()->withErrors(['payment' => $response->getMessage() ?? 'Payment failed.']);
$gateway->setHttpClient(new \GuzzleHttp\Client([
'debug' => true,
'handler' => \GuzzleHttp\HandlerStack::create(new \GuzzleHttp\Middleware::tap(
function ($request, $options) {
Log::debug('Omnipay Request:', [
'url' => (string) $request->getUri(),
'method' => $request->getMethod(),
'body' => $request->getBody() ? $request->getBody()->getContents() : null,
]);
}
)),
]));
4242 4242 4242 4242).
$gateway->setTestMode(true);
$gateway->setApiVersion('2018-11-29');
$request->setIdempotencyKey(str()->uuid());
Omnipay\Common\Message\AbstractRequest for provider-specific flows:
class CustomRequest extends \Omnipay\Common\Message\AbstractRequest {
public function getData() {
return [
'custom_field' => $this->getCustomField(),
] + parent::getData();
}
}
$response = $
How can I help you explore Laravel packages today?