如何根据Stripe API错误响应执行任务

时间:2018-06-12 22:24:50

标签: php stripe-payments

目前正在使用Stripe来处理付款,但我想执行一条SQL语句,禁止用户在条带风险评估为highest时禁止。

我使用stripe的PHP库的当前收费代码包含一个基本的错误异常消息:

 <?php
require 'lib/Stripe.php';

if ($_POST) {
  Stripe::setApiKey($stripeSecretKey);
  $error = '';
  $success = '';

  try {
    if (empty($_POST['street']) || empty($_POST['city']) || empty($_POST['zip']))
      throw new Exception("Fill out all required fields.");
    if (!isset($_POST['stripeToken']))
      throw new Exception("The Stripe Token was not generated correctly");
    Stripe_Charge::create(array("amount" => $price * 100,
                                "currency" => "gbp",
                                "card" => $_POST['stripeToken'],
                                "description" => "User: " . $userUsername . " - " . $userEmail,
                                "receipt_email" => $userEmail));

    $success = '<div class="alert alert-success">
                <strong>Success!</strong> Your payment was successful, Redirecting...
                </div>';
                header('Refresh: 3; URL=https://urlhere');


  }
  catch (Exception $e) {
    $error = '<div class="alert alert-danger">
              <strong>Error!</strong> '.$e->getMessage().'
              </div>';
  }
}

if(!(empty($success)))

                $txid = generateTxid();
{
                $SQL = $odb -> prepare("INSERT INTO `payments` VALUES(NULL, :price, :planid, :userid, :payer, :transactionid, UNIX_TIMESTAMP())");
                $SQL -> execute(array(':price' => $price, ':planid' => $planID, ':userid' => $userID, ':payer' => $userEmail, ':transactionid' => $txid));


                $unit = $plan['unit'];
                $length = $plan['length'];
                $newExpire = strtotime("+{$length} {$unit}");
                $updateSQL = $odb -> prepare("UPDATE `users` SET `expire` = :expire, `membership` = :plan WHERE `ID` = :id");
                $updateSQL -> execute(array(':expire' => $newExpire, ':plan' => (int)$planID, ':id' => (int)$userID));
}
?>

参考https://stripe.com/docs/api#charge_object我可以看到在PHP选项卡下它有outcome对象,可以在我的cas中使用,而不知道如何实现它。

1 个答案:

答案 0 :(得分:0)

检查您的收费创建的回复。查看risk_level属性并编写满足条件时执行某些操作的条件。

$response = Stripe_Charge::create(...

获取risk_level属性:

print $response->outcome->risk_level

P.S。你应该检查收费响应以验证它是否成功,而不是仅仅假设如果没有抛出异常则成功。它可能是Pending,这不是例外,但它也不是成功,例如。