【问题标题】:How to properly use payment intent, i keep getting error invalid paymentintent status如何正确使用付款意图,我不断收到错误无效的付款意图状态
【发布时间】:2019-12-30 05:25:49
【问题描述】:

我正在尝试实现支付意图 api,但我一直收到无效的支付意图状态,我在 stack-overflow 上看到了一个答案,但它没有帮助, 我不知道它是一个糟糕的服务器端还是客户端实现,因为我还是新手

我尝试将 requires_action 更改为 requires_source_action,在收费下但没有任何帮助,尝试修改 html,但我不知道出了什么问题。

负责,我有

<?php
  # vendor using composer
  require_once('vendor/autoload.php');

  \Stripe\Stripe::setApiKey(getenv('sk_test_HERE'));

  header('Content-Type: application/json');

  # retrieve json from POST body
  $json_str = file_get_contents('php://input');
  $json_obj = json_decode($json_str);
  //$amount=$POST['amount'];
//----------------???? changed the intent from null;
  $intent = null;
  try {
    if (isset($json_obj->payment_method_id)) {
      # Create the PaymentIntent
      $intent = \Stripe\PaymentIntent::create([
        'payment_method' => $json_obj->payment_method_id,
        'amount' => 1000,
        'currency' => 'eur',
        'confirmation_method' => 'manual',
        'confirm' => true,
      ]);
    }
    if (isset($json_obj->payment_intent_id)) {
      $intent = \Stripe\PaymentIntent::retrieve(
        $json_obj->payment_intent_id
      );
      $intent->confirm();
    }
    generatePaymentResponse($intent);
  } catch (\Stripe\Error\Base $e) {
    # Display error on client
    echo json_encode([
      'error' => $e->getMessage()
    ]);
  }
//--------------------updated from requires action to requires source action
  function generatePaymentResponse($intent) {
    # Note that if your API version is before 2019-02-11, 'requires_action'
    # appears as 'requires_source_action'.
    if ($intent->status == 'requires_source_action' &&
        $intent->next_action->type == 'use_stripe_sdk') {
      # Tell the client to handle the action
      echo json_encode([
        'requires_action' => true,
        'payment_intent_client_secret' => $intent->client_secret
      ]);
    } else if ($intent->status == 'succeeded') {
      # The payment didn’t need any additional actions and completed!
      # Handle post-payment fulfillment
      echo json_encode([
        "success" => true
      ]);
    } else {
      # Invalid status
      http_response_code(500);
      echo json_encode(['error' => 'Invalid PaymentIntent status']);
    }
  }
?>

在我拥有的 JavaScript 文件中


// Create a Stripe client.
var stripe = Stripe('pk_test_HERE');

// Create an instance of Elements.
var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
  base: {
    color: '#32325d',
    fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
    fontSmoothing: 'antialiased',
    fontSize: '16px',
    '::placeholder': {
      color: '#aab7c4'
    }
  },
  invalid: {
    color: '#fa755a',
    iconColor: '#fa755a'
  }
};

// Create an instance of the card Element.
var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>.
//                                     added an extra s for form control
card.mount('#card-elements');

// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
  var displayError = document.getElementById('card-errors');
  if (event.error) {
    displayError.textContent = event.error.message;
  } else {
    displayError.textContent = '';
  }
});

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

  stripe.createToken(card).then(function(result) {
    if (result.error) {
      // Inform the user if there was an error.
      var errorElement = document.getElementById('card-errors');
      errorElement.textContent = result.error.message;
    } else {
      // Send the token to your server.
      stripeTokenHandler(result.token);
    }
  });
});

// Submit the form with the token ID.
function stripeTokenHandler(token) {
  // Insert the token ID into the form so it gets submitted to the server
  var form = document.getElementById('payment-form');
  var hiddenInput = document.createElement('input');
  hiddenInput.setAttribute('type', 'hidden');
  hiddenInput.setAttribute('name', 'stripeToken');
  hiddenInput.setAttribute('value', token.id);
  form.appendChild(hiddenInput);

  // Submit the form
  form.submit();
}

这是收集细节,如果你看到任何业余的东西,请原谅我

<div class="container">
  <h2 class="my-4 text-center">Service Payments</h2>

    <form action="./charge.php" method="post" id="payment-form">
        <div class="form-row">
         <input type="text" name="first_name" class="form-control mb-3 StripeElement StripeElement--empty" placeholder="First Name" required>
         <input type="text" name="last_name" class="form-control mb-3 StripeElement StripeElement--empty" placeholder="Last Name" required>
         <!-- <label for="t2">What's your e-mail?</label> -->
         <input type="email" name="email" id="t2" class="form-control mb-3 StripeElement StripeElement--empty" placeholder="Email Address" required>

         <input type="amount" name="amount" class="form-control mb-3 StripeElement StripeElement--empty" placeholder="1000 translates to 10.00" required>
         <!-- changed card element to elements with an s, form control is another possible option -->
         <input id="cardholder-name" type="text">
            <div id="card-elements" class="form-control">
            <!-- /  a Stripe Element will be inserted here. / -->
            </div>
            <!-- Used to display form errors -->
             <div id="card-errors" role="alert"></div>
        </div>
        <button  id="card-button" data-secret="<?= $intent->client_secret ?>">Submit Payment</button>

  </form>
</div>

我不断收到错误消息:付款意图状态无效

【问题讨论】:

    标签: php stripe-payments payment-gateway


    【解决方案1】:

    看起来您可能正在处理较旧的文档。您收到“无效的付款意图状态”,因为您的代码正在寻找不存在的状态 requires_source_actionhttps://stripe.com/docs/payments/intents#intent-statuses

    实际状态可能是requires_confirmation,在这种情况下,您必须确认服务器上的 PaymentIntent。然后根据状态,您可以得到requires_action 状态或succeeded。您获得哪一个取决于相关卡是否需要 3DS。

    由于您的 PaymentIntent 使用的是手动确认流程,这意味着您必须在前端和后端之间进行多次往返,才能真正完成付款。我建议考虑改用自动确认。这样你只有两个步骤:

    1. 在服务器上创建 PaymentIntent,将客户端密钥 ID 传递给前端
    2. 像现在一样使用 Elements 收集付款详细信息,使用步骤 1 中的客户端密码和 Stripe.js 的handleCardPayment 完成付款

    在此处了解有关该流程的更多信息:https://stripe.com/docs/payments/payment-intents/quickstart#automatic-confirmation-flow

    除非您有充分的理由使用手动确认,否则我会坚持使用自动确认。

    【讨论】:

    • 当我根据文档使用这个php ?php $intent = # ... Fetch or create the PaymentIntent; ?&gt; 时,我得到一个错误,这是完全相同的错误,或者是我的html 错误,我也将charge.php 的代码更改为条带链接上的那个,这个意图应该是什么,(字符串,代码?),请帮助
    猜你喜欢
    • 1970-01-01
    • 2022-10-13
    • 2019-12-30
    • 2021-11-28
    • 2023-03-19
    • 2016-02-07
    • 2013-06-29
    • 2020-12-01
    • 2023-02-24
    相关资源
    最近更新 更多