亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 避免重復相同的 try catch

PHP 避免重復相同的 try catch

PHP
慕娘9325324 2021-09-05 20:42:54
我在 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"

    ]);

});


查看完整回答
反對 回復 2021-09-05
  • 1 回答
  • 0 關注
  • 207 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號