【问题标题】:Stripe error: you cannot use a stripe token more than once条带错误:您不能多次使用条带令牌
【发布时间】:2017-10-31 14:30:27
【问题描述】:

我在使用 Stripe 创建费用时不断收到 400 错误。奇怪的是,有时它可以正常工作,但大多数时候它根本不起作用。付款也每次都通过。不管错误。这个 php 脚本对于处理付款是否正确?

注意:我已经检查过,每次创建收费时,令牌都与上一个收费不同。

<?php


require_once('stripe.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("key");


$token = $_POST['stripeToken'];
$email = $_POST['email'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
// Create a Customer:

$customer = \Stripe\Customer::create(array(
  "email" => $email,
  "source" => $token,
));

// Charge the Customer instead of the card:
$charge = \Stripe\Charge::create(array(
  "amount" => 1000,
  "currency" => "usd",
  "customer" => $customer->id
));

} catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
  $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");
} catch (\Stripe\Error\RateLimit $e) {
  $response["error"] = TRUE;
		$response["error_msg"] = "Error processing payment - Rate Limit.";
		echo json_encode($response);
} catch (\Stripe\Error\InvalidRequest $e) {
	 $response["error"] = TRUE;
		$response["error_msg"] = "Error processing payment - Invalid request.";
		echo json_encode($response);
} catch (\Stripe\Error\Authentication $e) {
  $response["error"] = TRUE;
		$response["error_msg"] = "Error processing payment - Authentication.";
		echo json_encode($response);
} catch (\Stripe\Error\ApiConnection $e) {
  $response["error"] = TRUE;
		$response["error_msg"] = "Error processing payment - API Connection.";
		echo json_encode($response);
} catch (\Stripe\Error\Base $e) {
  $response["error"] = TRUE;
		$response["error_msg"] = "Error processing payment - Base.";
		echo json_encode($response);
} catch (Exception $e) {
  $response["error"] = TRUE;
		$response["error_msg"] = "Error processing payment - Exception.";
		echo json_encode($response);
}

?>

【问题讨论】:

    标签: php stripe-payments


    【解决方案1】:

    您正在使用该令牌两次。一次用于收费,另一次用于创建客户。您不必使用令牌来创建客户。

        $customer = \Stripe\Customer::create(array(
      "email" => $email,
      // "source" => $token, //remove
    ));
    

    【讨论】:

      【解决方案2】:

      最可能的原因是您的客户端代码多次发布令牌 - 可能是因为客户多次点击,或者是因为代码导致数据多次提交。

      【讨论】:

        猜你喜欢
        • 2015-08-25
        • 2016-08-21
        • 1970-01-01
        • 2014-03-19
        • 2014-06-02
        • 1970-01-01
        • 2020-01-09
        • 2017-04-23
        • 2018-07-05
        相关资源
        最近更新 更多