doushui3061 2018-06-12 22:24
浏览 91
已采纳

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

Currently using Stripe to process payments however I would like to perform an SQL statement which will ban a user lets say when the Stripe risk evaluation is highest.

My current charge code using stripe's PHP library contains a basic error exception message:

 <?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));
}
?>

With reference to https://stripe.com/docs/api#charge_object I can see that under the PHP tab it has the outcome object which could be used in my cas ehowever not sure how to implement it.

  • 写回答

1条回答 默认 最新

  • dssk35460 2018-06-12 22:46
    关注

    Check the response from your charge creation. Look at the risk_level attribute and write a condition that does something when the condition is met.

    $response = Stripe_Charge::create(...
    

    To get the risk_level attribute:

    print $response->outcome->risk_level
    

    P.S. you should be checking the charge response to verify it succeeded and not just assuming it succeeded if an Exception was not thrown. It could be Pending, which is not an Exception but it's also not a success, for example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题