【问题标题】:Using Stripe how can I pass PaymentIntent’s client secret to the form?使用 Stripe 如何将 PaymentIntent 的客户端密码传递给表单?
【发布时间】:2019-09-30 13:39:40
【问题描述】:

我想在我的网站中添加 Stripe,但我注意到它很难使用,而且网络上没有更多的帮助资源。

我的第一个问题是如何将 PaymentIntent 的客户端密码传递给客户端,那么如何在我的 html 表单中传递一个 php 变量。

在我应该创建一个 PaymentIntent 对象之前:

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

$intent = \Stripe\PaymentIntent::create([
    'amount' => 0.89,
    'currency' => 'eur',
]);

那么API Stripe建议通过这种方式将PaymentIntent的客户端秘钥传递给客户端:

<?php
    $intent = # ... Fetch or create the PaymentIntent;
?>
...
<input id="cardholder-name" type="text">
<!-- placeholder for Elements -->
<div id="card-element"></div>
<button id="card-button" data-secret="<?= $intent->client_secret ?>">
  Submit Payment
</button>
...

这是 API 页面:https://stripe.com/docs/payments/payment-intents/quickstart#passing-to-client

但是如何在 html 页面中传递一个 php 变量呢?

有人可以解释一下这个客户机密是什么?

【问题讨论】:

    标签: php html stripe-payments


    【解决方案1】:

    client_secret 是一个property of a PaymentIntent,您可以在前端使用您的可发布密钥。

    基于快速入门指南中的示例,这是一个基本示例:

    <?php
    $intent =\Stripe\PaymentIntent::create([
        "amount" => 2000,
        "currency" => "usd",
        "payment_method_types" => ["card"],
    ]);
    ?>
    ...
    <input id="cardholder-name" type="text">
    <!-- placeholder for Elements -->
    <div id="card-element"></div>
    <button id="card-button" data-secret="<?= $intent->client_secret ?>">
      Submit Payment
    </button>
    ...
    

    需要注意的关键事项是,您在代码中创建 $intent,然后稍后在表单中将 $intent-&gt;client_secret 回显为正确的值。

    【讨论】:

    • 是的,但我怎样才能在我的 html 页面中传递这个?如何在表单所在的 html 页面中包含 php 文件(我在其中创建 $intent)?
    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2017-11-14
    • 2021-12-18
    相关资源
    最近更新 更多