2015-04-23 37 views
1

我想处理这个错误,但是我无法让它在我的catch中工作。我试过使用多个错误短语,例如Stripe\Error\InvalidRequestinvalid_request_error,但它们都不起作用。条纹如何处理InvalidRequest错误

注:我只包括必要的代码,我的支付系统工作正常。

这里是我的代码:

try { 
     $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id, 
      'amount' => $amount, 
      'currency' => strtolower($active_user->currency->currency_id) 
     )); 
    } 
catch (Stripe\Error\InvalidRequest $e) { 
     $msg = "Sorry, you cannot make the same payment twice."; 
    } 
+0

试试这个http://stackoverflow.com/a/8439615/2156785 – ANjaNA

回答

2

从上错误的条纹API文档部分:

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 
} 
+0

我用同样的语法它仍然不起作用 – mightyspaj3

+0

你确定Stripe正在抛出一个无效的请求错误吗? –

+1

100%肯定,当你刷新页面时会发生 – mightyspaj3

2

可能是一个命名空间的问题。尝试使用一个斜线相对于引用您的异常类,以在全球范围内:

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

做不到这一点,添加一个catch为基PHP Exception类,看你能找到什么了什么存在抛出。从条纹

0

例子: 是你的回应设置返回JSON,如果是这样,在你} catch (... $e) {

$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");