如何处理条带错误PHP

时间:2014-09-04 16:08:51

标签: javascript php

抱怨PHP和Stripe的耳朵有点湿,但我想我们都必须从某个地方开始。

我已经设置了一个支付页面,条带一切正常,当卡的详细信息正确且已获得授权时。我遇到的这个问题是卡被拒绝或有细节问题。我得到的只是一个空白的白色屏幕,我不知道如何处理错误。

所以我想有人会这么善意帮助。

谢谢,下面是代码;

Main Index.php页面

<!-- STRIPE -->


<div class="payment">

<div class="title">
  <div class="copy">
    <h2>Deposit Payment</h2>
    <p>Please fill out the following form.</p>
  </div>
</div>

  <?php require_once('inc/config.php'); ?>
  <form action="inc/charge.php" method="post">
  <input type="text" name="business" placeholder="Advertiser Name">
  <input type="text" name="email" placeholder="Advertiser Email">
  <input type="text" name="hotel" placeholder="Hotel Name">
  <div class="two-blocks">
  <select class="small" id="currency">
  <option value="gbp">Great British Pounds</option>
  </select>
  <input type="text" class="small" id="price" name="price" placeholder="£500.00">
  </div>
  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="<?php echo $stripe['publishable_key']; ?>"
        data-currency="GBP"
        data-label="Submit"
        data-name="Guest Services Worldwide"
        data-image="http://www.itsjoeturner.com/pay/wallet.png"
        data-description="Advertising Deposit Payment"
        data-allow-remember-me="false">
  </script>
  </form>

</div>

<!-- // STRIPE -->

charge.php

<?php
  require_once('config.php');

  $token = $_POST['stripeToken'];
  $business = $_POST['business'];
  $email = $_POST['email'];
  $hotel = $_POST['hotel'];
  $currency = $_POST['currency'];
  $price =  $_POST['price'];

 $price .= "00";

  $customer = Stripe_Customer::create(array(
      'email' => $email,
      'card'  => $token,
      'description' => "Business Name: ".$business.""
  ));

  $charge = Stripe_Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $price,
      'currency' => 'gbp',
  ));

  header( 'Location: http://gswportal.co.uk/payments/thanks.html' ) ;

?>

2 个答案:

答案 0 :(得分:1)

当卡被拒绝时,Stripe会抛出Stripe_CardError个例外。

try {
    $charge = Stripe_Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $price,
      'currency' => 'gbp',
    ));
} catch (Stripe_CardError $e) {
    //card could not be charged
    $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");
}

有关详细信息,请参阅https://stripe.com/docs/api?lang=php#errors以及可能引发的异常的完整列表。

答案 1 :(得分:0)

以下是Google的解决方案。

 try {
    $charge = Stripe_Charge::create(array(
    "amount" => $clientPriceStripe, // amount in cents
    "currency" => "usd",
    "customer" => $customer->id,
    "description" => $description));
    $success = 1;
    $paymentProcessor="Credit card (www.stripe.com)";
} 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 ($success!=1)
{
    $_SESSION['error1'] = $error1;
    $_SESSION['error2'] = $error2;
    $_SESSION['error3'] = $error3;
    $_SESSION['error4'] = $error4;
    $_SESSION['error5'] = $error5;
    $_SESSION['error6'] = $error6;
    header('Location: checkout.php');
    exit();
}
相关问题