Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Stripe Laravel Package

crs/stripe

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to First Payment

  1. Installation

    composer require crs/stripe
    

    Ensure CRS\StripeBundle\CRSStripeBundle() is registered in app/Kernel.php under $bundles.

  2. Configuration Add Stripe keys to config/packages/crs_stripe.yaml (or config.yml in older Laravel/Symfony versions):

    crs_stripe:
        publishable_key: "pk_test_your_key"
        secret_key: "sk_test_your_key"
        currency: "USD"
    
  3. First Use Case: Direct Charge

    use CRS\StripeBundle\Stripe;
    
    $stripe = new Stripe();
    $stripe->setCardNumber('4242424242424242')
           ->setName('John Doe')
           ->setExpMonth(12)
           ->setExpYear(2030)
           ->setCVC('123');
    
    $charge = $stripe->Charge()->Pay(100); // $1.00 (amount in cents)
    

Implementation Patterns

Workflows

  1. Customer Creation & Subscription

    // Create a customer
    $customer = $stripe->Customer()->Create([
        'email' => 'user@example.com',
        'source' => 'tok_visa' // Token from Stripe.js
    ]);
    
    // Subscribe the customer
    $subscription = $stripe->Subscription()->Create([
        'customer' => $customer->id,
        'plan' => 'monthly_plan_id'
    ]);
    
  2. Webhook Handling Use Laravel’s route:web middleware to handle Stripe webhooks (e.g., payment_intent.succeeded):

    Route::post('/stripe/webhook', [StripeWebhookController::class, 'handle']);
    

    Verify signatures with:

    $event = \Stripe\Webhook::constructEvent(
        $request->getContent(),
        $request->header('Stripe-Signature'),
        'your_webhook_secret'
    );
    
  3. Token-Based Payments (Recommended) Use Stripe.js to generate tokens client-side, then pass to Laravel:

    // Frontend (Stripe.js)
    Stripe('pk_test_key').createToken(card).then(function(result) {
        if (result.error) { /* Handle error */ }
        else {
            fetch('/charge', { method: 'POST', body: JSON.stringify({ token: result.token.id }) });
        }
    });
    
    // Backend
    $charge = $stripe->Charge()->PayWithToken($request->token, 100);
    

Integration Tips

  • Laravel Service Provider: Bind the Stripe client to Laravel’s container for dependency injection:
    // In StripeServiceProvider
    $this->app->singleton(Stripe::class, function ($app) {
        return new Stripe($app['config']['crs_stripe']);
    });
    
  • Validation: Validate card data before processing:
    use CRS\StripeBundle\Validators\CardValidator;
    $validator = new CardValidator();
    if (!$validator->validate($stripe)) {
        throw new \InvalidArgumentException('Invalid card details');
    }
    
  • Logging: Log charges for auditing:
    \Log::info('Stripe Charge', [
        'amount' => $charge->amount,
        'customer' => $charge->customer,
        'status' => $charge->status
    ]);
    

Gotchas and Tips

Pitfalls

  1. Deprecated Methods

    • The bundle uses a legacy Stripe PHP SDK (v2.x). Update to stripe/stripe-php (v7+) for modern features:
      composer require stripe/stripe-php
      
    • Replace CRS\StripeBundle\Stripe with Laravel’s facade or direct SDK calls:
      \Stripe\Stripe::setApiKey(config('crs_stripe.secret_key'));
      $charge = \Stripe\Charge::create(['amount' => 100, 'currency' => 'usd']);
      
  2. Amount Handling

    • The bundle requires amounts in cents (e.g., $1.00 = 100). Forgetting this causes 402 Validation Error from Stripe.
    • Fix: Use a helper or middleware to auto-convert:
      $amountInCents = $amount * 100;
      
  3. Bundle Stagnation

  4. Webhook Security

    • The bundle lacks built-in webhook verification. Manually verify signatures (as shown above) to avoid replay attacks.

Debugging

  • Test Mode Errors: Use Stripe’s test cards (e.g., 4242424242424242) but never hardcode them in production.
  • Logging: Enable Stripe debug mode:
    \Stripe\Stripe::setApiKey(config('crs_stripe.secret_key'));
    \Stripe\Stripe::setAppInfo('MyApp', '1.0');
    \Stripe\Log::setLogLevel(\Stripe\Log::DEBUG); // Logs to storage/logs/stripe.log
    

Extension Points

  1. Custom Charge Logic Extend the Charge class to add metadata or business rules:

    class CustomCharge extends \CRS\StripeBundle\Charge
    {
        public function Pay($amount, array $options = [])
        {
            $options['metadata'] = ['user_id' => auth()->id()];
            return parent::Pay($amount, $options);
        }
    }
    
  2. Plan Management Dynamically fetch plans from a database:

    $plan = Plan::where('name', 'premium')->first();
    $subscription = $stripe->Subscription()->Create([
        'customer' => $customer->id,
        'items' => [['plan' => $plan->stripe_plan_id]]
    ]);
    
  3. Refunds & Disputes Use Stripe’s refund API:

    $refund = \Stripe\Refund::create([
        'charge' => $charge->id,
        'amount' => $charge->amount,
    ]);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope