2015-04-22 50 views
0

當我使用應該觸發條帶錯誤的條形卡號碼時,我正在嘗試在我的頁面上顯示錯誤,此數字爲4000000000000002。該錯誤似乎出現在控制檯作爲402,但我似乎無法掌握如何使用PHP輸出。使用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 <[email protected]>' . "\r\n"; 
    // $headers .= 'Bcc: [email protected]' . "\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> 
+0

什麼是當前輸出?以及當前控制檯上顯示的錯誤如何? –

+0

目前的輸出是什麼都沒有,這是我的控制檯與erros擴展的圖像http://i.imgur.com/SSLqW2E.png?1 –

+0

它確實成功地發送購買條紋,如果我把'4242424242424242'信用卡 –

回答

1

可以正好趕上所有的人,響應是JSON以及:

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

//做一個嘗試捕捉整個頁面

//等等頂部頁面把

try 

{ 

//...page code 

} 

//抓在頁面

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

    echo $errorMessage; 

//然後重定向到您的付款頁面 header(「refresh:10; url = mypaymentpage.php」);

}