Catch Stripe Error没有捕获

时间:2016-11-12 16:59:49

标签: php stripe-payments

我正在进行一项简单的测试,使用Stripes默认卡为Stripe Customer ID收取费用以触发卡片下降,但我一直认为它不会发现错误:

 try {
        \Stripe\Charge::create(array(
      "amount" => 1000, // Amount in cents
      "currency" => "usd",
      "customer" => $customer->id)
        );
            echo "Charge customer card entered";
        } catch(\Stripe\Error\Card $e) {
        echo "customer card declined exists";
        } 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();
        }

我一直得到一个未被捕获的异常

enter image description here

1 个答案:

答案 0 :(得分:1)

我的代码错了。我将客户的创建放在TRY处理程序中并且工作正常:

 try {
      $customer = \Stripe\Customer::create(array(
      "source" => $token,
      "description" => "Example customer")
        );

      \Stripe\Charge::create(array(
      "amount" => 1000, // Amount in cents
      "currency" => "usd",
      "customer" => $customer->id)
        );

      echo "Charge customer card entered";

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

      echo "customer card declined";
      exit;

    }