【问题标题】:Using php catch to display errors in stripe使用 php catch 在条带中显示错误
【发布时间】:2015-06-30 15:13:41
【问题描述】:

我试图在我的页面上显示错误,当使用一个应该会触发该号码4000000000000002 的条带错误的条带卡号时。该错误似乎在控制台中显示为 402,但是我似乎无法掌握如何使用 php 输出它。

我更习惯于 javascript,我不确定 try 和 catch 是如何工作的,我的假设是 try 运行代码直到抛出错误然后 catch 运行。我只是想吐出任何在条码收费点的页面错误。

这是我的代码

try {
  $customer = Stripe_Customer::create(array(
  'email' => $email,
  'card'  => $token,
  "description" => $quantity . " copies of The Economic Definition of Ore -Cut-off Grades in Theory and Practice"));

  $charge = Stripe_Charge::create(array(
  "amount" => $total, // amount in cents, again
  "currency" => "usd",
  'customer' => $customer->id,
  "metadata" => array("First Name:" => $firstName, "Last Name:" => $lastName)));
  $success = '<div class="alert alert-success">
  <strong>Success!</strong> Your payment was successful. For $' . $customertotal . ', and you have ordered ' . $quantity . ' copies </div>';

  $to  = $email; // note the comma
  // subject
  $subject = 'Economic Definition of Ore Purchase';
  // message
  $message = '
    <html>
    <head>
      <title>Sumary of Purchase</title>
     </head>
     <body>
       <table>
         <tr>
           <td> Hi ' . $firstName . '</td>
          </tr>
          <tr>
            <td>
             <p>Here is a summary of your recent purchase.</p>
            </td>
          </tr>
          <tr>
           <td>
            <p>Your total purchase is $'.$customertotal . ' </p>
            <p>The number of books you have ordered is <b>'  . $quantity . '</b> </p></td>
         </tr>
       </table>
      </body>
    </html>
   ';

   // To send HTML mail, the Content-type header must be set
   $headers  = 'MIME-Version: 1.0' . "\r\n";
   $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

   // Additional headers
   $headers .= 'To: '. $firstName .' <' . $email . ' >  ' . "\r\n";
   $headers .= 'From: Comet <info@cometstrategy.com>' . "\r\n";
   // $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

   // Mail it
   mail($to, $subject, $message, $headers);
}

catch(\Stripe\Error\Card $e){
   $e_json = $e->getJsonBody();
   $error = $e_json['error'];
}
}?>
<span class="payment-success">
 <?= $success ?>
 <?= $error ?>
</span>

我已经根据this 问题更新了我的代码。当我使用错误代码信用卡号码时,错误不显示,我不确定我做错了什么。

try {
  $customer = Stripe_Customer::create(array(
  'email' => $email,
  'card'  => $token,
  "description" => $quantity . " copies of The Economic Definition of Ore -Cut-off Grades in Theory and Practice"
            ));
  $charge = Stripe_Charge::create(array(
  "amount" => $total, // amount in cents, again
  "currency" => "usd",
  'customer' => $customer->id,
  "metadata" => array("First Name:" => $firstName, "Last Name:" => $lastName))
   );

   $successTest = 1;
   $success = '<div class="alert alert-success">
   <strong>Success!</strong> Your payment was successful. For $' .        $customertotal . ', and you have ordered ' . $quantity . ' copies </div>';
   }

   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();
   }

   if ($successTest!=1)
    {
   ?>
    <span class="payment-success">
      <?= $error1 ?>
      <?= $error2 ?>
      <?= $error3 ?>
      <?= $error4 ?>
      <?= $error5 ?>
      <?= $error6 ?>
    </span>
  <?php
   }
  }
   ?>

    <span class="payment-success">
      <?= $success ?>
    </span>

【问题讨论】:

  • 当前输出是多少?以及当前控制台上的错误是如何显示的?
  • 当前输出什么都没有,这是我的控制台的图像,错误扩展为i.imgur.com/SSLqW2E.png?1
  • 如果我放入 4242424242424242 信用卡,它确实可以成功地将购买发送到条带

标签: javascript php stripe-payments


【解决方案1】:

你可以全部捕获,响应也是 JSON:

catch (Exception $e) {
    $body = $e->getJsonBody();
    $err  = $body['error'];
    $errorMessage =  $err['message'];
}

【讨论】:

    【解决方案2】:

    //对整个页面做try catch

    //so放在页面顶部

    try 
    
    {
    
    //...page code
    
    }
    

    //在页面底部捕捉

    catch (Exception $e) {
            $body = $e->getJsonBody();
            $err  = $body['error'];
            $errorMessage =  $err['message'];
    
        echo $errorMessage;
    

    //然后重定向到您的付款页面 标头(“刷新:10;url=mypaymentpage.php”);

    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 2014-06-03
      • 1970-01-01
      • 2014-12-31
      • 2011-06-28
      • 2021-02-13
      相关资源
      最近更新 更多