javascript - 使用 php catch 显示 stripe 中的错误

标签 javascript php stripe-payments

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

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

这是我的代码

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>

最佳答案

你可以捕获所有这些,响应也是 JSON:

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

关于javascript - 使用 php catch 显示 stripe 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806434/

相关文章:

javascript - 将 Stripe JS 集成到 Laravel 6.0 : [Vue warn]: Error compiling template:

android - 从 Stripe Android API 获取卡资金参数

javascript - 如何垂直和水平对齐一个div到页面中心

php - 选择什么来存储一个整数?数据库?或文本文件?

php - PHP停止1行代码的警告

php - 打印下拉菜单的可见名称而不是其值名称

javascript - 将 Ideal 与 stripe 集成到 Node.js 中

javascript - requireJS 中的全局变量

javascript - 如何使用平衡括号算法替换 JavaScript 字符串中的单词?

javascript - Quicksand.js 干扰 Bootstrap 模态