条带化,使用幂等键的多个请求返回错误

时间:2019-02-05 23:41:43

标签: php stripe-payments

需要有关Stripe错误响应的帮助。 创建客户并随后将其注册到订阅中时,根据条形仪表板日志,一切似乎都可以正常工作,如果存在使用幂等密钥的多个请求,则生成费用。 在这种情况下,我如何得到这个stripe error response(捕获到$ error6异常),它会在运行代码的页面(charge.php)中打开,而不是发送到成功页面。

charge.php

\Stripe\Stripe::setApiKey('sk_live_xxxxxxxxxxx');

$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);

$email = $POST['email'];
$token = $POST['stripeToken'];
$membership_type = $POST['membership_type'];
$user_id = $POST['user_id'];
$success = 0;

try {
// Create customer in Stripe
$customer = \Stripe\Customer::create([
  "email" => $email,
  "source" => $token,
],[
  "idempotency_key" => $_SESSION['sid2'],
]);
$success = 1;
} 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;
    print_r($_SESSION);
}

// Add Customer to a Subscription in Stripe
$subscription = \Stripe\Subscription::create([
    'customer' => $customer->id,
    'items' => [['plan' => $membership_type]]
  ],[
    "idempotency_key" => $_SESSION['sid'],
]); 
//adding all relevent info into data base...

//send user to success page
header('Location: ../success.php?id='.$user_id.'&product='.$subscription->plan->nickname);

这可能是因为Stripe JS $ token参数每次更改吗?这是正常现象还是我做错了什么? (当仅在订阅客户时运行幂等时,我遇到了类似的错误,然后,条带使用相同的电子邮件和支付卡创建了多个客户,但客户-> id不同) 谁能建议我如何解决此错误页面?

2 个答案:

答案 0 :(得分:0)

  

这可能是因为Stripe JS $ token参数每次更改吗?

是的,我怀疑这里发生的事情是您在两个具有不同$_SESSION['sid2']参数的单独请求中重复使用source来创建客户。而且此错误响应是预期的行为!

您应该能够在Dashboard日志中看到它:假设这是一个测试模式请求,https://dashboard.stripe.com/test/logs/iar_IgylJRGpbLyVb6应该告诉您最初使用同一密钥的位置。

答案 1 :(得分:0)

我能找到的最佳选择是在Stripe JS事件侦听器之后禁用按钮

var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
  event.preventDefault();

$('.button').attr("disabled", true);
stripe.createToken(card).then(function(result) {...........