我在 Phalcon 3 中使用 PHP 7。我想在一個函數中實現 Stripe 響應。我可以處理類似的錯誤是。這是代碼: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}每次我需要調用 Stripe 方法時,我都需要實現所有這些 try catch。如何創建一個具有所有 Stripe 異常的函數?我的想法是在參數中發送條帶函數并在 try 中使用該函數,但它不起作用,因為該函數是在函數內部之前執行的。 function stripeResponse($function) { try { // Use Stripe's library to make requests... \Stripe\Stripe::setApiKey("my_key"); $function(); } catch(\Stripe\Error\Card $e) { // Since it's a decline, \Stripe\Error\Card will be caught } 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 } 你有想法做我想做的事嗎?
1 回答

慕容3067478
TA貢獻1773條經驗 獲得超3個贊
你打電話的$this->stripeResponse方式不對。您將響應傳遞給它\Stripe\Charge::create而不是可調用對象。
您可以將其更改為:
return $this->stripeResponse(function() {
\Stripe\Charge::create([
"amount" => 100,
"currency" => "eur",
"source" => "token",
"description" => "Description"
]);
});
- 1 回答
- 0 關注
- 207 瀏覽
添加回答
舉報
0/150
提交
取消