【问题标题】:Stripe charge Invalid RequestStripe Charge 无效请求
【发布时间】:2017-05-27 01:42:27
【问题描述】:

我成功地在我正在使用 Stripe 的说明构建的 Wordpress 插件中创建和实施收费:https://stripe.com/docs/charges

但是,我现在尝试使用客户功能将其从单次收费转换为节省的费用。添加部分以检查或创建客户后,我现在在每次收费尝试时都收到无效请求响应,但我无法弄清楚我哪里出错了。谁能发现错误?

// Get the payment token submitted by the form:
$token = $_POST['stripeToken']; // Token is created using Stripe.js, not hard-coded
$cartID = (int)$_POST['cartID'];
$cartTotal = (int)$_POST['cart-total'];

//Get the current user's info
$user = get_userdata(get_current_user_id());
$userid = $user->ID;

//See if this user already has a stripe customer ID set in their meta
$custID = get_user_meta($userid, 'stripeCustID', true);
if (!isset($custID)) {
    // Create a new Customer
    $customer = \Stripe\Customer::create(array(
      "email" => $user->user_email,
      "source" => $token,
        )
    );
    $custID = $customer->id;

    //Add the customer ID to the user meta
    add_user_meta($userid, 'stripeCustID', $custID, true);
}

// Try to charge the card
try {
    $charge = \Stripe\Charge::create(array(
      "amount" => $cartTotal,
      "currency" => "usd",
      "description" => "TPS Space Rental",
      //"source" => $token, //we're charging the customer instead of the token
      'customer' => $custID,
    ));
    if ($charge) {
        //Add the charge object as post meta
        add_post_meta($cartID, 'checkoutCharge', $charge, true); //only one checkCharge per cart please
        //Update the cart to published
        wp_update_post(array('ID'=>$cartID, 'post_status'=>'publish'));
        //Redirect to the confirmation page
        wp_redirect(get_bloginfo('url').'/rental-confirmation?confirmCart='.$cartID);
        exit;
    }

} catch (\Stripe\Error\ApiConnection $e) {
     // Network problem, perhaps try again.
    wp_redirect(get_bloginfo('url').'/space-checkout?error=networkProblems');
    exit;
} catch (\Stripe\Error\InvalidRequest $e) {
     // You screwed up in your programming.
     wp_redirect(get_bloginfo('url').'/space-checkout?error=invalidRequest');
    exit;
} catch (\Stripe\Error\Api $e) {
     // Stripe's servers are down!
     wp_redirect(get_bloginfo('url').'/space-checkout?error=stripeServers');
    exit;
} catch (\Stripe\Error\Card $e) {
    // Card was declined.
     $e_json = $e->getJsonBody();
     $error = $e_json['error'];
     // Use $error['message'] to display the specific error
     $_SESSION['declinedError'] = $error['message'];
     wp_redirect(get_bloginfo('url').'/space-checkout?error=declined');
     exit;
}

【问题讨论】:

    标签: php wordpress try-catch stripe-payments


    【解决方案1】:

    想通了。愚蠢的 PHP 错误。

    而不是if(!isset($custID)) {....

    我需要专门检查一个空字符串,所以我这样做了:

    if (empty($custID) || $custID == '') {
    

    一切正常。

    【讨论】:

      猜你喜欢
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 2018-11-04
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多