【问题标题】:Stripe - Payment Intents (3d secure issue)Stripe - 付款意图(3d 安全问题)
【发布时间】:2019-12-11 01:39:19
【问题描述】:

我确实在我的网站上实现了支付意图,现在可以完美地与这张测试卡 4242 4242 4242 4242 配合使用,但对于需要 3d 安全方法的其他卡,我会收到此错误 "Invalid PaymentIntent status"

我使用的代码与the Stripe documentation-flow 上存在的标准代码相同,其中包含一些用于管理 mysql、电子邮件、元数据等的代码。

我哪里出错了?提前致谢。

连接index.php的简化js代码

var stripe = Stripe('pk_test_xxx');
var elements = stripe.elements();
var cardElement = elements.create('card', {style: style});

cardElement.mount('#card-element');

var cardholderName = document.getElementById('cardholder-name');
var cardButton = document.getElementById('card-button');
var amount = $('#amount').val();

cardButton.addEventListener('click', function(ev) {
    ev.preventDefault();
    stripe.createPaymentMethod('card', cardElement, {
        billing_details: {name: cardholderName.value}
    }).then(function(result) {

    if (result.error) {


    } else {
      $body.addClass("loading");
      fetch('https://test.com/server.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ 
            payment_method_id: result.paymentMethod.id, 
            amount: amount
            })
      }).then(function(result) {


        // Handle server response (see Step 3)
        result.json().then(function(json) {
          handleServerResponse(json);
        })
      });
    }
  });
});

function handleServerResponse(response) {
  if (response.error) {

  } else if (response.requires_action) {

    stripe.handleCardAction(
      response.payment_intent_client_secret
    ).then(function(result) {
      if (result.error) {

      } else {
        // The card action has been handled
        // The PaymentIntent can be confirmed again on the server
        fetch('https://test.com/server.php', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ 
            payment_method_id: result.paymentMethod.id, 
             amount: amount


          })
        }).then(function(confirmResult) {
            console.log(confirmResult);
          return confirmResult.json();
        }).then(handleServerResponse);
      }
    });
  } else {


  }
}

server.php 上的简化代码

<?php

  # vendor using composer
  require_once('stripe6400/init.php');

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

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

  # retrieve json from POST body
  $json_str = file_get_contents('php://input');
  $json_obj = json_decode($json_str);

    $paymentid = $json_obj->payment_method_id;
    $amount = $json_obj->amount;

  $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' => $json_obj->amount,
        'payment_method_types' => ["card"],
        'currency' => 'gbp',
        '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()
    ]);
  }

  function generatePaymentResponse($intent) {
    if ($intent->status == 'requires_action' &&
        $intent->next_action->type == 'use_stripe_sdk') {

      echo json_encode([
        'requires_action' => true,
        'payment_intent_client_secret' => $intent->client_secret
      ]);
    } else if ($intent->status == 'succeeded') {


Stripe\Customer::create([
    "email" => $email,
    "name" => $customer_name,
    "source" => "tok_visa" // obtained with Stripe.js
]);


      echo json_encode([
        "success" => true
      ]);



    } else {
      # Invalid status
      http_response_code(500);
      echo json_encode(['error' => 'Invalid PaymentIntent status']);
    }
  }
?>

【问题讨论】:

  • 在使用 3d 安全卡时能否附上来自服务器的 ajax 响应?
  • {"error":"Invalid PaymentIntent 状态"}

标签: javascript php stripe-payments


【解决方案1】:

看来您可能遇到了与我刚刚遇到的相同的错误。来自条带的响应的状态是 requires_source_action 而不是 requires_action 所以你的 if 语句落入 Invalid PaymentIntent status

// change this
// $intent->status == 'requires_action'

// to this
$intent->status == 'requires_source_action'

在我的情况下,我正在检查两者,以便在我更新条带 SDK 时我的代码已准备好。

https://stripe.com/docs/payments/payment-intents/quickstart#confirm-again (代码中的第 33 行)

【讨论】:

  • 我不再收到错误,但付款没有被注册,请给我一段时间看看可能是什么问题
  • 我确实将 'requires_action' 更新为 'requires_source_action' 并关闭了 3d 安全显示,但我遇到了来自函数 handleServerResponse(response) >> TypeError: result.paymentMethod is undefined 的下一个问题
  • 看起来你应该发送payment_method_id,而你应该发送payment_intent_id: paymentIntent.id。查看代码示例的第 17 行:stripe.com/docs/payments/payment-intents/…
【解决方案2】:

Customer::create 上,您的来源属性 "tok_visa" 必须是来自 javascript @987654321 中创建令牌的真实 token.id @

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 2021-09-09
    • 2021-10-11
    • 1970-01-01
    • 2021-11-26
    • 2015-05-19
    • 2022-01-12
    • 2020-03-14
    相关资源
    最近更新 更多