boris
boris

Reputation: 23

Getting Stripe charge parameters from Error object

Im building a php site, using Stripe to do the billing. If a charge is successful, I log the results in a table, so far so good. If the charge fails (is declined) I want to do the same thing, log the message etc, but also log the amount and the currency. Is it possible to get these values from the Stripe\Error\Card object?

I seem to be able to get at the values using

catch(\Stripe\Error\Card $e) {

    $test = $e->getTrace();
    print_r($test[3]['args']);
}

but this looks pretty dodgy! I suppose I could just grab them from the original charge request, just wondered if there was another way?

Upvotes: 0

Views: 228

Answers (1)

Talented Cricket
Talented Cricket

Reputation: 26

I use try and catch for this and store the amount and currency in variables before the error, the same variables I was intending to send to stripe in a charge. For example:

$customer = \Stripe\Customer::create(array(
  'email' => $customer_email,
  'source'  => $token
));

try {

  $charge = \Stripe\Charge::create(array(
    'customer' => $customer->id,
    'amount'   => $amount_in_cents,
    'currency' => 'usd'
  ));

} catch(\Stripe\Error\Card $e) { // Your error handling code }

I hope this is helpful, I know it's not exactly what you wanted but if you're doing the charge and catching the error like this then you'll already have the amount and currency available to you and not need to look for it in the error card.

Upvotes: 1

Related Questions