Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

php - Stripe Insufficient Funds: Redirect to new page

I am having trouble trying to catch the insufficient funds error after a payment is submitted and redirect the user. Right now, if a user has insufficient funds, the site gives a 500 error.

In PHP, the error log is:

PHP Fatal error: Uncaught StripeErrorInvalidRequest: Must provide source or customer. in /home/betheexe/public_html/_www/legalrideshare.com/stripe-php/lib/ApiRequestor.php:210 from API request 'req_o4ePJL1WVn611Y'

Here's my code to accept payments:

CHECKOUT PAGE:

<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"                                                 
data-key="pk_live_XXXXXXXXXXXXXXXXXXX"
data-amount="2499"
data-name="LR"
data-description="Deactivation"
data-email="<?php echo $_SESSION["email"]; ?>"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto">
</script>

AFTER SUBMISSION:

StripeStripe::setApiKey("sk_live_XXXXXXXXXXXXXX");
// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];

$charge = StripeCharge::create([
    'amount' => '2499',
    'currency' => 'usd',
    'description' => 'Deactivation Letter',
    'source' => $token,
]);

I just want to catch any errors that occur so the page doesn't fail. I also must be missing a source?

question from:https://stackoverflow.com/questions/65598969/stripe-insufficient-funds-redirect-to-new-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to catch any errors like the one you're seeing from Stripe when a charge fails you will need to wrap your charge request in a try/catch block. For example:

try {

  $charge = StripeCharge::create([
    'amount' => '2499',
    'currency' => 'usd',
    'description' => 'Deactivation Letter',
    'source' => $token,
  ]);

} catch(StripeExceptionCardException $e) {
  // Since it's a decline, StripeExceptionCardException will be caught
  echo 'Status is:' . $e->getHttpStatus() . '
';
  echo 'Type is:' . $e->getError()->type . '
';
  echo 'Code is:' . $e->getError()->code . '
';
  // param is '' in this case
  echo 'Param is:' . $e->getError()->param . '
';
  echo 'Message is:' . $e->getError()->message . '
';
} catch (StripeExceptionRateLimitException $e) {
  // Too many requests made to the API too quickly
} catch (StripeExceptionInvalidRequestException $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (StripeExceptionAuthenticationException $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (StripeExceptionApiConnectionException $e) {
  // Network communication with Stripe failed
} catch (StripeExceptionApiErrorException $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}

Within each catch block you can add logic to return a helpful error message to the user with relevant next steps. The Stripe docs provide a full example of these errors and how to handle them in this section:

https://stripe.com/docs/api/errors/handling?lang=php

You can also use the following test card/token to test for the case of insufficient funds:

  • raw card number 4000000000009995
  • test token string tok_chargeDeclinedInsufficientFunds

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...