payum/stripe
Payum Stripe extension for integrating Stripe payments via the Payum payment processing library. Provides gateway support and related actions; see Payum docs for setup and usage. MIT-licensed.
Installation:
composer require payum/stripe payum/payum-bridge
Ensure payum/payum-bridge is configured for Laravel (check bridge docs).
Configuration:
Add Stripe credentials to .env:
STRIPE_SECRET_KEY=your_secret_key
STRIPE_PUBLISHABLE_KEY=your_publishable_key
Publish Payum config:
php artisan vendor:publish --provider="Payum\Bridge\Laravel\PayumServiceProvider"
Update config/payum.php to include Stripe gateway:
'gateways' => [
'stripe' => [
'factory' => 'stripe',
'username' => env('STRIPE_SECRET_KEY'),
'password' => '', // Not used for Stripe
'sandbox' => env('APP_ENV') === 'local',
],
],
First Use Case: Capture a payment in a Laravel controller:
use Payum\Core\Payum;
use Payum\Core\Request\Capture;
public function charge(Payum $payum, $token)
{
$gateway = $payum->getGateway('stripe');
$request = new Capture();
$request->setToken($token);
$gateway->execute($request);
return redirect()->route('payment.success');
}
capture) with Stripe’s API.One-Time Payments:
Capture action for direct charges.$gateway->execute(new Capture([
'amount' => 1000, // $10.00
'currency' => 'usd',
'description' => 'Product #123',
'token' => $stripeToken, // From Stripe.js
]));
Subscriptions:
Subscribe action for Stripe subscriptions.$gateway->execute(new Subscribe([
'plan' => 'monthly_plan_id',
'token' => $stripeToken,
'customer' => $customerId, // Optional: attach to existing customer
]));
Webhooks:
Route::post('/stripe/webhook', [StripeWebhookController::class, 'handle']);
Notification action to process events:
public function handle(Request $request)
{
$gateway = $payum->getGateway('stripe');
$gateway->execute(new Notification($request->getContent()));
}
Refunds:
Refund action:
$gateway->execute(new Refund([
'charge' => $chargeId,
'amount' => 500, // Partial refund
]));
Tokenization:
Stripe.card.createToken(cardElement, {name: 'user'}).then(function(result) {
if (result.error) {
// Handle error
} else {
// Send result.token to Laravel
}
});
Storage:
config/payum.php:
'storage' => [
'factory' => 'array', // or 'doctrine', 'laravel'
],
Laravel Events:
Event::listen('payum.action.post.capture', function ($request) {
// Post-capture logic (e.g., update order status)
});
Testing:
4242 4242 4242 4242) and Payum’s test mode:
'gateways' => [
'stripe' => [
'sandbox' => true,
],
],
Stripe API Version Mismatch:
Stripe_Charge (use PaymentIntent in v2).t parameter instead of Stripe-Signature header).Webhook Idempotency:
Notification action may not handle retries gracefully. Stripe retries failed webhooks by default.if (eventAlreadyProcessed($eventId)) {
return response()->json(['status' => 'success']);
}
PHP 8.x Incompatibility:
return_type declarations or use deprecated PHP features.Missing Laravel-Specific Features:
dispatch(new ProcessPayment($gateway, $request));
Documentation Gaps:
payum-laravel.md runbook with:
Enable Payum Logging:
Add to config/payum.php:
'logging' => [
'enabled' => true,
'level' => 'debug',
'file' => storage_path('logs/payum.log'),
],
Stripe API Debugging:
Common Errors:
InvalidRequestError: Ensure amount is in cents (e.g., 1000 for $10.00).AuthenticationError: Verify STRIPE_SECRET_KEY in .env.Custom Actions:
Extend Payum’s Action classes for Stripe-specific logic:
namespace App\Payum\Action;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Request\Generic;
class CustomStripeAction implements ActionInterface
{
public function execute($request)
{
// Custom Stripe logic
}
public function supports($request)
{
return $request instanceof Generic;
}
}
Storage Adapters: Replace Payum’s default storage with Laravel’s Eloquent:
'storage' => [
'factory' => 'laravel',
'model' => App\Models\PayumToken::class,
],
Hybrid Stripe SDK Integration: Combine Payum with Stripe’s PHP SDK for v2 features:
use Stripe\PaymentIntent;
$intent = PaymentIntent::create([
'amount' => 1000,
'currency' => 'usd',
'metadata' => ['order_id' => $orderId],
]);
// Use Payum for idempotency/storage
$gateway->execute(new Capture(['payment_intent' => $intent->id]));
Testing Utilities: Mock Stripe responses in PHPUnit:
$gateway->getApi()->setHttpClient(new \Payum\Core\HttpClientInterface() {
public function send($request) {
return new \Symfony\Component\HttpFoundation\Response(
json_encode(['id' => 'ch_123']),
200
);
}
How can I help you explore Laravel packages today?