Stripe卡支付场景下$charge->status可选值技术咨询
status 可选值及"pending"状态适配方案 Hey there! Let's break down all the possible values for Stripe's $charge->status and clear up that question about the "pending" state you're curious about—since you're handling card payment token generation and using exceptions to halt code on failures, this matters a lot.
All Possible $charge->status Values
Stripe charges can have these four core statuses:
succeeded: The payment went through successfully, and funds are confirmed.pending: This is the non-failure pending state you’re asking about! It pops up when a payment needs extra verification (like 3D Secure), is waiting on bank processing delays, or uses a payment method that requires manual confirmation in certain regions.failed: The payment didn’t go through—this is the state that should trigger your exception logic.canceled: The payment was either canceled by the user, timed out before completion, or voided by your system.
Confirmation & Adaptation for the "pending" State
First off: Yes, the pending status absolutely exists in Stripe charges, and it’s a valid intermediate state (not a failure). Since your current code throws exceptions for all payment failures, you’ll need to adjust to handle this scenario without interrupting the flow unnecessarily.
Here’s how to adapt your code:
- Separate pending logic from failure exceptions: Don’t treat pending as a failure—instead, track the charge and wait for final confirmation.
- Use webhooks for final status updates: Set up webhooks to listen for
charge.succeededandcharge.failedevents. When these events fire, you can update your internal records (like order status) based on the final outcome. - Notify your users: Let customers know their payment is being processed and they’ll get an update once it’s finalized.
Quick Code Example
Here’s a snippet showing how to handle the statuses in your PHP code:
try { $charge = \Stripe\Charge::create([ 'amount' => 1000, 'currency' => 'usd', 'source' => $paymentToken, // Token generated from card payment 'description' => 'Your purchase description', ]); match($charge->status) { 'succeeded' => handleSuccessfulPayment($charge), // Your custom success logic 'pending' => handlePendingPayment($charge), // Track charge ID, notify user 'failed' => throw new Exception("Payment failed: " . $charge->failure_message), 'canceled' => throw new Exception("Payment was canceled before completion"), }; } catch (\Stripe\Exception\CardException $e) { // Handle card-specific failures (e.g., insufficient funds, expired card) throw new Exception("Card payment error: " . $e->getMessage()); } catch (\Stripe\Exception\ApiErrorException $e) { // Handle general Stripe API errors throw new Exception("Stripe service error: " . $e->getMessage()); }
For the handlePendingPayment function, you’d want to save the Stripe charge ID to your order record, then rely on webhooks to update the order once the charge moves to succeeded or failed.
内容的提问来源于stack exchange,提问作者London Smith

