2015-04-23 39 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");