cartalyst / stripe-laravel

Cartalyst Stripe package integration for Laravel.
BSD 3-Clause "New" or "Revised" License
336 stars 58 forks source link

Get Stripe Charge ID from a charge with card declined #35

Closed ibarral closed 6 years ago

ibarral commented 6 years ago

Hello,

Is there any way to get the Charge ID when the card is declined?

Thanks!

brunogaspar commented 6 years ago

Hello,

Yes, that's possible.

When you catch the exception, you can retrieve the raw output that Stripe has sent and retrieve the charge id from the error array.

Example:

try {
    $charge = Stripe::charges()->create([
        'currency' => 'USD',
        'amount'   => 123,45,
        'card'     => 'some-token',
    ]);
} catch (\Exception $e) {
   $chargeId = $e->getRawOutput()['error']['charge'];
}

Hope it helps.