使用php catch显示条带中的错误

时间:2015-04-22 18:51:02

标签: javascript php stripe-payments

我正在尝试在使用条带卡号时在我的页面上显示错误,该条带卡号应该触发此数字4000000000000002的条带错误。该错误似乎在控制台中显示为402,但我似乎无法掌握如何使用php输出它。

我更习惯于javascript,我不确定如何尝试和捕获工作,我的假设是尝试运行代码,直到抛出错误然后捕获运行。我只是想向页面中的任何错误吐出条带代码的充电点。

这是我的代码

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>

2 个答案:

答案 0 :(得分:1)

你可以抓住所有这些,响应也是JSON:

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

答案 1 :(得分:0)

//为整个页面尝试捕捉

//在页面顶部放

try 

{

//...page code

}

//抓住页面底部

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

    echo $errorMessage;

//然后重定向到您的付款页面                  标题(&#34;刷新:10; url = mypaymentpage.php&#34;);

}