#karls-exception

1 messages · Page 1 of 1 (latest)

cedar canyon
#

@analog vessel yes you should. Really our API can return a wide range of errors and your code needs to catch and handle all potential errors as documented in https://stripe.com/docs/api/errors

analog vessel
#

What is funny, I accidentally tested first with LIVE api keys using the 0002 CC number and my code handled the exception just fine (user prompted with CC failed).

cedar canyon
#

maybe you handle error in some cases and not others?

analog vessel
#

yep, looking now. Not a PHP developer though! Supporting this app for a client.

#

Just a thought, but...

#

I see in code that we call chargeAmountFromCard and it gets a response. In code we test for "fail" and then echo the response to the user. So no CATCH of an exception in this case.

#

wouldn't a "declined" CC pass back an appropriate failure message, therefore I'm not catching any exception?

cedar canyon
#

no, declines raise an exception

analog vessel
#

ok! good to know.

analog vessel
#

so, just tried the test with live mode creds using test CC and (for whatever reason, still investigating) this object is passed back to the client, which I think is what i want, where a user friendly "card declined" dialog appears.

#

{
"error": {
"code": "card_declined",
"decline_code": "live_mode_test_card",
"doc_url": "https://stripe.com/docs/error-codes/card-declined",
"message": "Your card was declined. Your request was in live mode, but used a known test card.",
"param": "",
"type": "card_error"
}
}

Learn more about common error codes and how to resolve them.

serene axle
#

you cannot use the test cards in live mode

#

they only work in test mode

#

what are you trying to test?

analog vessel
#

declined CC.

#

I understand that, i'm just trying to determine how my code is working in one case and not the other.

serene axle
#

which test card are you using?

analog vessel
#

I understand the difference now. The object I shared with you is caught client-side, thus my code responds correctly, but I need to trap the (402) exception serverside and pass that back.

#

card ending in 0002

#

To be clear: the call to stripe /Tokens fails in live mode for test CC but in test mode my server side code in invoked and there I don't handle the exception properly.

serene axle
#

Right, you can't even create the token for the test card in live mode

analog vessel
#

yep yep

serene axle
#

this is expected - use your test mode PK to get the token, then try the payment

analog vessel
#

do you have a link to any PHP sample code for calling chargeAmountFromCard? I'm a hack PHP developer

serene axle
#

what is chargeAmountFromCard here? where do you see that?

analog vessel
#

This is the point in my back-end code where I call to charge the card (PHP mind you) and my code fails "unhandled exception"

serene axle
#

what does that code look like now?

analog vessel
#

I have a couple of TEMP statements in here to help trace the exception. My code reaches TEMP_0000, then call chargeAmountFromCard, then fails, but never reaches TEMP_0002 in my exception handler.

#

try
{
error_log("TEMP_0000");

        $stripeResponse = $stripePayment->chargeAmountFromCard($registrationInfo);
        
        error_log("TEMP_0001");
    }
    catch(exception $e)
    {
        error_log("TEMP_0002");
        
        error_log($e->getMessage());
    }
#

original code (no trace staments)

#

$stripePayment = new StripePayment();
$stripeResponse = $stripePayment->chargeAmountFromCard($registrationInfo);

    if ($stripeResponse['amount_refunded'] == 0 && empty($stripeResponse['failure_code']) && $stripeResponse['paid'] == 1 && $stripeResponse['captured'] == 1 && $stripeResponse['status'] == 'succeeded') {
        $registrationInfo['tx_ID'] = $stripeResponse["balance_transaction"];
        completeRegistrationWithPaymentDetails($registrationInfo);
    } else {
        echo "Payment Failed";
    }
analog vessel
#

are you still able to help?

#

no matter what I do, my code is not catching any exception.

serene axle
#

yes sorry, many threads

analog vessel
#

i think i got it. in PHP Exception is required, so capital E

#

I was using exception in my handler

serene axle
#

ah nice! does it get the expected error now?

analog vessel
#

actually, this is catching my exception

#

catch(\Stripe\Exception\CardException $e) {

#

yep, I can handle it from here. thank you.

serene axle
#

perfect!