You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Stripe及Laravel Cashier能否仅存储客户银行卡信息暂不扣费?

Stripe & Laravel Cashier: Collecting Card Details Without Immediate Charges

Hey there! Let's break down your two questions one by one, with practical Laravel Cashier-focused solutions:

1. Can I add a customer's card details via Stripe without charging them immediately?

Absolutely! This is a core use case Stripe supports natively. Instead of creating a charge right away, you'll create a Stripe Customer record (linked to your Laravel user via Cashier), then attach the customer's payment method (their card) to that Customer. This securely stores the card in Stripe's vault, so you can charge it whenever you need to later.

Here's the quick flow with Cashier:

  • Ensure your user model uses Cashier's Billable trait.
  • Use Stripe's secure tools (Elements or Checkout) to collect card details (never handle raw card data directly!).
  • Once you have a payment_method_id from Stripe, attach it to your user:
    $user = Auth::user();
    $user->addPaymentMethod($paymentMethodId);
    
  • The payment method is now stored on the Stripe Customer linked to your Laravel user, ready for future charges.

2. Can I use Stripe Checkout to store a payment method and charge later with Laravel Cashier?

Yes! The trick is to use Stripe Checkout in Setup Mode—this mode is specifically built for collecting payment methods without immediate charges.

Step 1: Create a Setup Session

In your Laravel controller, generate a Checkout Session with mode set to setup:

use Stripe\Stripe;
use Stripe\Checkout\Session;

Stripe::setApiKey(config('cashier.secret'));

$session = Session::create([
    'payment_method_types' => ['card'],
    'mode' => 'setup',
    'success_url' => route('setup.success', ['session_id' => '{CHECKOUT_SESSION_ID}']),
    'cancel_url' => route('setup.cancel'),
    'customer' => $user->stripe_id, // Link to your existing Cashier customer
]);

return view('checkout', ['sessionId' => $session->id]);

Step 2: Handle the Success Webhook (Critical!)

Don't rely only on the frontend redirect—use a Stripe webhook to confirm the setup completed successfully. Set up the checkout.session.completed webhook in your Stripe Dashboard, then handle it in Laravel:

In your routes/webhooks.php:

Cashier::webhook()->handle(function ($payload) {
    if ($payload['type'] === 'checkout.session.completed') {
        $session = $payload['data']['object'];
        $user = User::where('stripe_id', $session['customer'])->first();
        
        if ($session['setup_intent']['status'] === 'succeeded') {
            $paymentMethodId = $session['setup_intent']['payment_method'];
            $user->addPaymentMethod($paymentMethodId);
            
            // Optional: Set this as the default payment method for future charges
            $user->updateDefaultPaymentMethod($paymentMethodId);
        }
    }

    return response('Webhook handled', 200);
});

Step 3: Charge the Customer Later

Once the payment method is stored, you can charge it anytime with Cashier:

// Charge $10.00 (amount is in cents)
$user->charge(1000, $user->defaultPaymentMethod()->id);

// Or charge a specific stored payment method
$user->charge(1000, 'pm_123456789');

Quick note: Stripe now recommends Payment Methods over the older "card token" system—they're more flexible and support modern payment types beyond cards. The code above uses the modern API, which Cashier fully supports.

内容的提问来源于stack exchange,提问作者naneri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:10:49