用Try/Catch PHP方法捕获条纹错误。

时间:2021-10-05 07:44:21

During my testing of STRIPE in a website, I built the code like this:

在我测试一个网站的STRIPE时,我构建了如下代码:

   try {
        $charge = Stripe_Charge::create(array(
          "amount" => $clientPriceStripe, // amount in cents
          "currency" => "usd",
          "customer" => $customer->id,
          "description" => $description));
          $success = 1;
          $paymentProcessor="Credit card (www.stripe.com)";
    } 
    catch (Stripe_InvalidRequestError $a) {
        // Since it's a decline, Stripe_CardError will be caught
        $error3 = $a->getMessage();
    }

    catch (Stripe_Error $e) {
        // Since it's a decline, Stripe_CardError will be caught
        $error2 = $e->getMessage();
        $error = 1;
    }
if ($success!=1)
{
    $_SESSION['error3'] = $error3;
    $_SESSION['error2'] = $error2;
    header('Location: checkout.php');
    exit();
}

The problem is that sometimes there is an error with the card (not catched by the "catch" arguments I have there) and the "try" fails and the page immediately posts the error in the screen instead of going into the "if" and redirecting back to checkout.php.

问题是,有时card有一个错误(没有被我在那里的“catch”参数捕获),而“try”失败了,页面立即在屏幕上发布错误,而不是进入“if”,然后重定向回checkout.php。

How should I structure my error handling so I get the error and immediately redirect back to checkout.php and display the error there?

我应该如何构造错误处理,以便获得错误并立即重定向回检出。php并显示错误?

Thanks!

谢谢!


Error thrown:

错误扔:

Fatal error: Uncaught exception 'Stripe_CardError' with message 'Your card was declined.' in ............
/lib/Stripe/ApiRequestor.php on line 92

6 个解决方案

#1


48  

I think there is more than these exceptions (Stripe_InvalidRequestError and Stripe_Error) to catch.

我认为要捕获的异常不止这些(Stripe_InvalidRequestError和Stripe_Error)。

The code below is from Stripe's web site. Probably, these additional exceptions, which you didn't consider, occurs and your code fails sometimes.

下面的代码来自Stripe的网站。可能会出现这些您没有考虑的额外异常,有时您的代码会失败。

try {
  // Use Stripe's bindings...
} catch(Stripe_CardError $e) {
  // Since it's a decline, Stripe_CardError will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  // param is '' in this case
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");
} catch (Stripe_InvalidRequestError $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (Stripe_AuthenticationError $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (Stripe_ApiConnectionError $e) {
  // Network communication with Stripe failed
} catch (Stripe_Error $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
}

EDIT:

编辑:

try {
    $charge = Stripe_Charge::create(array(
    "amount" => $clientPriceStripe, // amount in cents
    "currency" => "usd",
    "customer" => $customer->id,
    "description" => $description));
    $success = 1;
    $paymentProcessor="Credit card (www.stripe.com)";
} catch(Stripe_CardError $e) {
  $error1 = $e->getMessage();
} catch (Stripe_InvalidRequestError $e) {
  // Invalid parameters were supplied to Stripe's API
  $error2 = $e->getMessage();
} catch (Stripe_AuthenticationError $e) {
  // Authentication with Stripe's API failed
  $error3 = $e->getMessage();
} catch (Stripe_ApiConnectionError $e) {
  // Network communication with Stripe failed
  $error4 = $e->getMessage();
} catch (Stripe_Error $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
  $error5 = $e->getMessage();
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
  $error6 = $e->getMessage();
}

if ($success!=1)
{
    $_SESSION['error1'] = $error1;
    $_SESSION['error2'] = $error2;
    $_SESSION['error3'] = $error3;
    $_SESSION['error4'] = $error4;
    $_SESSION['error5'] = $error5;
    $_SESSION['error6'] = $error6;
    header('Location: checkout.php');
    exit();
}

Now, you will catch all possible exceptions and you can display error message as you wish. And also $error6 is for unrelated exceptions.

现在,您将捕获所有可能的异常,并且您可以按照自己的意愿显示错误消息。$error6也适用于不相关的异常。

#2


70  

If you're using the Stripe PHP libraries and they have been namespaced (such as when they're installed via Composer) you can catch all Stripe exceptions with:

如果您使用的是带名称空间的Stripe PHP库(比如通过Composer安装的时候),那么可以通过以下方式捕获所有的Stripe异常:

<?php 
try {
  // Use a Stripe PHP library method that may throw an exception....
  \Stripe\Customer::create($args);
} catch (\Stripe\Error\Base $e) {
  // Code to do something with the $e exception object when an error occurs
  echo($e->getMessage());
} catch (Exception $e) {
  // Catch any other non-Stripe exceptions
}

#3


8  

I may be late to this question, but I ran into the same issue and found this.

我可能在这个问题上迟到了,但是我遇到了同样的问题并且发现了这个。

You just need to use "Stripe_Error" class.

您只需要使用“Stripe_Error”类。

use Stripe_Error;

After declaring that, I was able to catch errors successfully.

声明之后,我能够成功捕获错误。

#4


6  

This is an update to another answer, but the docs have changed very slightly so I had success using the following method:

这是对另一个答案的更新,但是文档的变化非常小,所以我成功地使用了以下方法:

try {
  // Use Stripe's library to make requests...
} catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  // param is '' in this case
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Error\Base $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
}

You can find the source of this in the Stripe docs right here:

你可以在条纹文档中找到它的来源:

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

https://stripe.com/docs/api?lang=php处理错误

#5


3  

This is how Stripe catches errors in 2017. Documentation. This snippet requires PHP 7.1+

这就是Stripe在2017年捕捉错误的方式。文档。这段代码需要PHP 7.1+

catch(\Stripe\Error\Card | \Stripe\Error\RateLimit | \Stripe\Error\InvalidRequest | \Stripe\Error\Authentication | \Stripe\Error\ApiConnection $e) 
{
    $body = $e->getJsonBody();
    $err  = $body["error"];
    $return_array = [
        "status" =>  $e->getHttpStatus(),
        "type" =>  $err["type"],
        "code" =>  $err["code"],
        "param" =>  $err["param"],
        "message" =>  $err["message"],
    ];
    $return_str = json_encode($return_array);          
    http_response_code($e->getHttpStatus());
    echo $return_str;
}

You can then catch the error in ajax with the following code:

然后,您可以使用以下代码捕获ajax中的错误:

$(document).ajaxError(function ajaxError(event, jqXHR, ajaxSettings, thrownError) {
    try {
        var url = ajaxSettings.url;
        var http_status_code = jqXHR.status;
        var response = jqXHR.responseText;
        var message = "";
        if (isJson(response)) {     // see here for function: https://*.com/a/32278428/4056146
            message = "  " + (JSON.parse(response)).message;
        }
        var error_str = "";

        // 1. handle HTTP status code
        switch (http_status_code) {
            case 0: {
                error_str = "No Connection.  Cannot connect to " + new URL(url).hostname + ".";
                break;
            }   // No Connection
            case 400: {
                error_str = "Bad Request." + message + "  Please see help.";
                break;
            }   // Bad Request
            case 401: {
                error_str = "Unauthorized." + message + "  Please see help.";
                break;
            }   // Unauthorized
            case 402: {
                error_str = "Request Failed." + message;
                break;
            }   // Request Failed
            case 404: {
                error_str = "Not Found." + message + "  Please see help.";
                break;
            }   // Not Found
            case 405: {
                error_str = "Method Not Allowed." + message + "  Please see help.";
                break;
            }   // Method Not Allowed
            case 409: {
                error_str = "Conflict." + message + "  Please see help.";
                break;
            }   // Conflict
            case 429: {
                error_str = "Too Many Requests." + message + "  Please try again later.";
                break;
            }   // Too Many Requests
            case 500: {
                error_str = "Internal Server Error." + message + "  Please see help.";
                break;
            }   // Internal Server Error
            case 502: {
                error_str = "Bad Gateway." + message + "  Please see help.";
                break;
            }   // Bad Gateway
            case 503: {
                error_str = "Service Unavailable." + message + "  Please see help.";
                break;
            }   // Service Unavailable
            case 504: {
                error_str = "Gateway Timeout." + message + "  Please see help.";
                break;
            }   // Gateway Timeout
            default: {
                console.error(loc + "http_status_code unhandled >> http_status_code = " + http_status_code);
                error_str = "Unknown Error." + message + "  Please see help.";
                break;
            }
        }

        // 2. show popup
        alert(error_str);
        console.error(arguments.callee.name + " >> http_status_code = " + http_status_code.toString() + "; thrownError = " + thrownError + "; URL = " + url + "; Response = " + response);

    }
    catch (e) {
        console.error(arguments.callee.name + " >> ERROR >> " + e.toString());
        alert("Internal Error.  Please see help.");
    }
});

#6


2  

I think all you really need to check is the base error class of Stripe and the exception if it's unrelated to Stripe. Here's how I do it.

我认为您真正需要检查的是Stripe的基本错误类和与Stripe无关的异常。我是这样做的。

/**
 * Config.
 */
require_once( dirname( __FILE__ ) . '/config.php' );

// Hit Stripe API.
try {
  // Register a Customer.
  $customer = \Stripe\Customer::create(array(
    'email'    => 'AA@TESTING.com',
    'source'   => $token,
    'metadata' => array( // Note: You can specify up to 20 keys, with key names up to 40 characters long and values up to 500 characters long.
        'NAME'          => 'AA',
        'EMAIL'         => 'a@a.c.o',
        'ORDER DETAILS' => $order_details,
    )
  ));

  // Charge a customer.
  $charge = \Stripe\Charge::create(array(
    'customer' => $customer->id,
    'amount'   => 5000, // In cents.
    'currency' => 'usd'
  ));



  // If there is an error from Stripe.
} catch ( Stripe\Error\Base $e ) {
  // Code to do something with the $e exception object when an error occurs.
  echo $e->getMessage();

  // DEBUG.
  $body = $e->getJsonBody();
  $err  = $body['error'];
  echo '<br> ——— <br>';
  echo '<br>THE ERROR DEFINED — <br>';
  echo '— Status is: ' . $e->getHttpStatus() . '<br>';
  echo '— Message is: ' . $err['message'] . '<br>';
  echo '— Type is: ' . $err['type'] . '<br>';
  echo '— Param is: ' . $err['param'] . '<br>';
  echo '— Code is: ' . $err['code'] . '<br>';
  echo '<br> ——— <br>';

// Catch any other non-Stripe exceptions.
} catch ( Exception $e ) {
    $body = $e->getJsonBody();
    $err  = $body['error'];
    echo '<br> ——— <br>';
    echo '<br>THE ERROR DEFINED — <br>';
    echo '— Status is: ' . $e->getHttpStatus() . '<br>';
    echo '— Message is: ' . $err['message'] . '<br>';
    echo '— Type is: ' . $err['type'] . '<br>';
    echo '— Param is: ' . $err['param'] . '<br>';
    echo '— Code is: ' . $err['code'] . '<br>';
    echo '<br> ——— <br>';
}

#1


48  

I think there is more than these exceptions (Stripe_InvalidRequestError and Stripe_Error) to catch.

我认为要捕获的异常不止这些(Stripe_InvalidRequestError和Stripe_Error)。

The code below is from Stripe's web site. Probably, these additional exceptions, which you didn't consider, occurs and your code fails sometimes.

下面的代码来自Stripe的网站。可能会出现这些您没有考虑的额外异常,有时您的代码会失败。

try {
  // Use Stripe's bindings...
} catch(Stripe_CardError $e) {
  // Since it's a decline, Stripe_CardError will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  // param is '' in this case
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");
} catch (Stripe_InvalidRequestError $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (Stripe_AuthenticationError $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (Stripe_ApiConnectionError $e) {
  // Network communication with Stripe failed
} catch (Stripe_Error $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
}

EDIT:

编辑:

try {
    $charge = Stripe_Charge::create(array(
    "amount" => $clientPriceStripe, // amount in cents
    "currency" => "usd",
    "customer" => $customer->id,
    "description" => $description));
    $success = 1;
    $paymentProcessor="Credit card (www.stripe.com)";
} catch(Stripe_CardError $e) {
  $error1 = $e->getMessage();
} catch (Stripe_InvalidRequestError $e) {
  // Invalid parameters were supplied to Stripe's API
  $error2 = $e->getMessage();
} catch (Stripe_AuthenticationError $e) {
  // Authentication with Stripe's API failed
  $error3 = $e->getMessage();
} catch (Stripe_ApiConnectionError $e) {
  // Network communication with Stripe failed
  $error4 = $e->getMessage();
} catch (Stripe_Error $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
  $error5 = $e->getMessage();
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
  $error6 = $e->getMessage();
}

if ($success!=1)
{
    $_SESSION['error1'] = $error1;
    $_SESSION['error2'] = $error2;
    $_SESSION['error3'] = $error3;
    $_SESSION['error4'] = $error4;
    $_SESSION['error5'] = $error5;
    $_SESSION['error6'] = $error6;
    header('Location: checkout.php');
    exit();
}

Now, you will catch all possible exceptions and you can display error message as you wish. And also $error6 is for unrelated exceptions.

现在,您将捕获所有可能的异常,并且您可以按照自己的意愿显示错误消息。$error6也适用于不相关的异常。

#2


70  

If you're using the Stripe PHP libraries and they have been namespaced (such as when they're installed via Composer) you can catch all Stripe exceptions with:

如果您使用的是带名称空间的Stripe PHP库(比如通过Composer安装的时候),那么可以通过以下方式捕获所有的Stripe异常:

<?php 
try {
  // Use a Stripe PHP library method that may throw an exception....
  \Stripe\Customer::create($args);
} catch (\Stripe\Error\Base $e) {
  // Code to do something with the $e exception object when an error occurs
  echo($e->getMessage());
} catch (Exception $e) {
  // Catch any other non-Stripe exceptions
}

#3


8  

I may be late to this question, but I ran into the same issue and found this.

我可能在这个问题上迟到了,但是我遇到了同样的问题并且发现了这个。

You just need to use "Stripe_Error" class.

您只需要使用“Stripe_Error”类。

use Stripe_Error;

After declaring that, I was able to catch errors successfully.

声明之后,我能够成功捕获错误。

#4


6  

This is an update to another answer, but the docs have changed very slightly so I had success using the following method:

这是对另一个答案的更新,但是文档的变化非常小,所以我成功地使用了以下方法:

try {
  // Use Stripe's library to make requests...
} catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  // param is '' in this case
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Error\Base $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
}

You can find the source of this in the Stripe docs right here:

你可以在条纹文档中找到它的来源:

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

https://stripe.com/docs/api?lang=php处理错误

#5


3  

This is how Stripe catches errors in 2017. Documentation. This snippet requires PHP 7.1+

这就是Stripe在2017年捕捉错误的方式。文档。这段代码需要PHP 7.1+

catch(\Stripe\Error\Card | \Stripe\Error\RateLimit | \Stripe\Error\InvalidRequest | \Stripe\Error\Authentication | \Stripe\Error\ApiConnection $e) 
{
    $body = $e->getJsonBody();
    $err  = $body["error"];
    $return_array = [
        "status" =>  $e->getHttpStatus(),
        "type" =>  $err["type"],
        "code" =>  $err["code"],
        "param" =>  $err["param"],
        "message" =>  $err["message"],
    ];
    $return_str = json_encode($return_array);          
    http_response_code($e->getHttpStatus());
    echo $return_str;
}

You can then catch the error in ajax with the following code:

然后,您可以使用以下代码捕获ajax中的错误:

$(document).ajaxError(function ajaxError(event, jqXHR, ajaxSettings, thrownError) {
    try {
        var url = ajaxSettings.url;
        var http_status_code = jqXHR.status;
        var response = jqXHR.responseText;
        var message = "";
        if (isJson(response)) {     // see here for function: https://*.com/a/32278428/4056146
            message = "  " + (JSON.parse(response)).message;
        }
        var error_str = "";

        // 1. handle HTTP status code
        switch (http_status_code) {
            case 0: {
                error_str = "No Connection.  Cannot connect to " + new URL(url).hostname + ".";
                break;
            }   // No Connection
            case 400: {
                error_str = "Bad Request." + message + "  Please see help.";
                break;
            }   // Bad Request
            case 401: {
                error_str = "Unauthorized." + message + "  Please see help.";
                break;
            }   // Unauthorized
            case 402: {
                error_str = "Request Failed." + message;
                break;
            }   // Request Failed
            case 404: {
                error_str = "Not Found." + message + "  Please see help.";
                break;
            }   // Not Found
            case 405: {
                error_str = "Method Not Allowed." + message + "  Please see help.";
                break;
            }   // Method Not Allowed
            case 409: {
                error_str = "Conflict." + message + "  Please see help.";
                break;
            }   // Conflict
            case 429: {
                error_str = "Too Many Requests." + message + "  Please try again later.";
                break;
            }   // Too Many Requests
            case 500: {
                error_str = "Internal Server Error." + message + "  Please see help.";
                break;
            }   // Internal Server Error
            case 502: {
                error_str = "Bad Gateway." + message + "  Please see help.";
                break;
            }   // Bad Gateway
            case 503: {
                error_str = "Service Unavailable." + message + "  Please see help.";
                break;
            }   // Service Unavailable
            case 504: {
                error_str = "Gateway Timeout." + message + "  Please see help.";
                break;
            }   // Gateway Timeout
            default: {
                console.error(loc + "http_status_code unhandled >> http_status_code = " + http_status_code);
                error_str = "Unknown Error." + message + "  Please see help.";
                break;
            }
        }

        // 2. show popup
        alert(error_str);
        console.error(arguments.callee.name + " >> http_status_code = " + http_status_code.toString() + "; thrownError = " + thrownError + "; URL = " + url + "; Response = " + response);

    }
    catch (e) {
        console.error(arguments.callee.name + " >> ERROR >> " + e.toString());
        alert("Internal Error.  Please see help.");
    }
});

#6


2  

I think all you really need to check is the base error class of Stripe and the exception if it's unrelated to Stripe. Here's how I do it.

我认为您真正需要检查的是Stripe的基本错误类和与Stripe无关的异常。我是这样做的。

/**
 * Config.
 */
require_once( dirname( __FILE__ ) . '/config.php' );

// Hit Stripe API.
try {
  // Register a Customer.
  $customer = \Stripe\Customer::create(array(
    'email'    => 'AA@TESTING.com',
    'source'   => $token,
    'metadata' => array( // Note: You can specify up to 20 keys, with key names up to 40 characters long and values up to 500 characters long.
        'NAME'          => 'AA',
        'EMAIL'         => 'a@a.c.o',
        'ORDER DETAILS' => $order_details,
    )
  ));

  // Charge a customer.
  $charge = \Stripe\Charge::create(array(
    'customer' => $customer->id,
    'amount'   => 5000, // In cents.
    'currency' => 'usd'
  ));



  // If there is an error from Stripe.
} catch ( Stripe\Error\Base $e ) {
  // Code to do something with the $e exception object when an error occurs.
  echo $e->getMessage();

  // DEBUG.
  $body = $e->getJsonBody();
  $err  = $body['error'];
  echo '<br> ——— <br>';
  echo '<br>THE ERROR DEFINED — <br>';
  echo '— Status is: ' . $e->getHttpStatus() . '<br>';
  echo '— Message is: ' . $err['message'] . '<br>';
  echo '— Type is: ' . $err['type'] . '<br>';
  echo '— Param is: ' . $err['param'] . '<br>';
  echo '— Code is: ' . $err['code'] . '<br>';
  echo '<br> ——— <br>';

// Catch any other non-Stripe exceptions.
} catch ( Exception $e ) {
    $body = $e->getJsonBody();
    $err  = $body['error'];
    echo '<br> ——— <br>';
    echo '<br>THE ERROR DEFINED — <br>';
    echo '— Status is: ' . $e->getHttpStatus() . '<br>';
    echo '— Message is: ' . $err['message'] . '<br>';
    echo '— Type is: ' . $err['type'] . '<br>';
    echo '— Param is: ' . $err['param'] . '<br>';
    echo '— Code is: ' . $err['code'] . '<br>';
    echo '<br> ——— <br>';
}