条带避免状态为“不完整”的付款意图

问题描述 投票:0回答:1

我正在使用 Stripe 的“付款”元素来创建付款意图和卡费用,使用 PHP、HTML 和 JS。 https://stripe.com/docs/ payments/ payment-element

a) 一旦我加载付款页面,Stripe 就会生成状态为“未完成”的付款意图。
b) 在我输入信用卡详细信息并点击“付款”后,Stripe 再次发出第二个付款意图,并显示相应的状态(例如“成功”)

结果是,我的仪表板现在充满了不必要的记录

这是因为我在页面加载后就初始化了

$paymentIntent = \Stripe\PaymentIntent::create
。 我知道这是 Stripe 默认行为,因为此时尚未附加 payment_method。

我的问题是:如何最好地解决这个问题,以避免此类“不完整”记录?

  • 也许附加一个对象并仅在以下情况下触发 paymentintent 创建 该物体存在吗?
  • 或者单击“付款”按钮..等待付款意图,确认存在然后提交表格?
  • 或者检索第一个付款意图,存储它,然后更新 表单提交?

创建.php

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

try {

    // retrieve JSON from POST body
    $jsonStr = file_get_contents('php://input');
    $jsonObj = json_decode($jsonStr);

    // Create a PaymentIntent with amount and currency
    $paymentIntent = \Stripe\PaymentIntent::create([
        'amount' => 1000,
        'currency' => 'eur',
        'receipt_email' => '[email protected]',
        'automatic_payment_methods' => ['enabled' => true,],
        'description' => 'Reservation Dimi123 / Name: John Doe',
        'metadata' => ['order_id' => '12345']
    ]);
 
    $output = [
        'clientSecret' => $paymentIntent->client_secret,
    ];

    echo json_encode($output);
} catch (Error $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}

脚本.js

const stripe = Stripe("pk_test_..");

let elements;

initialize();
checkStatus();

document
  .querySelector("#payment-form")
  .addEventListener("submit", handleSubmit);

// Fetches a payment intent and captures the client secret
async function initialize() {
  const { clientSecret } = await fetch("../create.php", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
  }).then((r) => r.json());

  elements = stripe.elements({ clientSecret });

  const paymentElement = elements.create("payment");
  paymentElement.mount("#payment-element");
}

async function handleSubmit(e) {
  e.preventDefault();
  setLoading(true);

  const { error } = await stripe.confirmPayment({
    elements,
    confirmParams: {
      return_url: "https://localhost/stripe/prebuilt-checkout-custom-flow/public/checkout.html",
    },
  });

  if (error.type === "card_error" || error.type === "validation_error") {
    showMessage(error.message);
  } else {
    showMessage("An unexpected error occurred.");
  }

  setLoading(false);
}
php stripe-payments
1个回答
1
投票

如何避免支付意图不完整?你不知道。

这就是系统设计的运作方式,因为您需要付款意图(或设置意图)中的

client_secret
来呈现付款元素。对您的帐户没有负面影响。

您应该放心地忽略它们。您可以调整“付款”选项卡中的过滤器以隐藏它们。它们并不是衡量个人客户访问您的支付界面并离开的重要指标,因为任何用户刷新浏览器都会触发新的浏览器。

© www.soinside.com 2019 - 2024. All rights reserved.